inode.c 283 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682
  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/compat.h>
  33. #include <linux/bit_spinlock.h>
  34. #include <linux/xattr.h>
  35. #include <linux/posix_acl.h>
  36. #include <linux/falloc.h>
  37. #include <linux/slab.h>
  38. #include <linux/ratelimit.h>
  39. #include <linux/mount.h>
  40. #include <linux/btrfs.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/posix_acl_xattr.h>
  43. #include <linux/uio.h>
  44. #include "ctree.h"
  45. #include "disk-io.h"
  46. #include "transaction.h"
  47. #include "btrfs_inode.h"
  48. #include "print-tree.h"
  49. #include "ordered-data.h"
  50. #include "xattr.h"
  51. #include "tree-log.h"
  52. #include "volumes.h"
  53. #include "compression.h"
  54. #include "locking.h"
  55. #include "free-space-cache.h"
  56. #include "inode-map.h"
  57. #include "backref.h"
  58. #include "hash.h"
  59. #include "props.h"
  60. #include "qgroup.h"
  61. #include "dedupe.h"
  62. struct btrfs_iget_args {
  63. struct btrfs_key *location;
  64. struct btrfs_root *root;
  65. };
  66. struct btrfs_dio_data {
  67. u64 outstanding_extents;
  68. u64 reserve;
  69. u64 unsubmitted_oe_range_start;
  70. u64 unsubmitted_oe_range_end;
  71. };
  72. static const struct inode_operations btrfs_dir_inode_operations;
  73. static const struct inode_operations btrfs_symlink_inode_operations;
  74. static const struct inode_operations btrfs_dir_ro_inode_operations;
  75. static const struct inode_operations btrfs_special_inode_operations;
  76. static const struct inode_operations btrfs_file_inode_operations;
  77. static const struct address_space_operations btrfs_aops;
  78. static const struct address_space_operations btrfs_symlink_aops;
  79. static const struct file_operations btrfs_dir_file_operations;
  80. static const struct extent_io_ops btrfs_extent_io_ops;
  81. static struct kmem_cache *btrfs_inode_cachep;
  82. struct kmem_cache *btrfs_trans_handle_cachep;
  83. struct kmem_cache *btrfs_transaction_cachep;
  84. struct kmem_cache *btrfs_path_cachep;
  85. struct kmem_cache *btrfs_free_space_cachep;
  86. #define S_SHIFT 12
  87. static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  88. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  89. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  90. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  91. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  92. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  93. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  94. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  95. };
  96. static int btrfs_setsize(struct inode *inode, struct iattr *attr);
  97. static int btrfs_truncate(struct inode *inode);
  98. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
  99. static noinline int cow_file_range(struct inode *inode,
  100. struct page *locked_page,
  101. u64 start, u64 end, u64 delalloc_end,
  102. int *page_started, unsigned long *nr_written,
  103. int unlock, struct btrfs_dedupe_hash *hash);
  104. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  105. u64 len, u64 orig_start,
  106. u64 block_start, u64 block_len,
  107. u64 orig_block_len, u64 ram_bytes,
  108. int type);
  109. static int btrfs_dirty_inode(struct inode *inode);
  110. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  111. void btrfs_test_inode_set_ops(struct inode *inode)
  112. {
  113. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  114. }
  115. #endif
  116. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  117. struct inode *inode, struct inode *dir,
  118. const struct qstr *qstr)
  119. {
  120. int err;
  121. err = btrfs_init_acl(trans, inode, dir);
  122. if (!err)
  123. err = btrfs_xattr_security_init(trans, inode, dir, qstr);
  124. return err;
  125. }
  126. /*
  127. * this does all the hard work for inserting an inline extent into
  128. * the btree. The caller should have done a btrfs_drop_extents so that
  129. * no overlapping inline items exist in the btree
  130. */
  131. static int insert_inline_extent(struct btrfs_trans_handle *trans,
  132. struct btrfs_path *path, int extent_inserted,
  133. struct btrfs_root *root, struct inode *inode,
  134. u64 start, size_t size, size_t compressed_size,
  135. int compress_type,
  136. struct page **compressed_pages)
  137. {
  138. struct extent_buffer *leaf;
  139. struct page *page = NULL;
  140. char *kaddr;
  141. unsigned long ptr;
  142. struct btrfs_file_extent_item *ei;
  143. int err = 0;
  144. int ret;
  145. size_t cur_size = size;
  146. unsigned long offset;
  147. if (compressed_size && compressed_pages)
  148. cur_size = compressed_size;
  149. inode_add_bytes(inode, size);
  150. if (!extent_inserted) {
  151. struct btrfs_key key;
  152. size_t datasize;
  153. key.objectid = btrfs_ino(inode);
  154. key.offset = start;
  155. key.type = BTRFS_EXTENT_DATA_KEY;
  156. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  157. path->leave_spinning = 1;
  158. ret = btrfs_insert_empty_item(trans, root, path, &key,
  159. datasize);
  160. if (ret) {
  161. err = ret;
  162. goto fail;
  163. }
  164. }
  165. leaf = path->nodes[0];
  166. ei = btrfs_item_ptr(leaf, path->slots[0],
  167. struct btrfs_file_extent_item);
  168. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  169. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  170. btrfs_set_file_extent_encryption(leaf, ei, 0);
  171. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  172. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  173. ptr = btrfs_file_extent_inline_start(ei);
  174. if (compress_type != BTRFS_COMPRESS_NONE) {
  175. struct page *cpage;
  176. int i = 0;
  177. while (compressed_size > 0) {
  178. cpage = compressed_pages[i];
  179. cur_size = min_t(unsigned long, compressed_size,
  180. PAGE_SIZE);
  181. kaddr = kmap_atomic(cpage);
  182. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  183. kunmap_atomic(kaddr);
  184. i++;
  185. ptr += cur_size;
  186. compressed_size -= cur_size;
  187. }
  188. btrfs_set_file_extent_compression(leaf, ei,
  189. compress_type);
  190. } else {
  191. page = find_get_page(inode->i_mapping,
  192. start >> PAGE_SHIFT);
  193. btrfs_set_file_extent_compression(leaf, ei, 0);
  194. kaddr = kmap_atomic(page);
  195. offset = start & (PAGE_SIZE - 1);
  196. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  197. kunmap_atomic(kaddr);
  198. put_page(page);
  199. }
  200. btrfs_mark_buffer_dirty(leaf);
  201. btrfs_release_path(path);
  202. /*
  203. * we're an inline extent, so nobody can
  204. * extend the file past i_size without locking
  205. * a page we already have locked.
  206. *
  207. * We must do any isize and inode updates
  208. * before we unlock the pages. Otherwise we
  209. * could end up racing with unlink.
  210. */
  211. BTRFS_I(inode)->disk_i_size = inode->i_size;
  212. ret = btrfs_update_inode(trans, root, inode);
  213. return ret;
  214. fail:
  215. return err;
  216. }
  217. /*
  218. * conditionally insert an inline extent into the file. This
  219. * does the checks required to make sure the data is small enough
  220. * to fit as an inline extent.
  221. */
  222. static noinline int cow_file_range_inline(struct btrfs_root *root,
  223. struct inode *inode, u64 start,
  224. u64 end, size_t compressed_size,
  225. int compress_type,
  226. struct page **compressed_pages)
  227. {
  228. struct btrfs_fs_info *fs_info = root->fs_info;
  229. struct btrfs_trans_handle *trans;
  230. u64 isize = i_size_read(inode);
  231. u64 actual_end = min(end + 1, isize);
  232. u64 inline_len = actual_end - start;
  233. u64 aligned_end = ALIGN(end, fs_info->sectorsize);
  234. u64 data_len = inline_len;
  235. int ret;
  236. struct btrfs_path *path;
  237. int extent_inserted = 0;
  238. u32 extent_item_size;
  239. if (compressed_size)
  240. data_len = compressed_size;
  241. if (start > 0 ||
  242. actual_end > fs_info->sectorsize ||
  243. data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
  244. (!compressed_size &&
  245. (actual_end & (fs_info->sectorsize - 1)) == 0) ||
  246. end + 1 < isize ||
  247. data_len > fs_info->max_inline) {
  248. return 1;
  249. }
  250. path = btrfs_alloc_path();
  251. if (!path)
  252. return -ENOMEM;
  253. trans = btrfs_join_transaction(root);
  254. if (IS_ERR(trans)) {
  255. btrfs_free_path(path);
  256. return PTR_ERR(trans);
  257. }
  258. trans->block_rsv = &fs_info->delalloc_block_rsv;
  259. if (compressed_size && compressed_pages)
  260. extent_item_size = btrfs_file_extent_calc_inline_size(
  261. compressed_size);
  262. else
  263. extent_item_size = btrfs_file_extent_calc_inline_size(
  264. inline_len);
  265. ret = __btrfs_drop_extents(trans, root, inode, path,
  266. start, aligned_end, NULL,
  267. 1, 1, extent_item_size, &extent_inserted);
  268. if (ret) {
  269. btrfs_abort_transaction(trans, ret);
  270. goto out;
  271. }
  272. if (isize > actual_end)
  273. inline_len = min_t(u64, isize, actual_end);
  274. ret = insert_inline_extent(trans, path, extent_inserted,
  275. root, inode, start,
  276. inline_len, compressed_size,
  277. compress_type, compressed_pages);
  278. if (ret && ret != -ENOSPC) {
  279. btrfs_abort_transaction(trans, ret);
  280. goto out;
  281. } else if (ret == -ENOSPC) {
  282. ret = 1;
  283. goto out;
  284. }
  285. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  286. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  287. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  288. out:
  289. /*
  290. * Don't forget to free the reserved space, as for inlined extent
  291. * it won't count as data extent, free them directly here.
  292. * And at reserve time, it's always aligned to page size, so
  293. * just free one page here.
  294. */
  295. btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
  296. btrfs_free_path(path);
  297. btrfs_end_transaction(trans);
  298. return ret;
  299. }
  300. struct async_extent {
  301. u64 start;
  302. u64 ram_size;
  303. u64 compressed_size;
  304. struct page **pages;
  305. unsigned long nr_pages;
  306. int compress_type;
  307. struct list_head list;
  308. };
  309. struct async_cow {
  310. struct inode *inode;
  311. struct btrfs_root *root;
  312. struct page *locked_page;
  313. u64 start;
  314. u64 end;
  315. struct list_head extents;
  316. struct btrfs_work work;
  317. };
  318. static noinline int add_async_extent(struct async_cow *cow,
  319. u64 start, u64 ram_size,
  320. u64 compressed_size,
  321. struct page **pages,
  322. unsigned long nr_pages,
  323. int compress_type)
  324. {
  325. struct async_extent *async_extent;
  326. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  327. BUG_ON(!async_extent); /* -ENOMEM */
  328. async_extent->start = start;
  329. async_extent->ram_size = ram_size;
  330. async_extent->compressed_size = compressed_size;
  331. async_extent->pages = pages;
  332. async_extent->nr_pages = nr_pages;
  333. async_extent->compress_type = compress_type;
  334. list_add_tail(&async_extent->list, &cow->extents);
  335. return 0;
  336. }
  337. static inline int inode_need_compress(struct inode *inode)
  338. {
  339. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  340. /* force compress */
  341. if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
  342. return 1;
  343. /* bad compression ratios */
  344. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  345. return 0;
  346. if (btrfs_test_opt(fs_info, COMPRESS) ||
  347. BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
  348. BTRFS_I(inode)->force_compress)
  349. return 1;
  350. return 0;
  351. }
  352. /*
  353. * we create compressed extents in two phases. The first
  354. * phase compresses a range of pages that have already been
  355. * locked (both pages and state bits are locked).
  356. *
  357. * This is done inside an ordered work queue, and the compression
  358. * is spread across many cpus. The actual IO submission is step
  359. * two, and the ordered work queue takes care of making sure that
  360. * happens in the same order things were put onto the queue by
  361. * writepages and friends.
  362. *
  363. * If this code finds it can't get good compression, it puts an
  364. * entry onto the work queue to write the uncompressed bytes. This
  365. * makes sure that both compressed inodes and uncompressed inodes
  366. * are written in the same order that the flusher thread sent them
  367. * down.
  368. */
  369. static noinline void compress_file_range(struct inode *inode,
  370. struct page *locked_page,
  371. u64 start, u64 end,
  372. struct async_cow *async_cow,
  373. int *num_added)
  374. {
  375. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  376. struct btrfs_root *root = BTRFS_I(inode)->root;
  377. u64 num_bytes;
  378. u64 blocksize = fs_info->sectorsize;
  379. u64 actual_end;
  380. u64 isize = i_size_read(inode);
  381. int ret = 0;
  382. struct page **pages = NULL;
  383. unsigned long nr_pages;
  384. unsigned long nr_pages_ret = 0;
  385. unsigned long total_compressed = 0;
  386. unsigned long total_in = 0;
  387. unsigned long max_compressed = SZ_128K;
  388. unsigned long max_uncompressed = SZ_128K;
  389. int i;
  390. int will_compress;
  391. int compress_type = fs_info->compress_type;
  392. int redirty = 0;
  393. /* if this is a small write inside eof, kick off a defrag */
  394. if ((end - start + 1) < SZ_16K &&
  395. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  396. btrfs_add_inode_defrag(NULL, inode);
  397. actual_end = min_t(u64, isize, end + 1);
  398. again:
  399. will_compress = 0;
  400. nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
  401. nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
  402. /*
  403. * we don't want to send crud past the end of i_size through
  404. * compression, that's just a waste of CPU time. So, if the
  405. * end of the file is before the start of our current
  406. * requested range of bytes, we bail out to the uncompressed
  407. * cleanup code that can deal with all of this.
  408. *
  409. * It isn't really the fastest way to fix things, but this is a
  410. * very uncommon corner.
  411. */
  412. if (actual_end <= start)
  413. goto cleanup_and_bail_uncompressed;
  414. total_compressed = actual_end - start;
  415. /*
  416. * skip compression for a small file range(<=blocksize) that
  417. * isn't an inline extent, since it doesn't save disk space at all.
  418. */
  419. if (total_compressed <= blocksize &&
  420. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  421. goto cleanup_and_bail_uncompressed;
  422. /* we want to make sure that amount of ram required to uncompress
  423. * an extent is reasonable, so we limit the total size in ram
  424. * of a compressed extent to 128k. This is a crucial number
  425. * because it also controls how easily we can spread reads across
  426. * cpus for decompression.
  427. *
  428. * We also want to make sure the amount of IO required to do
  429. * a random read is reasonably small, so we limit the size of
  430. * a compressed extent to 128k.
  431. */
  432. total_compressed = min(total_compressed, max_uncompressed);
  433. num_bytes = ALIGN(end - start + 1, blocksize);
  434. num_bytes = max(blocksize, num_bytes);
  435. total_in = 0;
  436. ret = 0;
  437. /*
  438. * we do compression for mount -o compress and when the
  439. * inode has not been flagged as nocompress. This flag can
  440. * change at any time if we discover bad compression ratios.
  441. */
  442. if (inode_need_compress(inode)) {
  443. WARN_ON(pages);
  444. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
  445. if (!pages) {
  446. /* just bail out to the uncompressed code */
  447. goto cont;
  448. }
  449. if (BTRFS_I(inode)->force_compress)
  450. compress_type = BTRFS_I(inode)->force_compress;
  451. /*
  452. * we need to call clear_page_dirty_for_io on each
  453. * page in the range. Otherwise applications with the file
  454. * mmap'd can wander in and change the page contents while
  455. * we are compressing them.
  456. *
  457. * If the compression fails for any reason, we set the pages
  458. * dirty again later on.
  459. */
  460. extent_range_clear_dirty_for_io(inode, start, end);
  461. redirty = 1;
  462. ret = btrfs_compress_pages(compress_type,
  463. inode->i_mapping, start,
  464. total_compressed, pages,
  465. nr_pages, &nr_pages_ret,
  466. &total_in,
  467. &total_compressed,
  468. max_compressed);
  469. if (!ret) {
  470. unsigned long offset = total_compressed &
  471. (PAGE_SIZE - 1);
  472. struct page *page = pages[nr_pages_ret - 1];
  473. char *kaddr;
  474. /* zero the tail end of the last page, we might be
  475. * sending it down to disk
  476. */
  477. if (offset) {
  478. kaddr = kmap_atomic(page);
  479. memset(kaddr + offset, 0,
  480. PAGE_SIZE - offset);
  481. kunmap_atomic(kaddr);
  482. }
  483. will_compress = 1;
  484. }
  485. }
  486. cont:
  487. if (start == 0) {
  488. /* lets try to make an inline extent */
  489. if (ret || total_in < (actual_end - start)) {
  490. /* we didn't compress the entire range, try
  491. * to make an uncompressed inline extent.
  492. */
  493. ret = cow_file_range_inline(root, inode, start, end,
  494. 0, 0, NULL);
  495. } else {
  496. /* try making a compressed inline extent */
  497. ret = cow_file_range_inline(root, inode, start, end,
  498. total_compressed,
  499. compress_type, pages);
  500. }
  501. if (ret <= 0) {
  502. unsigned long clear_flags = EXTENT_DELALLOC |
  503. EXTENT_DEFRAG;
  504. unsigned long page_error_op;
  505. clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
  506. page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
  507. /*
  508. * inline extent creation worked or returned error,
  509. * we don't need to create any more async work items.
  510. * Unlock and free up our temp pages.
  511. */
  512. extent_clear_unlock_delalloc(inode, start, end, end,
  513. NULL, clear_flags,
  514. PAGE_UNLOCK |
  515. PAGE_CLEAR_DIRTY |
  516. PAGE_SET_WRITEBACK |
  517. page_error_op |
  518. PAGE_END_WRITEBACK);
  519. btrfs_free_reserved_data_space_noquota(inode, start,
  520. end - start + 1);
  521. goto free_pages_out;
  522. }
  523. }
  524. if (will_compress) {
  525. /*
  526. * we aren't doing an inline extent round the compressed size
  527. * up to a block size boundary so the allocator does sane
  528. * things
  529. */
  530. total_compressed = ALIGN(total_compressed, blocksize);
  531. /*
  532. * one last check to make sure the compression is really a
  533. * win, compare the page count read with the blocks on disk
  534. */
  535. total_in = ALIGN(total_in, PAGE_SIZE);
  536. if (total_compressed >= total_in) {
  537. will_compress = 0;
  538. } else {
  539. num_bytes = total_in;
  540. *num_added += 1;
  541. /*
  542. * The async work queues will take care of doing actual
  543. * allocation on disk for these compressed pages, and
  544. * will submit them to the elevator.
  545. */
  546. add_async_extent(async_cow, start, num_bytes,
  547. total_compressed, pages, nr_pages_ret,
  548. compress_type);
  549. if (start + num_bytes < end) {
  550. start += num_bytes;
  551. pages = NULL;
  552. cond_resched();
  553. goto again;
  554. }
  555. return;
  556. }
  557. }
  558. if (pages) {
  559. /*
  560. * the compression code ran but failed to make things smaller,
  561. * free any pages it allocated and our page pointer array
  562. */
  563. for (i = 0; i < nr_pages_ret; i++) {
  564. WARN_ON(pages[i]->mapping);
  565. put_page(pages[i]);
  566. }
  567. kfree(pages);
  568. pages = NULL;
  569. total_compressed = 0;
  570. nr_pages_ret = 0;
  571. /* flag the file so we don't compress in the future */
  572. if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
  573. !(BTRFS_I(inode)->force_compress)) {
  574. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  575. }
  576. }
  577. cleanup_and_bail_uncompressed:
  578. /*
  579. * No compression, but we still need to write the pages in the file
  580. * we've been given so far. redirty the locked page if it corresponds
  581. * to our extent and set things up for the async work queue to run
  582. * cow_file_range to do the normal delalloc dance.
  583. */
  584. if (page_offset(locked_page) >= start &&
  585. page_offset(locked_page) <= end)
  586. __set_page_dirty_nobuffers(locked_page);
  587. /* unlocked later on in the async handlers */
  588. if (redirty)
  589. extent_range_redirty_for_io(inode, start, end);
  590. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
  591. BTRFS_COMPRESS_NONE);
  592. *num_added += 1;
  593. return;
  594. free_pages_out:
  595. for (i = 0; i < nr_pages_ret; i++) {
  596. WARN_ON(pages[i]->mapping);
  597. put_page(pages[i]);
  598. }
  599. kfree(pages);
  600. }
  601. static void free_async_extent_pages(struct async_extent *async_extent)
  602. {
  603. int i;
  604. if (!async_extent->pages)
  605. return;
  606. for (i = 0; i < async_extent->nr_pages; i++) {
  607. WARN_ON(async_extent->pages[i]->mapping);
  608. put_page(async_extent->pages[i]);
  609. }
  610. kfree(async_extent->pages);
  611. async_extent->nr_pages = 0;
  612. async_extent->pages = NULL;
  613. }
  614. /*
  615. * phase two of compressed writeback. This is the ordered portion
  616. * of the code, which only gets called in the order the work was
  617. * queued. We walk all the async extents created by compress_file_range
  618. * and send them down to the disk.
  619. */
  620. static noinline void submit_compressed_extents(struct inode *inode,
  621. struct async_cow *async_cow)
  622. {
  623. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  624. struct async_extent *async_extent;
  625. u64 alloc_hint = 0;
  626. struct btrfs_key ins;
  627. struct extent_map *em;
  628. struct btrfs_root *root = BTRFS_I(inode)->root;
  629. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  630. struct extent_io_tree *io_tree;
  631. int ret = 0;
  632. again:
  633. while (!list_empty(&async_cow->extents)) {
  634. async_extent = list_entry(async_cow->extents.next,
  635. struct async_extent, list);
  636. list_del(&async_extent->list);
  637. io_tree = &BTRFS_I(inode)->io_tree;
  638. retry:
  639. /* did the compression code fall back to uncompressed IO? */
  640. if (!async_extent->pages) {
  641. int page_started = 0;
  642. unsigned long nr_written = 0;
  643. lock_extent(io_tree, async_extent->start,
  644. async_extent->start +
  645. async_extent->ram_size - 1);
  646. /* allocate blocks */
  647. ret = cow_file_range(inode, async_cow->locked_page,
  648. async_extent->start,
  649. async_extent->start +
  650. async_extent->ram_size - 1,
  651. async_extent->start +
  652. async_extent->ram_size - 1,
  653. &page_started, &nr_written, 0,
  654. NULL);
  655. /* JDM XXX */
  656. /*
  657. * if page_started, cow_file_range inserted an
  658. * inline extent and took care of all the unlocking
  659. * and IO for us. Otherwise, we need to submit
  660. * all those pages down to the drive.
  661. */
  662. if (!page_started && !ret)
  663. extent_write_locked_range(io_tree,
  664. inode, async_extent->start,
  665. async_extent->start +
  666. async_extent->ram_size - 1,
  667. btrfs_get_extent,
  668. WB_SYNC_ALL);
  669. else if (ret)
  670. unlock_page(async_cow->locked_page);
  671. kfree(async_extent);
  672. cond_resched();
  673. continue;
  674. }
  675. lock_extent(io_tree, async_extent->start,
  676. async_extent->start + async_extent->ram_size - 1);
  677. ret = btrfs_reserve_extent(root, async_extent->ram_size,
  678. async_extent->compressed_size,
  679. async_extent->compressed_size,
  680. 0, alloc_hint, &ins, 1, 1);
  681. if (ret) {
  682. free_async_extent_pages(async_extent);
  683. if (ret == -ENOSPC) {
  684. unlock_extent(io_tree, async_extent->start,
  685. async_extent->start +
  686. async_extent->ram_size - 1);
  687. /*
  688. * we need to redirty the pages if we decide to
  689. * fallback to uncompressed IO, otherwise we
  690. * will not submit these pages down to lower
  691. * layers.
  692. */
  693. extent_range_redirty_for_io(inode,
  694. async_extent->start,
  695. async_extent->start +
  696. async_extent->ram_size - 1);
  697. goto retry;
  698. }
  699. goto out_free;
  700. }
  701. /*
  702. * here we're doing allocation and writeback of the
  703. * compressed pages
  704. */
  705. btrfs_drop_extent_cache(inode, async_extent->start,
  706. async_extent->start +
  707. async_extent->ram_size - 1, 0);
  708. em = alloc_extent_map();
  709. if (!em) {
  710. ret = -ENOMEM;
  711. goto out_free_reserve;
  712. }
  713. em->start = async_extent->start;
  714. em->len = async_extent->ram_size;
  715. em->orig_start = em->start;
  716. em->mod_start = em->start;
  717. em->mod_len = em->len;
  718. em->block_start = ins.objectid;
  719. em->block_len = ins.offset;
  720. em->orig_block_len = ins.offset;
  721. em->ram_bytes = async_extent->ram_size;
  722. em->bdev = fs_info->fs_devices->latest_bdev;
  723. em->compress_type = async_extent->compress_type;
  724. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  725. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  726. em->generation = -1;
  727. while (1) {
  728. write_lock(&em_tree->lock);
  729. ret = add_extent_mapping(em_tree, em, 1);
  730. write_unlock(&em_tree->lock);
  731. if (ret != -EEXIST) {
  732. free_extent_map(em);
  733. break;
  734. }
  735. btrfs_drop_extent_cache(inode, async_extent->start,
  736. async_extent->start +
  737. async_extent->ram_size - 1, 0);
  738. }
  739. if (ret)
  740. goto out_free_reserve;
  741. ret = btrfs_add_ordered_extent_compress(inode,
  742. async_extent->start,
  743. ins.objectid,
  744. async_extent->ram_size,
  745. ins.offset,
  746. BTRFS_ORDERED_COMPRESSED,
  747. async_extent->compress_type);
  748. if (ret) {
  749. btrfs_drop_extent_cache(inode, async_extent->start,
  750. async_extent->start +
  751. async_extent->ram_size - 1, 0);
  752. goto out_free_reserve;
  753. }
  754. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  755. /*
  756. * clear dirty, set writeback and unlock the pages.
  757. */
  758. extent_clear_unlock_delalloc(inode, async_extent->start,
  759. async_extent->start +
  760. async_extent->ram_size - 1,
  761. async_extent->start +
  762. async_extent->ram_size - 1,
  763. NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
  764. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  765. PAGE_SET_WRITEBACK);
  766. ret = btrfs_submit_compressed_write(inode,
  767. async_extent->start,
  768. async_extent->ram_size,
  769. ins.objectid,
  770. ins.offset, async_extent->pages,
  771. async_extent->nr_pages);
  772. if (ret) {
  773. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  774. struct page *p = async_extent->pages[0];
  775. const u64 start = async_extent->start;
  776. const u64 end = start + async_extent->ram_size - 1;
  777. p->mapping = inode->i_mapping;
  778. tree->ops->writepage_end_io_hook(p, start, end,
  779. NULL, 0);
  780. p->mapping = NULL;
  781. extent_clear_unlock_delalloc(inode, start, end, end,
  782. NULL, 0,
  783. PAGE_END_WRITEBACK |
  784. PAGE_SET_ERROR);
  785. free_async_extent_pages(async_extent);
  786. }
  787. alloc_hint = ins.objectid + ins.offset;
  788. kfree(async_extent);
  789. cond_resched();
  790. }
  791. return;
  792. out_free_reserve:
  793. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  794. btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
  795. out_free:
  796. extent_clear_unlock_delalloc(inode, async_extent->start,
  797. async_extent->start +
  798. async_extent->ram_size - 1,
  799. async_extent->start +
  800. async_extent->ram_size - 1,
  801. NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
  802. EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
  803. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  804. PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
  805. PAGE_SET_ERROR);
  806. free_async_extent_pages(async_extent);
  807. kfree(async_extent);
  808. goto again;
  809. }
  810. static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
  811. u64 num_bytes)
  812. {
  813. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  814. struct extent_map *em;
  815. u64 alloc_hint = 0;
  816. read_lock(&em_tree->lock);
  817. em = search_extent_mapping(em_tree, start, num_bytes);
  818. if (em) {
  819. /*
  820. * if block start isn't an actual block number then find the
  821. * first block in this inode and use that as a hint. If that
  822. * block is also bogus then just don't worry about it.
  823. */
  824. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  825. free_extent_map(em);
  826. em = search_extent_mapping(em_tree, 0, 0);
  827. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  828. alloc_hint = em->block_start;
  829. if (em)
  830. free_extent_map(em);
  831. } else {
  832. alloc_hint = em->block_start;
  833. free_extent_map(em);
  834. }
  835. }
  836. read_unlock(&em_tree->lock);
  837. return alloc_hint;
  838. }
  839. /*
  840. * when extent_io.c finds a delayed allocation range in the file,
  841. * the call backs end up in this code. The basic idea is to
  842. * allocate extents on disk for the range, and create ordered data structs
  843. * in ram to track those extents.
  844. *
  845. * locked_page is the page that writepage had locked already. We use
  846. * it to make sure we don't do extra locks or unlocks.
  847. *
  848. * *page_started is set to one if we unlock locked_page and do everything
  849. * required to start IO on it. It may be clean and already done with
  850. * IO when we return.
  851. */
  852. static noinline int cow_file_range(struct inode *inode,
  853. struct page *locked_page,
  854. u64 start, u64 end, u64 delalloc_end,
  855. int *page_started, unsigned long *nr_written,
  856. int unlock, struct btrfs_dedupe_hash *hash)
  857. {
  858. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  859. struct btrfs_root *root = BTRFS_I(inode)->root;
  860. u64 alloc_hint = 0;
  861. u64 num_bytes;
  862. unsigned long ram_size;
  863. u64 disk_num_bytes;
  864. u64 cur_alloc_size;
  865. u64 blocksize = fs_info->sectorsize;
  866. struct btrfs_key ins;
  867. struct extent_map *em;
  868. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  869. int ret = 0;
  870. if (btrfs_is_free_space_inode(inode)) {
  871. WARN_ON_ONCE(1);
  872. ret = -EINVAL;
  873. goto out_unlock;
  874. }
  875. num_bytes = ALIGN(end - start + 1, blocksize);
  876. num_bytes = max(blocksize, num_bytes);
  877. disk_num_bytes = num_bytes;
  878. /* if this is a small write inside eof, kick off defrag */
  879. if (num_bytes < SZ_64K &&
  880. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  881. btrfs_add_inode_defrag(NULL, inode);
  882. if (start == 0) {
  883. /* lets try to make an inline extent */
  884. ret = cow_file_range_inline(root, inode, start, end, 0, 0,
  885. NULL);
  886. if (ret == 0) {
  887. extent_clear_unlock_delalloc(inode, start, end,
  888. delalloc_end, NULL,
  889. EXTENT_LOCKED | EXTENT_DELALLOC |
  890. EXTENT_DEFRAG, PAGE_UNLOCK |
  891. PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
  892. PAGE_END_WRITEBACK);
  893. btrfs_free_reserved_data_space_noquota(inode, start,
  894. end - start + 1);
  895. *nr_written = *nr_written +
  896. (end - start + PAGE_SIZE) / PAGE_SIZE;
  897. *page_started = 1;
  898. goto out;
  899. } else if (ret < 0) {
  900. goto out_unlock;
  901. }
  902. }
  903. BUG_ON(disk_num_bytes >
  904. btrfs_super_total_bytes(fs_info->super_copy));
  905. alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
  906. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  907. while (disk_num_bytes > 0) {
  908. unsigned long op;
  909. cur_alloc_size = disk_num_bytes;
  910. ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
  911. fs_info->sectorsize, 0, alloc_hint,
  912. &ins, 1, 1);
  913. if (ret < 0)
  914. goto out_unlock;
  915. em = alloc_extent_map();
  916. if (!em) {
  917. ret = -ENOMEM;
  918. goto out_reserve;
  919. }
  920. em->start = start;
  921. em->orig_start = em->start;
  922. ram_size = ins.offset;
  923. em->len = ins.offset;
  924. em->mod_start = em->start;
  925. em->mod_len = em->len;
  926. em->block_start = ins.objectid;
  927. em->block_len = ins.offset;
  928. em->orig_block_len = ins.offset;
  929. em->ram_bytes = ram_size;
  930. em->bdev = fs_info->fs_devices->latest_bdev;
  931. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  932. em->generation = -1;
  933. while (1) {
  934. write_lock(&em_tree->lock);
  935. ret = add_extent_mapping(em_tree, em, 1);
  936. write_unlock(&em_tree->lock);
  937. if (ret != -EEXIST) {
  938. free_extent_map(em);
  939. break;
  940. }
  941. btrfs_drop_extent_cache(inode, start,
  942. start + ram_size - 1, 0);
  943. }
  944. if (ret)
  945. goto out_reserve;
  946. cur_alloc_size = ins.offset;
  947. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  948. ram_size, cur_alloc_size, 0);
  949. if (ret)
  950. goto out_drop_extent_cache;
  951. if (root->root_key.objectid ==
  952. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  953. ret = btrfs_reloc_clone_csums(inode, start,
  954. cur_alloc_size);
  955. if (ret)
  956. goto out_drop_extent_cache;
  957. }
  958. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  959. if (disk_num_bytes < cur_alloc_size)
  960. break;
  961. /* we're not doing compressed IO, don't unlock the first
  962. * page (which the caller expects to stay locked), don't
  963. * clear any dirty bits and don't set any writeback bits
  964. *
  965. * Do set the Private2 bit so we know this page was properly
  966. * setup for writepage
  967. */
  968. op = unlock ? PAGE_UNLOCK : 0;
  969. op |= PAGE_SET_PRIVATE2;
  970. extent_clear_unlock_delalloc(inode, start,
  971. start + ram_size - 1,
  972. delalloc_end, locked_page,
  973. EXTENT_LOCKED | EXTENT_DELALLOC,
  974. op);
  975. disk_num_bytes -= cur_alloc_size;
  976. num_bytes -= cur_alloc_size;
  977. alloc_hint = ins.objectid + ins.offset;
  978. start += cur_alloc_size;
  979. }
  980. out:
  981. return ret;
  982. out_drop_extent_cache:
  983. btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
  984. out_reserve:
  985. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  986. btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
  987. out_unlock:
  988. extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
  989. locked_page,
  990. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  991. EXTENT_DELALLOC | EXTENT_DEFRAG,
  992. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  993. PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
  994. goto out;
  995. }
  996. /*
  997. * work queue call back to started compression on a file and pages
  998. */
  999. static noinline void async_cow_start(struct btrfs_work *work)
  1000. {
  1001. struct async_cow *async_cow;
  1002. int num_added = 0;
  1003. async_cow = container_of(work, struct async_cow, work);
  1004. compress_file_range(async_cow->inode, async_cow->locked_page,
  1005. async_cow->start, async_cow->end, async_cow,
  1006. &num_added);
  1007. if (num_added == 0) {
  1008. btrfs_add_delayed_iput(async_cow->inode);
  1009. async_cow->inode = NULL;
  1010. }
  1011. }
  1012. /*
  1013. * work queue call back to submit previously compressed pages
  1014. */
  1015. static noinline void async_cow_submit(struct btrfs_work *work)
  1016. {
  1017. struct btrfs_fs_info *fs_info;
  1018. struct async_cow *async_cow;
  1019. struct btrfs_root *root;
  1020. unsigned long nr_pages;
  1021. async_cow = container_of(work, struct async_cow, work);
  1022. root = async_cow->root;
  1023. fs_info = root->fs_info;
  1024. nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
  1025. PAGE_SHIFT;
  1026. /*
  1027. * atomic_sub_return implies a barrier for waitqueue_active
  1028. */
  1029. if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
  1030. 5 * SZ_1M &&
  1031. waitqueue_active(&fs_info->async_submit_wait))
  1032. wake_up(&fs_info->async_submit_wait);
  1033. if (async_cow->inode)
  1034. submit_compressed_extents(async_cow->inode, async_cow);
  1035. }
  1036. static noinline void async_cow_free(struct btrfs_work *work)
  1037. {
  1038. struct async_cow *async_cow;
  1039. async_cow = container_of(work, struct async_cow, work);
  1040. if (async_cow->inode)
  1041. btrfs_add_delayed_iput(async_cow->inode);
  1042. kfree(async_cow);
  1043. }
  1044. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  1045. u64 start, u64 end, int *page_started,
  1046. unsigned long *nr_written)
  1047. {
  1048. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1049. struct async_cow *async_cow;
  1050. struct btrfs_root *root = BTRFS_I(inode)->root;
  1051. unsigned long nr_pages;
  1052. u64 cur_end;
  1053. int limit = 10 * SZ_1M;
  1054. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  1055. 1, 0, NULL, GFP_NOFS);
  1056. while (start < end) {
  1057. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  1058. BUG_ON(!async_cow); /* -ENOMEM */
  1059. async_cow->inode = igrab(inode);
  1060. async_cow->root = root;
  1061. async_cow->locked_page = locked_page;
  1062. async_cow->start = start;
  1063. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
  1064. !btrfs_test_opt(fs_info, FORCE_COMPRESS))
  1065. cur_end = end;
  1066. else
  1067. cur_end = min(end, start + SZ_512K - 1);
  1068. async_cow->end = cur_end;
  1069. INIT_LIST_HEAD(&async_cow->extents);
  1070. btrfs_init_work(&async_cow->work,
  1071. btrfs_delalloc_helper,
  1072. async_cow_start, async_cow_submit,
  1073. async_cow_free);
  1074. nr_pages = (cur_end - start + PAGE_SIZE) >>
  1075. PAGE_SHIFT;
  1076. atomic_add(nr_pages, &fs_info->async_delalloc_pages);
  1077. btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
  1078. if (atomic_read(&fs_info->async_delalloc_pages) > limit) {
  1079. wait_event(fs_info->async_submit_wait,
  1080. (atomic_read(&fs_info->async_delalloc_pages) <
  1081. limit));
  1082. }
  1083. while (atomic_read(&fs_info->async_submit_draining) &&
  1084. atomic_read(&fs_info->async_delalloc_pages)) {
  1085. wait_event(fs_info->async_submit_wait,
  1086. (atomic_read(&fs_info->async_delalloc_pages) ==
  1087. 0));
  1088. }
  1089. *nr_written += nr_pages;
  1090. start = cur_end + 1;
  1091. }
  1092. *page_started = 1;
  1093. return 0;
  1094. }
  1095. static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
  1096. u64 bytenr, u64 num_bytes)
  1097. {
  1098. int ret;
  1099. struct btrfs_ordered_sum *sums;
  1100. LIST_HEAD(list);
  1101. ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
  1102. bytenr + num_bytes - 1, &list, 0);
  1103. if (ret == 0 && list_empty(&list))
  1104. return 0;
  1105. while (!list_empty(&list)) {
  1106. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  1107. list_del(&sums->list);
  1108. kfree(sums);
  1109. }
  1110. return 1;
  1111. }
  1112. /*
  1113. * when nowcow writeback call back. This checks for snapshots or COW copies
  1114. * of the extents that exist in the file, and COWs the file as required.
  1115. *
  1116. * If no cow copies or snapshots exist, we write directly to the existing
  1117. * blocks on disk
  1118. */
  1119. static noinline int run_delalloc_nocow(struct inode *inode,
  1120. struct page *locked_page,
  1121. u64 start, u64 end, int *page_started, int force,
  1122. unsigned long *nr_written)
  1123. {
  1124. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1125. struct btrfs_root *root = BTRFS_I(inode)->root;
  1126. struct btrfs_trans_handle *trans;
  1127. struct extent_buffer *leaf;
  1128. struct btrfs_path *path;
  1129. struct btrfs_file_extent_item *fi;
  1130. struct btrfs_key found_key;
  1131. u64 cow_start;
  1132. u64 cur_offset;
  1133. u64 extent_end;
  1134. u64 extent_offset;
  1135. u64 disk_bytenr;
  1136. u64 num_bytes;
  1137. u64 disk_num_bytes;
  1138. u64 ram_bytes;
  1139. int extent_type;
  1140. int ret, err;
  1141. int type;
  1142. int nocow;
  1143. int check_prev = 1;
  1144. bool nolock;
  1145. u64 ino = btrfs_ino(inode);
  1146. path = btrfs_alloc_path();
  1147. if (!path) {
  1148. extent_clear_unlock_delalloc(inode, start, end, end,
  1149. locked_page,
  1150. EXTENT_LOCKED | EXTENT_DELALLOC |
  1151. EXTENT_DO_ACCOUNTING |
  1152. EXTENT_DEFRAG, PAGE_UNLOCK |
  1153. PAGE_CLEAR_DIRTY |
  1154. PAGE_SET_WRITEBACK |
  1155. PAGE_END_WRITEBACK);
  1156. return -ENOMEM;
  1157. }
  1158. nolock = btrfs_is_free_space_inode(inode);
  1159. if (nolock)
  1160. trans = btrfs_join_transaction_nolock(root);
  1161. else
  1162. trans = btrfs_join_transaction(root);
  1163. if (IS_ERR(trans)) {
  1164. extent_clear_unlock_delalloc(inode, start, end, end,
  1165. locked_page,
  1166. EXTENT_LOCKED | EXTENT_DELALLOC |
  1167. EXTENT_DO_ACCOUNTING |
  1168. EXTENT_DEFRAG, PAGE_UNLOCK |
  1169. PAGE_CLEAR_DIRTY |
  1170. PAGE_SET_WRITEBACK |
  1171. PAGE_END_WRITEBACK);
  1172. btrfs_free_path(path);
  1173. return PTR_ERR(trans);
  1174. }
  1175. trans->block_rsv = &fs_info->delalloc_block_rsv;
  1176. cow_start = (u64)-1;
  1177. cur_offset = start;
  1178. while (1) {
  1179. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  1180. cur_offset, 0);
  1181. if (ret < 0)
  1182. goto error;
  1183. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  1184. leaf = path->nodes[0];
  1185. btrfs_item_key_to_cpu(leaf, &found_key,
  1186. path->slots[0] - 1);
  1187. if (found_key.objectid == ino &&
  1188. found_key.type == BTRFS_EXTENT_DATA_KEY)
  1189. path->slots[0]--;
  1190. }
  1191. check_prev = 0;
  1192. next_slot:
  1193. leaf = path->nodes[0];
  1194. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1195. ret = btrfs_next_leaf(root, path);
  1196. if (ret < 0)
  1197. goto error;
  1198. if (ret > 0)
  1199. break;
  1200. leaf = path->nodes[0];
  1201. }
  1202. nocow = 0;
  1203. disk_bytenr = 0;
  1204. num_bytes = 0;
  1205. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1206. if (found_key.objectid > ino)
  1207. break;
  1208. if (WARN_ON_ONCE(found_key.objectid < ino) ||
  1209. found_key.type < BTRFS_EXTENT_DATA_KEY) {
  1210. path->slots[0]++;
  1211. goto next_slot;
  1212. }
  1213. if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
  1214. found_key.offset > end)
  1215. break;
  1216. if (found_key.offset > cur_offset) {
  1217. extent_end = found_key.offset;
  1218. extent_type = 0;
  1219. goto out_check;
  1220. }
  1221. fi = btrfs_item_ptr(leaf, path->slots[0],
  1222. struct btrfs_file_extent_item);
  1223. extent_type = btrfs_file_extent_type(leaf, fi);
  1224. ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  1225. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  1226. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1227. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1228. extent_offset = btrfs_file_extent_offset(leaf, fi);
  1229. extent_end = found_key.offset +
  1230. btrfs_file_extent_num_bytes(leaf, fi);
  1231. disk_num_bytes =
  1232. btrfs_file_extent_disk_num_bytes(leaf, fi);
  1233. if (extent_end <= start) {
  1234. path->slots[0]++;
  1235. goto next_slot;
  1236. }
  1237. if (disk_bytenr == 0)
  1238. goto out_check;
  1239. if (btrfs_file_extent_compression(leaf, fi) ||
  1240. btrfs_file_extent_encryption(leaf, fi) ||
  1241. btrfs_file_extent_other_encoding(leaf, fi))
  1242. goto out_check;
  1243. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  1244. goto out_check;
  1245. if (btrfs_extent_readonly(fs_info, disk_bytenr))
  1246. goto out_check;
  1247. if (btrfs_cross_ref_exist(trans, root, ino,
  1248. found_key.offset -
  1249. extent_offset, disk_bytenr))
  1250. goto out_check;
  1251. disk_bytenr += extent_offset;
  1252. disk_bytenr += cur_offset - found_key.offset;
  1253. num_bytes = min(end + 1, extent_end) - cur_offset;
  1254. /*
  1255. * if there are pending snapshots for this root,
  1256. * we fall into common COW way.
  1257. */
  1258. if (!nolock) {
  1259. err = btrfs_start_write_no_snapshoting(root);
  1260. if (!err)
  1261. goto out_check;
  1262. }
  1263. /*
  1264. * force cow if csum exists in the range.
  1265. * this ensure that csum for a given extent are
  1266. * either valid or do not exist.
  1267. */
  1268. if (csum_exist_in_range(fs_info, disk_bytenr,
  1269. num_bytes))
  1270. goto out_check;
  1271. if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
  1272. goto out_check;
  1273. nocow = 1;
  1274. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1275. extent_end = found_key.offset +
  1276. btrfs_file_extent_inline_len(leaf,
  1277. path->slots[0], fi);
  1278. extent_end = ALIGN(extent_end,
  1279. fs_info->sectorsize);
  1280. } else {
  1281. BUG_ON(1);
  1282. }
  1283. out_check:
  1284. if (extent_end <= start) {
  1285. path->slots[0]++;
  1286. if (!nolock && nocow)
  1287. btrfs_end_write_no_snapshoting(root);
  1288. if (nocow)
  1289. btrfs_dec_nocow_writers(fs_info, disk_bytenr);
  1290. goto next_slot;
  1291. }
  1292. if (!nocow) {
  1293. if (cow_start == (u64)-1)
  1294. cow_start = cur_offset;
  1295. cur_offset = extent_end;
  1296. if (cur_offset > end)
  1297. break;
  1298. path->slots[0]++;
  1299. goto next_slot;
  1300. }
  1301. btrfs_release_path(path);
  1302. if (cow_start != (u64)-1) {
  1303. ret = cow_file_range(inode, locked_page,
  1304. cow_start, found_key.offset - 1,
  1305. end, page_started, nr_written, 1,
  1306. NULL);
  1307. if (ret) {
  1308. if (!nolock && nocow)
  1309. btrfs_end_write_no_snapshoting(root);
  1310. if (nocow)
  1311. btrfs_dec_nocow_writers(fs_info,
  1312. disk_bytenr);
  1313. goto error;
  1314. }
  1315. cow_start = (u64)-1;
  1316. }
  1317. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1318. struct extent_map *em;
  1319. struct extent_map_tree *em_tree;
  1320. em_tree = &BTRFS_I(inode)->extent_tree;
  1321. em = alloc_extent_map();
  1322. BUG_ON(!em); /* -ENOMEM */
  1323. em->start = cur_offset;
  1324. em->orig_start = found_key.offset - extent_offset;
  1325. em->len = num_bytes;
  1326. em->block_len = num_bytes;
  1327. em->block_start = disk_bytenr;
  1328. em->orig_block_len = disk_num_bytes;
  1329. em->ram_bytes = ram_bytes;
  1330. em->bdev = fs_info->fs_devices->latest_bdev;
  1331. em->mod_start = em->start;
  1332. em->mod_len = em->len;
  1333. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1334. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  1335. em->generation = -1;
  1336. while (1) {
  1337. write_lock(&em_tree->lock);
  1338. ret = add_extent_mapping(em_tree, em, 1);
  1339. write_unlock(&em_tree->lock);
  1340. if (ret != -EEXIST) {
  1341. free_extent_map(em);
  1342. break;
  1343. }
  1344. btrfs_drop_extent_cache(inode, em->start,
  1345. em->start + em->len - 1, 0);
  1346. }
  1347. type = BTRFS_ORDERED_PREALLOC;
  1348. } else {
  1349. type = BTRFS_ORDERED_NOCOW;
  1350. }
  1351. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1352. num_bytes, num_bytes, type);
  1353. if (nocow)
  1354. btrfs_dec_nocow_writers(fs_info, disk_bytenr);
  1355. BUG_ON(ret); /* -ENOMEM */
  1356. if (root->root_key.objectid ==
  1357. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  1358. ret = btrfs_reloc_clone_csums(inode, cur_offset,
  1359. num_bytes);
  1360. if (ret) {
  1361. if (!nolock && nocow)
  1362. btrfs_end_write_no_snapshoting(root);
  1363. goto error;
  1364. }
  1365. }
  1366. extent_clear_unlock_delalloc(inode, cur_offset,
  1367. cur_offset + num_bytes - 1, end,
  1368. locked_page, EXTENT_LOCKED |
  1369. EXTENT_DELALLOC |
  1370. EXTENT_CLEAR_DATA_RESV,
  1371. PAGE_UNLOCK | PAGE_SET_PRIVATE2);
  1372. if (!nolock && nocow)
  1373. btrfs_end_write_no_snapshoting(root);
  1374. cur_offset = extent_end;
  1375. if (cur_offset > end)
  1376. break;
  1377. }
  1378. btrfs_release_path(path);
  1379. if (cur_offset <= end && cow_start == (u64)-1) {
  1380. cow_start = cur_offset;
  1381. cur_offset = end;
  1382. }
  1383. if (cow_start != (u64)-1) {
  1384. ret = cow_file_range(inode, locked_page, cow_start, end, end,
  1385. page_started, nr_written, 1, NULL);
  1386. if (ret)
  1387. goto error;
  1388. }
  1389. error:
  1390. err = btrfs_end_transaction(trans);
  1391. if (!ret)
  1392. ret = err;
  1393. if (ret && cur_offset < end)
  1394. extent_clear_unlock_delalloc(inode, cur_offset, end, end,
  1395. locked_page, EXTENT_LOCKED |
  1396. EXTENT_DELALLOC | EXTENT_DEFRAG |
  1397. EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
  1398. PAGE_CLEAR_DIRTY |
  1399. PAGE_SET_WRITEBACK |
  1400. PAGE_END_WRITEBACK);
  1401. btrfs_free_path(path);
  1402. return ret;
  1403. }
  1404. static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
  1405. {
  1406. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  1407. !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
  1408. return 0;
  1409. /*
  1410. * @defrag_bytes is a hint value, no spinlock held here,
  1411. * if is not zero, it means the file is defragging.
  1412. * Force cow if given extent needs to be defragged.
  1413. */
  1414. if (BTRFS_I(inode)->defrag_bytes &&
  1415. test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  1416. EXTENT_DEFRAG, 0, NULL))
  1417. return 1;
  1418. return 0;
  1419. }
  1420. /*
  1421. * extent_io.c call back to do delayed allocation processing
  1422. */
  1423. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1424. u64 start, u64 end, int *page_started,
  1425. unsigned long *nr_written)
  1426. {
  1427. int ret;
  1428. int force_cow = need_force_cow(inode, start, end);
  1429. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
  1430. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1431. page_started, 1, nr_written);
  1432. } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
  1433. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1434. page_started, 0, nr_written);
  1435. } else if (!inode_need_compress(inode)) {
  1436. ret = cow_file_range(inode, locked_page, start, end, end,
  1437. page_started, nr_written, 1, NULL);
  1438. } else {
  1439. set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1440. &BTRFS_I(inode)->runtime_flags);
  1441. ret = cow_file_range_async(inode, locked_page, start, end,
  1442. page_started, nr_written);
  1443. }
  1444. return ret;
  1445. }
  1446. static void btrfs_split_extent_hook(struct inode *inode,
  1447. struct extent_state *orig, u64 split)
  1448. {
  1449. u64 size;
  1450. /* not delalloc, ignore it */
  1451. if (!(orig->state & EXTENT_DELALLOC))
  1452. return;
  1453. size = orig->end - orig->start + 1;
  1454. if (size > BTRFS_MAX_EXTENT_SIZE) {
  1455. u64 num_extents;
  1456. u64 new_size;
  1457. /*
  1458. * See the explanation in btrfs_merge_extent_hook, the same
  1459. * applies here, just in reverse.
  1460. */
  1461. new_size = orig->end - split + 1;
  1462. num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1463. BTRFS_MAX_EXTENT_SIZE);
  1464. new_size = split - orig->start;
  1465. num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1466. BTRFS_MAX_EXTENT_SIZE);
  1467. if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
  1468. BTRFS_MAX_EXTENT_SIZE) >= num_extents)
  1469. return;
  1470. }
  1471. spin_lock(&BTRFS_I(inode)->lock);
  1472. BTRFS_I(inode)->outstanding_extents++;
  1473. spin_unlock(&BTRFS_I(inode)->lock);
  1474. }
  1475. /*
  1476. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1477. * extents so we can keep track of new extents that are just merged onto old
  1478. * extents, such as when we are doing sequential writes, so we can properly
  1479. * account for the metadata space we'll need.
  1480. */
  1481. static void btrfs_merge_extent_hook(struct inode *inode,
  1482. struct extent_state *new,
  1483. struct extent_state *other)
  1484. {
  1485. u64 new_size, old_size;
  1486. u64 num_extents;
  1487. /* not delalloc, ignore it */
  1488. if (!(other->state & EXTENT_DELALLOC))
  1489. return;
  1490. if (new->start > other->start)
  1491. new_size = new->end - other->start + 1;
  1492. else
  1493. new_size = other->end - new->start + 1;
  1494. /* we're not bigger than the max, unreserve the space and go */
  1495. if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
  1496. spin_lock(&BTRFS_I(inode)->lock);
  1497. BTRFS_I(inode)->outstanding_extents--;
  1498. spin_unlock(&BTRFS_I(inode)->lock);
  1499. return;
  1500. }
  1501. /*
  1502. * We have to add up either side to figure out how many extents were
  1503. * accounted for before we merged into one big extent. If the number of
  1504. * extents we accounted for is <= the amount we need for the new range
  1505. * then we can return, otherwise drop. Think of it like this
  1506. *
  1507. * [ 4k][MAX_SIZE]
  1508. *
  1509. * So we've grown the extent by a MAX_SIZE extent, this would mean we
  1510. * need 2 outstanding extents, on one side we have 1 and the other side
  1511. * we have 1 so they are == and we can return. But in this case
  1512. *
  1513. * [MAX_SIZE+4k][MAX_SIZE+4k]
  1514. *
  1515. * Each range on their own accounts for 2 extents, but merged together
  1516. * they are only 3 extents worth of accounting, so we need to drop in
  1517. * this case.
  1518. */
  1519. old_size = other->end - other->start + 1;
  1520. num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1521. BTRFS_MAX_EXTENT_SIZE);
  1522. old_size = new->end - new->start + 1;
  1523. num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1524. BTRFS_MAX_EXTENT_SIZE);
  1525. if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1526. BTRFS_MAX_EXTENT_SIZE) >= num_extents)
  1527. return;
  1528. spin_lock(&BTRFS_I(inode)->lock);
  1529. BTRFS_I(inode)->outstanding_extents--;
  1530. spin_unlock(&BTRFS_I(inode)->lock);
  1531. }
  1532. static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
  1533. struct inode *inode)
  1534. {
  1535. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1536. spin_lock(&root->delalloc_lock);
  1537. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1538. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1539. &root->delalloc_inodes);
  1540. set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1541. &BTRFS_I(inode)->runtime_flags);
  1542. root->nr_delalloc_inodes++;
  1543. if (root->nr_delalloc_inodes == 1) {
  1544. spin_lock(&fs_info->delalloc_root_lock);
  1545. BUG_ON(!list_empty(&root->delalloc_root));
  1546. list_add_tail(&root->delalloc_root,
  1547. &fs_info->delalloc_roots);
  1548. spin_unlock(&fs_info->delalloc_root_lock);
  1549. }
  1550. }
  1551. spin_unlock(&root->delalloc_lock);
  1552. }
  1553. static void btrfs_del_delalloc_inode(struct btrfs_root *root,
  1554. struct inode *inode)
  1555. {
  1556. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1557. spin_lock(&root->delalloc_lock);
  1558. if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1559. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1560. clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1561. &BTRFS_I(inode)->runtime_flags);
  1562. root->nr_delalloc_inodes--;
  1563. if (!root->nr_delalloc_inodes) {
  1564. spin_lock(&fs_info->delalloc_root_lock);
  1565. BUG_ON(list_empty(&root->delalloc_root));
  1566. list_del_init(&root->delalloc_root);
  1567. spin_unlock(&fs_info->delalloc_root_lock);
  1568. }
  1569. }
  1570. spin_unlock(&root->delalloc_lock);
  1571. }
  1572. /*
  1573. * extent_io.c set_bit_hook, used to track delayed allocation
  1574. * bytes in this file, and to maintain the list of inodes that
  1575. * have pending delalloc work to be done.
  1576. */
  1577. static void btrfs_set_bit_hook(struct inode *inode,
  1578. struct extent_state *state, unsigned *bits)
  1579. {
  1580. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1581. if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
  1582. WARN_ON(1);
  1583. /*
  1584. * set_bit and clear bit hooks normally require _irqsave/restore
  1585. * but in this case, we are only testing for the DELALLOC
  1586. * bit, which is only set or cleared with irqs on
  1587. */
  1588. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1589. struct btrfs_root *root = BTRFS_I(inode)->root;
  1590. u64 len = state->end + 1 - state->start;
  1591. bool do_list = !btrfs_is_free_space_inode(inode);
  1592. if (*bits & EXTENT_FIRST_DELALLOC) {
  1593. *bits &= ~EXTENT_FIRST_DELALLOC;
  1594. } else {
  1595. spin_lock(&BTRFS_I(inode)->lock);
  1596. BTRFS_I(inode)->outstanding_extents++;
  1597. spin_unlock(&BTRFS_I(inode)->lock);
  1598. }
  1599. /* For sanity tests */
  1600. if (btrfs_is_testing(fs_info))
  1601. return;
  1602. __percpu_counter_add(&fs_info->delalloc_bytes, len,
  1603. fs_info->delalloc_batch);
  1604. spin_lock(&BTRFS_I(inode)->lock);
  1605. BTRFS_I(inode)->delalloc_bytes += len;
  1606. if (*bits & EXTENT_DEFRAG)
  1607. BTRFS_I(inode)->defrag_bytes += len;
  1608. if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1609. &BTRFS_I(inode)->runtime_flags))
  1610. btrfs_add_delalloc_inodes(root, inode);
  1611. spin_unlock(&BTRFS_I(inode)->lock);
  1612. }
  1613. }
  1614. /*
  1615. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1616. */
  1617. static void btrfs_clear_bit_hook(struct inode *inode,
  1618. struct extent_state *state,
  1619. unsigned *bits)
  1620. {
  1621. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1622. u64 len = state->end + 1 - state->start;
  1623. u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
  1624. BTRFS_MAX_EXTENT_SIZE);
  1625. spin_lock(&BTRFS_I(inode)->lock);
  1626. if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
  1627. BTRFS_I(inode)->defrag_bytes -= len;
  1628. spin_unlock(&BTRFS_I(inode)->lock);
  1629. /*
  1630. * set_bit and clear bit hooks normally require _irqsave/restore
  1631. * but in this case, we are only testing for the DELALLOC
  1632. * bit, which is only set or cleared with irqs on
  1633. */
  1634. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1635. struct btrfs_root *root = BTRFS_I(inode)->root;
  1636. bool do_list = !btrfs_is_free_space_inode(inode);
  1637. if (*bits & EXTENT_FIRST_DELALLOC) {
  1638. *bits &= ~EXTENT_FIRST_DELALLOC;
  1639. } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
  1640. spin_lock(&BTRFS_I(inode)->lock);
  1641. BTRFS_I(inode)->outstanding_extents -= num_extents;
  1642. spin_unlock(&BTRFS_I(inode)->lock);
  1643. }
  1644. /*
  1645. * We don't reserve metadata space for space cache inodes so we
  1646. * don't need to call dellalloc_release_metadata if there is an
  1647. * error.
  1648. */
  1649. if (*bits & EXTENT_DO_ACCOUNTING &&
  1650. root != fs_info->tree_root)
  1651. btrfs_delalloc_release_metadata(inode, len);
  1652. /* For sanity tests. */
  1653. if (btrfs_is_testing(fs_info))
  1654. return;
  1655. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  1656. && do_list && !(state->state & EXTENT_NORESERVE)
  1657. && (*bits & (EXTENT_DO_ACCOUNTING |
  1658. EXTENT_CLEAR_DATA_RESV)))
  1659. btrfs_free_reserved_data_space_noquota(inode,
  1660. state->start, len);
  1661. __percpu_counter_add(&fs_info->delalloc_bytes, -len,
  1662. fs_info->delalloc_batch);
  1663. spin_lock(&BTRFS_I(inode)->lock);
  1664. BTRFS_I(inode)->delalloc_bytes -= len;
  1665. if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
  1666. test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1667. &BTRFS_I(inode)->runtime_flags))
  1668. btrfs_del_delalloc_inode(root, inode);
  1669. spin_unlock(&BTRFS_I(inode)->lock);
  1670. }
  1671. }
  1672. /*
  1673. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1674. * we don't create bios that span stripes or chunks
  1675. *
  1676. * return 1 if page cannot be merged to bio
  1677. * return 0 if page can be merged to bio
  1678. * return error otherwise
  1679. */
  1680. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1681. size_t size, struct bio *bio,
  1682. unsigned long bio_flags)
  1683. {
  1684. struct inode *inode = page->mapping->host;
  1685. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1686. u64 logical = (u64)bio->bi_iter.bi_sector << 9;
  1687. u64 length = 0;
  1688. u64 map_length;
  1689. int ret;
  1690. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1691. return 0;
  1692. length = bio->bi_iter.bi_size;
  1693. map_length = length;
  1694. ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
  1695. NULL, 0);
  1696. if (ret < 0)
  1697. return ret;
  1698. if (map_length < length + size)
  1699. return 1;
  1700. return 0;
  1701. }
  1702. /*
  1703. * in order to insert checksums into the metadata in large chunks,
  1704. * we wait until bio submission time. All the pages in the bio are
  1705. * checksummed and sums are attached onto the ordered extent record.
  1706. *
  1707. * At IO completion time the cums attached on the ordered extent record
  1708. * are inserted into the btree
  1709. */
  1710. static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
  1711. int mirror_num, unsigned long bio_flags,
  1712. u64 bio_offset)
  1713. {
  1714. int ret = 0;
  1715. ret = btrfs_csum_one_bio(inode, bio, 0, 0);
  1716. BUG_ON(ret); /* -ENOMEM */
  1717. return 0;
  1718. }
  1719. /*
  1720. * in order to insert checksums into the metadata in large chunks,
  1721. * we wait until bio submission time. All the pages in the bio are
  1722. * checksummed and sums are attached onto the ordered extent record.
  1723. *
  1724. * At IO completion time the cums attached on the ordered extent record
  1725. * are inserted into the btree
  1726. */
  1727. static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
  1728. int mirror_num, unsigned long bio_flags,
  1729. u64 bio_offset)
  1730. {
  1731. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1732. int ret;
  1733. ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
  1734. if (ret) {
  1735. bio->bi_error = ret;
  1736. bio_endio(bio);
  1737. }
  1738. return ret;
  1739. }
  1740. /*
  1741. * extent_io.c submission hook. This does the right thing for csum calculation
  1742. * on write, or reading the csums from the tree before a read
  1743. */
  1744. static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
  1745. int mirror_num, unsigned long bio_flags,
  1746. u64 bio_offset)
  1747. {
  1748. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1749. struct btrfs_root *root = BTRFS_I(inode)->root;
  1750. enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
  1751. int ret = 0;
  1752. int skip_sum;
  1753. int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
  1754. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1755. if (btrfs_is_free_space_inode(inode))
  1756. metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
  1757. if (bio_op(bio) != REQ_OP_WRITE) {
  1758. ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
  1759. if (ret)
  1760. goto out;
  1761. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1762. ret = btrfs_submit_compressed_read(inode, bio,
  1763. mirror_num,
  1764. bio_flags);
  1765. goto out;
  1766. } else if (!skip_sum) {
  1767. ret = btrfs_lookup_bio_sums(inode, bio, NULL);
  1768. if (ret)
  1769. goto out;
  1770. }
  1771. goto mapit;
  1772. } else if (async && !skip_sum) {
  1773. /* csum items have already been cloned */
  1774. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1775. goto mapit;
  1776. /* we're doing a write, do the async checksumming */
  1777. ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
  1778. bio_flags, bio_offset,
  1779. __btrfs_submit_bio_start,
  1780. __btrfs_submit_bio_done);
  1781. goto out;
  1782. } else if (!skip_sum) {
  1783. ret = btrfs_csum_one_bio(inode, bio, 0, 0);
  1784. if (ret)
  1785. goto out;
  1786. }
  1787. mapit:
  1788. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  1789. out:
  1790. if (ret < 0) {
  1791. bio->bi_error = ret;
  1792. bio_endio(bio);
  1793. }
  1794. return ret;
  1795. }
  1796. /*
  1797. * given a list of ordered sums record them in the inode. This happens
  1798. * at IO completion time based on sums calculated at bio submission time.
  1799. */
  1800. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1801. struct inode *inode, u64 file_offset,
  1802. struct list_head *list)
  1803. {
  1804. struct btrfs_ordered_sum *sum;
  1805. list_for_each_entry(sum, list, list) {
  1806. trans->adding_csums = 1;
  1807. btrfs_csum_file_blocks(trans,
  1808. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1809. trans->adding_csums = 0;
  1810. }
  1811. return 0;
  1812. }
  1813. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1814. struct extent_state **cached_state, int dedupe)
  1815. {
  1816. WARN_ON((end & (PAGE_SIZE - 1)) == 0);
  1817. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1818. cached_state);
  1819. }
  1820. /* see btrfs_writepage_start_hook for details on why this is required */
  1821. struct btrfs_writepage_fixup {
  1822. struct page *page;
  1823. struct btrfs_work work;
  1824. };
  1825. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1826. {
  1827. struct btrfs_writepage_fixup *fixup;
  1828. struct btrfs_ordered_extent *ordered;
  1829. struct extent_state *cached_state = NULL;
  1830. struct page *page;
  1831. struct inode *inode;
  1832. u64 page_start;
  1833. u64 page_end;
  1834. int ret;
  1835. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1836. page = fixup->page;
  1837. again:
  1838. lock_page(page);
  1839. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1840. ClearPageChecked(page);
  1841. goto out_page;
  1842. }
  1843. inode = page->mapping->host;
  1844. page_start = page_offset(page);
  1845. page_end = page_offset(page) + PAGE_SIZE - 1;
  1846. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1847. &cached_state);
  1848. /* already ordered? We're done */
  1849. if (PagePrivate2(page))
  1850. goto out;
  1851. ordered = btrfs_lookup_ordered_range(inode, page_start,
  1852. PAGE_SIZE);
  1853. if (ordered) {
  1854. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1855. page_end, &cached_state, GFP_NOFS);
  1856. unlock_page(page);
  1857. btrfs_start_ordered_extent(inode, ordered, 1);
  1858. btrfs_put_ordered_extent(ordered);
  1859. goto again;
  1860. }
  1861. ret = btrfs_delalloc_reserve_space(inode, page_start,
  1862. PAGE_SIZE);
  1863. if (ret) {
  1864. mapping_set_error(page->mapping, ret);
  1865. end_extent_writepage(page, ret, page_start, page_end);
  1866. ClearPageChecked(page);
  1867. goto out;
  1868. }
  1869. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
  1870. 0);
  1871. ClearPageChecked(page);
  1872. set_page_dirty(page);
  1873. out:
  1874. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1875. &cached_state, GFP_NOFS);
  1876. out_page:
  1877. unlock_page(page);
  1878. put_page(page);
  1879. kfree(fixup);
  1880. }
  1881. /*
  1882. * There are a few paths in the higher layers of the kernel that directly
  1883. * set the page dirty bit without asking the filesystem if it is a
  1884. * good idea. This causes problems because we want to make sure COW
  1885. * properly happens and the data=ordered rules are followed.
  1886. *
  1887. * In our case any range that doesn't have the ORDERED bit set
  1888. * hasn't been properly setup for IO. We kick off an async process
  1889. * to fix it up. The async helper will wait for ordered extents, set
  1890. * the delalloc bit and make it safe to write the page.
  1891. */
  1892. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1893. {
  1894. struct inode *inode = page->mapping->host;
  1895. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1896. struct btrfs_writepage_fixup *fixup;
  1897. /* this page is properly in the ordered list */
  1898. if (TestClearPagePrivate2(page))
  1899. return 0;
  1900. if (PageChecked(page))
  1901. return -EAGAIN;
  1902. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1903. if (!fixup)
  1904. return -EAGAIN;
  1905. SetPageChecked(page);
  1906. get_page(page);
  1907. btrfs_init_work(&fixup->work, btrfs_fixup_helper,
  1908. btrfs_writepage_fixup_worker, NULL, NULL);
  1909. fixup->page = page;
  1910. btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
  1911. return -EBUSY;
  1912. }
  1913. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1914. struct inode *inode, u64 file_pos,
  1915. u64 disk_bytenr, u64 disk_num_bytes,
  1916. u64 num_bytes, u64 ram_bytes,
  1917. u8 compression, u8 encryption,
  1918. u16 other_encoding, int extent_type)
  1919. {
  1920. struct btrfs_root *root = BTRFS_I(inode)->root;
  1921. struct btrfs_file_extent_item *fi;
  1922. struct btrfs_path *path;
  1923. struct extent_buffer *leaf;
  1924. struct btrfs_key ins;
  1925. int extent_inserted = 0;
  1926. int ret;
  1927. path = btrfs_alloc_path();
  1928. if (!path)
  1929. return -ENOMEM;
  1930. /*
  1931. * we may be replacing one extent in the tree with another.
  1932. * The new extent is pinned in the extent map, and we don't want
  1933. * to drop it from the cache until it is completely in the btree.
  1934. *
  1935. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1936. * the caller is expected to unpin it and allow it to be merged
  1937. * with the others.
  1938. */
  1939. ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
  1940. file_pos + num_bytes, NULL, 0,
  1941. 1, sizeof(*fi), &extent_inserted);
  1942. if (ret)
  1943. goto out;
  1944. if (!extent_inserted) {
  1945. ins.objectid = btrfs_ino(inode);
  1946. ins.offset = file_pos;
  1947. ins.type = BTRFS_EXTENT_DATA_KEY;
  1948. path->leave_spinning = 1;
  1949. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  1950. sizeof(*fi));
  1951. if (ret)
  1952. goto out;
  1953. }
  1954. leaf = path->nodes[0];
  1955. fi = btrfs_item_ptr(leaf, path->slots[0],
  1956. struct btrfs_file_extent_item);
  1957. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1958. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1959. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1960. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1961. btrfs_set_file_extent_offset(leaf, fi, 0);
  1962. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1963. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1964. btrfs_set_file_extent_compression(leaf, fi, compression);
  1965. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1966. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1967. btrfs_mark_buffer_dirty(leaf);
  1968. btrfs_release_path(path);
  1969. inode_add_bytes(inode, num_bytes);
  1970. ins.objectid = disk_bytenr;
  1971. ins.offset = disk_num_bytes;
  1972. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1973. ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
  1974. btrfs_ino(inode), file_pos,
  1975. ram_bytes, &ins);
  1976. /*
  1977. * Release the reserved range from inode dirty range map, as it is
  1978. * already moved into delayed_ref_head
  1979. */
  1980. btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
  1981. out:
  1982. btrfs_free_path(path);
  1983. return ret;
  1984. }
  1985. /* snapshot-aware defrag */
  1986. struct sa_defrag_extent_backref {
  1987. struct rb_node node;
  1988. struct old_sa_defrag_extent *old;
  1989. u64 root_id;
  1990. u64 inum;
  1991. u64 file_pos;
  1992. u64 extent_offset;
  1993. u64 num_bytes;
  1994. u64 generation;
  1995. };
  1996. struct old_sa_defrag_extent {
  1997. struct list_head list;
  1998. struct new_sa_defrag_extent *new;
  1999. u64 extent_offset;
  2000. u64 bytenr;
  2001. u64 offset;
  2002. u64 len;
  2003. int count;
  2004. };
  2005. struct new_sa_defrag_extent {
  2006. struct rb_root root;
  2007. struct list_head head;
  2008. struct btrfs_path *path;
  2009. struct inode *inode;
  2010. u64 file_pos;
  2011. u64 len;
  2012. u64 bytenr;
  2013. u64 disk_len;
  2014. u8 compress_type;
  2015. };
  2016. static int backref_comp(struct sa_defrag_extent_backref *b1,
  2017. struct sa_defrag_extent_backref *b2)
  2018. {
  2019. if (b1->root_id < b2->root_id)
  2020. return -1;
  2021. else if (b1->root_id > b2->root_id)
  2022. return 1;
  2023. if (b1->inum < b2->inum)
  2024. return -1;
  2025. else if (b1->inum > b2->inum)
  2026. return 1;
  2027. if (b1->file_pos < b2->file_pos)
  2028. return -1;
  2029. else if (b1->file_pos > b2->file_pos)
  2030. return 1;
  2031. /*
  2032. * [------------------------------] ===> (a range of space)
  2033. * |<--->| |<---->| =============> (fs/file tree A)
  2034. * |<---------------------------->| ===> (fs/file tree B)
  2035. *
  2036. * A range of space can refer to two file extents in one tree while
  2037. * refer to only one file extent in another tree.
  2038. *
  2039. * So we may process a disk offset more than one time(two extents in A)
  2040. * and locate at the same extent(one extent in B), then insert two same
  2041. * backrefs(both refer to the extent in B).
  2042. */
  2043. return 0;
  2044. }
  2045. static void backref_insert(struct rb_root *root,
  2046. struct sa_defrag_extent_backref *backref)
  2047. {
  2048. struct rb_node **p = &root->rb_node;
  2049. struct rb_node *parent = NULL;
  2050. struct sa_defrag_extent_backref *entry;
  2051. int ret;
  2052. while (*p) {
  2053. parent = *p;
  2054. entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
  2055. ret = backref_comp(backref, entry);
  2056. if (ret < 0)
  2057. p = &(*p)->rb_left;
  2058. else
  2059. p = &(*p)->rb_right;
  2060. }
  2061. rb_link_node(&backref->node, parent, p);
  2062. rb_insert_color(&backref->node, root);
  2063. }
  2064. /*
  2065. * Note the backref might has changed, and in this case we just return 0.
  2066. */
  2067. static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
  2068. void *ctx)
  2069. {
  2070. struct btrfs_file_extent_item *extent;
  2071. struct old_sa_defrag_extent *old = ctx;
  2072. struct new_sa_defrag_extent *new = old->new;
  2073. struct btrfs_path *path = new->path;
  2074. struct btrfs_key key;
  2075. struct btrfs_root *root;
  2076. struct sa_defrag_extent_backref *backref;
  2077. struct extent_buffer *leaf;
  2078. struct inode *inode = new->inode;
  2079. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2080. int slot;
  2081. int ret;
  2082. u64 extent_offset;
  2083. u64 num_bytes;
  2084. if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
  2085. inum == btrfs_ino(inode))
  2086. return 0;
  2087. key.objectid = root_id;
  2088. key.type = BTRFS_ROOT_ITEM_KEY;
  2089. key.offset = (u64)-1;
  2090. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2091. if (IS_ERR(root)) {
  2092. if (PTR_ERR(root) == -ENOENT)
  2093. return 0;
  2094. WARN_ON(1);
  2095. btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
  2096. inum, offset, root_id);
  2097. return PTR_ERR(root);
  2098. }
  2099. key.objectid = inum;
  2100. key.type = BTRFS_EXTENT_DATA_KEY;
  2101. if (offset > (u64)-1 << 32)
  2102. key.offset = 0;
  2103. else
  2104. key.offset = offset;
  2105. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2106. if (WARN_ON(ret < 0))
  2107. return ret;
  2108. ret = 0;
  2109. while (1) {
  2110. cond_resched();
  2111. leaf = path->nodes[0];
  2112. slot = path->slots[0];
  2113. if (slot >= btrfs_header_nritems(leaf)) {
  2114. ret = btrfs_next_leaf(root, path);
  2115. if (ret < 0) {
  2116. goto out;
  2117. } else if (ret > 0) {
  2118. ret = 0;
  2119. goto out;
  2120. }
  2121. continue;
  2122. }
  2123. path->slots[0]++;
  2124. btrfs_item_key_to_cpu(leaf, &key, slot);
  2125. if (key.objectid > inum)
  2126. goto out;
  2127. if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
  2128. continue;
  2129. extent = btrfs_item_ptr(leaf, slot,
  2130. struct btrfs_file_extent_item);
  2131. if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
  2132. continue;
  2133. /*
  2134. * 'offset' refers to the exact key.offset,
  2135. * NOT the 'offset' field in btrfs_extent_data_ref, ie.
  2136. * (key.offset - extent_offset).
  2137. */
  2138. if (key.offset != offset)
  2139. continue;
  2140. extent_offset = btrfs_file_extent_offset(leaf, extent);
  2141. num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
  2142. if (extent_offset >= old->extent_offset + old->offset +
  2143. old->len || extent_offset + num_bytes <=
  2144. old->extent_offset + old->offset)
  2145. continue;
  2146. break;
  2147. }
  2148. backref = kmalloc(sizeof(*backref), GFP_NOFS);
  2149. if (!backref) {
  2150. ret = -ENOENT;
  2151. goto out;
  2152. }
  2153. backref->root_id = root_id;
  2154. backref->inum = inum;
  2155. backref->file_pos = offset;
  2156. backref->num_bytes = num_bytes;
  2157. backref->extent_offset = extent_offset;
  2158. backref->generation = btrfs_file_extent_generation(leaf, extent);
  2159. backref->old = old;
  2160. backref_insert(&new->root, backref);
  2161. old->count++;
  2162. out:
  2163. btrfs_release_path(path);
  2164. WARN_ON(ret);
  2165. return ret;
  2166. }
  2167. static noinline bool record_extent_backrefs(struct btrfs_path *path,
  2168. struct new_sa_defrag_extent *new)
  2169. {
  2170. struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
  2171. struct old_sa_defrag_extent *old, *tmp;
  2172. int ret;
  2173. new->path = path;
  2174. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2175. ret = iterate_inodes_from_logical(old->bytenr +
  2176. old->extent_offset, fs_info,
  2177. path, record_one_backref,
  2178. old);
  2179. if (ret < 0 && ret != -ENOENT)
  2180. return false;
  2181. /* no backref to be processed for this extent */
  2182. if (!old->count) {
  2183. list_del(&old->list);
  2184. kfree(old);
  2185. }
  2186. }
  2187. if (list_empty(&new->head))
  2188. return false;
  2189. return true;
  2190. }
  2191. static int relink_is_mergable(struct extent_buffer *leaf,
  2192. struct btrfs_file_extent_item *fi,
  2193. struct new_sa_defrag_extent *new)
  2194. {
  2195. if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
  2196. return 0;
  2197. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  2198. return 0;
  2199. if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
  2200. return 0;
  2201. if (btrfs_file_extent_encryption(leaf, fi) ||
  2202. btrfs_file_extent_other_encoding(leaf, fi))
  2203. return 0;
  2204. return 1;
  2205. }
  2206. /*
  2207. * Note the backref might has changed, and in this case we just return 0.
  2208. */
  2209. static noinline int relink_extent_backref(struct btrfs_path *path,
  2210. struct sa_defrag_extent_backref *prev,
  2211. struct sa_defrag_extent_backref *backref)
  2212. {
  2213. struct btrfs_file_extent_item *extent;
  2214. struct btrfs_file_extent_item *item;
  2215. struct btrfs_ordered_extent *ordered;
  2216. struct btrfs_trans_handle *trans;
  2217. struct btrfs_root *root;
  2218. struct btrfs_key key;
  2219. struct extent_buffer *leaf;
  2220. struct old_sa_defrag_extent *old = backref->old;
  2221. struct new_sa_defrag_extent *new = old->new;
  2222. struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
  2223. struct inode *inode;
  2224. struct extent_state *cached = NULL;
  2225. int ret = 0;
  2226. u64 start;
  2227. u64 len;
  2228. u64 lock_start;
  2229. u64 lock_end;
  2230. bool merge = false;
  2231. int index;
  2232. if (prev && prev->root_id == backref->root_id &&
  2233. prev->inum == backref->inum &&
  2234. prev->file_pos + prev->num_bytes == backref->file_pos)
  2235. merge = true;
  2236. /* step 1: get root */
  2237. key.objectid = backref->root_id;
  2238. key.type = BTRFS_ROOT_ITEM_KEY;
  2239. key.offset = (u64)-1;
  2240. index = srcu_read_lock(&fs_info->subvol_srcu);
  2241. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2242. if (IS_ERR(root)) {
  2243. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2244. if (PTR_ERR(root) == -ENOENT)
  2245. return 0;
  2246. return PTR_ERR(root);
  2247. }
  2248. if (btrfs_root_readonly(root)) {
  2249. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2250. return 0;
  2251. }
  2252. /* step 2: get inode */
  2253. key.objectid = backref->inum;
  2254. key.type = BTRFS_INODE_ITEM_KEY;
  2255. key.offset = 0;
  2256. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  2257. if (IS_ERR(inode)) {
  2258. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2259. return 0;
  2260. }
  2261. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2262. /* step 3: relink backref */
  2263. lock_start = backref->file_pos;
  2264. lock_end = backref->file_pos + backref->num_bytes - 1;
  2265. lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2266. &cached);
  2267. ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
  2268. if (ordered) {
  2269. btrfs_put_ordered_extent(ordered);
  2270. goto out_unlock;
  2271. }
  2272. trans = btrfs_join_transaction(root);
  2273. if (IS_ERR(trans)) {
  2274. ret = PTR_ERR(trans);
  2275. goto out_unlock;
  2276. }
  2277. key.objectid = backref->inum;
  2278. key.type = BTRFS_EXTENT_DATA_KEY;
  2279. key.offset = backref->file_pos;
  2280. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2281. if (ret < 0) {
  2282. goto out_free_path;
  2283. } else if (ret > 0) {
  2284. ret = 0;
  2285. goto out_free_path;
  2286. }
  2287. extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2288. struct btrfs_file_extent_item);
  2289. if (btrfs_file_extent_generation(path->nodes[0], extent) !=
  2290. backref->generation)
  2291. goto out_free_path;
  2292. btrfs_release_path(path);
  2293. start = backref->file_pos;
  2294. if (backref->extent_offset < old->extent_offset + old->offset)
  2295. start += old->extent_offset + old->offset -
  2296. backref->extent_offset;
  2297. len = min(backref->extent_offset + backref->num_bytes,
  2298. old->extent_offset + old->offset + old->len);
  2299. len -= max(backref->extent_offset, old->extent_offset + old->offset);
  2300. ret = btrfs_drop_extents(trans, root, inode, start,
  2301. start + len, 1);
  2302. if (ret)
  2303. goto out_free_path;
  2304. again:
  2305. key.objectid = btrfs_ino(inode);
  2306. key.type = BTRFS_EXTENT_DATA_KEY;
  2307. key.offset = start;
  2308. path->leave_spinning = 1;
  2309. if (merge) {
  2310. struct btrfs_file_extent_item *fi;
  2311. u64 extent_len;
  2312. struct btrfs_key found_key;
  2313. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  2314. if (ret < 0)
  2315. goto out_free_path;
  2316. path->slots[0]--;
  2317. leaf = path->nodes[0];
  2318. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2319. fi = btrfs_item_ptr(leaf, path->slots[0],
  2320. struct btrfs_file_extent_item);
  2321. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  2322. if (extent_len + found_key.offset == start &&
  2323. relink_is_mergable(leaf, fi, new)) {
  2324. btrfs_set_file_extent_num_bytes(leaf, fi,
  2325. extent_len + len);
  2326. btrfs_mark_buffer_dirty(leaf);
  2327. inode_add_bytes(inode, len);
  2328. ret = 1;
  2329. goto out_free_path;
  2330. } else {
  2331. merge = false;
  2332. btrfs_release_path(path);
  2333. goto again;
  2334. }
  2335. }
  2336. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2337. sizeof(*extent));
  2338. if (ret) {
  2339. btrfs_abort_transaction(trans, ret);
  2340. goto out_free_path;
  2341. }
  2342. leaf = path->nodes[0];
  2343. item = btrfs_item_ptr(leaf, path->slots[0],
  2344. struct btrfs_file_extent_item);
  2345. btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
  2346. btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
  2347. btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
  2348. btrfs_set_file_extent_num_bytes(leaf, item, len);
  2349. btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
  2350. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  2351. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  2352. btrfs_set_file_extent_compression(leaf, item, new->compress_type);
  2353. btrfs_set_file_extent_encryption(leaf, item, 0);
  2354. btrfs_set_file_extent_other_encoding(leaf, item, 0);
  2355. btrfs_mark_buffer_dirty(leaf);
  2356. inode_add_bytes(inode, len);
  2357. btrfs_release_path(path);
  2358. ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
  2359. new->disk_len, 0,
  2360. backref->root_id, backref->inum,
  2361. new->file_pos); /* start - extent_offset */
  2362. if (ret) {
  2363. btrfs_abort_transaction(trans, ret);
  2364. goto out_free_path;
  2365. }
  2366. ret = 1;
  2367. out_free_path:
  2368. btrfs_release_path(path);
  2369. path->leave_spinning = 0;
  2370. btrfs_end_transaction(trans);
  2371. out_unlock:
  2372. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2373. &cached, GFP_NOFS);
  2374. iput(inode);
  2375. return ret;
  2376. }
  2377. static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
  2378. {
  2379. struct old_sa_defrag_extent *old, *tmp;
  2380. if (!new)
  2381. return;
  2382. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2383. kfree(old);
  2384. }
  2385. kfree(new);
  2386. }
  2387. static void relink_file_extents(struct new_sa_defrag_extent *new)
  2388. {
  2389. struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
  2390. struct btrfs_path *path;
  2391. struct sa_defrag_extent_backref *backref;
  2392. struct sa_defrag_extent_backref *prev = NULL;
  2393. struct inode *inode;
  2394. struct btrfs_root *root;
  2395. struct rb_node *node;
  2396. int ret;
  2397. inode = new->inode;
  2398. root = BTRFS_I(inode)->root;
  2399. path = btrfs_alloc_path();
  2400. if (!path)
  2401. return;
  2402. if (!record_extent_backrefs(path, new)) {
  2403. btrfs_free_path(path);
  2404. goto out;
  2405. }
  2406. btrfs_release_path(path);
  2407. while (1) {
  2408. node = rb_first(&new->root);
  2409. if (!node)
  2410. break;
  2411. rb_erase(node, &new->root);
  2412. backref = rb_entry(node, struct sa_defrag_extent_backref, node);
  2413. ret = relink_extent_backref(path, prev, backref);
  2414. WARN_ON(ret < 0);
  2415. kfree(prev);
  2416. if (ret == 1)
  2417. prev = backref;
  2418. else
  2419. prev = NULL;
  2420. cond_resched();
  2421. }
  2422. kfree(prev);
  2423. btrfs_free_path(path);
  2424. out:
  2425. free_sa_defrag_extent(new);
  2426. atomic_dec(&fs_info->defrag_running);
  2427. wake_up(&fs_info->transaction_wait);
  2428. }
  2429. static struct new_sa_defrag_extent *
  2430. record_old_file_extents(struct inode *inode,
  2431. struct btrfs_ordered_extent *ordered)
  2432. {
  2433. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2434. struct btrfs_root *root = BTRFS_I(inode)->root;
  2435. struct btrfs_path *path;
  2436. struct btrfs_key key;
  2437. struct old_sa_defrag_extent *old;
  2438. struct new_sa_defrag_extent *new;
  2439. int ret;
  2440. new = kmalloc(sizeof(*new), GFP_NOFS);
  2441. if (!new)
  2442. return NULL;
  2443. new->inode = inode;
  2444. new->file_pos = ordered->file_offset;
  2445. new->len = ordered->len;
  2446. new->bytenr = ordered->start;
  2447. new->disk_len = ordered->disk_len;
  2448. new->compress_type = ordered->compress_type;
  2449. new->root = RB_ROOT;
  2450. INIT_LIST_HEAD(&new->head);
  2451. path = btrfs_alloc_path();
  2452. if (!path)
  2453. goto out_kfree;
  2454. key.objectid = btrfs_ino(inode);
  2455. key.type = BTRFS_EXTENT_DATA_KEY;
  2456. key.offset = new->file_pos;
  2457. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2458. if (ret < 0)
  2459. goto out_free_path;
  2460. if (ret > 0 && path->slots[0] > 0)
  2461. path->slots[0]--;
  2462. /* find out all the old extents for the file range */
  2463. while (1) {
  2464. struct btrfs_file_extent_item *extent;
  2465. struct extent_buffer *l;
  2466. int slot;
  2467. u64 num_bytes;
  2468. u64 offset;
  2469. u64 end;
  2470. u64 disk_bytenr;
  2471. u64 extent_offset;
  2472. l = path->nodes[0];
  2473. slot = path->slots[0];
  2474. if (slot >= btrfs_header_nritems(l)) {
  2475. ret = btrfs_next_leaf(root, path);
  2476. if (ret < 0)
  2477. goto out_free_path;
  2478. else if (ret > 0)
  2479. break;
  2480. continue;
  2481. }
  2482. btrfs_item_key_to_cpu(l, &key, slot);
  2483. if (key.objectid != btrfs_ino(inode))
  2484. break;
  2485. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2486. break;
  2487. if (key.offset >= new->file_pos + new->len)
  2488. break;
  2489. extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
  2490. num_bytes = btrfs_file_extent_num_bytes(l, extent);
  2491. if (key.offset + num_bytes < new->file_pos)
  2492. goto next;
  2493. disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
  2494. if (!disk_bytenr)
  2495. goto next;
  2496. extent_offset = btrfs_file_extent_offset(l, extent);
  2497. old = kmalloc(sizeof(*old), GFP_NOFS);
  2498. if (!old)
  2499. goto out_free_path;
  2500. offset = max(new->file_pos, key.offset);
  2501. end = min(new->file_pos + new->len, key.offset + num_bytes);
  2502. old->bytenr = disk_bytenr;
  2503. old->extent_offset = extent_offset;
  2504. old->offset = offset - key.offset;
  2505. old->len = end - offset;
  2506. old->new = new;
  2507. old->count = 0;
  2508. list_add_tail(&old->list, &new->head);
  2509. next:
  2510. path->slots[0]++;
  2511. cond_resched();
  2512. }
  2513. btrfs_free_path(path);
  2514. atomic_inc(&fs_info->defrag_running);
  2515. return new;
  2516. out_free_path:
  2517. btrfs_free_path(path);
  2518. out_kfree:
  2519. free_sa_defrag_extent(new);
  2520. return NULL;
  2521. }
  2522. static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
  2523. u64 start, u64 len)
  2524. {
  2525. struct btrfs_block_group_cache *cache;
  2526. cache = btrfs_lookup_block_group(fs_info, start);
  2527. ASSERT(cache);
  2528. spin_lock(&cache->lock);
  2529. cache->delalloc_bytes -= len;
  2530. spin_unlock(&cache->lock);
  2531. btrfs_put_block_group(cache);
  2532. }
  2533. /* as ordered data IO finishes, this gets called so we can finish
  2534. * an ordered extent if the range of bytes in the file it covers are
  2535. * fully written.
  2536. */
  2537. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
  2538. {
  2539. struct inode *inode = ordered_extent->inode;
  2540. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2541. struct btrfs_root *root = BTRFS_I(inode)->root;
  2542. struct btrfs_trans_handle *trans = NULL;
  2543. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2544. struct extent_state *cached_state = NULL;
  2545. struct new_sa_defrag_extent *new = NULL;
  2546. int compress_type = 0;
  2547. int ret = 0;
  2548. u64 logical_len = ordered_extent->len;
  2549. bool nolock;
  2550. bool truncated = false;
  2551. nolock = btrfs_is_free_space_inode(inode);
  2552. if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
  2553. ret = -EIO;
  2554. goto out;
  2555. }
  2556. btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
  2557. ordered_extent->file_offset +
  2558. ordered_extent->len - 1);
  2559. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
  2560. truncated = true;
  2561. logical_len = ordered_extent->truncated_len;
  2562. /* Truncated the entire extent, don't bother adding */
  2563. if (!logical_len)
  2564. goto out;
  2565. }
  2566. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  2567. BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
  2568. /*
  2569. * For mwrite(mmap + memset to write) case, we still reserve
  2570. * space for NOCOW range.
  2571. * As NOCOW won't cause a new delayed ref, just free the space
  2572. */
  2573. btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
  2574. ordered_extent->len);
  2575. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2576. if (nolock)
  2577. trans = btrfs_join_transaction_nolock(root);
  2578. else
  2579. trans = btrfs_join_transaction(root);
  2580. if (IS_ERR(trans)) {
  2581. ret = PTR_ERR(trans);
  2582. trans = NULL;
  2583. goto out;
  2584. }
  2585. trans->block_rsv = &fs_info->delalloc_block_rsv;
  2586. ret = btrfs_update_inode_fallback(trans, root, inode);
  2587. if (ret) /* -ENOMEM or corruption */
  2588. btrfs_abort_transaction(trans, ret);
  2589. goto out;
  2590. }
  2591. lock_extent_bits(io_tree, ordered_extent->file_offset,
  2592. ordered_extent->file_offset + ordered_extent->len - 1,
  2593. &cached_state);
  2594. ret = test_range_bit(io_tree, ordered_extent->file_offset,
  2595. ordered_extent->file_offset + ordered_extent->len - 1,
  2596. EXTENT_DEFRAG, 1, cached_state);
  2597. if (ret) {
  2598. u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  2599. if (0 && last_snapshot >= BTRFS_I(inode)->generation)
  2600. /* the inode is shared */
  2601. new = record_old_file_extents(inode, ordered_extent);
  2602. clear_extent_bit(io_tree, ordered_extent->file_offset,
  2603. ordered_extent->file_offset + ordered_extent->len - 1,
  2604. EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
  2605. }
  2606. if (nolock)
  2607. trans = btrfs_join_transaction_nolock(root);
  2608. else
  2609. trans = btrfs_join_transaction(root);
  2610. if (IS_ERR(trans)) {
  2611. ret = PTR_ERR(trans);
  2612. trans = NULL;
  2613. goto out_unlock;
  2614. }
  2615. trans->block_rsv = &fs_info->delalloc_block_rsv;
  2616. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  2617. compress_type = ordered_extent->compress_type;
  2618. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  2619. BUG_ON(compress_type);
  2620. ret = btrfs_mark_extent_written(trans, inode,
  2621. ordered_extent->file_offset,
  2622. ordered_extent->file_offset +
  2623. logical_len);
  2624. } else {
  2625. BUG_ON(root == fs_info->tree_root);
  2626. ret = insert_reserved_file_extent(trans, inode,
  2627. ordered_extent->file_offset,
  2628. ordered_extent->start,
  2629. ordered_extent->disk_len,
  2630. logical_len, logical_len,
  2631. compress_type, 0, 0,
  2632. BTRFS_FILE_EXTENT_REG);
  2633. if (!ret)
  2634. btrfs_release_delalloc_bytes(fs_info,
  2635. ordered_extent->start,
  2636. ordered_extent->disk_len);
  2637. }
  2638. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  2639. ordered_extent->file_offset, ordered_extent->len,
  2640. trans->transid);
  2641. if (ret < 0) {
  2642. btrfs_abort_transaction(trans, ret);
  2643. goto out_unlock;
  2644. }
  2645. add_pending_csums(trans, inode, ordered_extent->file_offset,
  2646. &ordered_extent->list);
  2647. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2648. ret = btrfs_update_inode_fallback(trans, root, inode);
  2649. if (ret) { /* -ENOMEM or corruption */
  2650. btrfs_abort_transaction(trans, ret);
  2651. goto out_unlock;
  2652. }
  2653. ret = 0;
  2654. out_unlock:
  2655. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  2656. ordered_extent->file_offset +
  2657. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  2658. out:
  2659. if (root != fs_info->tree_root)
  2660. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  2661. if (trans)
  2662. btrfs_end_transaction(trans);
  2663. if (ret || truncated) {
  2664. u64 start, end;
  2665. if (truncated)
  2666. start = ordered_extent->file_offset + logical_len;
  2667. else
  2668. start = ordered_extent->file_offset;
  2669. end = ordered_extent->file_offset + ordered_extent->len - 1;
  2670. clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
  2671. /* Drop the cache for the part of the extent we didn't write. */
  2672. btrfs_drop_extent_cache(inode, start, end, 0);
  2673. /*
  2674. * If the ordered extent had an IOERR or something else went
  2675. * wrong we need to return the space for this ordered extent
  2676. * back to the allocator. We only free the extent in the
  2677. * truncated case if we didn't write out the extent at all.
  2678. */
  2679. if ((ret || !logical_len) &&
  2680. !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
  2681. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
  2682. btrfs_free_reserved_extent(fs_info,
  2683. ordered_extent->start,
  2684. ordered_extent->disk_len, 1);
  2685. }
  2686. /*
  2687. * This needs to be done to make sure anybody waiting knows we are done
  2688. * updating everything for this ordered extent.
  2689. */
  2690. btrfs_remove_ordered_extent(inode, ordered_extent);
  2691. /* for snapshot-aware defrag */
  2692. if (new) {
  2693. if (ret) {
  2694. free_sa_defrag_extent(new);
  2695. atomic_dec(&fs_info->defrag_running);
  2696. } else {
  2697. relink_file_extents(new);
  2698. }
  2699. }
  2700. /* once for us */
  2701. btrfs_put_ordered_extent(ordered_extent);
  2702. /* once for the tree */
  2703. btrfs_put_ordered_extent(ordered_extent);
  2704. return ret;
  2705. }
  2706. static void finish_ordered_fn(struct btrfs_work *work)
  2707. {
  2708. struct btrfs_ordered_extent *ordered_extent;
  2709. ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
  2710. btrfs_finish_ordered_io(ordered_extent);
  2711. }
  2712. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  2713. struct extent_state *state, int uptodate)
  2714. {
  2715. struct inode *inode = page->mapping->host;
  2716. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2717. struct btrfs_ordered_extent *ordered_extent = NULL;
  2718. struct btrfs_workqueue *wq;
  2719. btrfs_work_func_t func;
  2720. trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
  2721. ClearPagePrivate2(page);
  2722. if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  2723. end - start + 1, uptodate))
  2724. return 0;
  2725. if (btrfs_is_free_space_inode(inode)) {
  2726. wq = fs_info->endio_freespace_worker;
  2727. func = btrfs_freespace_write_helper;
  2728. } else {
  2729. wq = fs_info->endio_write_workers;
  2730. func = btrfs_endio_write_helper;
  2731. }
  2732. btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
  2733. NULL);
  2734. btrfs_queue_work(wq, &ordered_extent->work);
  2735. return 0;
  2736. }
  2737. static int __readpage_endio_check(struct inode *inode,
  2738. struct btrfs_io_bio *io_bio,
  2739. int icsum, struct page *page,
  2740. int pgoff, u64 start, size_t len)
  2741. {
  2742. char *kaddr;
  2743. u32 csum_expected;
  2744. u32 csum = ~(u32)0;
  2745. csum_expected = *(((u32 *)io_bio->csum) + icsum);
  2746. kaddr = kmap_atomic(page);
  2747. csum = btrfs_csum_data(kaddr + pgoff, csum, len);
  2748. btrfs_csum_final(csum, (u8 *)&csum);
  2749. if (csum != csum_expected)
  2750. goto zeroit;
  2751. kunmap_atomic(kaddr);
  2752. return 0;
  2753. zeroit:
  2754. btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
  2755. "csum failed ino %llu off %llu csum %u expected csum %u",
  2756. btrfs_ino(inode), start, csum, csum_expected);
  2757. memset(kaddr + pgoff, 1, len);
  2758. flush_dcache_page(page);
  2759. kunmap_atomic(kaddr);
  2760. if (csum_expected == 0)
  2761. return 0;
  2762. return -EIO;
  2763. }
  2764. /*
  2765. * when reads are done, we need to check csums to verify the data is correct
  2766. * if there's a match, we allow the bio to finish. If not, the code in
  2767. * extent_io.c will try to find good copies for us.
  2768. */
  2769. static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
  2770. u64 phy_offset, struct page *page,
  2771. u64 start, u64 end, int mirror)
  2772. {
  2773. size_t offset = start - page_offset(page);
  2774. struct inode *inode = page->mapping->host;
  2775. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2776. struct btrfs_root *root = BTRFS_I(inode)->root;
  2777. if (PageChecked(page)) {
  2778. ClearPageChecked(page);
  2779. return 0;
  2780. }
  2781. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  2782. return 0;
  2783. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  2784. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  2785. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
  2786. return 0;
  2787. }
  2788. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2789. return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
  2790. start, (size_t)(end - start + 1));
  2791. }
  2792. void btrfs_add_delayed_iput(struct inode *inode)
  2793. {
  2794. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2795. struct btrfs_inode *binode = BTRFS_I(inode);
  2796. if (atomic_add_unless(&inode->i_count, -1, 1))
  2797. return;
  2798. spin_lock(&fs_info->delayed_iput_lock);
  2799. if (binode->delayed_iput_count == 0) {
  2800. ASSERT(list_empty(&binode->delayed_iput));
  2801. list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
  2802. } else {
  2803. binode->delayed_iput_count++;
  2804. }
  2805. spin_unlock(&fs_info->delayed_iput_lock);
  2806. }
  2807. void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
  2808. {
  2809. spin_lock(&fs_info->delayed_iput_lock);
  2810. while (!list_empty(&fs_info->delayed_iputs)) {
  2811. struct btrfs_inode *inode;
  2812. inode = list_first_entry(&fs_info->delayed_iputs,
  2813. struct btrfs_inode, delayed_iput);
  2814. if (inode->delayed_iput_count) {
  2815. inode->delayed_iput_count--;
  2816. list_move_tail(&inode->delayed_iput,
  2817. &fs_info->delayed_iputs);
  2818. } else {
  2819. list_del_init(&inode->delayed_iput);
  2820. }
  2821. spin_unlock(&fs_info->delayed_iput_lock);
  2822. iput(&inode->vfs_inode);
  2823. spin_lock(&fs_info->delayed_iput_lock);
  2824. }
  2825. spin_unlock(&fs_info->delayed_iput_lock);
  2826. }
  2827. /*
  2828. * This is called in transaction commit time. If there are no orphan
  2829. * files in the subvolume, it removes orphan item and frees block_rsv
  2830. * structure.
  2831. */
  2832. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  2833. struct btrfs_root *root)
  2834. {
  2835. struct btrfs_fs_info *fs_info = root->fs_info;
  2836. struct btrfs_block_rsv *block_rsv;
  2837. int ret;
  2838. if (atomic_read(&root->orphan_inodes) ||
  2839. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  2840. return;
  2841. spin_lock(&root->orphan_lock);
  2842. if (atomic_read(&root->orphan_inodes)) {
  2843. spin_unlock(&root->orphan_lock);
  2844. return;
  2845. }
  2846. if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
  2847. spin_unlock(&root->orphan_lock);
  2848. return;
  2849. }
  2850. block_rsv = root->orphan_block_rsv;
  2851. root->orphan_block_rsv = NULL;
  2852. spin_unlock(&root->orphan_lock);
  2853. if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
  2854. btrfs_root_refs(&root->root_item) > 0) {
  2855. ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
  2856. root->root_key.objectid);
  2857. if (ret)
  2858. btrfs_abort_transaction(trans, ret);
  2859. else
  2860. clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
  2861. &root->state);
  2862. }
  2863. if (block_rsv) {
  2864. WARN_ON(block_rsv->size > 0);
  2865. btrfs_free_block_rsv(fs_info, block_rsv);
  2866. }
  2867. }
  2868. /*
  2869. * This creates an orphan entry for the given inode in case something goes
  2870. * wrong in the middle of an unlink/truncate.
  2871. *
  2872. * NOTE: caller of this function should reserve 5 units of metadata for
  2873. * this function.
  2874. */
  2875. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  2876. {
  2877. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2878. struct btrfs_root *root = BTRFS_I(inode)->root;
  2879. struct btrfs_block_rsv *block_rsv = NULL;
  2880. int reserve = 0;
  2881. int insert = 0;
  2882. int ret;
  2883. if (!root->orphan_block_rsv) {
  2884. block_rsv = btrfs_alloc_block_rsv(fs_info,
  2885. BTRFS_BLOCK_RSV_TEMP);
  2886. if (!block_rsv)
  2887. return -ENOMEM;
  2888. }
  2889. spin_lock(&root->orphan_lock);
  2890. if (!root->orphan_block_rsv) {
  2891. root->orphan_block_rsv = block_rsv;
  2892. } else if (block_rsv) {
  2893. btrfs_free_block_rsv(fs_info, block_rsv);
  2894. block_rsv = NULL;
  2895. }
  2896. if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2897. &BTRFS_I(inode)->runtime_flags)) {
  2898. #if 0
  2899. /*
  2900. * For proper ENOSPC handling, we should do orphan
  2901. * cleanup when mounting. But this introduces backward
  2902. * compatibility issue.
  2903. */
  2904. if (!xchg(&root->orphan_item_inserted, 1))
  2905. insert = 2;
  2906. else
  2907. insert = 1;
  2908. #endif
  2909. insert = 1;
  2910. atomic_inc(&root->orphan_inodes);
  2911. }
  2912. if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2913. &BTRFS_I(inode)->runtime_flags))
  2914. reserve = 1;
  2915. spin_unlock(&root->orphan_lock);
  2916. /* grab metadata reservation from transaction handle */
  2917. if (reserve) {
  2918. ret = btrfs_orphan_reserve_metadata(trans, inode);
  2919. ASSERT(!ret);
  2920. if (ret) {
  2921. atomic_dec(&root->orphan_inodes);
  2922. clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2923. &BTRFS_I(inode)->runtime_flags);
  2924. if (insert)
  2925. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2926. &BTRFS_I(inode)->runtime_flags);
  2927. return ret;
  2928. }
  2929. }
  2930. /* insert an orphan item to track this unlinked/truncated file */
  2931. if (insert >= 1) {
  2932. ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
  2933. if (ret) {
  2934. atomic_dec(&root->orphan_inodes);
  2935. if (reserve) {
  2936. clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2937. &BTRFS_I(inode)->runtime_flags);
  2938. btrfs_orphan_release_metadata(inode);
  2939. }
  2940. if (ret != -EEXIST) {
  2941. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2942. &BTRFS_I(inode)->runtime_flags);
  2943. btrfs_abort_transaction(trans, ret);
  2944. return ret;
  2945. }
  2946. }
  2947. ret = 0;
  2948. }
  2949. /* insert an orphan item to track subvolume contains orphan files */
  2950. if (insert >= 2) {
  2951. ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
  2952. root->root_key.objectid);
  2953. if (ret && ret != -EEXIST) {
  2954. btrfs_abort_transaction(trans, ret);
  2955. return ret;
  2956. }
  2957. }
  2958. return 0;
  2959. }
  2960. /*
  2961. * We have done the truncate/delete so we can go ahead and remove the orphan
  2962. * item for this particular inode.
  2963. */
  2964. static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
  2965. struct inode *inode)
  2966. {
  2967. struct btrfs_root *root = BTRFS_I(inode)->root;
  2968. int delete_item = 0;
  2969. int release_rsv = 0;
  2970. int ret = 0;
  2971. spin_lock(&root->orphan_lock);
  2972. if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2973. &BTRFS_I(inode)->runtime_flags))
  2974. delete_item = 1;
  2975. if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2976. &BTRFS_I(inode)->runtime_flags))
  2977. release_rsv = 1;
  2978. spin_unlock(&root->orphan_lock);
  2979. if (delete_item) {
  2980. atomic_dec(&root->orphan_inodes);
  2981. if (trans)
  2982. ret = btrfs_del_orphan_item(trans, root,
  2983. btrfs_ino(inode));
  2984. }
  2985. if (release_rsv)
  2986. btrfs_orphan_release_metadata(inode);
  2987. return ret;
  2988. }
  2989. /*
  2990. * this cleans up any orphans that may be left on the list from the last use
  2991. * of this root.
  2992. */
  2993. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2994. {
  2995. struct btrfs_fs_info *fs_info = root->fs_info;
  2996. struct btrfs_path *path;
  2997. struct extent_buffer *leaf;
  2998. struct btrfs_key key, found_key;
  2999. struct btrfs_trans_handle *trans;
  3000. struct inode *inode;
  3001. u64 last_objectid = 0;
  3002. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  3003. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  3004. return 0;
  3005. path = btrfs_alloc_path();
  3006. if (!path) {
  3007. ret = -ENOMEM;
  3008. goto out;
  3009. }
  3010. path->reada = READA_BACK;
  3011. key.objectid = BTRFS_ORPHAN_OBJECTID;
  3012. key.type = BTRFS_ORPHAN_ITEM_KEY;
  3013. key.offset = (u64)-1;
  3014. while (1) {
  3015. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3016. if (ret < 0)
  3017. goto out;
  3018. /*
  3019. * if ret == 0 means we found what we were searching for, which
  3020. * is weird, but possible, so only screw with path if we didn't
  3021. * find the key and see if we have stuff that matches
  3022. */
  3023. if (ret > 0) {
  3024. ret = 0;
  3025. if (path->slots[0] == 0)
  3026. break;
  3027. path->slots[0]--;
  3028. }
  3029. /* pull out the item */
  3030. leaf = path->nodes[0];
  3031. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3032. /* make sure the item matches what we want */
  3033. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  3034. break;
  3035. if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
  3036. break;
  3037. /* release the path since we're done with it */
  3038. btrfs_release_path(path);
  3039. /*
  3040. * this is where we are basically btrfs_lookup, without the
  3041. * crossing root thing. we store the inode number in the
  3042. * offset of the orphan item.
  3043. */
  3044. if (found_key.offset == last_objectid) {
  3045. btrfs_err(fs_info,
  3046. "Error removing orphan entry, stopping orphan cleanup");
  3047. ret = -EINVAL;
  3048. goto out;
  3049. }
  3050. last_objectid = found_key.offset;
  3051. found_key.objectid = found_key.offset;
  3052. found_key.type = BTRFS_INODE_ITEM_KEY;
  3053. found_key.offset = 0;
  3054. inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
  3055. ret = PTR_ERR_OR_ZERO(inode);
  3056. if (ret && ret != -ENOENT)
  3057. goto out;
  3058. if (ret == -ENOENT && root == fs_info->tree_root) {
  3059. struct btrfs_root *dead_root;
  3060. struct btrfs_fs_info *fs_info = root->fs_info;
  3061. int is_dead_root = 0;
  3062. /*
  3063. * this is an orphan in the tree root. Currently these
  3064. * could come from 2 sources:
  3065. * a) a snapshot deletion in progress
  3066. * b) a free space cache inode
  3067. * We need to distinguish those two, as the snapshot
  3068. * orphan must not get deleted.
  3069. * find_dead_roots already ran before us, so if this
  3070. * is a snapshot deletion, we should find the root
  3071. * in the dead_roots list
  3072. */
  3073. spin_lock(&fs_info->trans_lock);
  3074. list_for_each_entry(dead_root, &fs_info->dead_roots,
  3075. root_list) {
  3076. if (dead_root->root_key.objectid ==
  3077. found_key.objectid) {
  3078. is_dead_root = 1;
  3079. break;
  3080. }
  3081. }
  3082. spin_unlock(&fs_info->trans_lock);
  3083. if (is_dead_root) {
  3084. /* prevent this orphan from being found again */
  3085. key.offset = found_key.objectid - 1;
  3086. continue;
  3087. }
  3088. }
  3089. /*
  3090. * Inode is already gone but the orphan item is still there,
  3091. * kill the orphan item.
  3092. */
  3093. if (ret == -ENOENT) {
  3094. trans = btrfs_start_transaction(root, 1);
  3095. if (IS_ERR(trans)) {
  3096. ret = PTR_ERR(trans);
  3097. goto out;
  3098. }
  3099. btrfs_debug(fs_info, "auto deleting %Lu",
  3100. found_key.objectid);
  3101. ret = btrfs_del_orphan_item(trans, root,
  3102. found_key.objectid);
  3103. btrfs_end_transaction(trans);
  3104. if (ret)
  3105. goto out;
  3106. continue;
  3107. }
  3108. /*
  3109. * add this inode to the orphan list so btrfs_orphan_del does
  3110. * the proper thing when we hit it
  3111. */
  3112. set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  3113. &BTRFS_I(inode)->runtime_flags);
  3114. atomic_inc(&root->orphan_inodes);
  3115. /* if we have links, this was a truncate, lets do that */
  3116. if (inode->i_nlink) {
  3117. if (WARN_ON(!S_ISREG(inode->i_mode))) {
  3118. iput(inode);
  3119. continue;
  3120. }
  3121. nr_truncate++;
  3122. /* 1 for the orphan item deletion. */
  3123. trans = btrfs_start_transaction(root, 1);
  3124. if (IS_ERR(trans)) {
  3125. iput(inode);
  3126. ret = PTR_ERR(trans);
  3127. goto out;
  3128. }
  3129. ret = btrfs_orphan_add(trans, inode);
  3130. btrfs_end_transaction(trans);
  3131. if (ret) {
  3132. iput(inode);
  3133. goto out;
  3134. }
  3135. ret = btrfs_truncate(inode);
  3136. if (ret)
  3137. btrfs_orphan_del(NULL, inode);
  3138. } else {
  3139. nr_unlink++;
  3140. }
  3141. /* this will do delete_inode and everything for us */
  3142. iput(inode);
  3143. if (ret)
  3144. goto out;
  3145. }
  3146. /* release the path since we're done with it */
  3147. btrfs_release_path(path);
  3148. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  3149. if (root->orphan_block_rsv)
  3150. btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
  3151. (u64)-1);
  3152. if (root->orphan_block_rsv ||
  3153. test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
  3154. trans = btrfs_join_transaction(root);
  3155. if (!IS_ERR(trans))
  3156. btrfs_end_transaction(trans);
  3157. }
  3158. if (nr_unlink)
  3159. btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
  3160. if (nr_truncate)
  3161. btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
  3162. out:
  3163. if (ret)
  3164. btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
  3165. btrfs_free_path(path);
  3166. return ret;
  3167. }
  3168. /*
  3169. * very simple check to peek ahead in the leaf looking for xattrs. If we
  3170. * don't find any xattrs, we know there can't be any acls.
  3171. *
  3172. * slot is the slot the inode is in, objectid is the objectid of the inode
  3173. */
  3174. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  3175. int slot, u64 objectid,
  3176. int *first_xattr_slot)
  3177. {
  3178. u32 nritems = btrfs_header_nritems(leaf);
  3179. struct btrfs_key found_key;
  3180. static u64 xattr_access = 0;
  3181. static u64 xattr_default = 0;
  3182. int scanned = 0;
  3183. if (!xattr_access) {
  3184. xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
  3185. strlen(XATTR_NAME_POSIX_ACL_ACCESS));
  3186. xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
  3187. strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
  3188. }
  3189. slot++;
  3190. *first_xattr_slot = -1;
  3191. while (slot < nritems) {
  3192. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3193. /* we found a different objectid, there must not be acls */
  3194. if (found_key.objectid != objectid)
  3195. return 0;
  3196. /* we found an xattr, assume we've got an acl */
  3197. if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
  3198. if (*first_xattr_slot == -1)
  3199. *first_xattr_slot = slot;
  3200. if (found_key.offset == xattr_access ||
  3201. found_key.offset == xattr_default)
  3202. return 1;
  3203. }
  3204. /*
  3205. * we found a key greater than an xattr key, there can't
  3206. * be any acls later on
  3207. */
  3208. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  3209. return 0;
  3210. slot++;
  3211. scanned++;
  3212. /*
  3213. * it goes inode, inode backrefs, xattrs, extents,
  3214. * so if there are a ton of hard links to an inode there can
  3215. * be a lot of backrefs. Don't waste time searching too hard,
  3216. * this is just an optimization
  3217. */
  3218. if (scanned >= 8)
  3219. break;
  3220. }
  3221. /* we hit the end of the leaf before we found an xattr or
  3222. * something larger than an xattr. We have to assume the inode
  3223. * has acls
  3224. */
  3225. if (*first_xattr_slot == -1)
  3226. *first_xattr_slot = slot;
  3227. return 1;
  3228. }
  3229. /*
  3230. * read an inode from the btree into the in-memory inode
  3231. */
  3232. static int btrfs_read_locked_inode(struct inode *inode)
  3233. {
  3234. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3235. struct btrfs_path *path;
  3236. struct extent_buffer *leaf;
  3237. struct btrfs_inode_item *inode_item;
  3238. struct btrfs_root *root = BTRFS_I(inode)->root;
  3239. struct btrfs_key location;
  3240. unsigned long ptr;
  3241. int maybe_acls;
  3242. u32 rdev;
  3243. int ret;
  3244. bool filled = false;
  3245. int first_xattr_slot;
  3246. ret = btrfs_fill_inode(inode, &rdev);
  3247. if (!ret)
  3248. filled = true;
  3249. path = btrfs_alloc_path();
  3250. if (!path) {
  3251. ret = -ENOMEM;
  3252. goto make_bad;
  3253. }
  3254. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  3255. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  3256. if (ret) {
  3257. if (ret > 0)
  3258. ret = -ENOENT;
  3259. goto make_bad;
  3260. }
  3261. leaf = path->nodes[0];
  3262. if (filled)
  3263. goto cache_index;
  3264. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3265. struct btrfs_inode_item);
  3266. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  3267. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  3268. i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
  3269. i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
  3270. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  3271. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
  3272. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
  3273. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
  3274. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
  3275. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
  3276. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
  3277. BTRFS_I(inode)->i_otime.tv_sec =
  3278. btrfs_timespec_sec(leaf, &inode_item->otime);
  3279. BTRFS_I(inode)->i_otime.tv_nsec =
  3280. btrfs_timespec_nsec(leaf, &inode_item->otime);
  3281. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  3282. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  3283. BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
  3284. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  3285. inode->i_generation = BTRFS_I(inode)->generation;
  3286. inode->i_rdev = 0;
  3287. rdev = btrfs_inode_rdev(leaf, inode_item);
  3288. BTRFS_I(inode)->index_cnt = (u64)-1;
  3289. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  3290. cache_index:
  3291. /*
  3292. * If we were modified in the current generation and evicted from memory
  3293. * and then re-read we need to do a full sync since we don't have any
  3294. * idea about which extents were modified before we were evicted from
  3295. * cache.
  3296. *
  3297. * This is required for both inode re-read from disk and delayed inode
  3298. * in delayed_nodes_tree.
  3299. */
  3300. if (BTRFS_I(inode)->last_trans == fs_info->generation)
  3301. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3302. &BTRFS_I(inode)->runtime_flags);
  3303. /*
  3304. * We don't persist the id of the transaction where an unlink operation
  3305. * against the inode was last made. So here we assume the inode might
  3306. * have been evicted, and therefore the exact value of last_unlink_trans
  3307. * lost, and set it to last_trans to avoid metadata inconsistencies
  3308. * between the inode and its parent if the inode is fsync'ed and the log
  3309. * replayed. For example, in the scenario:
  3310. *
  3311. * touch mydir/foo
  3312. * ln mydir/foo mydir/bar
  3313. * sync
  3314. * unlink mydir/bar
  3315. * echo 2 > /proc/sys/vm/drop_caches # evicts inode
  3316. * xfs_io -c fsync mydir/foo
  3317. * <power failure>
  3318. * mount fs, triggers fsync log replay
  3319. *
  3320. * We must make sure that when we fsync our inode foo we also log its
  3321. * parent inode, otherwise after log replay the parent still has the
  3322. * dentry with the "bar" name but our inode foo has a link count of 1
  3323. * and doesn't have an inode ref with the name "bar" anymore.
  3324. *
  3325. * Setting last_unlink_trans to last_trans is a pessimistic approach,
  3326. * but it guarantees correctness at the expense of occasional full
  3327. * transaction commits on fsync if our inode is a directory, or if our
  3328. * inode is not a directory, logging its parent unnecessarily.
  3329. */
  3330. BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
  3331. path->slots[0]++;
  3332. if (inode->i_nlink != 1 ||
  3333. path->slots[0] >= btrfs_header_nritems(leaf))
  3334. goto cache_acl;
  3335. btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
  3336. if (location.objectid != btrfs_ino(inode))
  3337. goto cache_acl;
  3338. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3339. if (location.type == BTRFS_INODE_REF_KEY) {
  3340. struct btrfs_inode_ref *ref;
  3341. ref = (struct btrfs_inode_ref *)ptr;
  3342. BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
  3343. } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
  3344. struct btrfs_inode_extref *extref;
  3345. extref = (struct btrfs_inode_extref *)ptr;
  3346. BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
  3347. extref);
  3348. }
  3349. cache_acl:
  3350. /*
  3351. * try to precache a NULL acl entry for files that don't have
  3352. * any xattrs or acls
  3353. */
  3354. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  3355. btrfs_ino(inode), &first_xattr_slot);
  3356. if (first_xattr_slot != -1) {
  3357. path->slots[0] = first_xattr_slot;
  3358. ret = btrfs_load_inode_props(inode, path);
  3359. if (ret)
  3360. btrfs_err(fs_info,
  3361. "error loading props for ino %llu (root %llu): %d",
  3362. btrfs_ino(inode),
  3363. root->root_key.objectid, ret);
  3364. }
  3365. btrfs_free_path(path);
  3366. if (!maybe_acls)
  3367. cache_no_acl(inode);
  3368. switch (inode->i_mode & S_IFMT) {
  3369. case S_IFREG:
  3370. inode->i_mapping->a_ops = &btrfs_aops;
  3371. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3372. inode->i_fop = &btrfs_file_operations;
  3373. inode->i_op = &btrfs_file_inode_operations;
  3374. break;
  3375. case S_IFDIR:
  3376. inode->i_fop = &btrfs_dir_file_operations;
  3377. inode->i_op = &btrfs_dir_inode_operations;
  3378. break;
  3379. case S_IFLNK:
  3380. inode->i_op = &btrfs_symlink_inode_operations;
  3381. inode_nohighmem(inode);
  3382. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  3383. break;
  3384. default:
  3385. inode->i_op = &btrfs_special_inode_operations;
  3386. init_special_inode(inode, inode->i_mode, rdev);
  3387. break;
  3388. }
  3389. btrfs_update_iflags(inode);
  3390. return 0;
  3391. make_bad:
  3392. btrfs_free_path(path);
  3393. make_bad_inode(inode);
  3394. return ret;
  3395. }
  3396. /*
  3397. * given a leaf and an inode, copy the inode fields into the leaf
  3398. */
  3399. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3400. struct extent_buffer *leaf,
  3401. struct btrfs_inode_item *item,
  3402. struct inode *inode)
  3403. {
  3404. struct btrfs_map_token token;
  3405. btrfs_init_map_token(&token);
  3406. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3407. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3408. btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
  3409. &token);
  3410. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3411. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3412. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3413. inode->i_atime.tv_sec, &token);
  3414. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3415. inode->i_atime.tv_nsec, &token);
  3416. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3417. inode->i_mtime.tv_sec, &token);
  3418. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3419. inode->i_mtime.tv_nsec, &token);
  3420. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3421. inode->i_ctime.tv_sec, &token);
  3422. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3423. inode->i_ctime.tv_nsec, &token);
  3424. btrfs_set_token_timespec_sec(leaf, &item->otime,
  3425. BTRFS_I(inode)->i_otime.tv_sec, &token);
  3426. btrfs_set_token_timespec_nsec(leaf, &item->otime,
  3427. BTRFS_I(inode)->i_otime.tv_nsec, &token);
  3428. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3429. &token);
  3430. btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
  3431. &token);
  3432. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3433. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3434. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3435. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3436. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3437. }
  3438. /*
  3439. * copy everything in the in-memory inode into the btree.
  3440. */
  3441. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  3442. struct btrfs_root *root, struct inode *inode)
  3443. {
  3444. struct btrfs_inode_item *inode_item;
  3445. struct btrfs_path *path;
  3446. struct extent_buffer *leaf;
  3447. int ret;
  3448. path = btrfs_alloc_path();
  3449. if (!path)
  3450. return -ENOMEM;
  3451. path->leave_spinning = 1;
  3452. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  3453. 1);
  3454. if (ret) {
  3455. if (ret > 0)
  3456. ret = -ENOENT;
  3457. goto failed;
  3458. }
  3459. leaf = path->nodes[0];
  3460. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3461. struct btrfs_inode_item);
  3462. fill_inode_item(trans, leaf, inode_item, inode);
  3463. btrfs_mark_buffer_dirty(leaf);
  3464. btrfs_set_inode_last_trans(trans, inode);
  3465. ret = 0;
  3466. failed:
  3467. btrfs_free_path(path);
  3468. return ret;
  3469. }
  3470. /*
  3471. * copy everything in the in-memory inode into the btree.
  3472. */
  3473. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  3474. struct btrfs_root *root, struct inode *inode)
  3475. {
  3476. struct btrfs_fs_info *fs_info = root->fs_info;
  3477. int ret;
  3478. /*
  3479. * If the inode is a free space inode, we can deadlock during commit
  3480. * if we put it into the delayed code.
  3481. *
  3482. * The data relocation inode should also be directly updated
  3483. * without delay
  3484. */
  3485. if (!btrfs_is_free_space_inode(inode)
  3486. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  3487. && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
  3488. btrfs_update_root_times(trans, root);
  3489. ret = btrfs_delayed_update_inode(trans, root, inode);
  3490. if (!ret)
  3491. btrfs_set_inode_last_trans(trans, inode);
  3492. return ret;
  3493. }
  3494. return btrfs_update_inode_item(trans, root, inode);
  3495. }
  3496. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  3497. struct btrfs_root *root,
  3498. struct inode *inode)
  3499. {
  3500. int ret;
  3501. ret = btrfs_update_inode(trans, root, inode);
  3502. if (ret == -ENOSPC)
  3503. return btrfs_update_inode_item(trans, root, inode);
  3504. return ret;
  3505. }
  3506. /*
  3507. * unlink helper that gets used here in inode.c and in the tree logging
  3508. * recovery code. It remove a link in a directory with a given name, and
  3509. * also drops the back refs in the inode to the directory
  3510. */
  3511. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3512. struct btrfs_root *root,
  3513. struct inode *dir, struct inode *inode,
  3514. const char *name, int name_len)
  3515. {
  3516. struct btrfs_fs_info *fs_info = root->fs_info;
  3517. struct btrfs_path *path;
  3518. int ret = 0;
  3519. struct extent_buffer *leaf;
  3520. struct btrfs_dir_item *di;
  3521. struct btrfs_key key;
  3522. u64 index;
  3523. u64 ino = btrfs_ino(inode);
  3524. u64 dir_ino = btrfs_ino(dir);
  3525. path = btrfs_alloc_path();
  3526. if (!path) {
  3527. ret = -ENOMEM;
  3528. goto out;
  3529. }
  3530. path->leave_spinning = 1;
  3531. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3532. name, name_len, -1);
  3533. if (IS_ERR(di)) {
  3534. ret = PTR_ERR(di);
  3535. goto err;
  3536. }
  3537. if (!di) {
  3538. ret = -ENOENT;
  3539. goto err;
  3540. }
  3541. leaf = path->nodes[0];
  3542. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3543. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3544. if (ret)
  3545. goto err;
  3546. btrfs_release_path(path);
  3547. /*
  3548. * If we don't have dir index, we have to get it by looking up
  3549. * the inode ref, since we get the inode ref, remove it directly,
  3550. * it is unnecessary to do delayed deletion.
  3551. *
  3552. * But if we have dir index, needn't search inode ref to get it.
  3553. * Since the inode ref is close to the inode item, it is better
  3554. * that we delay to delete it, and just do this deletion when
  3555. * we update the inode item.
  3556. */
  3557. if (BTRFS_I(inode)->dir_index) {
  3558. ret = btrfs_delayed_delete_inode_ref(inode);
  3559. if (!ret) {
  3560. index = BTRFS_I(inode)->dir_index;
  3561. goto skip_backref;
  3562. }
  3563. }
  3564. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  3565. dir_ino, &index);
  3566. if (ret) {
  3567. btrfs_info(fs_info,
  3568. "failed to delete reference to %.*s, inode %llu parent %llu",
  3569. name_len, name, ino, dir_ino);
  3570. btrfs_abort_transaction(trans, ret);
  3571. goto err;
  3572. }
  3573. skip_backref:
  3574. ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
  3575. if (ret) {
  3576. btrfs_abort_transaction(trans, ret);
  3577. goto err;
  3578. }
  3579. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  3580. inode, dir_ino);
  3581. if (ret != 0 && ret != -ENOENT) {
  3582. btrfs_abort_transaction(trans, ret);
  3583. goto err;
  3584. }
  3585. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  3586. dir, index);
  3587. if (ret == -ENOENT)
  3588. ret = 0;
  3589. else if (ret)
  3590. btrfs_abort_transaction(trans, ret);
  3591. err:
  3592. btrfs_free_path(path);
  3593. if (ret)
  3594. goto out;
  3595. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3596. inode_inc_iversion(inode);
  3597. inode_inc_iversion(dir);
  3598. inode->i_ctime = dir->i_mtime =
  3599. dir->i_ctime = current_time(inode);
  3600. ret = btrfs_update_inode(trans, root, dir);
  3601. out:
  3602. return ret;
  3603. }
  3604. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3605. struct btrfs_root *root,
  3606. struct inode *dir, struct inode *inode,
  3607. const char *name, int name_len)
  3608. {
  3609. int ret;
  3610. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  3611. if (!ret) {
  3612. drop_nlink(inode);
  3613. ret = btrfs_update_inode(trans, root, inode);
  3614. }
  3615. return ret;
  3616. }
  3617. /*
  3618. * helper to start transaction for unlink and rmdir.
  3619. *
  3620. * unlink and rmdir are special in btrfs, they do not always free space, so
  3621. * if we cannot make our reservations the normal way try and see if there is
  3622. * plenty of slack room in the global reserve to migrate, otherwise we cannot
  3623. * allow the unlink to occur.
  3624. */
  3625. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
  3626. {
  3627. struct btrfs_root *root = BTRFS_I(dir)->root;
  3628. /*
  3629. * 1 for the possible orphan item
  3630. * 1 for the dir item
  3631. * 1 for the dir index
  3632. * 1 for the inode ref
  3633. * 1 for the inode
  3634. */
  3635. return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
  3636. }
  3637. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  3638. {
  3639. struct btrfs_root *root = BTRFS_I(dir)->root;
  3640. struct btrfs_trans_handle *trans;
  3641. struct inode *inode = d_inode(dentry);
  3642. int ret;
  3643. trans = __unlink_start_trans(dir);
  3644. if (IS_ERR(trans))
  3645. return PTR_ERR(trans);
  3646. btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
  3647. ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
  3648. dentry->d_name.name, dentry->d_name.len);
  3649. if (ret)
  3650. goto out;
  3651. if (inode->i_nlink == 0) {
  3652. ret = btrfs_orphan_add(trans, inode);
  3653. if (ret)
  3654. goto out;
  3655. }
  3656. out:
  3657. btrfs_end_transaction(trans);
  3658. btrfs_btree_balance_dirty(root->fs_info);
  3659. return ret;
  3660. }
  3661. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  3662. struct btrfs_root *root,
  3663. struct inode *dir, u64 objectid,
  3664. const char *name, int name_len)
  3665. {
  3666. struct btrfs_fs_info *fs_info = root->fs_info;
  3667. struct btrfs_path *path;
  3668. struct extent_buffer *leaf;
  3669. struct btrfs_dir_item *di;
  3670. struct btrfs_key key;
  3671. u64 index;
  3672. int ret;
  3673. u64 dir_ino = btrfs_ino(dir);
  3674. path = btrfs_alloc_path();
  3675. if (!path)
  3676. return -ENOMEM;
  3677. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3678. name, name_len, -1);
  3679. if (IS_ERR_OR_NULL(di)) {
  3680. if (!di)
  3681. ret = -ENOENT;
  3682. else
  3683. ret = PTR_ERR(di);
  3684. goto out;
  3685. }
  3686. leaf = path->nodes[0];
  3687. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3688. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3689. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3690. if (ret) {
  3691. btrfs_abort_transaction(trans, ret);
  3692. goto out;
  3693. }
  3694. btrfs_release_path(path);
  3695. ret = btrfs_del_root_ref(trans, fs_info, objectid,
  3696. root->root_key.objectid, dir_ino,
  3697. &index, name, name_len);
  3698. if (ret < 0) {
  3699. if (ret != -ENOENT) {
  3700. btrfs_abort_transaction(trans, ret);
  3701. goto out;
  3702. }
  3703. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3704. name, name_len);
  3705. if (IS_ERR_OR_NULL(di)) {
  3706. if (!di)
  3707. ret = -ENOENT;
  3708. else
  3709. ret = PTR_ERR(di);
  3710. btrfs_abort_transaction(trans, ret);
  3711. goto out;
  3712. }
  3713. leaf = path->nodes[0];
  3714. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3715. btrfs_release_path(path);
  3716. index = key.offset;
  3717. }
  3718. btrfs_release_path(path);
  3719. ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
  3720. if (ret) {
  3721. btrfs_abort_transaction(trans, ret);
  3722. goto out;
  3723. }
  3724. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3725. inode_inc_iversion(dir);
  3726. dir->i_mtime = dir->i_ctime = current_time(dir);
  3727. ret = btrfs_update_inode_fallback(trans, root, dir);
  3728. if (ret)
  3729. btrfs_abort_transaction(trans, ret);
  3730. out:
  3731. btrfs_free_path(path);
  3732. return ret;
  3733. }
  3734. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3735. {
  3736. struct inode *inode = d_inode(dentry);
  3737. int err = 0;
  3738. struct btrfs_root *root = BTRFS_I(dir)->root;
  3739. struct btrfs_trans_handle *trans;
  3740. u64 last_unlink_trans;
  3741. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3742. return -ENOTEMPTY;
  3743. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  3744. return -EPERM;
  3745. trans = __unlink_start_trans(dir);
  3746. if (IS_ERR(trans))
  3747. return PTR_ERR(trans);
  3748. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3749. err = btrfs_unlink_subvol(trans, root, dir,
  3750. BTRFS_I(inode)->location.objectid,
  3751. dentry->d_name.name,
  3752. dentry->d_name.len);
  3753. goto out;
  3754. }
  3755. err = btrfs_orphan_add(trans, inode);
  3756. if (err)
  3757. goto out;
  3758. last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
  3759. /* now the directory is empty */
  3760. err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
  3761. dentry->d_name.name, dentry->d_name.len);
  3762. if (!err) {
  3763. btrfs_i_size_write(inode, 0);
  3764. /*
  3765. * Propagate the last_unlink_trans value of the deleted dir to
  3766. * its parent directory. This is to prevent an unrecoverable
  3767. * log tree in the case we do something like this:
  3768. * 1) create dir foo
  3769. * 2) create snapshot under dir foo
  3770. * 3) delete the snapshot
  3771. * 4) rmdir foo
  3772. * 5) mkdir foo
  3773. * 6) fsync foo or some file inside foo
  3774. */
  3775. if (last_unlink_trans >= trans->transid)
  3776. BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
  3777. }
  3778. out:
  3779. btrfs_end_transaction(trans);
  3780. btrfs_btree_balance_dirty(root->fs_info);
  3781. return err;
  3782. }
  3783. static int truncate_space_check(struct btrfs_trans_handle *trans,
  3784. struct btrfs_root *root,
  3785. u64 bytes_deleted)
  3786. {
  3787. struct btrfs_fs_info *fs_info = root->fs_info;
  3788. int ret;
  3789. /*
  3790. * This is only used to apply pressure to the enospc system, we don't
  3791. * intend to use this reservation at all.
  3792. */
  3793. bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
  3794. bytes_deleted *= fs_info->nodesize;
  3795. ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
  3796. bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
  3797. if (!ret) {
  3798. trace_btrfs_space_reservation(fs_info, "transaction",
  3799. trans->transid,
  3800. bytes_deleted, 1);
  3801. trans->bytes_reserved += bytes_deleted;
  3802. }
  3803. return ret;
  3804. }
  3805. static int truncate_inline_extent(struct inode *inode,
  3806. struct btrfs_path *path,
  3807. struct btrfs_key *found_key,
  3808. const u64 item_end,
  3809. const u64 new_size)
  3810. {
  3811. struct extent_buffer *leaf = path->nodes[0];
  3812. int slot = path->slots[0];
  3813. struct btrfs_file_extent_item *fi;
  3814. u32 size = (u32)(new_size - found_key->offset);
  3815. struct btrfs_root *root = BTRFS_I(inode)->root;
  3816. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  3817. if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
  3818. loff_t offset = new_size;
  3819. loff_t page_end = ALIGN(offset, PAGE_SIZE);
  3820. /*
  3821. * Zero out the remaining of the last page of our inline extent,
  3822. * instead of directly truncating our inline extent here - that
  3823. * would be much more complex (decompressing all the data, then
  3824. * compressing the truncated data, which might be bigger than
  3825. * the size of the inline extent, resize the extent, etc).
  3826. * We release the path because to get the page we might need to
  3827. * read the extent item from disk (data not in the page cache).
  3828. */
  3829. btrfs_release_path(path);
  3830. return btrfs_truncate_block(inode, offset, page_end - offset,
  3831. 0);
  3832. }
  3833. btrfs_set_file_extent_ram_bytes(leaf, fi, size);
  3834. size = btrfs_file_extent_calc_inline_size(size);
  3835. btrfs_truncate_item(root->fs_info, path, size, 1);
  3836. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3837. inode_sub_bytes(inode, item_end + 1 - new_size);
  3838. return 0;
  3839. }
  3840. /*
  3841. * this can truncate away extent items, csum items and directory items.
  3842. * It starts at a high offset and removes keys until it can't find
  3843. * any higher than new_size
  3844. *
  3845. * csum items that cross the new i_size are truncated to the new size
  3846. * as well.
  3847. *
  3848. * min_type is the minimum key type to truncate down to. If set to 0, this
  3849. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3850. */
  3851. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3852. struct btrfs_root *root,
  3853. struct inode *inode,
  3854. u64 new_size, u32 min_type)
  3855. {
  3856. struct btrfs_fs_info *fs_info = root->fs_info;
  3857. struct btrfs_path *path;
  3858. struct extent_buffer *leaf;
  3859. struct btrfs_file_extent_item *fi;
  3860. struct btrfs_key key;
  3861. struct btrfs_key found_key;
  3862. u64 extent_start = 0;
  3863. u64 extent_num_bytes = 0;
  3864. u64 extent_offset = 0;
  3865. u64 item_end = 0;
  3866. u64 last_size = new_size;
  3867. u32 found_type = (u8)-1;
  3868. int found_extent;
  3869. int del_item;
  3870. int pending_del_nr = 0;
  3871. int pending_del_slot = 0;
  3872. int extent_type = -1;
  3873. int ret;
  3874. int err = 0;
  3875. u64 ino = btrfs_ino(inode);
  3876. u64 bytes_deleted = 0;
  3877. bool be_nice = 0;
  3878. bool should_throttle = 0;
  3879. bool should_end = 0;
  3880. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  3881. /*
  3882. * for non-free space inodes and ref cows, we want to back off from
  3883. * time to time
  3884. */
  3885. if (!btrfs_is_free_space_inode(inode) &&
  3886. test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3887. be_nice = 1;
  3888. path = btrfs_alloc_path();
  3889. if (!path)
  3890. return -ENOMEM;
  3891. path->reada = READA_BACK;
  3892. /*
  3893. * We want to drop from the next block forward in case this new size is
  3894. * not block aligned since we will be keeping the last block of the
  3895. * extent just the way it is.
  3896. */
  3897. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  3898. root == fs_info->tree_root)
  3899. btrfs_drop_extent_cache(inode, ALIGN(new_size,
  3900. fs_info->sectorsize),
  3901. (u64)-1, 0);
  3902. /*
  3903. * This function is also used to drop the items in the log tree before
  3904. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  3905. * it is used to drop the loged items. So we shouldn't kill the delayed
  3906. * items.
  3907. */
  3908. if (min_type == 0 && root == BTRFS_I(inode)->root)
  3909. btrfs_kill_delayed_inode_items(inode);
  3910. key.objectid = ino;
  3911. key.offset = (u64)-1;
  3912. key.type = (u8)-1;
  3913. search_again:
  3914. /*
  3915. * with a 16K leaf size and 128MB extents, you can actually queue
  3916. * up a huge file in a single leaf. Most of the time that
  3917. * bytes_deleted is > 0, it will be huge by the time we get here
  3918. */
  3919. if (be_nice && bytes_deleted > SZ_32M) {
  3920. if (btrfs_should_end_transaction(trans)) {
  3921. err = -EAGAIN;
  3922. goto error;
  3923. }
  3924. }
  3925. path->leave_spinning = 1;
  3926. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3927. if (ret < 0) {
  3928. err = ret;
  3929. goto out;
  3930. }
  3931. if (ret > 0) {
  3932. /* there are no items in the tree for us to truncate, we're
  3933. * done
  3934. */
  3935. if (path->slots[0] == 0)
  3936. goto out;
  3937. path->slots[0]--;
  3938. }
  3939. while (1) {
  3940. fi = NULL;
  3941. leaf = path->nodes[0];
  3942. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3943. found_type = found_key.type;
  3944. if (found_key.objectid != ino)
  3945. break;
  3946. if (found_type < min_type)
  3947. break;
  3948. item_end = found_key.offset;
  3949. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  3950. fi = btrfs_item_ptr(leaf, path->slots[0],
  3951. struct btrfs_file_extent_item);
  3952. extent_type = btrfs_file_extent_type(leaf, fi);
  3953. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3954. item_end +=
  3955. btrfs_file_extent_num_bytes(leaf, fi);
  3956. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3957. item_end += btrfs_file_extent_inline_len(leaf,
  3958. path->slots[0], fi);
  3959. }
  3960. item_end--;
  3961. }
  3962. if (found_type > min_type) {
  3963. del_item = 1;
  3964. } else {
  3965. if (item_end < new_size) {
  3966. /*
  3967. * With NO_HOLES mode, for the following mapping
  3968. *
  3969. * [0-4k][hole][8k-12k]
  3970. *
  3971. * if truncating isize down to 6k, it ends up
  3972. * isize being 8k.
  3973. */
  3974. if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
  3975. last_size = new_size;
  3976. break;
  3977. }
  3978. if (found_key.offset >= new_size)
  3979. del_item = 1;
  3980. else
  3981. del_item = 0;
  3982. }
  3983. found_extent = 0;
  3984. /* FIXME, shrink the extent if the ref count is only 1 */
  3985. if (found_type != BTRFS_EXTENT_DATA_KEY)
  3986. goto delete;
  3987. if (del_item)
  3988. last_size = found_key.offset;
  3989. else
  3990. last_size = new_size;
  3991. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3992. u64 num_dec;
  3993. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  3994. if (!del_item) {
  3995. u64 orig_num_bytes =
  3996. btrfs_file_extent_num_bytes(leaf, fi);
  3997. extent_num_bytes = ALIGN(new_size -
  3998. found_key.offset,
  3999. fs_info->sectorsize);
  4000. btrfs_set_file_extent_num_bytes(leaf, fi,
  4001. extent_num_bytes);
  4002. num_dec = (orig_num_bytes -
  4003. extent_num_bytes);
  4004. if (test_bit(BTRFS_ROOT_REF_COWS,
  4005. &root->state) &&
  4006. extent_start != 0)
  4007. inode_sub_bytes(inode, num_dec);
  4008. btrfs_mark_buffer_dirty(leaf);
  4009. } else {
  4010. extent_num_bytes =
  4011. btrfs_file_extent_disk_num_bytes(leaf,
  4012. fi);
  4013. extent_offset = found_key.offset -
  4014. btrfs_file_extent_offset(leaf, fi);
  4015. /* FIXME blocksize != 4096 */
  4016. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  4017. if (extent_start != 0) {
  4018. found_extent = 1;
  4019. if (test_bit(BTRFS_ROOT_REF_COWS,
  4020. &root->state))
  4021. inode_sub_bytes(inode, num_dec);
  4022. }
  4023. }
  4024. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  4025. /*
  4026. * we can't truncate inline items that have had
  4027. * special encodings
  4028. */
  4029. if (!del_item &&
  4030. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  4031. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  4032. /*
  4033. * Need to release path in order to truncate a
  4034. * compressed extent. So delete any accumulated
  4035. * extent items so far.
  4036. */
  4037. if (btrfs_file_extent_compression(leaf, fi) !=
  4038. BTRFS_COMPRESS_NONE && pending_del_nr) {
  4039. err = btrfs_del_items(trans, root, path,
  4040. pending_del_slot,
  4041. pending_del_nr);
  4042. if (err) {
  4043. btrfs_abort_transaction(trans,
  4044. err);
  4045. goto error;
  4046. }
  4047. pending_del_nr = 0;
  4048. }
  4049. err = truncate_inline_extent(inode, path,
  4050. &found_key,
  4051. item_end,
  4052. new_size);
  4053. if (err) {
  4054. btrfs_abort_transaction(trans, err);
  4055. goto error;
  4056. }
  4057. } else if (test_bit(BTRFS_ROOT_REF_COWS,
  4058. &root->state)) {
  4059. inode_sub_bytes(inode, item_end + 1 - new_size);
  4060. }
  4061. }
  4062. delete:
  4063. if (del_item) {
  4064. if (!pending_del_nr) {
  4065. /* no pending yet, add ourselves */
  4066. pending_del_slot = path->slots[0];
  4067. pending_del_nr = 1;
  4068. } else if (pending_del_nr &&
  4069. path->slots[0] + 1 == pending_del_slot) {
  4070. /* hop on the pending chunk */
  4071. pending_del_nr++;
  4072. pending_del_slot = path->slots[0];
  4073. } else {
  4074. BUG();
  4075. }
  4076. } else {
  4077. break;
  4078. }
  4079. should_throttle = 0;
  4080. if (found_extent &&
  4081. (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  4082. root == fs_info->tree_root)) {
  4083. btrfs_set_path_blocking(path);
  4084. bytes_deleted += extent_num_bytes;
  4085. ret = btrfs_free_extent(trans, fs_info, extent_start,
  4086. extent_num_bytes, 0,
  4087. btrfs_header_owner(leaf),
  4088. ino, extent_offset);
  4089. BUG_ON(ret);
  4090. if (btrfs_should_throttle_delayed_refs(trans, fs_info))
  4091. btrfs_async_run_delayed_refs(fs_info,
  4092. trans->delayed_ref_updates * 2,
  4093. trans->transid, 0);
  4094. if (be_nice) {
  4095. if (truncate_space_check(trans, root,
  4096. extent_num_bytes)) {
  4097. should_end = 1;
  4098. }
  4099. if (btrfs_should_throttle_delayed_refs(trans,
  4100. fs_info))
  4101. should_throttle = 1;
  4102. }
  4103. }
  4104. if (found_type == BTRFS_INODE_ITEM_KEY)
  4105. break;
  4106. if (path->slots[0] == 0 ||
  4107. path->slots[0] != pending_del_slot ||
  4108. should_throttle || should_end) {
  4109. if (pending_del_nr) {
  4110. ret = btrfs_del_items(trans, root, path,
  4111. pending_del_slot,
  4112. pending_del_nr);
  4113. if (ret) {
  4114. btrfs_abort_transaction(trans, ret);
  4115. goto error;
  4116. }
  4117. pending_del_nr = 0;
  4118. }
  4119. btrfs_release_path(path);
  4120. if (should_throttle) {
  4121. unsigned long updates = trans->delayed_ref_updates;
  4122. if (updates) {
  4123. trans->delayed_ref_updates = 0;
  4124. ret = btrfs_run_delayed_refs(trans,
  4125. fs_info,
  4126. updates * 2);
  4127. if (ret && !err)
  4128. err = ret;
  4129. }
  4130. }
  4131. /*
  4132. * if we failed to refill our space rsv, bail out
  4133. * and let the transaction restart
  4134. */
  4135. if (should_end) {
  4136. err = -EAGAIN;
  4137. goto error;
  4138. }
  4139. goto search_again;
  4140. } else {
  4141. path->slots[0]--;
  4142. }
  4143. }
  4144. out:
  4145. if (pending_del_nr) {
  4146. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  4147. pending_del_nr);
  4148. if (ret)
  4149. btrfs_abort_transaction(trans, ret);
  4150. }
  4151. error:
  4152. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  4153. btrfs_ordered_update_i_size(inode, last_size, NULL);
  4154. btrfs_free_path(path);
  4155. if (be_nice && bytes_deleted > SZ_32M) {
  4156. unsigned long updates = trans->delayed_ref_updates;
  4157. if (updates) {
  4158. trans->delayed_ref_updates = 0;
  4159. ret = btrfs_run_delayed_refs(trans, fs_info,
  4160. updates * 2);
  4161. if (ret && !err)
  4162. err = ret;
  4163. }
  4164. }
  4165. return err;
  4166. }
  4167. /*
  4168. * btrfs_truncate_block - read, zero a chunk and write a block
  4169. * @inode - inode that we're zeroing
  4170. * @from - the offset to start zeroing
  4171. * @len - the length to zero, 0 to zero the entire range respective to the
  4172. * offset
  4173. * @front - zero up to the offset instead of from the offset on
  4174. *
  4175. * This will find the block for the "from" offset and cow the block and zero the
  4176. * part we want to zero. This is used with truncate and hole punching.
  4177. */
  4178. int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
  4179. int front)
  4180. {
  4181. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4182. struct address_space *mapping = inode->i_mapping;
  4183. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4184. struct btrfs_ordered_extent *ordered;
  4185. struct extent_state *cached_state = NULL;
  4186. char *kaddr;
  4187. u32 blocksize = fs_info->sectorsize;
  4188. pgoff_t index = from >> PAGE_SHIFT;
  4189. unsigned offset = from & (blocksize - 1);
  4190. struct page *page;
  4191. gfp_t mask = btrfs_alloc_write_mask(mapping);
  4192. int ret = 0;
  4193. u64 block_start;
  4194. u64 block_end;
  4195. if ((offset & (blocksize - 1)) == 0 &&
  4196. (!len || ((len & (blocksize - 1)) == 0)))
  4197. goto out;
  4198. ret = btrfs_delalloc_reserve_space(inode,
  4199. round_down(from, blocksize), blocksize);
  4200. if (ret)
  4201. goto out;
  4202. again:
  4203. page = find_or_create_page(mapping, index, mask);
  4204. if (!page) {
  4205. btrfs_delalloc_release_space(inode,
  4206. round_down(from, blocksize),
  4207. blocksize);
  4208. ret = -ENOMEM;
  4209. goto out;
  4210. }
  4211. block_start = round_down(from, blocksize);
  4212. block_end = block_start + blocksize - 1;
  4213. if (!PageUptodate(page)) {
  4214. ret = btrfs_readpage(NULL, page);
  4215. lock_page(page);
  4216. if (page->mapping != mapping) {
  4217. unlock_page(page);
  4218. put_page(page);
  4219. goto again;
  4220. }
  4221. if (!PageUptodate(page)) {
  4222. ret = -EIO;
  4223. goto out_unlock;
  4224. }
  4225. }
  4226. wait_on_page_writeback(page);
  4227. lock_extent_bits(io_tree, block_start, block_end, &cached_state);
  4228. set_page_extent_mapped(page);
  4229. ordered = btrfs_lookup_ordered_extent(inode, block_start);
  4230. if (ordered) {
  4231. unlock_extent_cached(io_tree, block_start, block_end,
  4232. &cached_state, GFP_NOFS);
  4233. unlock_page(page);
  4234. put_page(page);
  4235. btrfs_start_ordered_extent(inode, ordered, 1);
  4236. btrfs_put_ordered_extent(ordered);
  4237. goto again;
  4238. }
  4239. clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
  4240. EXTENT_DIRTY | EXTENT_DELALLOC |
  4241. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  4242. 0, 0, &cached_state, GFP_NOFS);
  4243. ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
  4244. &cached_state, 0);
  4245. if (ret) {
  4246. unlock_extent_cached(io_tree, block_start, block_end,
  4247. &cached_state, GFP_NOFS);
  4248. goto out_unlock;
  4249. }
  4250. if (offset != blocksize) {
  4251. if (!len)
  4252. len = blocksize - offset;
  4253. kaddr = kmap(page);
  4254. if (front)
  4255. memset(kaddr + (block_start - page_offset(page)),
  4256. 0, offset);
  4257. else
  4258. memset(kaddr + (block_start - page_offset(page)) + offset,
  4259. 0, len);
  4260. flush_dcache_page(page);
  4261. kunmap(page);
  4262. }
  4263. ClearPageChecked(page);
  4264. set_page_dirty(page);
  4265. unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
  4266. GFP_NOFS);
  4267. out_unlock:
  4268. if (ret)
  4269. btrfs_delalloc_release_space(inode, block_start,
  4270. blocksize);
  4271. unlock_page(page);
  4272. put_page(page);
  4273. out:
  4274. return ret;
  4275. }
  4276. static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
  4277. u64 offset, u64 len)
  4278. {
  4279. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4280. struct btrfs_trans_handle *trans;
  4281. int ret;
  4282. /*
  4283. * Still need to make sure the inode looks like it's been updated so
  4284. * that any holes get logged if we fsync.
  4285. */
  4286. if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
  4287. BTRFS_I(inode)->last_trans = fs_info->generation;
  4288. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  4289. BTRFS_I(inode)->last_log_commit = root->last_log_commit;
  4290. return 0;
  4291. }
  4292. /*
  4293. * 1 - for the one we're dropping
  4294. * 1 - for the one we're adding
  4295. * 1 - for updating the inode.
  4296. */
  4297. trans = btrfs_start_transaction(root, 3);
  4298. if (IS_ERR(trans))
  4299. return PTR_ERR(trans);
  4300. ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
  4301. if (ret) {
  4302. btrfs_abort_transaction(trans, ret);
  4303. btrfs_end_transaction(trans);
  4304. return ret;
  4305. }
  4306. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  4307. 0, 0, len, 0, len, 0, 0, 0);
  4308. if (ret)
  4309. btrfs_abort_transaction(trans, ret);
  4310. else
  4311. btrfs_update_inode(trans, root, inode);
  4312. btrfs_end_transaction(trans);
  4313. return ret;
  4314. }
  4315. /*
  4316. * This function puts in dummy file extents for the area we're creating a hole
  4317. * for. So if we are truncating this file to a larger size we need to insert
  4318. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  4319. * the range between oldsize and size
  4320. */
  4321. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  4322. {
  4323. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4324. struct btrfs_root *root = BTRFS_I(inode)->root;
  4325. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4326. struct extent_map *em = NULL;
  4327. struct extent_state *cached_state = NULL;
  4328. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4329. u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
  4330. u64 block_end = ALIGN(size, fs_info->sectorsize);
  4331. u64 last_byte;
  4332. u64 cur_offset;
  4333. u64 hole_size;
  4334. int err = 0;
  4335. /*
  4336. * If our size started in the middle of a block we need to zero out the
  4337. * rest of the block before we expand the i_size, otherwise we could
  4338. * expose stale data.
  4339. */
  4340. err = btrfs_truncate_block(inode, oldsize, 0, 0);
  4341. if (err)
  4342. return err;
  4343. if (size <= hole_start)
  4344. return 0;
  4345. while (1) {
  4346. struct btrfs_ordered_extent *ordered;
  4347. lock_extent_bits(io_tree, hole_start, block_end - 1,
  4348. &cached_state);
  4349. ordered = btrfs_lookup_ordered_range(inode, hole_start,
  4350. block_end - hole_start);
  4351. if (!ordered)
  4352. break;
  4353. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  4354. &cached_state, GFP_NOFS);
  4355. btrfs_start_ordered_extent(inode, ordered, 1);
  4356. btrfs_put_ordered_extent(ordered);
  4357. }
  4358. cur_offset = hole_start;
  4359. while (1) {
  4360. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4361. block_end - cur_offset, 0);
  4362. if (IS_ERR(em)) {
  4363. err = PTR_ERR(em);
  4364. em = NULL;
  4365. break;
  4366. }
  4367. last_byte = min(extent_map_end(em), block_end);
  4368. last_byte = ALIGN(last_byte, fs_info->sectorsize);
  4369. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  4370. struct extent_map *hole_em;
  4371. hole_size = last_byte - cur_offset;
  4372. err = maybe_insert_hole(root, inode, cur_offset,
  4373. hole_size);
  4374. if (err)
  4375. break;
  4376. btrfs_drop_extent_cache(inode, cur_offset,
  4377. cur_offset + hole_size - 1, 0);
  4378. hole_em = alloc_extent_map();
  4379. if (!hole_em) {
  4380. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4381. &BTRFS_I(inode)->runtime_flags);
  4382. goto next;
  4383. }
  4384. hole_em->start = cur_offset;
  4385. hole_em->len = hole_size;
  4386. hole_em->orig_start = cur_offset;
  4387. hole_em->block_start = EXTENT_MAP_HOLE;
  4388. hole_em->block_len = 0;
  4389. hole_em->orig_block_len = 0;
  4390. hole_em->ram_bytes = hole_size;
  4391. hole_em->bdev = fs_info->fs_devices->latest_bdev;
  4392. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  4393. hole_em->generation = fs_info->generation;
  4394. while (1) {
  4395. write_lock(&em_tree->lock);
  4396. err = add_extent_mapping(em_tree, hole_em, 1);
  4397. write_unlock(&em_tree->lock);
  4398. if (err != -EEXIST)
  4399. break;
  4400. btrfs_drop_extent_cache(inode, cur_offset,
  4401. cur_offset +
  4402. hole_size - 1, 0);
  4403. }
  4404. free_extent_map(hole_em);
  4405. }
  4406. next:
  4407. free_extent_map(em);
  4408. em = NULL;
  4409. cur_offset = last_byte;
  4410. if (cur_offset >= block_end)
  4411. break;
  4412. }
  4413. free_extent_map(em);
  4414. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  4415. GFP_NOFS);
  4416. return err;
  4417. }
  4418. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  4419. {
  4420. struct btrfs_root *root = BTRFS_I(inode)->root;
  4421. struct btrfs_trans_handle *trans;
  4422. loff_t oldsize = i_size_read(inode);
  4423. loff_t newsize = attr->ia_size;
  4424. int mask = attr->ia_valid;
  4425. int ret;
  4426. /*
  4427. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  4428. * special case where we need to update the times despite not having
  4429. * these flags set. For all other operations the VFS set these flags
  4430. * explicitly if it wants a timestamp update.
  4431. */
  4432. if (newsize != oldsize) {
  4433. inode_inc_iversion(inode);
  4434. if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
  4435. inode->i_ctime = inode->i_mtime =
  4436. current_time(inode);
  4437. }
  4438. if (newsize > oldsize) {
  4439. /*
  4440. * Don't do an expanding truncate while snapshoting is ongoing.
  4441. * This is to ensure the snapshot captures a fully consistent
  4442. * state of this file - if the snapshot captures this expanding
  4443. * truncation, it must capture all writes that happened before
  4444. * this truncation.
  4445. */
  4446. btrfs_wait_for_snapshot_creation(root);
  4447. ret = btrfs_cont_expand(inode, oldsize, newsize);
  4448. if (ret) {
  4449. btrfs_end_write_no_snapshoting(root);
  4450. return ret;
  4451. }
  4452. trans = btrfs_start_transaction(root, 1);
  4453. if (IS_ERR(trans)) {
  4454. btrfs_end_write_no_snapshoting(root);
  4455. return PTR_ERR(trans);
  4456. }
  4457. i_size_write(inode, newsize);
  4458. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  4459. pagecache_isize_extended(inode, oldsize, newsize);
  4460. ret = btrfs_update_inode(trans, root, inode);
  4461. btrfs_end_write_no_snapshoting(root);
  4462. btrfs_end_transaction(trans);
  4463. } else {
  4464. /*
  4465. * We're truncating a file that used to have good data down to
  4466. * zero. Make sure it gets into the ordered flush list so that
  4467. * any new writes get down to disk quickly.
  4468. */
  4469. if (newsize == 0)
  4470. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  4471. &BTRFS_I(inode)->runtime_flags);
  4472. /*
  4473. * 1 for the orphan item we're going to add
  4474. * 1 for the orphan item deletion.
  4475. */
  4476. trans = btrfs_start_transaction(root, 2);
  4477. if (IS_ERR(trans))
  4478. return PTR_ERR(trans);
  4479. /*
  4480. * We need to do this in case we fail at _any_ point during the
  4481. * actual truncate. Once we do the truncate_setsize we could
  4482. * invalidate pages which forces any outstanding ordered io to
  4483. * be instantly completed which will give us extents that need
  4484. * to be truncated. If we fail to get an orphan inode down we
  4485. * could have left over extents that were never meant to live,
  4486. * so we need to guarantee from this point on that everything
  4487. * will be consistent.
  4488. */
  4489. ret = btrfs_orphan_add(trans, inode);
  4490. btrfs_end_transaction(trans);
  4491. if (ret)
  4492. return ret;
  4493. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  4494. truncate_setsize(inode, newsize);
  4495. /* Disable nonlocked read DIO to avoid the end less truncate */
  4496. btrfs_inode_block_unlocked_dio(inode);
  4497. inode_dio_wait(inode);
  4498. btrfs_inode_resume_unlocked_dio(inode);
  4499. ret = btrfs_truncate(inode);
  4500. if (ret && inode->i_nlink) {
  4501. int err;
  4502. /*
  4503. * failed to truncate, disk_i_size is only adjusted down
  4504. * as we remove extents, so it should represent the true
  4505. * size of the inode, so reset the in memory size and
  4506. * delete our orphan entry.
  4507. */
  4508. trans = btrfs_join_transaction(root);
  4509. if (IS_ERR(trans)) {
  4510. btrfs_orphan_del(NULL, inode);
  4511. return ret;
  4512. }
  4513. i_size_write(inode, BTRFS_I(inode)->disk_i_size);
  4514. err = btrfs_orphan_del(trans, inode);
  4515. if (err)
  4516. btrfs_abort_transaction(trans, err);
  4517. btrfs_end_transaction(trans);
  4518. }
  4519. }
  4520. return ret;
  4521. }
  4522. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  4523. {
  4524. struct inode *inode = d_inode(dentry);
  4525. struct btrfs_root *root = BTRFS_I(inode)->root;
  4526. int err;
  4527. if (btrfs_root_readonly(root))
  4528. return -EROFS;
  4529. err = setattr_prepare(dentry, attr);
  4530. if (err)
  4531. return err;
  4532. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  4533. err = btrfs_setsize(inode, attr);
  4534. if (err)
  4535. return err;
  4536. }
  4537. if (attr->ia_valid) {
  4538. setattr_copy(inode, attr);
  4539. inode_inc_iversion(inode);
  4540. err = btrfs_dirty_inode(inode);
  4541. if (!err && attr->ia_valid & ATTR_MODE)
  4542. err = posix_acl_chmod(inode, inode->i_mode);
  4543. }
  4544. return err;
  4545. }
  4546. /*
  4547. * While truncating the inode pages during eviction, we get the VFS calling
  4548. * btrfs_invalidatepage() against each page of the inode. This is slow because
  4549. * the calls to btrfs_invalidatepage() result in a huge amount of calls to
  4550. * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
  4551. * extent_state structures over and over, wasting lots of time.
  4552. *
  4553. * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
  4554. * those expensive operations on a per page basis and do only the ordered io
  4555. * finishing, while we release here the extent_map and extent_state structures,
  4556. * without the excessive merging and splitting.
  4557. */
  4558. static void evict_inode_truncate_pages(struct inode *inode)
  4559. {
  4560. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4561. struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
  4562. struct rb_node *node;
  4563. ASSERT(inode->i_state & I_FREEING);
  4564. truncate_inode_pages_final(&inode->i_data);
  4565. write_lock(&map_tree->lock);
  4566. while (!RB_EMPTY_ROOT(&map_tree->map)) {
  4567. struct extent_map *em;
  4568. node = rb_first(&map_tree->map);
  4569. em = rb_entry(node, struct extent_map, rb_node);
  4570. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  4571. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  4572. remove_extent_mapping(map_tree, em);
  4573. free_extent_map(em);
  4574. if (need_resched()) {
  4575. write_unlock(&map_tree->lock);
  4576. cond_resched();
  4577. write_lock(&map_tree->lock);
  4578. }
  4579. }
  4580. write_unlock(&map_tree->lock);
  4581. /*
  4582. * Keep looping until we have no more ranges in the io tree.
  4583. * We can have ongoing bios started by readpages (called from readahead)
  4584. * that have their endio callback (extent_io.c:end_bio_extent_readpage)
  4585. * still in progress (unlocked the pages in the bio but did not yet
  4586. * unlocked the ranges in the io tree). Therefore this means some
  4587. * ranges can still be locked and eviction started because before
  4588. * submitting those bios, which are executed by a separate task (work
  4589. * queue kthread), inode references (inode->i_count) were not taken
  4590. * (which would be dropped in the end io callback of each bio).
  4591. * Therefore here we effectively end up waiting for those bios and
  4592. * anyone else holding locked ranges without having bumped the inode's
  4593. * reference count - if we don't do it, when they access the inode's
  4594. * io_tree to unlock a range it may be too late, leading to an
  4595. * use-after-free issue.
  4596. */
  4597. spin_lock(&io_tree->lock);
  4598. while (!RB_EMPTY_ROOT(&io_tree->state)) {
  4599. struct extent_state *state;
  4600. struct extent_state *cached_state = NULL;
  4601. u64 start;
  4602. u64 end;
  4603. node = rb_first(&io_tree->state);
  4604. state = rb_entry(node, struct extent_state, rb_node);
  4605. start = state->start;
  4606. end = state->end;
  4607. spin_unlock(&io_tree->lock);
  4608. lock_extent_bits(io_tree, start, end, &cached_state);
  4609. /*
  4610. * If still has DELALLOC flag, the extent didn't reach disk,
  4611. * and its reserved space won't be freed by delayed_ref.
  4612. * So we need to free its reserved space here.
  4613. * (Refer to comment in btrfs_invalidatepage, case 2)
  4614. *
  4615. * Note, end is the bytenr of last byte, so we need + 1 here.
  4616. */
  4617. if (state->state & EXTENT_DELALLOC)
  4618. btrfs_qgroup_free_data(inode, start, end - start + 1);
  4619. clear_extent_bit(io_tree, start, end,
  4620. EXTENT_LOCKED | EXTENT_DIRTY |
  4621. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  4622. EXTENT_DEFRAG, 1, 1,
  4623. &cached_state, GFP_NOFS);
  4624. cond_resched();
  4625. spin_lock(&io_tree->lock);
  4626. }
  4627. spin_unlock(&io_tree->lock);
  4628. }
  4629. void btrfs_evict_inode(struct inode *inode)
  4630. {
  4631. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4632. struct btrfs_trans_handle *trans;
  4633. struct btrfs_root *root = BTRFS_I(inode)->root;
  4634. struct btrfs_block_rsv *rsv, *global_rsv;
  4635. int steal_from_global = 0;
  4636. u64 min_size;
  4637. int ret;
  4638. trace_btrfs_inode_evict(inode);
  4639. if (!root) {
  4640. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4641. return;
  4642. }
  4643. min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
  4644. evict_inode_truncate_pages(inode);
  4645. if (inode->i_nlink &&
  4646. ((btrfs_root_refs(&root->root_item) != 0 &&
  4647. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
  4648. btrfs_is_free_space_inode(inode)))
  4649. goto no_delete;
  4650. if (is_bad_inode(inode)) {
  4651. btrfs_orphan_del(NULL, inode);
  4652. goto no_delete;
  4653. }
  4654. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  4655. if (!special_file(inode->i_mode))
  4656. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4657. btrfs_free_io_failure_record(inode, 0, (u64)-1);
  4658. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
  4659. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  4660. &BTRFS_I(inode)->runtime_flags));
  4661. goto no_delete;
  4662. }
  4663. if (inode->i_nlink > 0) {
  4664. BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
  4665. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
  4666. goto no_delete;
  4667. }
  4668. ret = btrfs_commit_inode_delayed_inode(inode);
  4669. if (ret) {
  4670. btrfs_orphan_del(NULL, inode);
  4671. goto no_delete;
  4672. }
  4673. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  4674. if (!rsv) {
  4675. btrfs_orphan_del(NULL, inode);
  4676. goto no_delete;
  4677. }
  4678. rsv->size = min_size;
  4679. rsv->failfast = 1;
  4680. global_rsv = &fs_info->global_block_rsv;
  4681. btrfs_i_size_write(inode, 0);
  4682. /*
  4683. * This is a bit simpler than btrfs_truncate since we've already
  4684. * reserved our space for our orphan item in the unlink, so we just
  4685. * need to reserve some slack space in case we add bytes and update
  4686. * inode item when doing the truncate.
  4687. */
  4688. while (1) {
  4689. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4690. BTRFS_RESERVE_FLUSH_LIMIT);
  4691. /*
  4692. * Try and steal from the global reserve since we will
  4693. * likely not use this space anyway, we want to try as
  4694. * hard as possible to get this to work.
  4695. */
  4696. if (ret)
  4697. steal_from_global++;
  4698. else
  4699. steal_from_global = 0;
  4700. ret = 0;
  4701. /*
  4702. * steal_from_global == 0: we reserved stuff, hooray!
  4703. * steal_from_global == 1: we didn't reserve stuff, boo!
  4704. * steal_from_global == 2: we've committed, still not a lot of
  4705. * room but maybe we'll have room in the global reserve this
  4706. * time.
  4707. * steal_from_global == 3: abandon all hope!
  4708. */
  4709. if (steal_from_global > 2) {
  4710. btrfs_warn(fs_info,
  4711. "Could not get space for a delete, will truncate on mount %d",
  4712. ret);
  4713. btrfs_orphan_del(NULL, inode);
  4714. btrfs_free_block_rsv(fs_info, rsv);
  4715. goto no_delete;
  4716. }
  4717. trans = btrfs_join_transaction(root);
  4718. if (IS_ERR(trans)) {
  4719. btrfs_orphan_del(NULL, inode);
  4720. btrfs_free_block_rsv(fs_info, rsv);
  4721. goto no_delete;
  4722. }
  4723. /*
  4724. * We can't just steal from the global reserve, we need to make
  4725. * sure there is room to do it, if not we need to commit and try
  4726. * again.
  4727. */
  4728. if (steal_from_global) {
  4729. if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
  4730. ret = btrfs_block_rsv_migrate(global_rsv, rsv,
  4731. min_size, 0);
  4732. else
  4733. ret = -ENOSPC;
  4734. }
  4735. /*
  4736. * Couldn't steal from the global reserve, we have too much
  4737. * pending stuff built up, commit the transaction and try it
  4738. * again.
  4739. */
  4740. if (ret) {
  4741. ret = btrfs_commit_transaction(trans);
  4742. if (ret) {
  4743. btrfs_orphan_del(NULL, inode);
  4744. btrfs_free_block_rsv(fs_info, rsv);
  4745. goto no_delete;
  4746. }
  4747. continue;
  4748. } else {
  4749. steal_from_global = 0;
  4750. }
  4751. trans->block_rsv = rsv;
  4752. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4753. if (ret != -ENOSPC && ret != -EAGAIN)
  4754. break;
  4755. trans->block_rsv = &fs_info->trans_block_rsv;
  4756. btrfs_end_transaction(trans);
  4757. trans = NULL;
  4758. btrfs_btree_balance_dirty(fs_info);
  4759. }
  4760. btrfs_free_block_rsv(fs_info, rsv);
  4761. /*
  4762. * Errors here aren't a big deal, it just means we leave orphan items
  4763. * in the tree. They will be cleaned up on the next mount.
  4764. */
  4765. if (ret == 0) {
  4766. trans->block_rsv = root->orphan_block_rsv;
  4767. btrfs_orphan_del(trans, inode);
  4768. } else {
  4769. btrfs_orphan_del(NULL, inode);
  4770. }
  4771. trans->block_rsv = &fs_info->trans_block_rsv;
  4772. if (!(root == fs_info->tree_root ||
  4773. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4774. btrfs_return_ino(root, btrfs_ino(inode));
  4775. btrfs_end_transaction(trans);
  4776. btrfs_btree_balance_dirty(fs_info);
  4777. no_delete:
  4778. btrfs_remove_delayed_node(inode);
  4779. clear_inode(inode);
  4780. }
  4781. /*
  4782. * this returns the key found in the dir entry in the location pointer.
  4783. * If no dir entries were found, location->objectid is 0.
  4784. */
  4785. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4786. struct btrfs_key *location)
  4787. {
  4788. const char *name = dentry->d_name.name;
  4789. int namelen = dentry->d_name.len;
  4790. struct btrfs_dir_item *di;
  4791. struct btrfs_path *path;
  4792. struct btrfs_root *root = BTRFS_I(dir)->root;
  4793. int ret = 0;
  4794. path = btrfs_alloc_path();
  4795. if (!path)
  4796. return -ENOMEM;
  4797. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  4798. namelen, 0);
  4799. if (IS_ERR(di))
  4800. ret = PTR_ERR(di);
  4801. if (IS_ERR_OR_NULL(di))
  4802. goto out_err;
  4803. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4804. out:
  4805. btrfs_free_path(path);
  4806. return ret;
  4807. out_err:
  4808. location->objectid = 0;
  4809. goto out;
  4810. }
  4811. /*
  4812. * when we hit a tree root in a directory, the btrfs part of the inode
  4813. * needs to be changed to reflect the root directory of the tree root. This
  4814. * is kind of like crossing a mount point.
  4815. */
  4816. static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
  4817. struct inode *dir,
  4818. struct dentry *dentry,
  4819. struct btrfs_key *location,
  4820. struct btrfs_root **sub_root)
  4821. {
  4822. struct btrfs_path *path;
  4823. struct btrfs_root *new_root;
  4824. struct btrfs_root_ref *ref;
  4825. struct extent_buffer *leaf;
  4826. struct btrfs_key key;
  4827. int ret;
  4828. int err = 0;
  4829. path = btrfs_alloc_path();
  4830. if (!path) {
  4831. err = -ENOMEM;
  4832. goto out;
  4833. }
  4834. err = -ENOENT;
  4835. key.objectid = BTRFS_I(dir)->root->root_key.objectid;
  4836. key.type = BTRFS_ROOT_REF_KEY;
  4837. key.offset = location->objectid;
  4838. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  4839. if (ret) {
  4840. if (ret < 0)
  4841. err = ret;
  4842. goto out;
  4843. }
  4844. leaf = path->nodes[0];
  4845. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4846. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  4847. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4848. goto out;
  4849. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4850. (unsigned long)(ref + 1),
  4851. dentry->d_name.len);
  4852. if (ret)
  4853. goto out;
  4854. btrfs_release_path(path);
  4855. new_root = btrfs_read_fs_root_no_name(fs_info, location);
  4856. if (IS_ERR(new_root)) {
  4857. err = PTR_ERR(new_root);
  4858. goto out;
  4859. }
  4860. *sub_root = new_root;
  4861. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4862. location->type = BTRFS_INODE_ITEM_KEY;
  4863. location->offset = 0;
  4864. err = 0;
  4865. out:
  4866. btrfs_free_path(path);
  4867. return err;
  4868. }
  4869. static void inode_tree_add(struct inode *inode)
  4870. {
  4871. struct btrfs_root *root = BTRFS_I(inode)->root;
  4872. struct btrfs_inode *entry;
  4873. struct rb_node **p;
  4874. struct rb_node *parent;
  4875. struct rb_node *new = &BTRFS_I(inode)->rb_node;
  4876. u64 ino = btrfs_ino(inode);
  4877. if (inode_unhashed(inode))
  4878. return;
  4879. parent = NULL;
  4880. spin_lock(&root->inode_lock);
  4881. p = &root->inode_tree.rb_node;
  4882. while (*p) {
  4883. parent = *p;
  4884. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4885. if (ino < btrfs_ino(&entry->vfs_inode))
  4886. p = &parent->rb_left;
  4887. else if (ino > btrfs_ino(&entry->vfs_inode))
  4888. p = &parent->rb_right;
  4889. else {
  4890. WARN_ON(!(entry->vfs_inode.i_state &
  4891. (I_WILL_FREE | I_FREEING)));
  4892. rb_replace_node(parent, new, &root->inode_tree);
  4893. RB_CLEAR_NODE(parent);
  4894. spin_unlock(&root->inode_lock);
  4895. return;
  4896. }
  4897. }
  4898. rb_link_node(new, parent, p);
  4899. rb_insert_color(new, &root->inode_tree);
  4900. spin_unlock(&root->inode_lock);
  4901. }
  4902. static void inode_tree_del(struct inode *inode)
  4903. {
  4904. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4905. struct btrfs_root *root = BTRFS_I(inode)->root;
  4906. int empty = 0;
  4907. spin_lock(&root->inode_lock);
  4908. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4909. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4910. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4911. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4912. }
  4913. spin_unlock(&root->inode_lock);
  4914. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  4915. synchronize_srcu(&fs_info->subvol_srcu);
  4916. spin_lock(&root->inode_lock);
  4917. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4918. spin_unlock(&root->inode_lock);
  4919. if (empty)
  4920. btrfs_add_dead_root(root);
  4921. }
  4922. }
  4923. void btrfs_invalidate_inodes(struct btrfs_root *root)
  4924. {
  4925. struct btrfs_fs_info *fs_info = root->fs_info;
  4926. struct rb_node *node;
  4927. struct rb_node *prev;
  4928. struct btrfs_inode *entry;
  4929. struct inode *inode;
  4930. u64 objectid = 0;
  4931. if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  4932. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  4933. spin_lock(&root->inode_lock);
  4934. again:
  4935. node = root->inode_tree.rb_node;
  4936. prev = NULL;
  4937. while (node) {
  4938. prev = node;
  4939. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4940. if (objectid < btrfs_ino(&entry->vfs_inode))
  4941. node = node->rb_left;
  4942. else if (objectid > btrfs_ino(&entry->vfs_inode))
  4943. node = node->rb_right;
  4944. else
  4945. break;
  4946. }
  4947. if (!node) {
  4948. while (prev) {
  4949. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  4950. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  4951. node = prev;
  4952. break;
  4953. }
  4954. prev = rb_next(prev);
  4955. }
  4956. }
  4957. while (node) {
  4958. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4959. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  4960. inode = igrab(&entry->vfs_inode);
  4961. if (inode) {
  4962. spin_unlock(&root->inode_lock);
  4963. if (atomic_read(&inode->i_count) > 1)
  4964. d_prune_aliases(inode);
  4965. /*
  4966. * btrfs_drop_inode will have it removed from
  4967. * the inode cache when its usage count
  4968. * hits zero.
  4969. */
  4970. iput(inode);
  4971. cond_resched();
  4972. spin_lock(&root->inode_lock);
  4973. goto again;
  4974. }
  4975. if (cond_resched_lock(&root->inode_lock))
  4976. goto again;
  4977. node = rb_next(node);
  4978. }
  4979. spin_unlock(&root->inode_lock);
  4980. }
  4981. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4982. {
  4983. struct btrfs_iget_args *args = p;
  4984. inode->i_ino = args->location->objectid;
  4985. memcpy(&BTRFS_I(inode)->location, args->location,
  4986. sizeof(*args->location));
  4987. BTRFS_I(inode)->root = args->root;
  4988. return 0;
  4989. }
  4990. static int btrfs_find_actor(struct inode *inode, void *opaque)
  4991. {
  4992. struct btrfs_iget_args *args = opaque;
  4993. return args->location->objectid == BTRFS_I(inode)->location.objectid &&
  4994. args->root == BTRFS_I(inode)->root;
  4995. }
  4996. static struct inode *btrfs_iget_locked(struct super_block *s,
  4997. struct btrfs_key *location,
  4998. struct btrfs_root *root)
  4999. {
  5000. struct inode *inode;
  5001. struct btrfs_iget_args args;
  5002. unsigned long hashval = btrfs_inode_hash(location->objectid, root);
  5003. args.location = location;
  5004. args.root = root;
  5005. inode = iget5_locked(s, hashval, btrfs_find_actor,
  5006. btrfs_init_locked_inode,
  5007. (void *)&args);
  5008. return inode;
  5009. }
  5010. /* Get an inode object given its location and corresponding root.
  5011. * Returns in *is_new if the inode was read from disk
  5012. */
  5013. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  5014. struct btrfs_root *root, int *new)
  5015. {
  5016. struct inode *inode;
  5017. inode = btrfs_iget_locked(s, location, root);
  5018. if (!inode)
  5019. return ERR_PTR(-ENOMEM);
  5020. if (inode->i_state & I_NEW) {
  5021. int ret;
  5022. ret = btrfs_read_locked_inode(inode);
  5023. if (!is_bad_inode(inode)) {
  5024. inode_tree_add(inode);
  5025. unlock_new_inode(inode);
  5026. if (new)
  5027. *new = 1;
  5028. } else {
  5029. unlock_new_inode(inode);
  5030. iput(inode);
  5031. ASSERT(ret < 0);
  5032. inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
  5033. }
  5034. }
  5035. return inode;
  5036. }
  5037. static struct inode *new_simple_dir(struct super_block *s,
  5038. struct btrfs_key *key,
  5039. struct btrfs_root *root)
  5040. {
  5041. struct inode *inode = new_inode(s);
  5042. if (!inode)
  5043. return ERR_PTR(-ENOMEM);
  5044. BTRFS_I(inode)->root = root;
  5045. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  5046. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  5047. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  5048. inode->i_op = &btrfs_dir_ro_inode_operations;
  5049. inode->i_opflags &= ~IOP_XATTR;
  5050. inode->i_fop = &simple_dir_operations;
  5051. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  5052. inode->i_mtime = current_time(inode);
  5053. inode->i_atime = inode->i_mtime;
  5054. inode->i_ctime = inode->i_mtime;
  5055. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5056. return inode;
  5057. }
  5058. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  5059. {
  5060. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5061. struct inode *inode;
  5062. struct btrfs_root *root = BTRFS_I(dir)->root;
  5063. struct btrfs_root *sub_root = root;
  5064. struct btrfs_key location;
  5065. int index;
  5066. int ret = 0;
  5067. if (dentry->d_name.len > BTRFS_NAME_LEN)
  5068. return ERR_PTR(-ENAMETOOLONG);
  5069. ret = btrfs_inode_by_name(dir, dentry, &location);
  5070. if (ret < 0)
  5071. return ERR_PTR(ret);
  5072. if (location.objectid == 0)
  5073. return ERR_PTR(-ENOENT);
  5074. if (location.type == BTRFS_INODE_ITEM_KEY) {
  5075. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  5076. return inode;
  5077. }
  5078. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  5079. index = srcu_read_lock(&fs_info->subvol_srcu);
  5080. ret = fixup_tree_root_location(fs_info, dir, dentry,
  5081. &location, &sub_root);
  5082. if (ret < 0) {
  5083. if (ret != -ENOENT)
  5084. inode = ERR_PTR(ret);
  5085. else
  5086. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  5087. } else {
  5088. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  5089. }
  5090. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5091. if (!IS_ERR(inode) && root != sub_root) {
  5092. down_read(&fs_info->cleanup_work_sem);
  5093. if (!(inode->i_sb->s_flags & MS_RDONLY))
  5094. ret = btrfs_orphan_cleanup(sub_root);
  5095. up_read(&fs_info->cleanup_work_sem);
  5096. if (ret) {
  5097. iput(inode);
  5098. inode = ERR_PTR(ret);
  5099. }
  5100. }
  5101. return inode;
  5102. }
  5103. static int btrfs_dentry_delete(const struct dentry *dentry)
  5104. {
  5105. struct btrfs_root *root;
  5106. struct inode *inode = d_inode(dentry);
  5107. if (!inode && !IS_ROOT(dentry))
  5108. inode = d_inode(dentry->d_parent);
  5109. if (inode) {
  5110. root = BTRFS_I(inode)->root;
  5111. if (btrfs_root_refs(&root->root_item) == 0)
  5112. return 1;
  5113. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  5114. return 1;
  5115. }
  5116. return 0;
  5117. }
  5118. static void btrfs_dentry_release(struct dentry *dentry)
  5119. {
  5120. kfree(dentry->d_fsdata);
  5121. }
  5122. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  5123. unsigned int flags)
  5124. {
  5125. struct inode *inode;
  5126. inode = btrfs_lookup_dentry(dir, dentry);
  5127. if (IS_ERR(inode)) {
  5128. if (PTR_ERR(inode) == -ENOENT)
  5129. inode = NULL;
  5130. else
  5131. return ERR_CAST(inode);
  5132. }
  5133. return d_splice_alias(inode, dentry);
  5134. }
  5135. unsigned char btrfs_filetype_table[] = {
  5136. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  5137. };
  5138. static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
  5139. {
  5140. struct inode *inode = file_inode(file);
  5141. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5142. struct btrfs_root *root = BTRFS_I(inode)->root;
  5143. struct btrfs_item *item;
  5144. struct btrfs_dir_item *di;
  5145. struct btrfs_key key;
  5146. struct btrfs_key found_key;
  5147. struct btrfs_path *path;
  5148. struct list_head ins_list;
  5149. struct list_head del_list;
  5150. int ret;
  5151. struct extent_buffer *leaf;
  5152. int slot;
  5153. unsigned char d_type;
  5154. int over = 0;
  5155. char tmp_name[32];
  5156. char *name_ptr;
  5157. int name_len;
  5158. bool put = false;
  5159. struct btrfs_key location;
  5160. if (!dir_emit_dots(file, ctx))
  5161. return 0;
  5162. path = btrfs_alloc_path();
  5163. if (!path)
  5164. return -ENOMEM;
  5165. path->reada = READA_FORWARD;
  5166. INIT_LIST_HEAD(&ins_list);
  5167. INIT_LIST_HEAD(&del_list);
  5168. put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
  5169. key.type = BTRFS_DIR_INDEX_KEY;
  5170. key.offset = ctx->pos;
  5171. key.objectid = btrfs_ino(inode);
  5172. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5173. if (ret < 0)
  5174. goto err;
  5175. while (1) {
  5176. leaf = path->nodes[0];
  5177. slot = path->slots[0];
  5178. if (slot >= btrfs_header_nritems(leaf)) {
  5179. ret = btrfs_next_leaf(root, path);
  5180. if (ret < 0)
  5181. goto err;
  5182. else if (ret > 0)
  5183. break;
  5184. continue;
  5185. }
  5186. item = btrfs_item_nr(slot);
  5187. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5188. if (found_key.objectid != key.objectid)
  5189. break;
  5190. if (found_key.type != BTRFS_DIR_INDEX_KEY)
  5191. break;
  5192. if (found_key.offset < ctx->pos)
  5193. goto next;
  5194. if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
  5195. goto next;
  5196. ctx->pos = found_key.offset;
  5197. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  5198. if (verify_dir_item(fs_info, leaf, di))
  5199. goto next;
  5200. name_len = btrfs_dir_name_len(leaf, di);
  5201. if (name_len <= sizeof(tmp_name)) {
  5202. name_ptr = tmp_name;
  5203. } else {
  5204. name_ptr = kmalloc(name_len, GFP_KERNEL);
  5205. if (!name_ptr) {
  5206. ret = -ENOMEM;
  5207. goto err;
  5208. }
  5209. }
  5210. read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
  5211. name_len);
  5212. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  5213. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  5214. over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
  5215. d_type);
  5216. if (name_ptr != tmp_name)
  5217. kfree(name_ptr);
  5218. if (over)
  5219. goto nopos;
  5220. ctx->pos++;
  5221. next:
  5222. path->slots[0]++;
  5223. }
  5224. ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
  5225. if (ret)
  5226. goto nopos;
  5227. /*
  5228. * Stop new entries from being returned after we return the last
  5229. * entry.
  5230. *
  5231. * New directory entries are assigned a strictly increasing
  5232. * offset. This means that new entries created during readdir
  5233. * are *guaranteed* to be seen in the future by that readdir.
  5234. * This has broken buggy programs which operate on names as
  5235. * they're returned by readdir. Until we re-use freed offsets
  5236. * we have this hack to stop new entries from being returned
  5237. * under the assumption that they'll never reach this huge
  5238. * offset.
  5239. *
  5240. * This is being careful not to overflow 32bit loff_t unless the
  5241. * last entry requires it because doing so has broken 32bit apps
  5242. * in the past.
  5243. */
  5244. if (ctx->pos >= INT_MAX)
  5245. ctx->pos = LLONG_MAX;
  5246. else
  5247. ctx->pos = INT_MAX;
  5248. nopos:
  5249. ret = 0;
  5250. err:
  5251. if (put)
  5252. btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
  5253. btrfs_free_path(path);
  5254. return ret;
  5255. }
  5256. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  5257. {
  5258. struct btrfs_root *root = BTRFS_I(inode)->root;
  5259. struct btrfs_trans_handle *trans;
  5260. int ret = 0;
  5261. bool nolock = false;
  5262. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5263. return 0;
  5264. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  5265. nolock = true;
  5266. if (wbc->sync_mode == WB_SYNC_ALL) {
  5267. if (nolock)
  5268. trans = btrfs_join_transaction_nolock(root);
  5269. else
  5270. trans = btrfs_join_transaction(root);
  5271. if (IS_ERR(trans))
  5272. return PTR_ERR(trans);
  5273. ret = btrfs_commit_transaction(trans);
  5274. }
  5275. return ret;
  5276. }
  5277. /*
  5278. * This is somewhat expensive, updating the tree every time the
  5279. * inode changes. But, it is most likely to find the inode in cache.
  5280. * FIXME, needs more benchmarking...there are no reasons other than performance
  5281. * to keep or drop this code.
  5282. */
  5283. static int btrfs_dirty_inode(struct inode *inode)
  5284. {
  5285. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5286. struct btrfs_root *root = BTRFS_I(inode)->root;
  5287. struct btrfs_trans_handle *trans;
  5288. int ret;
  5289. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5290. return 0;
  5291. trans = btrfs_join_transaction(root);
  5292. if (IS_ERR(trans))
  5293. return PTR_ERR(trans);
  5294. ret = btrfs_update_inode(trans, root, inode);
  5295. if (ret && ret == -ENOSPC) {
  5296. /* whoops, lets try again with the full transaction */
  5297. btrfs_end_transaction(trans);
  5298. trans = btrfs_start_transaction(root, 1);
  5299. if (IS_ERR(trans))
  5300. return PTR_ERR(trans);
  5301. ret = btrfs_update_inode(trans, root, inode);
  5302. }
  5303. btrfs_end_transaction(trans);
  5304. if (BTRFS_I(inode)->delayed_node)
  5305. btrfs_balance_delayed_items(fs_info);
  5306. return ret;
  5307. }
  5308. /*
  5309. * This is a copy of file_update_time. We need this so we can return error on
  5310. * ENOSPC for updating the inode in the case of file write and mmap writes.
  5311. */
  5312. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  5313. int flags)
  5314. {
  5315. struct btrfs_root *root = BTRFS_I(inode)->root;
  5316. if (btrfs_root_readonly(root))
  5317. return -EROFS;
  5318. if (flags & S_VERSION)
  5319. inode_inc_iversion(inode);
  5320. if (flags & S_CTIME)
  5321. inode->i_ctime = *now;
  5322. if (flags & S_MTIME)
  5323. inode->i_mtime = *now;
  5324. if (flags & S_ATIME)
  5325. inode->i_atime = *now;
  5326. return btrfs_dirty_inode(inode);
  5327. }
  5328. /*
  5329. * find the highest existing sequence number in a directory
  5330. * and then set the in-memory index_cnt variable to reflect
  5331. * free sequence numbers
  5332. */
  5333. static int btrfs_set_inode_index_count(struct inode *inode)
  5334. {
  5335. struct btrfs_root *root = BTRFS_I(inode)->root;
  5336. struct btrfs_key key, found_key;
  5337. struct btrfs_path *path;
  5338. struct extent_buffer *leaf;
  5339. int ret;
  5340. key.objectid = btrfs_ino(inode);
  5341. key.type = BTRFS_DIR_INDEX_KEY;
  5342. key.offset = (u64)-1;
  5343. path = btrfs_alloc_path();
  5344. if (!path)
  5345. return -ENOMEM;
  5346. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5347. if (ret < 0)
  5348. goto out;
  5349. /* FIXME: we should be able to handle this */
  5350. if (ret == 0)
  5351. goto out;
  5352. ret = 0;
  5353. /*
  5354. * MAGIC NUMBER EXPLANATION:
  5355. * since we search a directory based on f_pos we have to start at 2
  5356. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  5357. * else has to start at 2
  5358. */
  5359. if (path->slots[0] == 0) {
  5360. BTRFS_I(inode)->index_cnt = 2;
  5361. goto out;
  5362. }
  5363. path->slots[0]--;
  5364. leaf = path->nodes[0];
  5365. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5366. if (found_key.objectid != btrfs_ino(inode) ||
  5367. found_key.type != BTRFS_DIR_INDEX_KEY) {
  5368. BTRFS_I(inode)->index_cnt = 2;
  5369. goto out;
  5370. }
  5371. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  5372. out:
  5373. btrfs_free_path(path);
  5374. return ret;
  5375. }
  5376. /*
  5377. * helper to find a free sequence number in a given directory. This current
  5378. * code is very simple, later versions will do smarter things in the btree
  5379. */
  5380. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  5381. {
  5382. int ret = 0;
  5383. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  5384. ret = btrfs_inode_delayed_dir_index_count(dir);
  5385. if (ret) {
  5386. ret = btrfs_set_inode_index_count(dir);
  5387. if (ret)
  5388. return ret;
  5389. }
  5390. }
  5391. *index = BTRFS_I(dir)->index_cnt;
  5392. BTRFS_I(dir)->index_cnt++;
  5393. return ret;
  5394. }
  5395. static int btrfs_insert_inode_locked(struct inode *inode)
  5396. {
  5397. struct btrfs_iget_args args;
  5398. args.location = &BTRFS_I(inode)->location;
  5399. args.root = BTRFS_I(inode)->root;
  5400. return insert_inode_locked4(inode,
  5401. btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
  5402. btrfs_find_actor, &args);
  5403. }
  5404. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  5405. struct btrfs_root *root,
  5406. struct inode *dir,
  5407. const char *name, int name_len,
  5408. u64 ref_objectid, u64 objectid,
  5409. umode_t mode, u64 *index)
  5410. {
  5411. struct btrfs_fs_info *fs_info = root->fs_info;
  5412. struct inode *inode;
  5413. struct btrfs_inode_item *inode_item;
  5414. struct btrfs_key *location;
  5415. struct btrfs_path *path;
  5416. struct btrfs_inode_ref *ref;
  5417. struct btrfs_key key[2];
  5418. u32 sizes[2];
  5419. int nitems = name ? 2 : 1;
  5420. unsigned long ptr;
  5421. int ret;
  5422. path = btrfs_alloc_path();
  5423. if (!path)
  5424. return ERR_PTR(-ENOMEM);
  5425. inode = new_inode(fs_info->sb);
  5426. if (!inode) {
  5427. btrfs_free_path(path);
  5428. return ERR_PTR(-ENOMEM);
  5429. }
  5430. /*
  5431. * O_TMPFILE, set link count to 0, so that after this point,
  5432. * we fill in an inode item with the correct link count.
  5433. */
  5434. if (!name)
  5435. set_nlink(inode, 0);
  5436. /*
  5437. * we have to initialize this early, so we can reclaim the inode
  5438. * number if we fail afterwards in this function.
  5439. */
  5440. inode->i_ino = objectid;
  5441. if (dir && name) {
  5442. trace_btrfs_inode_request(dir);
  5443. ret = btrfs_set_inode_index(dir, index);
  5444. if (ret) {
  5445. btrfs_free_path(path);
  5446. iput(inode);
  5447. return ERR_PTR(ret);
  5448. }
  5449. } else if (dir) {
  5450. *index = 0;
  5451. }
  5452. /*
  5453. * index_cnt is ignored for everything but a dir,
  5454. * btrfs_get_inode_index_count has an explanation for the magic
  5455. * number
  5456. */
  5457. BTRFS_I(inode)->index_cnt = 2;
  5458. BTRFS_I(inode)->dir_index = *index;
  5459. BTRFS_I(inode)->root = root;
  5460. BTRFS_I(inode)->generation = trans->transid;
  5461. inode->i_generation = BTRFS_I(inode)->generation;
  5462. /*
  5463. * We could have gotten an inode number from somebody who was fsynced
  5464. * and then removed in this same transaction, so let's just set full
  5465. * sync since it will be a full sync anyway and this will blow away the
  5466. * old info in the log.
  5467. */
  5468. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  5469. key[0].objectid = objectid;
  5470. key[0].type = BTRFS_INODE_ITEM_KEY;
  5471. key[0].offset = 0;
  5472. sizes[0] = sizeof(struct btrfs_inode_item);
  5473. if (name) {
  5474. /*
  5475. * Start new inodes with an inode_ref. This is slightly more
  5476. * efficient for small numbers of hard links since they will
  5477. * be packed into one item. Extended refs will kick in if we
  5478. * add more hard links than can fit in the ref item.
  5479. */
  5480. key[1].objectid = objectid;
  5481. key[1].type = BTRFS_INODE_REF_KEY;
  5482. key[1].offset = ref_objectid;
  5483. sizes[1] = name_len + sizeof(*ref);
  5484. }
  5485. location = &BTRFS_I(inode)->location;
  5486. location->objectid = objectid;
  5487. location->offset = 0;
  5488. location->type = BTRFS_INODE_ITEM_KEY;
  5489. ret = btrfs_insert_inode_locked(inode);
  5490. if (ret < 0)
  5491. goto fail;
  5492. path->leave_spinning = 1;
  5493. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
  5494. if (ret != 0)
  5495. goto fail_unlock;
  5496. inode_init_owner(inode, dir, mode);
  5497. inode_set_bytes(inode, 0);
  5498. inode->i_mtime = current_time(inode);
  5499. inode->i_atime = inode->i_mtime;
  5500. inode->i_ctime = inode->i_mtime;
  5501. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5502. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5503. struct btrfs_inode_item);
  5504. memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
  5505. sizeof(*inode_item));
  5506. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  5507. if (name) {
  5508. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  5509. struct btrfs_inode_ref);
  5510. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  5511. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  5512. ptr = (unsigned long)(ref + 1);
  5513. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  5514. }
  5515. btrfs_mark_buffer_dirty(path->nodes[0]);
  5516. btrfs_free_path(path);
  5517. btrfs_inherit_iflags(inode, dir);
  5518. if (S_ISREG(mode)) {
  5519. if (btrfs_test_opt(fs_info, NODATASUM))
  5520. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  5521. if (btrfs_test_opt(fs_info, NODATACOW))
  5522. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  5523. BTRFS_INODE_NODATASUM;
  5524. }
  5525. inode_tree_add(inode);
  5526. trace_btrfs_inode_new(inode);
  5527. btrfs_set_inode_last_trans(trans, inode);
  5528. btrfs_update_root_times(trans, root);
  5529. ret = btrfs_inode_inherit_props(trans, inode, dir);
  5530. if (ret)
  5531. btrfs_err(fs_info,
  5532. "error inheriting props for ino %llu (root %llu): %d",
  5533. btrfs_ino(inode), root->root_key.objectid, ret);
  5534. return inode;
  5535. fail_unlock:
  5536. unlock_new_inode(inode);
  5537. fail:
  5538. if (dir && name)
  5539. BTRFS_I(dir)->index_cnt--;
  5540. btrfs_free_path(path);
  5541. iput(inode);
  5542. return ERR_PTR(ret);
  5543. }
  5544. static inline u8 btrfs_inode_type(struct inode *inode)
  5545. {
  5546. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  5547. }
  5548. /*
  5549. * utility function to add 'inode' into 'parent_inode' with
  5550. * a give name and a given sequence number.
  5551. * if 'add_backref' is true, also insert a backref from the
  5552. * inode to the parent directory.
  5553. */
  5554. int btrfs_add_link(struct btrfs_trans_handle *trans,
  5555. struct inode *parent_inode, struct inode *inode,
  5556. const char *name, int name_len, int add_backref, u64 index)
  5557. {
  5558. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5559. int ret = 0;
  5560. struct btrfs_key key;
  5561. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  5562. u64 ino = btrfs_ino(inode);
  5563. u64 parent_ino = btrfs_ino(parent_inode);
  5564. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5565. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  5566. } else {
  5567. key.objectid = ino;
  5568. key.type = BTRFS_INODE_ITEM_KEY;
  5569. key.offset = 0;
  5570. }
  5571. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5572. ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
  5573. root->root_key.objectid, parent_ino,
  5574. index, name, name_len);
  5575. } else if (add_backref) {
  5576. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  5577. parent_ino, index);
  5578. }
  5579. /* Nothing to clean up yet */
  5580. if (ret)
  5581. return ret;
  5582. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  5583. parent_inode, &key,
  5584. btrfs_inode_type(inode), index);
  5585. if (ret == -EEXIST || ret == -EOVERFLOW)
  5586. goto fail_dir_item;
  5587. else if (ret) {
  5588. btrfs_abort_transaction(trans, ret);
  5589. return ret;
  5590. }
  5591. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  5592. name_len * 2);
  5593. inode_inc_iversion(parent_inode);
  5594. parent_inode->i_mtime = parent_inode->i_ctime =
  5595. current_time(parent_inode);
  5596. ret = btrfs_update_inode(trans, root, parent_inode);
  5597. if (ret)
  5598. btrfs_abort_transaction(trans, ret);
  5599. return ret;
  5600. fail_dir_item:
  5601. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5602. u64 local_index;
  5603. int err;
  5604. err = btrfs_del_root_ref(trans, fs_info, key.objectid,
  5605. root->root_key.objectid, parent_ino,
  5606. &local_index, name, name_len);
  5607. } else if (add_backref) {
  5608. u64 local_index;
  5609. int err;
  5610. err = btrfs_del_inode_ref(trans, root, name, name_len,
  5611. ino, parent_ino, &local_index);
  5612. }
  5613. return ret;
  5614. }
  5615. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  5616. struct inode *dir, struct dentry *dentry,
  5617. struct inode *inode, int backref, u64 index)
  5618. {
  5619. int err = btrfs_add_link(trans, dir, inode,
  5620. dentry->d_name.name, dentry->d_name.len,
  5621. backref, index);
  5622. if (err > 0)
  5623. err = -EEXIST;
  5624. return err;
  5625. }
  5626. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  5627. umode_t mode, dev_t rdev)
  5628. {
  5629. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5630. struct btrfs_trans_handle *trans;
  5631. struct btrfs_root *root = BTRFS_I(dir)->root;
  5632. struct inode *inode = NULL;
  5633. int err;
  5634. int drop_inode = 0;
  5635. u64 objectid;
  5636. u64 index = 0;
  5637. /*
  5638. * 2 for inode item and ref
  5639. * 2 for dir items
  5640. * 1 for xattr if selinux is on
  5641. */
  5642. trans = btrfs_start_transaction(root, 5);
  5643. if (IS_ERR(trans))
  5644. return PTR_ERR(trans);
  5645. err = btrfs_find_free_ino(root, &objectid);
  5646. if (err)
  5647. goto out_unlock;
  5648. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5649. dentry->d_name.len, btrfs_ino(dir), objectid,
  5650. mode, &index);
  5651. if (IS_ERR(inode)) {
  5652. err = PTR_ERR(inode);
  5653. goto out_unlock;
  5654. }
  5655. /*
  5656. * If the active LSM wants to access the inode during
  5657. * d_instantiate it needs these. Smack checks to see
  5658. * if the filesystem supports xattrs by looking at the
  5659. * ops vector.
  5660. */
  5661. inode->i_op = &btrfs_special_inode_operations;
  5662. init_special_inode(inode, inode->i_mode, rdev);
  5663. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5664. if (err)
  5665. goto out_unlock_inode;
  5666. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5667. if (err) {
  5668. goto out_unlock_inode;
  5669. } else {
  5670. btrfs_update_inode(trans, root, inode);
  5671. unlock_new_inode(inode);
  5672. d_instantiate(dentry, inode);
  5673. }
  5674. out_unlock:
  5675. btrfs_end_transaction(trans);
  5676. btrfs_balance_delayed_items(fs_info);
  5677. btrfs_btree_balance_dirty(fs_info);
  5678. if (drop_inode) {
  5679. inode_dec_link_count(inode);
  5680. iput(inode);
  5681. }
  5682. return err;
  5683. out_unlock_inode:
  5684. drop_inode = 1;
  5685. unlock_new_inode(inode);
  5686. goto out_unlock;
  5687. }
  5688. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  5689. umode_t mode, bool excl)
  5690. {
  5691. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5692. struct btrfs_trans_handle *trans;
  5693. struct btrfs_root *root = BTRFS_I(dir)->root;
  5694. struct inode *inode = NULL;
  5695. int drop_inode_on_err = 0;
  5696. int err;
  5697. u64 objectid;
  5698. u64 index = 0;
  5699. /*
  5700. * 2 for inode item and ref
  5701. * 2 for dir items
  5702. * 1 for xattr if selinux is on
  5703. */
  5704. trans = btrfs_start_transaction(root, 5);
  5705. if (IS_ERR(trans))
  5706. return PTR_ERR(trans);
  5707. err = btrfs_find_free_ino(root, &objectid);
  5708. if (err)
  5709. goto out_unlock;
  5710. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5711. dentry->d_name.len, btrfs_ino(dir), objectid,
  5712. mode, &index);
  5713. if (IS_ERR(inode)) {
  5714. err = PTR_ERR(inode);
  5715. goto out_unlock;
  5716. }
  5717. drop_inode_on_err = 1;
  5718. /*
  5719. * If the active LSM wants to access the inode during
  5720. * d_instantiate it needs these. Smack checks to see
  5721. * if the filesystem supports xattrs by looking at the
  5722. * ops vector.
  5723. */
  5724. inode->i_fop = &btrfs_file_operations;
  5725. inode->i_op = &btrfs_file_inode_operations;
  5726. inode->i_mapping->a_ops = &btrfs_aops;
  5727. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5728. if (err)
  5729. goto out_unlock_inode;
  5730. err = btrfs_update_inode(trans, root, inode);
  5731. if (err)
  5732. goto out_unlock_inode;
  5733. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5734. if (err)
  5735. goto out_unlock_inode;
  5736. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5737. unlock_new_inode(inode);
  5738. d_instantiate(dentry, inode);
  5739. out_unlock:
  5740. btrfs_end_transaction(trans);
  5741. if (err && drop_inode_on_err) {
  5742. inode_dec_link_count(inode);
  5743. iput(inode);
  5744. }
  5745. btrfs_balance_delayed_items(fs_info);
  5746. btrfs_btree_balance_dirty(fs_info);
  5747. return err;
  5748. out_unlock_inode:
  5749. unlock_new_inode(inode);
  5750. goto out_unlock;
  5751. }
  5752. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5753. struct dentry *dentry)
  5754. {
  5755. struct btrfs_trans_handle *trans = NULL;
  5756. struct btrfs_root *root = BTRFS_I(dir)->root;
  5757. struct inode *inode = d_inode(old_dentry);
  5758. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5759. u64 index;
  5760. int err;
  5761. int drop_inode = 0;
  5762. /* do not allow sys_link's with other subvols of the same device */
  5763. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5764. return -EXDEV;
  5765. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5766. return -EMLINK;
  5767. err = btrfs_set_inode_index(dir, &index);
  5768. if (err)
  5769. goto fail;
  5770. /*
  5771. * 2 items for inode and inode ref
  5772. * 2 items for dir items
  5773. * 1 item for parent inode
  5774. */
  5775. trans = btrfs_start_transaction(root, 5);
  5776. if (IS_ERR(trans)) {
  5777. err = PTR_ERR(trans);
  5778. trans = NULL;
  5779. goto fail;
  5780. }
  5781. /* There are several dir indexes for this inode, clear the cache. */
  5782. BTRFS_I(inode)->dir_index = 0ULL;
  5783. inc_nlink(inode);
  5784. inode_inc_iversion(inode);
  5785. inode->i_ctime = current_time(inode);
  5786. ihold(inode);
  5787. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5788. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  5789. if (err) {
  5790. drop_inode = 1;
  5791. } else {
  5792. struct dentry *parent = dentry->d_parent;
  5793. err = btrfs_update_inode(trans, root, inode);
  5794. if (err)
  5795. goto fail;
  5796. if (inode->i_nlink == 1) {
  5797. /*
  5798. * If new hard link count is 1, it's a file created
  5799. * with open(2) O_TMPFILE flag.
  5800. */
  5801. err = btrfs_orphan_del(trans, inode);
  5802. if (err)
  5803. goto fail;
  5804. }
  5805. d_instantiate(dentry, inode);
  5806. btrfs_log_new_name(trans, inode, NULL, parent);
  5807. }
  5808. btrfs_balance_delayed_items(fs_info);
  5809. fail:
  5810. if (trans)
  5811. btrfs_end_transaction(trans);
  5812. if (drop_inode) {
  5813. inode_dec_link_count(inode);
  5814. iput(inode);
  5815. }
  5816. btrfs_btree_balance_dirty(fs_info);
  5817. return err;
  5818. }
  5819. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5820. {
  5821. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5822. struct inode *inode = NULL;
  5823. struct btrfs_trans_handle *trans;
  5824. struct btrfs_root *root = BTRFS_I(dir)->root;
  5825. int err = 0;
  5826. int drop_on_err = 0;
  5827. u64 objectid = 0;
  5828. u64 index = 0;
  5829. /*
  5830. * 2 items for inode and ref
  5831. * 2 items for dir items
  5832. * 1 for xattr if selinux is on
  5833. */
  5834. trans = btrfs_start_transaction(root, 5);
  5835. if (IS_ERR(trans))
  5836. return PTR_ERR(trans);
  5837. err = btrfs_find_free_ino(root, &objectid);
  5838. if (err)
  5839. goto out_fail;
  5840. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5841. dentry->d_name.len, btrfs_ino(dir), objectid,
  5842. S_IFDIR | mode, &index);
  5843. if (IS_ERR(inode)) {
  5844. err = PTR_ERR(inode);
  5845. goto out_fail;
  5846. }
  5847. drop_on_err = 1;
  5848. /* these must be set before we unlock the inode */
  5849. inode->i_op = &btrfs_dir_inode_operations;
  5850. inode->i_fop = &btrfs_dir_file_operations;
  5851. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5852. if (err)
  5853. goto out_fail_inode;
  5854. btrfs_i_size_write(inode, 0);
  5855. err = btrfs_update_inode(trans, root, inode);
  5856. if (err)
  5857. goto out_fail_inode;
  5858. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  5859. dentry->d_name.len, 0, index);
  5860. if (err)
  5861. goto out_fail_inode;
  5862. d_instantiate(dentry, inode);
  5863. /*
  5864. * mkdir is special. We're unlocking after we call d_instantiate
  5865. * to avoid a race with nfsd calling d_instantiate.
  5866. */
  5867. unlock_new_inode(inode);
  5868. drop_on_err = 0;
  5869. out_fail:
  5870. btrfs_end_transaction(trans);
  5871. if (drop_on_err) {
  5872. inode_dec_link_count(inode);
  5873. iput(inode);
  5874. }
  5875. btrfs_balance_delayed_items(fs_info);
  5876. btrfs_btree_balance_dirty(fs_info);
  5877. return err;
  5878. out_fail_inode:
  5879. unlock_new_inode(inode);
  5880. goto out_fail;
  5881. }
  5882. /* Find next extent map of a given extent map, caller needs to ensure locks */
  5883. static struct extent_map *next_extent_map(struct extent_map *em)
  5884. {
  5885. struct rb_node *next;
  5886. next = rb_next(&em->rb_node);
  5887. if (!next)
  5888. return NULL;
  5889. return container_of(next, struct extent_map, rb_node);
  5890. }
  5891. static struct extent_map *prev_extent_map(struct extent_map *em)
  5892. {
  5893. struct rb_node *prev;
  5894. prev = rb_prev(&em->rb_node);
  5895. if (!prev)
  5896. return NULL;
  5897. return container_of(prev, struct extent_map, rb_node);
  5898. }
  5899. /* helper for btfs_get_extent. Given an existing extent in the tree,
  5900. * the existing extent is the nearest extent to map_start,
  5901. * and an extent that you want to insert, deal with overlap and insert
  5902. * the best fitted new extent into the tree.
  5903. */
  5904. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  5905. struct extent_map *existing,
  5906. struct extent_map *em,
  5907. u64 map_start)
  5908. {
  5909. struct extent_map *prev;
  5910. struct extent_map *next;
  5911. u64 start;
  5912. u64 end;
  5913. u64 start_diff;
  5914. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  5915. if (existing->start > map_start) {
  5916. next = existing;
  5917. prev = prev_extent_map(next);
  5918. } else {
  5919. prev = existing;
  5920. next = next_extent_map(prev);
  5921. }
  5922. start = prev ? extent_map_end(prev) : em->start;
  5923. start = max_t(u64, start, em->start);
  5924. end = next ? next->start : extent_map_end(em);
  5925. end = min_t(u64, end, extent_map_end(em));
  5926. start_diff = start - em->start;
  5927. em->start = start;
  5928. em->len = end - start;
  5929. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  5930. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  5931. em->block_start += start_diff;
  5932. em->block_len -= start_diff;
  5933. }
  5934. return add_extent_mapping(em_tree, em, 0);
  5935. }
  5936. static noinline int uncompress_inline(struct btrfs_path *path,
  5937. struct page *page,
  5938. size_t pg_offset, u64 extent_offset,
  5939. struct btrfs_file_extent_item *item)
  5940. {
  5941. int ret;
  5942. struct extent_buffer *leaf = path->nodes[0];
  5943. char *tmp;
  5944. size_t max_size;
  5945. unsigned long inline_size;
  5946. unsigned long ptr;
  5947. int compress_type;
  5948. WARN_ON(pg_offset != 0);
  5949. compress_type = btrfs_file_extent_compression(leaf, item);
  5950. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5951. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5952. btrfs_item_nr(path->slots[0]));
  5953. tmp = kmalloc(inline_size, GFP_NOFS);
  5954. if (!tmp)
  5955. return -ENOMEM;
  5956. ptr = btrfs_file_extent_inline_start(item);
  5957. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5958. max_size = min_t(unsigned long, PAGE_SIZE, max_size);
  5959. ret = btrfs_decompress(compress_type, tmp, page,
  5960. extent_offset, inline_size, max_size);
  5961. kfree(tmp);
  5962. return ret;
  5963. }
  5964. /*
  5965. * a bit scary, this does extent mapping from logical file offset to the disk.
  5966. * the ugly parts come from merging extents from the disk with the in-ram
  5967. * representation. This gets more complex because of the data=ordered code,
  5968. * where the in-ram extents might be locked pending data=ordered completion.
  5969. *
  5970. * This also copies inline extents directly into the page.
  5971. */
  5972. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  5973. size_t pg_offset, u64 start, u64 len,
  5974. int create)
  5975. {
  5976. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5977. int ret;
  5978. int err = 0;
  5979. u64 extent_start = 0;
  5980. u64 extent_end = 0;
  5981. u64 objectid = btrfs_ino(inode);
  5982. u32 found_type;
  5983. struct btrfs_path *path = NULL;
  5984. struct btrfs_root *root = BTRFS_I(inode)->root;
  5985. struct btrfs_file_extent_item *item;
  5986. struct extent_buffer *leaf;
  5987. struct btrfs_key found_key;
  5988. struct extent_map *em = NULL;
  5989. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5990. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5991. struct btrfs_trans_handle *trans = NULL;
  5992. const bool new_inline = !page || create;
  5993. again:
  5994. read_lock(&em_tree->lock);
  5995. em = lookup_extent_mapping(em_tree, start, len);
  5996. if (em)
  5997. em->bdev = fs_info->fs_devices->latest_bdev;
  5998. read_unlock(&em_tree->lock);
  5999. if (em) {
  6000. if (em->start > start || em->start + em->len <= start)
  6001. free_extent_map(em);
  6002. else if (em->block_start == EXTENT_MAP_INLINE && page)
  6003. free_extent_map(em);
  6004. else
  6005. goto out;
  6006. }
  6007. em = alloc_extent_map();
  6008. if (!em) {
  6009. err = -ENOMEM;
  6010. goto out;
  6011. }
  6012. em->bdev = fs_info->fs_devices->latest_bdev;
  6013. em->start = EXTENT_MAP_HOLE;
  6014. em->orig_start = EXTENT_MAP_HOLE;
  6015. em->len = (u64)-1;
  6016. em->block_len = (u64)-1;
  6017. if (!path) {
  6018. path = btrfs_alloc_path();
  6019. if (!path) {
  6020. err = -ENOMEM;
  6021. goto out;
  6022. }
  6023. /*
  6024. * Chances are we'll be called again, so go ahead and do
  6025. * readahead
  6026. */
  6027. path->reada = READA_FORWARD;
  6028. }
  6029. ret = btrfs_lookup_file_extent(trans, root, path,
  6030. objectid, start, trans != NULL);
  6031. if (ret < 0) {
  6032. err = ret;
  6033. goto out;
  6034. }
  6035. if (ret != 0) {
  6036. if (path->slots[0] == 0)
  6037. goto not_found;
  6038. path->slots[0]--;
  6039. }
  6040. leaf = path->nodes[0];
  6041. item = btrfs_item_ptr(leaf, path->slots[0],
  6042. struct btrfs_file_extent_item);
  6043. /* are we inside the extent that was found? */
  6044. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6045. found_type = found_key.type;
  6046. if (found_key.objectid != objectid ||
  6047. found_type != BTRFS_EXTENT_DATA_KEY) {
  6048. /*
  6049. * If we backup past the first extent we want to move forward
  6050. * and see if there is an extent in front of us, otherwise we'll
  6051. * say there is a hole for our whole search range which can
  6052. * cause problems.
  6053. */
  6054. extent_end = start;
  6055. goto next;
  6056. }
  6057. found_type = btrfs_file_extent_type(leaf, item);
  6058. extent_start = found_key.offset;
  6059. if (found_type == BTRFS_FILE_EXTENT_REG ||
  6060. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6061. extent_end = extent_start +
  6062. btrfs_file_extent_num_bytes(leaf, item);
  6063. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  6064. size_t size;
  6065. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  6066. extent_end = ALIGN(extent_start + size,
  6067. fs_info->sectorsize);
  6068. }
  6069. next:
  6070. if (start >= extent_end) {
  6071. path->slots[0]++;
  6072. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  6073. ret = btrfs_next_leaf(root, path);
  6074. if (ret < 0) {
  6075. err = ret;
  6076. goto out;
  6077. }
  6078. if (ret > 0)
  6079. goto not_found;
  6080. leaf = path->nodes[0];
  6081. }
  6082. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6083. if (found_key.objectid != objectid ||
  6084. found_key.type != BTRFS_EXTENT_DATA_KEY)
  6085. goto not_found;
  6086. if (start + len <= found_key.offset)
  6087. goto not_found;
  6088. if (start > found_key.offset)
  6089. goto next;
  6090. em->start = start;
  6091. em->orig_start = start;
  6092. em->len = found_key.offset - start;
  6093. goto not_found_em;
  6094. }
  6095. btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
  6096. if (found_type == BTRFS_FILE_EXTENT_REG ||
  6097. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6098. goto insert;
  6099. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  6100. unsigned long ptr;
  6101. char *map;
  6102. size_t size;
  6103. size_t extent_offset;
  6104. size_t copy_size;
  6105. if (new_inline)
  6106. goto out;
  6107. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  6108. extent_offset = page_offset(page) + pg_offset - extent_start;
  6109. copy_size = min_t(u64, PAGE_SIZE - pg_offset,
  6110. size - extent_offset);
  6111. em->start = extent_start + extent_offset;
  6112. em->len = ALIGN(copy_size, fs_info->sectorsize);
  6113. em->orig_block_len = em->len;
  6114. em->orig_start = em->start;
  6115. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  6116. if (create == 0 && !PageUptodate(page)) {
  6117. if (btrfs_file_extent_compression(leaf, item) !=
  6118. BTRFS_COMPRESS_NONE) {
  6119. ret = uncompress_inline(path, page, pg_offset,
  6120. extent_offset, item);
  6121. if (ret) {
  6122. err = ret;
  6123. goto out;
  6124. }
  6125. } else {
  6126. map = kmap(page);
  6127. read_extent_buffer(leaf, map + pg_offset, ptr,
  6128. copy_size);
  6129. if (pg_offset + copy_size < PAGE_SIZE) {
  6130. memset(map + pg_offset + copy_size, 0,
  6131. PAGE_SIZE - pg_offset -
  6132. copy_size);
  6133. }
  6134. kunmap(page);
  6135. }
  6136. flush_dcache_page(page);
  6137. } else if (create && PageUptodate(page)) {
  6138. BUG();
  6139. if (!trans) {
  6140. kunmap(page);
  6141. free_extent_map(em);
  6142. em = NULL;
  6143. btrfs_release_path(path);
  6144. trans = btrfs_join_transaction(root);
  6145. if (IS_ERR(trans))
  6146. return ERR_CAST(trans);
  6147. goto again;
  6148. }
  6149. map = kmap(page);
  6150. write_extent_buffer(leaf, map + pg_offset, ptr,
  6151. copy_size);
  6152. kunmap(page);
  6153. btrfs_mark_buffer_dirty(leaf);
  6154. }
  6155. set_extent_uptodate(io_tree, em->start,
  6156. extent_map_end(em) - 1, NULL, GFP_NOFS);
  6157. goto insert;
  6158. }
  6159. not_found:
  6160. em->start = start;
  6161. em->orig_start = start;
  6162. em->len = len;
  6163. not_found_em:
  6164. em->block_start = EXTENT_MAP_HOLE;
  6165. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  6166. insert:
  6167. btrfs_release_path(path);
  6168. if (em->start > start || extent_map_end(em) <= start) {
  6169. btrfs_err(fs_info,
  6170. "bad extent! em: [%llu %llu] passed [%llu %llu]",
  6171. em->start, em->len, start, len);
  6172. err = -EIO;
  6173. goto out;
  6174. }
  6175. err = 0;
  6176. write_lock(&em_tree->lock);
  6177. ret = add_extent_mapping(em_tree, em, 0);
  6178. /* it is possible that someone inserted the extent into the tree
  6179. * while we had the lock dropped. It is also possible that
  6180. * an overlapping map exists in the tree
  6181. */
  6182. if (ret == -EEXIST) {
  6183. struct extent_map *existing;
  6184. ret = 0;
  6185. existing = search_extent_mapping(em_tree, start, len);
  6186. /*
  6187. * existing will always be non-NULL, since there must be
  6188. * extent causing the -EEXIST.
  6189. */
  6190. if (existing->start == em->start &&
  6191. extent_map_end(existing) >= extent_map_end(em) &&
  6192. em->block_start == existing->block_start) {
  6193. /*
  6194. * The existing extent map already encompasses the
  6195. * entire extent map we tried to add.
  6196. */
  6197. free_extent_map(em);
  6198. em = existing;
  6199. err = 0;
  6200. } else if (start >= extent_map_end(existing) ||
  6201. start <= existing->start) {
  6202. /*
  6203. * The existing extent map is the one nearest to
  6204. * the [start, start + len) range which overlaps
  6205. */
  6206. err = merge_extent_mapping(em_tree, existing,
  6207. em, start);
  6208. free_extent_map(existing);
  6209. if (err) {
  6210. free_extent_map(em);
  6211. em = NULL;
  6212. }
  6213. } else {
  6214. free_extent_map(em);
  6215. em = existing;
  6216. err = 0;
  6217. }
  6218. }
  6219. write_unlock(&em_tree->lock);
  6220. out:
  6221. trace_btrfs_get_extent(root, inode, em);
  6222. btrfs_free_path(path);
  6223. if (trans) {
  6224. ret = btrfs_end_transaction(trans);
  6225. if (!err)
  6226. err = ret;
  6227. }
  6228. if (err) {
  6229. free_extent_map(em);
  6230. return ERR_PTR(err);
  6231. }
  6232. BUG_ON(!em); /* Error is always set */
  6233. return em;
  6234. }
  6235. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  6236. size_t pg_offset, u64 start, u64 len,
  6237. int create)
  6238. {
  6239. struct extent_map *em;
  6240. struct extent_map *hole_em = NULL;
  6241. u64 range_start = start;
  6242. u64 end;
  6243. u64 found;
  6244. u64 found_end;
  6245. int err = 0;
  6246. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  6247. if (IS_ERR(em))
  6248. return em;
  6249. if (em) {
  6250. /*
  6251. * if our em maps to
  6252. * - a hole or
  6253. * - a pre-alloc extent,
  6254. * there might actually be delalloc bytes behind it.
  6255. */
  6256. if (em->block_start != EXTENT_MAP_HOLE &&
  6257. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6258. return em;
  6259. else
  6260. hole_em = em;
  6261. }
  6262. /* check to see if we've wrapped (len == -1 or similar) */
  6263. end = start + len;
  6264. if (end < start)
  6265. end = (u64)-1;
  6266. else
  6267. end -= 1;
  6268. em = NULL;
  6269. /* ok, we didn't find anything, lets look for delalloc */
  6270. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  6271. end, len, EXTENT_DELALLOC, 1);
  6272. found_end = range_start + found;
  6273. if (found_end < range_start)
  6274. found_end = (u64)-1;
  6275. /*
  6276. * we didn't find anything useful, return
  6277. * the original results from get_extent()
  6278. */
  6279. if (range_start > end || found_end <= start) {
  6280. em = hole_em;
  6281. hole_em = NULL;
  6282. goto out;
  6283. }
  6284. /* adjust the range_start to make sure it doesn't
  6285. * go backwards from the start they passed in
  6286. */
  6287. range_start = max(start, range_start);
  6288. found = found_end - range_start;
  6289. if (found > 0) {
  6290. u64 hole_start = start;
  6291. u64 hole_len = len;
  6292. em = alloc_extent_map();
  6293. if (!em) {
  6294. err = -ENOMEM;
  6295. goto out;
  6296. }
  6297. /*
  6298. * when btrfs_get_extent can't find anything it
  6299. * returns one huge hole
  6300. *
  6301. * make sure what it found really fits our range, and
  6302. * adjust to make sure it is based on the start from
  6303. * the caller
  6304. */
  6305. if (hole_em) {
  6306. u64 calc_end = extent_map_end(hole_em);
  6307. if (calc_end <= start || (hole_em->start > end)) {
  6308. free_extent_map(hole_em);
  6309. hole_em = NULL;
  6310. } else {
  6311. hole_start = max(hole_em->start, start);
  6312. hole_len = calc_end - hole_start;
  6313. }
  6314. }
  6315. em->bdev = NULL;
  6316. if (hole_em && range_start > hole_start) {
  6317. /* our hole starts before our delalloc, so we
  6318. * have to return just the parts of the hole
  6319. * that go until the delalloc starts
  6320. */
  6321. em->len = min(hole_len,
  6322. range_start - hole_start);
  6323. em->start = hole_start;
  6324. em->orig_start = hole_start;
  6325. /*
  6326. * don't adjust block start at all,
  6327. * it is fixed at EXTENT_MAP_HOLE
  6328. */
  6329. em->block_start = hole_em->block_start;
  6330. em->block_len = hole_len;
  6331. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  6332. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  6333. } else {
  6334. em->start = range_start;
  6335. em->len = found;
  6336. em->orig_start = range_start;
  6337. em->block_start = EXTENT_MAP_DELALLOC;
  6338. em->block_len = found;
  6339. }
  6340. } else if (hole_em) {
  6341. return hole_em;
  6342. }
  6343. out:
  6344. free_extent_map(hole_em);
  6345. if (err) {
  6346. free_extent_map(em);
  6347. return ERR_PTR(err);
  6348. }
  6349. return em;
  6350. }
  6351. static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
  6352. const u64 start,
  6353. const u64 len,
  6354. const u64 orig_start,
  6355. const u64 block_start,
  6356. const u64 block_len,
  6357. const u64 orig_block_len,
  6358. const u64 ram_bytes,
  6359. const int type)
  6360. {
  6361. struct extent_map *em = NULL;
  6362. int ret;
  6363. if (type != BTRFS_ORDERED_NOCOW) {
  6364. em = create_pinned_em(inode, start, len, orig_start,
  6365. block_start, block_len, orig_block_len,
  6366. ram_bytes, type);
  6367. if (IS_ERR(em))
  6368. goto out;
  6369. }
  6370. ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
  6371. len, block_len, type);
  6372. if (ret) {
  6373. if (em) {
  6374. free_extent_map(em);
  6375. btrfs_drop_extent_cache(inode, start,
  6376. start + len - 1, 0);
  6377. }
  6378. em = ERR_PTR(ret);
  6379. }
  6380. out:
  6381. return em;
  6382. }
  6383. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  6384. u64 start, u64 len)
  6385. {
  6386. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6387. struct btrfs_root *root = BTRFS_I(inode)->root;
  6388. struct extent_map *em;
  6389. struct btrfs_key ins;
  6390. u64 alloc_hint;
  6391. int ret;
  6392. alloc_hint = get_extent_allocation_hint(inode, start, len);
  6393. ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
  6394. 0, alloc_hint, &ins, 1, 1);
  6395. if (ret)
  6396. return ERR_PTR(ret);
  6397. em = btrfs_create_dio_extent(inode, start, ins.offset, start,
  6398. ins.objectid, ins.offset, ins.offset,
  6399. ins.offset, 0);
  6400. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  6401. if (IS_ERR(em))
  6402. btrfs_free_reserved_extent(fs_info, ins.objectid,
  6403. ins.offset, 1);
  6404. return em;
  6405. }
  6406. /*
  6407. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  6408. * block must be cow'd
  6409. */
  6410. noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
  6411. u64 *orig_start, u64 *orig_block_len,
  6412. u64 *ram_bytes)
  6413. {
  6414. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6415. struct btrfs_trans_handle *trans;
  6416. struct btrfs_path *path;
  6417. int ret;
  6418. struct extent_buffer *leaf;
  6419. struct btrfs_root *root = BTRFS_I(inode)->root;
  6420. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6421. struct btrfs_file_extent_item *fi;
  6422. struct btrfs_key key;
  6423. u64 disk_bytenr;
  6424. u64 backref_offset;
  6425. u64 extent_end;
  6426. u64 num_bytes;
  6427. int slot;
  6428. int found_type;
  6429. bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
  6430. path = btrfs_alloc_path();
  6431. if (!path)
  6432. return -ENOMEM;
  6433. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
  6434. offset, 0);
  6435. if (ret < 0)
  6436. goto out;
  6437. slot = path->slots[0];
  6438. if (ret == 1) {
  6439. if (slot == 0) {
  6440. /* can't find the item, must cow */
  6441. ret = 0;
  6442. goto out;
  6443. }
  6444. slot--;
  6445. }
  6446. ret = 0;
  6447. leaf = path->nodes[0];
  6448. btrfs_item_key_to_cpu(leaf, &key, slot);
  6449. if (key.objectid != btrfs_ino(inode) ||
  6450. key.type != BTRFS_EXTENT_DATA_KEY) {
  6451. /* not our file or wrong item type, must cow */
  6452. goto out;
  6453. }
  6454. if (key.offset > offset) {
  6455. /* Wrong offset, must cow */
  6456. goto out;
  6457. }
  6458. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  6459. found_type = btrfs_file_extent_type(leaf, fi);
  6460. if (found_type != BTRFS_FILE_EXTENT_REG &&
  6461. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  6462. /* not a regular extent, must cow */
  6463. goto out;
  6464. }
  6465. if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
  6466. goto out;
  6467. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  6468. if (extent_end <= offset)
  6469. goto out;
  6470. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6471. if (disk_bytenr == 0)
  6472. goto out;
  6473. if (btrfs_file_extent_compression(leaf, fi) ||
  6474. btrfs_file_extent_encryption(leaf, fi) ||
  6475. btrfs_file_extent_other_encoding(leaf, fi))
  6476. goto out;
  6477. backref_offset = btrfs_file_extent_offset(leaf, fi);
  6478. if (orig_start) {
  6479. *orig_start = key.offset - backref_offset;
  6480. *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6481. *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6482. }
  6483. if (btrfs_extent_readonly(fs_info, disk_bytenr))
  6484. goto out;
  6485. num_bytes = min(offset + *len, extent_end) - offset;
  6486. if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6487. u64 range_end;
  6488. range_end = round_up(offset + num_bytes,
  6489. root->fs_info->sectorsize) - 1;
  6490. ret = test_range_bit(io_tree, offset, range_end,
  6491. EXTENT_DELALLOC, 0, NULL);
  6492. if (ret) {
  6493. ret = -EAGAIN;
  6494. goto out;
  6495. }
  6496. }
  6497. btrfs_release_path(path);
  6498. /*
  6499. * look for other files referencing this extent, if we
  6500. * find any we must cow
  6501. */
  6502. trans = btrfs_join_transaction(root);
  6503. if (IS_ERR(trans)) {
  6504. ret = 0;
  6505. goto out;
  6506. }
  6507. ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  6508. key.offset - backref_offset, disk_bytenr);
  6509. btrfs_end_transaction(trans);
  6510. if (ret) {
  6511. ret = 0;
  6512. goto out;
  6513. }
  6514. /*
  6515. * adjust disk_bytenr and num_bytes to cover just the bytes
  6516. * in this extent we are about to write. If there
  6517. * are any csums in that range we have to cow in order
  6518. * to keep the csums correct
  6519. */
  6520. disk_bytenr += backref_offset;
  6521. disk_bytenr += offset - key.offset;
  6522. if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
  6523. goto out;
  6524. /*
  6525. * all of the above have passed, it is safe to overwrite this extent
  6526. * without cow
  6527. */
  6528. *len = num_bytes;
  6529. ret = 1;
  6530. out:
  6531. btrfs_free_path(path);
  6532. return ret;
  6533. }
  6534. bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
  6535. {
  6536. struct radix_tree_root *root = &inode->i_mapping->page_tree;
  6537. int found = false;
  6538. void **pagep = NULL;
  6539. struct page *page = NULL;
  6540. int start_idx;
  6541. int end_idx;
  6542. start_idx = start >> PAGE_SHIFT;
  6543. /*
  6544. * end is the last byte in the last page. end == start is legal
  6545. */
  6546. end_idx = end >> PAGE_SHIFT;
  6547. rcu_read_lock();
  6548. /* Most of the code in this while loop is lifted from
  6549. * find_get_page. It's been modified to begin searching from a
  6550. * page and return just the first page found in that range. If the
  6551. * found idx is less than or equal to the end idx then we know that
  6552. * a page exists. If no pages are found or if those pages are
  6553. * outside of the range then we're fine (yay!) */
  6554. while (page == NULL &&
  6555. radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
  6556. page = radix_tree_deref_slot(pagep);
  6557. if (unlikely(!page))
  6558. break;
  6559. if (radix_tree_exception(page)) {
  6560. if (radix_tree_deref_retry(page)) {
  6561. page = NULL;
  6562. continue;
  6563. }
  6564. /*
  6565. * Otherwise, shmem/tmpfs must be storing a swap entry
  6566. * here as an exceptional entry: so return it without
  6567. * attempting to raise page count.
  6568. */
  6569. page = NULL;
  6570. break; /* TODO: Is this relevant for this use case? */
  6571. }
  6572. if (!page_cache_get_speculative(page)) {
  6573. page = NULL;
  6574. continue;
  6575. }
  6576. /*
  6577. * Has the page moved?
  6578. * This is part of the lockless pagecache protocol. See
  6579. * include/linux/pagemap.h for details.
  6580. */
  6581. if (unlikely(page != *pagep)) {
  6582. put_page(page);
  6583. page = NULL;
  6584. }
  6585. }
  6586. if (page) {
  6587. if (page->index <= end_idx)
  6588. found = true;
  6589. put_page(page);
  6590. }
  6591. rcu_read_unlock();
  6592. return found;
  6593. }
  6594. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  6595. struct extent_state **cached_state, int writing)
  6596. {
  6597. struct btrfs_ordered_extent *ordered;
  6598. int ret = 0;
  6599. while (1) {
  6600. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6601. cached_state);
  6602. /*
  6603. * We're concerned with the entire range that we're going to be
  6604. * doing DIO to, so we need to make sure there's no ordered
  6605. * extents in this range.
  6606. */
  6607. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  6608. lockend - lockstart + 1);
  6609. /*
  6610. * We need to make sure there are no buffered pages in this
  6611. * range either, we could have raced between the invalidate in
  6612. * generic_file_direct_write and locking the extent. The
  6613. * invalidate needs to happen so that reads after a write do not
  6614. * get stale data.
  6615. */
  6616. if (!ordered &&
  6617. (!writing ||
  6618. !btrfs_page_exists_in_range(inode, lockstart, lockend)))
  6619. break;
  6620. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6621. cached_state, GFP_NOFS);
  6622. if (ordered) {
  6623. /*
  6624. * If we are doing a DIO read and the ordered extent we
  6625. * found is for a buffered write, we can not wait for it
  6626. * to complete and retry, because if we do so we can
  6627. * deadlock with concurrent buffered writes on page
  6628. * locks. This happens only if our DIO read covers more
  6629. * than one extent map, if at this point has already
  6630. * created an ordered extent for a previous extent map
  6631. * and locked its range in the inode's io tree, and a
  6632. * concurrent write against that previous extent map's
  6633. * range and this range started (we unlock the ranges
  6634. * in the io tree only when the bios complete and
  6635. * buffered writes always lock pages before attempting
  6636. * to lock range in the io tree).
  6637. */
  6638. if (writing ||
  6639. test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
  6640. btrfs_start_ordered_extent(inode, ordered, 1);
  6641. else
  6642. ret = -ENOTBLK;
  6643. btrfs_put_ordered_extent(ordered);
  6644. } else {
  6645. /*
  6646. * We could trigger writeback for this range (and wait
  6647. * for it to complete) and then invalidate the pages for
  6648. * this range (through invalidate_inode_pages2_range()),
  6649. * but that can lead us to a deadlock with a concurrent
  6650. * call to readpages() (a buffered read or a defrag call
  6651. * triggered a readahead) on a page lock due to an
  6652. * ordered dio extent we created before but did not have
  6653. * yet a corresponding bio submitted (whence it can not
  6654. * complete), which makes readpages() wait for that
  6655. * ordered extent to complete while holding a lock on
  6656. * that page.
  6657. */
  6658. ret = -ENOTBLK;
  6659. }
  6660. if (ret)
  6661. break;
  6662. cond_resched();
  6663. }
  6664. return ret;
  6665. }
  6666. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  6667. u64 len, u64 orig_start,
  6668. u64 block_start, u64 block_len,
  6669. u64 orig_block_len, u64 ram_bytes,
  6670. int type)
  6671. {
  6672. struct extent_map_tree *em_tree;
  6673. struct extent_map *em;
  6674. struct btrfs_root *root = BTRFS_I(inode)->root;
  6675. int ret;
  6676. em_tree = &BTRFS_I(inode)->extent_tree;
  6677. em = alloc_extent_map();
  6678. if (!em)
  6679. return ERR_PTR(-ENOMEM);
  6680. em->start = start;
  6681. em->orig_start = orig_start;
  6682. em->mod_start = start;
  6683. em->mod_len = len;
  6684. em->len = len;
  6685. em->block_len = block_len;
  6686. em->block_start = block_start;
  6687. em->bdev = root->fs_info->fs_devices->latest_bdev;
  6688. em->orig_block_len = orig_block_len;
  6689. em->ram_bytes = ram_bytes;
  6690. em->generation = -1;
  6691. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  6692. if (type == BTRFS_ORDERED_PREALLOC)
  6693. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  6694. do {
  6695. btrfs_drop_extent_cache(inode, em->start,
  6696. em->start + em->len - 1, 0);
  6697. write_lock(&em_tree->lock);
  6698. ret = add_extent_mapping(em_tree, em, 1);
  6699. write_unlock(&em_tree->lock);
  6700. } while (ret == -EEXIST);
  6701. if (ret) {
  6702. free_extent_map(em);
  6703. return ERR_PTR(ret);
  6704. }
  6705. return em;
  6706. }
  6707. static void adjust_dio_outstanding_extents(struct inode *inode,
  6708. struct btrfs_dio_data *dio_data,
  6709. const u64 len)
  6710. {
  6711. unsigned num_extents;
  6712. num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
  6713. BTRFS_MAX_EXTENT_SIZE);
  6714. /*
  6715. * If we have an outstanding_extents count still set then we're
  6716. * within our reservation, otherwise we need to adjust our inode
  6717. * counter appropriately.
  6718. */
  6719. if (dio_data->outstanding_extents >= num_extents) {
  6720. dio_data->outstanding_extents -= num_extents;
  6721. } else {
  6722. /*
  6723. * If dio write length has been split due to no large enough
  6724. * contiguous space, we need to compensate our inode counter
  6725. * appropriately.
  6726. */
  6727. u64 num_needed = num_extents - dio_data->outstanding_extents;
  6728. spin_lock(&BTRFS_I(inode)->lock);
  6729. BTRFS_I(inode)->outstanding_extents += num_needed;
  6730. spin_unlock(&BTRFS_I(inode)->lock);
  6731. }
  6732. }
  6733. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  6734. struct buffer_head *bh_result, int create)
  6735. {
  6736. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6737. struct extent_map *em;
  6738. struct extent_state *cached_state = NULL;
  6739. struct btrfs_dio_data *dio_data = NULL;
  6740. u64 start = iblock << inode->i_blkbits;
  6741. u64 lockstart, lockend;
  6742. u64 len = bh_result->b_size;
  6743. int unlock_bits = EXTENT_LOCKED;
  6744. int ret = 0;
  6745. if (create)
  6746. unlock_bits |= EXTENT_DIRTY;
  6747. else
  6748. len = min_t(u64, len, fs_info->sectorsize);
  6749. lockstart = start;
  6750. lockend = start + len - 1;
  6751. if (current->journal_info) {
  6752. /*
  6753. * Need to pull our outstanding extents and set journal_info to NULL so
  6754. * that anything that needs to check if there's a transaction doesn't get
  6755. * confused.
  6756. */
  6757. dio_data = current->journal_info;
  6758. current->journal_info = NULL;
  6759. }
  6760. /*
  6761. * If this errors out it's because we couldn't invalidate pagecache for
  6762. * this range and we need to fallback to buffered.
  6763. */
  6764. if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
  6765. create)) {
  6766. ret = -ENOTBLK;
  6767. goto err;
  6768. }
  6769. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  6770. if (IS_ERR(em)) {
  6771. ret = PTR_ERR(em);
  6772. goto unlock_err;
  6773. }
  6774. /*
  6775. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  6776. * io. INLINE is special, and we could probably kludge it in here, but
  6777. * it's still buffered so for safety lets just fall back to the generic
  6778. * buffered path.
  6779. *
  6780. * For COMPRESSED we _have_ to read the entire extent in so we can
  6781. * decompress it, so there will be buffering required no matter what we
  6782. * do, so go ahead and fallback to buffered.
  6783. *
  6784. * We return -ENOTBLK because that's what makes DIO go ahead and go back
  6785. * to buffered IO. Don't blame me, this is the price we pay for using
  6786. * the generic code.
  6787. */
  6788. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  6789. em->block_start == EXTENT_MAP_INLINE) {
  6790. free_extent_map(em);
  6791. ret = -ENOTBLK;
  6792. goto unlock_err;
  6793. }
  6794. /* Just a good old fashioned hole, return */
  6795. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  6796. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  6797. free_extent_map(em);
  6798. goto unlock_err;
  6799. }
  6800. /*
  6801. * We don't allocate a new extent in the following cases
  6802. *
  6803. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  6804. * existing extent.
  6805. * 2) The extent is marked as PREALLOC. We're good to go here and can
  6806. * just use the extent.
  6807. *
  6808. */
  6809. if (!create) {
  6810. len = min(len, em->len - (start - em->start));
  6811. lockstart = start + len;
  6812. goto unlock;
  6813. }
  6814. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  6815. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  6816. em->block_start != EXTENT_MAP_HOLE)) {
  6817. int type;
  6818. u64 block_start, orig_start, orig_block_len, ram_bytes;
  6819. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6820. type = BTRFS_ORDERED_PREALLOC;
  6821. else
  6822. type = BTRFS_ORDERED_NOCOW;
  6823. len = min(len, em->len - (start - em->start));
  6824. block_start = em->block_start + (start - em->start);
  6825. if (can_nocow_extent(inode, start, &len, &orig_start,
  6826. &orig_block_len, &ram_bytes) == 1 &&
  6827. btrfs_inc_nocow_writers(fs_info, block_start)) {
  6828. struct extent_map *em2;
  6829. em2 = btrfs_create_dio_extent(inode, start, len,
  6830. orig_start, block_start,
  6831. len, orig_block_len,
  6832. ram_bytes, type);
  6833. btrfs_dec_nocow_writers(fs_info, block_start);
  6834. if (type == BTRFS_ORDERED_PREALLOC) {
  6835. free_extent_map(em);
  6836. em = em2;
  6837. }
  6838. if (em2 && IS_ERR(em2)) {
  6839. ret = PTR_ERR(em2);
  6840. goto unlock_err;
  6841. }
  6842. /*
  6843. * For inode marked NODATACOW or extent marked PREALLOC,
  6844. * use the existing or preallocated extent, so does not
  6845. * need to adjust btrfs_space_info's bytes_may_use.
  6846. */
  6847. btrfs_free_reserved_data_space_noquota(inode,
  6848. start, len);
  6849. goto unlock;
  6850. }
  6851. }
  6852. /*
  6853. * this will cow the extent, reset the len in case we changed
  6854. * it above
  6855. */
  6856. len = bh_result->b_size;
  6857. free_extent_map(em);
  6858. em = btrfs_new_extent_direct(inode, start, len);
  6859. if (IS_ERR(em)) {
  6860. ret = PTR_ERR(em);
  6861. goto unlock_err;
  6862. }
  6863. len = min(len, em->len - (start - em->start));
  6864. unlock:
  6865. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6866. inode->i_blkbits;
  6867. bh_result->b_size = len;
  6868. bh_result->b_bdev = em->bdev;
  6869. set_buffer_mapped(bh_result);
  6870. if (create) {
  6871. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6872. set_buffer_new(bh_result);
  6873. /*
  6874. * Need to update the i_size under the extent lock so buffered
  6875. * readers will get the updated i_size when we unlock.
  6876. */
  6877. if (start + len > i_size_read(inode))
  6878. i_size_write(inode, start + len);
  6879. adjust_dio_outstanding_extents(inode, dio_data, len);
  6880. WARN_ON(dio_data->reserve < len);
  6881. dio_data->reserve -= len;
  6882. dio_data->unsubmitted_oe_range_end = start + len;
  6883. current->journal_info = dio_data;
  6884. }
  6885. /*
  6886. * In the case of write we need to clear and unlock the entire range,
  6887. * in the case of read we need to unlock only the end area that we
  6888. * aren't using if there is any left over space.
  6889. */
  6890. if (lockstart < lockend) {
  6891. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6892. lockend, unlock_bits, 1, 0,
  6893. &cached_state, GFP_NOFS);
  6894. } else {
  6895. free_extent_state(cached_state);
  6896. }
  6897. free_extent_map(em);
  6898. return 0;
  6899. unlock_err:
  6900. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6901. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  6902. err:
  6903. if (dio_data)
  6904. current->journal_info = dio_data;
  6905. /*
  6906. * Compensate the delalloc release we do in btrfs_direct_IO() when we
  6907. * write less data then expected, so that we don't underflow our inode's
  6908. * outstanding extents counter.
  6909. */
  6910. if (create && dio_data)
  6911. adjust_dio_outstanding_extents(inode, dio_data, len);
  6912. return ret;
  6913. }
  6914. static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
  6915. int mirror_num)
  6916. {
  6917. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6918. int ret;
  6919. BUG_ON(bio_op(bio) == REQ_OP_WRITE);
  6920. bio_get(bio);
  6921. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
  6922. if (ret)
  6923. goto err;
  6924. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  6925. err:
  6926. bio_put(bio);
  6927. return ret;
  6928. }
  6929. static int btrfs_check_dio_repairable(struct inode *inode,
  6930. struct bio *failed_bio,
  6931. struct io_failure_record *failrec,
  6932. int failed_mirror)
  6933. {
  6934. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6935. int num_copies;
  6936. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  6937. if (num_copies == 1) {
  6938. /*
  6939. * we only have a single copy of the data, so don't bother with
  6940. * all the retry and error correction code that follows. no
  6941. * matter what the error is, it is very likely to persist.
  6942. */
  6943. btrfs_debug(fs_info,
  6944. "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  6945. num_copies, failrec->this_mirror, failed_mirror);
  6946. return 0;
  6947. }
  6948. failrec->failed_mirror = failed_mirror;
  6949. failrec->this_mirror++;
  6950. if (failrec->this_mirror == failed_mirror)
  6951. failrec->this_mirror++;
  6952. if (failrec->this_mirror > num_copies) {
  6953. btrfs_debug(fs_info,
  6954. "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  6955. num_copies, failrec->this_mirror, failed_mirror);
  6956. return 0;
  6957. }
  6958. return 1;
  6959. }
  6960. static int dio_read_error(struct inode *inode, struct bio *failed_bio,
  6961. struct page *page, unsigned int pgoff,
  6962. u64 start, u64 end, int failed_mirror,
  6963. bio_end_io_t *repair_endio, void *repair_arg)
  6964. {
  6965. struct io_failure_record *failrec;
  6966. struct bio *bio;
  6967. int isector;
  6968. int read_mode = 0;
  6969. int ret;
  6970. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  6971. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  6972. if (ret)
  6973. return ret;
  6974. ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
  6975. failed_mirror);
  6976. if (!ret) {
  6977. free_io_failure(inode, failrec);
  6978. return -EIO;
  6979. }
  6980. if ((failed_bio->bi_vcnt > 1)
  6981. || (failed_bio->bi_io_vec->bv_len
  6982. > btrfs_inode_sectorsize(inode)))
  6983. read_mode |= REQ_FAILFAST_DEV;
  6984. isector = start - btrfs_io_bio(failed_bio)->logical;
  6985. isector >>= inode->i_sb->s_blocksize_bits;
  6986. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  6987. pgoff, isector, repair_endio, repair_arg);
  6988. if (!bio) {
  6989. free_io_failure(inode, failrec);
  6990. return -EIO;
  6991. }
  6992. bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
  6993. btrfs_debug(BTRFS_I(inode)->root->fs_info,
  6994. "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
  6995. read_mode, failrec->this_mirror, failrec->in_validation);
  6996. ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
  6997. if (ret) {
  6998. free_io_failure(inode, failrec);
  6999. bio_put(bio);
  7000. }
  7001. return ret;
  7002. }
  7003. struct btrfs_retry_complete {
  7004. struct completion done;
  7005. struct inode *inode;
  7006. u64 start;
  7007. int uptodate;
  7008. };
  7009. static void btrfs_retry_endio_nocsum(struct bio *bio)
  7010. {
  7011. struct btrfs_retry_complete *done = bio->bi_private;
  7012. struct inode *inode;
  7013. struct bio_vec *bvec;
  7014. int i;
  7015. if (bio->bi_error)
  7016. goto end;
  7017. ASSERT(bio->bi_vcnt == 1);
  7018. inode = bio->bi_io_vec->bv_page->mapping->host;
  7019. ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
  7020. done->uptodate = 1;
  7021. bio_for_each_segment_all(bvec, bio, i)
  7022. clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
  7023. end:
  7024. complete(&done->done);
  7025. bio_put(bio);
  7026. }
  7027. static int __btrfs_correct_data_nocsum(struct inode *inode,
  7028. struct btrfs_io_bio *io_bio)
  7029. {
  7030. struct btrfs_fs_info *fs_info;
  7031. struct bio_vec *bvec;
  7032. struct btrfs_retry_complete done;
  7033. u64 start;
  7034. unsigned int pgoff;
  7035. u32 sectorsize;
  7036. int nr_sectors;
  7037. int i;
  7038. int ret;
  7039. fs_info = BTRFS_I(inode)->root->fs_info;
  7040. sectorsize = fs_info->sectorsize;
  7041. start = io_bio->logical;
  7042. done.inode = inode;
  7043. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  7044. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
  7045. pgoff = bvec->bv_offset;
  7046. next_block_or_try_again:
  7047. done.uptodate = 0;
  7048. done.start = start;
  7049. init_completion(&done.done);
  7050. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
  7051. pgoff, start, start + sectorsize - 1,
  7052. io_bio->mirror_num,
  7053. btrfs_retry_endio_nocsum, &done);
  7054. if (ret)
  7055. return ret;
  7056. wait_for_completion(&done.done);
  7057. if (!done.uptodate) {
  7058. /* We might have another mirror, so try again */
  7059. goto next_block_or_try_again;
  7060. }
  7061. start += sectorsize;
  7062. if (nr_sectors--) {
  7063. pgoff += sectorsize;
  7064. goto next_block_or_try_again;
  7065. }
  7066. }
  7067. return 0;
  7068. }
  7069. static void btrfs_retry_endio(struct bio *bio)
  7070. {
  7071. struct btrfs_retry_complete *done = bio->bi_private;
  7072. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7073. struct inode *inode;
  7074. struct bio_vec *bvec;
  7075. u64 start;
  7076. int uptodate;
  7077. int ret;
  7078. int i;
  7079. if (bio->bi_error)
  7080. goto end;
  7081. uptodate = 1;
  7082. start = done->start;
  7083. ASSERT(bio->bi_vcnt == 1);
  7084. inode = bio->bi_io_vec->bv_page->mapping->host;
  7085. ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
  7086. bio_for_each_segment_all(bvec, bio, i) {
  7087. ret = __readpage_endio_check(done->inode, io_bio, i,
  7088. bvec->bv_page, bvec->bv_offset,
  7089. done->start, bvec->bv_len);
  7090. if (!ret)
  7091. clean_io_failure(done->inode, done->start,
  7092. bvec->bv_page, bvec->bv_offset);
  7093. else
  7094. uptodate = 0;
  7095. }
  7096. done->uptodate = uptodate;
  7097. end:
  7098. complete(&done->done);
  7099. bio_put(bio);
  7100. }
  7101. static int __btrfs_subio_endio_read(struct inode *inode,
  7102. struct btrfs_io_bio *io_bio, int err)
  7103. {
  7104. struct btrfs_fs_info *fs_info;
  7105. struct bio_vec *bvec;
  7106. struct btrfs_retry_complete done;
  7107. u64 start;
  7108. u64 offset = 0;
  7109. u32 sectorsize;
  7110. int nr_sectors;
  7111. unsigned int pgoff;
  7112. int csum_pos;
  7113. int i;
  7114. int ret;
  7115. fs_info = BTRFS_I(inode)->root->fs_info;
  7116. sectorsize = fs_info->sectorsize;
  7117. err = 0;
  7118. start = io_bio->logical;
  7119. done.inode = inode;
  7120. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  7121. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
  7122. pgoff = bvec->bv_offset;
  7123. next_block:
  7124. csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
  7125. ret = __readpage_endio_check(inode, io_bio, csum_pos,
  7126. bvec->bv_page, pgoff, start,
  7127. sectorsize);
  7128. if (likely(!ret))
  7129. goto next;
  7130. try_again:
  7131. done.uptodate = 0;
  7132. done.start = start;
  7133. init_completion(&done.done);
  7134. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
  7135. pgoff, start, start + sectorsize - 1,
  7136. io_bio->mirror_num,
  7137. btrfs_retry_endio, &done);
  7138. if (ret) {
  7139. err = ret;
  7140. goto next;
  7141. }
  7142. wait_for_completion(&done.done);
  7143. if (!done.uptodate) {
  7144. /* We might have another mirror, so try again */
  7145. goto try_again;
  7146. }
  7147. next:
  7148. offset += sectorsize;
  7149. start += sectorsize;
  7150. ASSERT(nr_sectors);
  7151. if (--nr_sectors) {
  7152. pgoff += sectorsize;
  7153. goto next_block;
  7154. }
  7155. }
  7156. return err;
  7157. }
  7158. static int btrfs_subio_endio_read(struct inode *inode,
  7159. struct btrfs_io_bio *io_bio, int err)
  7160. {
  7161. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  7162. if (skip_csum) {
  7163. if (unlikely(err))
  7164. return __btrfs_correct_data_nocsum(inode, io_bio);
  7165. else
  7166. return 0;
  7167. } else {
  7168. return __btrfs_subio_endio_read(inode, io_bio, err);
  7169. }
  7170. }
  7171. static void btrfs_endio_direct_read(struct bio *bio)
  7172. {
  7173. struct btrfs_dio_private *dip = bio->bi_private;
  7174. struct inode *inode = dip->inode;
  7175. struct bio *dio_bio;
  7176. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7177. int err = bio->bi_error;
  7178. if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
  7179. err = btrfs_subio_endio_read(inode, io_bio, err);
  7180. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  7181. dip->logical_offset + dip->bytes - 1);
  7182. dio_bio = dip->dio_bio;
  7183. kfree(dip);
  7184. dio_bio->bi_error = bio->bi_error;
  7185. dio_end_io(dio_bio, bio->bi_error);
  7186. if (io_bio->end_io)
  7187. io_bio->end_io(io_bio, err);
  7188. bio_put(bio);
  7189. }
  7190. static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
  7191. const u64 offset,
  7192. const u64 bytes,
  7193. const int uptodate)
  7194. {
  7195. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7196. struct btrfs_ordered_extent *ordered = NULL;
  7197. u64 ordered_offset = offset;
  7198. u64 ordered_bytes = bytes;
  7199. int ret;
  7200. again:
  7201. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  7202. &ordered_offset,
  7203. ordered_bytes,
  7204. uptodate);
  7205. if (!ret)
  7206. goto out_test;
  7207. btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
  7208. finish_ordered_fn, NULL, NULL);
  7209. btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
  7210. out_test:
  7211. /*
  7212. * our bio might span multiple ordered extents. If we haven't
  7213. * completed the accounting for the whole dio, go back and try again
  7214. */
  7215. if (ordered_offset < offset + bytes) {
  7216. ordered_bytes = offset + bytes - ordered_offset;
  7217. ordered = NULL;
  7218. goto again;
  7219. }
  7220. }
  7221. static void btrfs_endio_direct_write(struct bio *bio)
  7222. {
  7223. struct btrfs_dio_private *dip = bio->bi_private;
  7224. struct bio *dio_bio = dip->dio_bio;
  7225. btrfs_endio_direct_write_update_ordered(dip->inode,
  7226. dip->logical_offset,
  7227. dip->bytes,
  7228. !bio->bi_error);
  7229. kfree(dip);
  7230. dio_bio->bi_error = bio->bi_error;
  7231. dio_end_io(dio_bio, bio->bi_error);
  7232. bio_put(bio);
  7233. }
  7234. static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
  7235. struct bio *bio, int mirror_num,
  7236. unsigned long bio_flags, u64 offset)
  7237. {
  7238. int ret;
  7239. ret = btrfs_csum_one_bio(inode, bio, offset, 1);
  7240. BUG_ON(ret); /* -ENOMEM */
  7241. return 0;
  7242. }
  7243. static void btrfs_end_dio_bio(struct bio *bio)
  7244. {
  7245. struct btrfs_dio_private *dip = bio->bi_private;
  7246. int err = bio->bi_error;
  7247. if (err)
  7248. btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
  7249. "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
  7250. btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
  7251. (unsigned long long)bio->bi_iter.bi_sector,
  7252. bio->bi_iter.bi_size, err);
  7253. if (dip->subio_endio)
  7254. err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
  7255. if (err) {
  7256. dip->errors = 1;
  7257. /*
  7258. * before atomic variable goto zero, we must make sure
  7259. * dip->errors is perceived to be set.
  7260. */
  7261. smp_mb__before_atomic();
  7262. }
  7263. /* if there are more bios still pending for this dio, just exit */
  7264. if (!atomic_dec_and_test(&dip->pending_bios))
  7265. goto out;
  7266. if (dip->errors) {
  7267. bio_io_error(dip->orig_bio);
  7268. } else {
  7269. dip->dio_bio->bi_error = 0;
  7270. bio_endio(dip->orig_bio);
  7271. }
  7272. out:
  7273. bio_put(bio);
  7274. }
  7275. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  7276. u64 first_sector, gfp_t gfp_flags)
  7277. {
  7278. struct bio *bio;
  7279. bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
  7280. if (bio)
  7281. bio_associate_current(bio);
  7282. return bio;
  7283. }
  7284. static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
  7285. struct btrfs_dio_private *dip,
  7286. struct bio *bio,
  7287. u64 file_offset)
  7288. {
  7289. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7290. struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
  7291. int ret;
  7292. /*
  7293. * We load all the csum data we need when we submit
  7294. * the first bio to reduce the csum tree search and
  7295. * contention.
  7296. */
  7297. if (dip->logical_offset == file_offset) {
  7298. ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
  7299. file_offset);
  7300. if (ret)
  7301. return ret;
  7302. }
  7303. if (bio == dip->orig_bio)
  7304. return 0;
  7305. file_offset -= dip->logical_offset;
  7306. file_offset >>= inode->i_sb->s_blocksize_bits;
  7307. io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
  7308. return 0;
  7309. }
  7310. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  7311. u64 file_offset, int skip_sum,
  7312. int async_submit)
  7313. {
  7314. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7315. struct btrfs_dio_private *dip = bio->bi_private;
  7316. bool write = bio_op(bio) == REQ_OP_WRITE;
  7317. int ret;
  7318. if (async_submit)
  7319. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  7320. bio_get(bio);
  7321. if (!write) {
  7322. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
  7323. if (ret)
  7324. goto err;
  7325. }
  7326. if (skip_sum)
  7327. goto map;
  7328. if (write && async_submit) {
  7329. ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
  7330. file_offset,
  7331. __btrfs_submit_bio_start_direct_io,
  7332. __btrfs_submit_bio_done);
  7333. goto err;
  7334. } else if (write) {
  7335. /*
  7336. * If we aren't doing async submit, calculate the csum of the
  7337. * bio now.
  7338. */
  7339. ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
  7340. if (ret)
  7341. goto err;
  7342. } else {
  7343. ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
  7344. file_offset);
  7345. if (ret)
  7346. goto err;
  7347. }
  7348. map:
  7349. ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
  7350. err:
  7351. bio_put(bio);
  7352. return ret;
  7353. }
  7354. static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
  7355. int skip_sum)
  7356. {
  7357. struct inode *inode = dip->inode;
  7358. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7359. struct btrfs_root *root = BTRFS_I(inode)->root;
  7360. struct bio *bio;
  7361. struct bio *orig_bio = dip->orig_bio;
  7362. struct bio_vec *bvec;
  7363. u64 start_sector = orig_bio->bi_iter.bi_sector;
  7364. u64 file_offset = dip->logical_offset;
  7365. u64 submit_len = 0;
  7366. u64 map_length;
  7367. u32 blocksize = fs_info->sectorsize;
  7368. int async_submit = 0;
  7369. int nr_sectors;
  7370. int ret;
  7371. int i, j;
  7372. map_length = orig_bio->bi_iter.bi_size;
  7373. ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
  7374. &map_length, NULL, 0);
  7375. if (ret)
  7376. return -EIO;
  7377. if (map_length >= orig_bio->bi_iter.bi_size) {
  7378. bio = orig_bio;
  7379. dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
  7380. goto submit;
  7381. }
  7382. /* async crcs make it difficult to collect full stripe writes. */
  7383. if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
  7384. async_submit = 0;
  7385. else
  7386. async_submit = 1;
  7387. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  7388. if (!bio)
  7389. return -ENOMEM;
  7390. bio->bi_opf = orig_bio->bi_opf;
  7391. bio->bi_private = dip;
  7392. bio->bi_end_io = btrfs_end_dio_bio;
  7393. btrfs_io_bio(bio)->logical = file_offset;
  7394. atomic_inc(&dip->pending_bios);
  7395. bio_for_each_segment_all(bvec, orig_bio, j) {
  7396. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
  7397. i = 0;
  7398. next_block:
  7399. if (unlikely(map_length < submit_len + blocksize ||
  7400. bio_add_page(bio, bvec->bv_page, blocksize,
  7401. bvec->bv_offset + (i * blocksize)) < blocksize)) {
  7402. /*
  7403. * inc the count before we submit the bio so
  7404. * we know the end IO handler won't happen before
  7405. * we inc the count. Otherwise, the dip might get freed
  7406. * before we're done setting it up
  7407. */
  7408. atomic_inc(&dip->pending_bios);
  7409. ret = __btrfs_submit_dio_bio(bio, inode,
  7410. file_offset, skip_sum,
  7411. async_submit);
  7412. if (ret) {
  7413. bio_put(bio);
  7414. atomic_dec(&dip->pending_bios);
  7415. goto out_err;
  7416. }
  7417. start_sector += submit_len >> 9;
  7418. file_offset += submit_len;
  7419. submit_len = 0;
  7420. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  7421. start_sector, GFP_NOFS);
  7422. if (!bio)
  7423. goto out_err;
  7424. bio->bi_opf = orig_bio->bi_opf;
  7425. bio->bi_private = dip;
  7426. bio->bi_end_io = btrfs_end_dio_bio;
  7427. btrfs_io_bio(bio)->logical = file_offset;
  7428. map_length = orig_bio->bi_iter.bi_size;
  7429. ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
  7430. start_sector << 9,
  7431. &map_length, NULL, 0);
  7432. if (ret) {
  7433. bio_put(bio);
  7434. goto out_err;
  7435. }
  7436. goto next_block;
  7437. } else {
  7438. submit_len += blocksize;
  7439. if (--nr_sectors) {
  7440. i++;
  7441. goto next_block;
  7442. }
  7443. }
  7444. }
  7445. submit:
  7446. ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
  7447. async_submit);
  7448. if (!ret)
  7449. return 0;
  7450. bio_put(bio);
  7451. out_err:
  7452. dip->errors = 1;
  7453. /*
  7454. * before atomic variable goto zero, we must
  7455. * make sure dip->errors is perceived to be set.
  7456. */
  7457. smp_mb__before_atomic();
  7458. if (atomic_dec_and_test(&dip->pending_bios))
  7459. bio_io_error(dip->orig_bio);
  7460. /* bio_end_io() will handle error, so we needn't return it */
  7461. return 0;
  7462. }
  7463. static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
  7464. loff_t file_offset)
  7465. {
  7466. struct btrfs_dio_private *dip = NULL;
  7467. struct bio *io_bio = NULL;
  7468. struct btrfs_io_bio *btrfs_bio;
  7469. int skip_sum;
  7470. bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
  7471. int ret = 0;
  7472. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  7473. io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
  7474. if (!io_bio) {
  7475. ret = -ENOMEM;
  7476. goto free_ordered;
  7477. }
  7478. dip = kzalloc(sizeof(*dip), GFP_NOFS);
  7479. if (!dip) {
  7480. ret = -ENOMEM;
  7481. goto free_ordered;
  7482. }
  7483. dip->private = dio_bio->bi_private;
  7484. dip->inode = inode;
  7485. dip->logical_offset = file_offset;
  7486. dip->bytes = dio_bio->bi_iter.bi_size;
  7487. dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
  7488. io_bio->bi_private = dip;
  7489. dip->orig_bio = io_bio;
  7490. dip->dio_bio = dio_bio;
  7491. atomic_set(&dip->pending_bios, 0);
  7492. btrfs_bio = btrfs_io_bio(io_bio);
  7493. btrfs_bio->logical = file_offset;
  7494. if (write) {
  7495. io_bio->bi_end_io = btrfs_endio_direct_write;
  7496. } else {
  7497. io_bio->bi_end_io = btrfs_endio_direct_read;
  7498. dip->subio_endio = btrfs_subio_endio_read;
  7499. }
  7500. /*
  7501. * Reset the range for unsubmitted ordered extents (to a 0 length range)
  7502. * even if we fail to submit a bio, because in such case we do the
  7503. * corresponding error handling below and it must not be done a second
  7504. * time by btrfs_direct_IO().
  7505. */
  7506. if (write) {
  7507. struct btrfs_dio_data *dio_data = current->journal_info;
  7508. dio_data->unsubmitted_oe_range_end = dip->logical_offset +
  7509. dip->bytes;
  7510. dio_data->unsubmitted_oe_range_start =
  7511. dio_data->unsubmitted_oe_range_end;
  7512. }
  7513. ret = btrfs_submit_direct_hook(dip, skip_sum);
  7514. if (!ret)
  7515. return;
  7516. if (btrfs_bio->end_io)
  7517. btrfs_bio->end_io(btrfs_bio, ret);
  7518. free_ordered:
  7519. /*
  7520. * If we arrived here it means either we failed to submit the dip
  7521. * or we either failed to clone the dio_bio or failed to allocate the
  7522. * dip. If we cloned the dio_bio and allocated the dip, we can just
  7523. * call bio_endio against our io_bio so that we get proper resource
  7524. * cleanup if we fail to submit the dip, otherwise, we must do the
  7525. * same as btrfs_endio_direct_[write|read] because we can't call these
  7526. * callbacks - they require an allocated dip and a clone of dio_bio.
  7527. */
  7528. if (io_bio && dip) {
  7529. io_bio->bi_error = -EIO;
  7530. bio_endio(io_bio);
  7531. /*
  7532. * The end io callbacks free our dip, do the final put on io_bio
  7533. * and all the cleanup and final put for dio_bio (through
  7534. * dio_end_io()).
  7535. */
  7536. dip = NULL;
  7537. io_bio = NULL;
  7538. } else {
  7539. if (write)
  7540. btrfs_endio_direct_write_update_ordered(inode,
  7541. file_offset,
  7542. dio_bio->bi_iter.bi_size,
  7543. 0);
  7544. else
  7545. unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
  7546. file_offset + dio_bio->bi_iter.bi_size - 1);
  7547. dio_bio->bi_error = -EIO;
  7548. /*
  7549. * Releases and cleans up our dio_bio, no need to bio_put()
  7550. * nor bio_endio()/bio_io_error() against dio_bio.
  7551. */
  7552. dio_end_io(dio_bio, ret);
  7553. }
  7554. if (io_bio)
  7555. bio_put(io_bio);
  7556. kfree(dip);
  7557. }
  7558. static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
  7559. struct kiocb *iocb,
  7560. const struct iov_iter *iter, loff_t offset)
  7561. {
  7562. int seg;
  7563. int i;
  7564. unsigned int blocksize_mask = fs_info->sectorsize - 1;
  7565. ssize_t retval = -EINVAL;
  7566. if (offset & blocksize_mask)
  7567. goto out;
  7568. if (iov_iter_alignment(iter) & blocksize_mask)
  7569. goto out;
  7570. /* If this is a write we don't need to check anymore */
  7571. if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
  7572. return 0;
  7573. /*
  7574. * Check to make sure we don't have duplicate iov_base's in this
  7575. * iovec, if so return EINVAL, otherwise we'll get csum errors
  7576. * when reading back.
  7577. */
  7578. for (seg = 0; seg < iter->nr_segs; seg++) {
  7579. for (i = seg + 1; i < iter->nr_segs; i++) {
  7580. if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
  7581. goto out;
  7582. }
  7583. }
  7584. retval = 0;
  7585. out:
  7586. return retval;
  7587. }
  7588. static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  7589. {
  7590. struct file *file = iocb->ki_filp;
  7591. struct inode *inode = file->f_mapping->host;
  7592. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7593. struct btrfs_dio_data dio_data = { 0 };
  7594. loff_t offset = iocb->ki_pos;
  7595. size_t count = 0;
  7596. int flags = 0;
  7597. bool wakeup = true;
  7598. bool relock = false;
  7599. ssize_t ret;
  7600. if (check_direct_IO(fs_info, iocb, iter, offset))
  7601. return 0;
  7602. inode_dio_begin(inode);
  7603. smp_mb__after_atomic();
  7604. /*
  7605. * The generic stuff only does filemap_write_and_wait_range, which
  7606. * isn't enough if we've written compressed pages to this area, so
  7607. * we need to flush the dirty pages again to make absolutely sure
  7608. * that any outstanding dirty pages are on disk.
  7609. */
  7610. count = iov_iter_count(iter);
  7611. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  7612. &BTRFS_I(inode)->runtime_flags))
  7613. filemap_fdatawrite_range(inode->i_mapping, offset,
  7614. offset + count - 1);
  7615. if (iov_iter_rw(iter) == WRITE) {
  7616. /*
  7617. * If the write DIO is beyond the EOF, we need update
  7618. * the isize, but it is protected by i_mutex. So we can
  7619. * not unlock the i_mutex at this case.
  7620. */
  7621. if (offset + count <= inode->i_size) {
  7622. inode_unlock(inode);
  7623. relock = true;
  7624. }
  7625. ret = btrfs_delalloc_reserve_space(inode, offset, count);
  7626. if (ret)
  7627. goto out;
  7628. dio_data.outstanding_extents = div64_u64(count +
  7629. BTRFS_MAX_EXTENT_SIZE - 1,
  7630. BTRFS_MAX_EXTENT_SIZE);
  7631. /*
  7632. * We need to know how many extents we reserved so that we can
  7633. * do the accounting properly if we go over the number we
  7634. * originally calculated. Abuse current->journal_info for this.
  7635. */
  7636. dio_data.reserve = round_up(count,
  7637. fs_info->sectorsize);
  7638. dio_data.unsubmitted_oe_range_start = (u64)offset;
  7639. dio_data.unsubmitted_oe_range_end = (u64)offset;
  7640. current->journal_info = &dio_data;
  7641. down_read(&BTRFS_I(inode)->dio_sem);
  7642. } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  7643. &BTRFS_I(inode)->runtime_flags)) {
  7644. inode_dio_end(inode);
  7645. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  7646. wakeup = false;
  7647. }
  7648. ret = __blockdev_direct_IO(iocb, inode,
  7649. fs_info->fs_devices->latest_bdev,
  7650. iter, btrfs_get_blocks_direct, NULL,
  7651. btrfs_submit_direct, flags);
  7652. if (iov_iter_rw(iter) == WRITE) {
  7653. up_read(&BTRFS_I(inode)->dio_sem);
  7654. current->journal_info = NULL;
  7655. if (ret < 0 && ret != -EIOCBQUEUED) {
  7656. if (dio_data.reserve)
  7657. btrfs_delalloc_release_space(inode, offset,
  7658. dio_data.reserve);
  7659. /*
  7660. * On error we might have left some ordered extents
  7661. * without submitting corresponding bios for them, so
  7662. * cleanup them up to avoid other tasks getting them
  7663. * and waiting for them to complete forever.
  7664. */
  7665. if (dio_data.unsubmitted_oe_range_start <
  7666. dio_data.unsubmitted_oe_range_end)
  7667. btrfs_endio_direct_write_update_ordered(inode,
  7668. dio_data.unsubmitted_oe_range_start,
  7669. dio_data.unsubmitted_oe_range_end -
  7670. dio_data.unsubmitted_oe_range_start,
  7671. 0);
  7672. } else if (ret >= 0 && (size_t)ret < count)
  7673. btrfs_delalloc_release_space(inode, offset,
  7674. count - (size_t)ret);
  7675. }
  7676. out:
  7677. if (wakeup)
  7678. inode_dio_end(inode);
  7679. if (relock)
  7680. inode_lock(inode);
  7681. return ret;
  7682. }
  7683. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  7684. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  7685. __u64 start, __u64 len)
  7686. {
  7687. int ret;
  7688. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  7689. if (ret)
  7690. return ret;
  7691. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  7692. }
  7693. int btrfs_readpage(struct file *file, struct page *page)
  7694. {
  7695. struct extent_io_tree *tree;
  7696. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7697. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  7698. }
  7699. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  7700. {
  7701. struct extent_io_tree *tree;
  7702. struct inode *inode = page->mapping->host;
  7703. int ret;
  7704. if (current->flags & PF_MEMALLOC) {
  7705. redirty_page_for_writepage(wbc, page);
  7706. unlock_page(page);
  7707. return 0;
  7708. }
  7709. /*
  7710. * If we are under memory pressure we will call this directly from the
  7711. * VM, we need to make sure we have the inode referenced for the ordered
  7712. * extent. If not just return like we didn't do anything.
  7713. */
  7714. if (!igrab(inode)) {
  7715. redirty_page_for_writepage(wbc, page);
  7716. return AOP_WRITEPAGE_ACTIVATE;
  7717. }
  7718. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7719. ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  7720. btrfs_add_delayed_iput(inode);
  7721. return ret;
  7722. }
  7723. static int btrfs_writepages(struct address_space *mapping,
  7724. struct writeback_control *wbc)
  7725. {
  7726. struct extent_io_tree *tree;
  7727. tree = &BTRFS_I(mapping->host)->io_tree;
  7728. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  7729. }
  7730. static int
  7731. btrfs_readpages(struct file *file, struct address_space *mapping,
  7732. struct list_head *pages, unsigned nr_pages)
  7733. {
  7734. struct extent_io_tree *tree;
  7735. tree = &BTRFS_I(mapping->host)->io_tree;
  7736. return extent_readpages(tree, mapping, pages, nr_pages,
  7737. btrfs_get_extent);
  7738. }
  7739. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7740. {
  7741. struct extent_io_tree *tree;
  7742. struct extent_map_tree *map;
  7743. int ret;
  7744. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7745. map = &BTRFS_I(page->mapping->host)->extent_tree;
  7746. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  7747. if (ret == 1) {
  7748. ClearPagePrivate(page);
  7749. set_page_private(page, 0);
  7750. put_page(page);
  7751. }
  7752. return ret;
  7753. }
  7754. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7755. {
  7756. if (PageWriteback(page) || PageDirty(page))
  7757. return 0;
  7758. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  7759. }
  7760. static void btrfs_invalidatepage(struct page *page, unsigned int offset,
  7761. unsigned int length)
  7762. {
  7763. struct inode *inode = page->mapping->host;
  7764. struct extent_io_tree *tree;
  7765. struct btrfs_ordered_extent *ordered;
  7766. struct extent_state *cached_state = NULL;
  7767. u64 page_start = page_offset(page);
  7768. u64 page_end = page_start + PAGE_SIZE - 1;
  7769. u64 start;
  7770. u64 end;
  7771. int inode_evicting = inode->i_state & I_FREEING;
  7772. /*
  7773. * we have the page locked, so new writeback can't start,
  7774. * and the dirty bit won't be cleared while we are here.
  7775. *
  7776. * Wait for IO on this page so that we can safely clear
  7777. * the PagePrivate2 bit and do ordered accounting
  7778. */
  7779. wait_on_page_writeback(page);
  7780. tree = &BTRFS_I(inode)->io_tree;
  7781. if (offset) {
  7782. btrfs_releasepage(page, GFP_NOFS);
  7783. return;
  7784. }
  7785. if (!inode_evicting)
  7786. lock_extent_bits(tree, page_start, page_end, &cached_state);
  7787. again:
  7788. start = page_start;
  7789. ordered = btrfs_lookup_ordered_range(inode, start,
  7790. page_end - start + 1);
  7791. if (ordered) {
  7792. end = min(page_end, ordered->file_offset + ordered->len - 1);
  7793. /*
  7794. * IO on this page will never be started, so we need
  7795. * to account for any ordered extents now
  7796. */
  7797. if (!inode_evicting)
  7798. clear_extent_bit(tree, start, end,
  7799. EXTENT_DIRTY | EXTENT_DELALLOC |
  7800. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  7801. EXTENT_DEFRAG, 1, 0, &cached_state,
  7802. GFP_NOFS);
  7803. /*
  7804. * whoever cleared the private bit is responsible
  7805. * for the finish_ordered_io
  7806. */
  7807. if (TestClearPagePrivate2(page)) {
  7808. struct btrfs_ordered_inode_tree *tree;
  7809. u64 new_len;
  7810. tree = &BTRFS_I(inode)->ordered_tree;
  7811. spin_lock_irq(&tree->lock);
  7812. set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
  7813. new_len = start - ordered->file_offset;
  7814. if (new_len < ordered->truncated_len)
  7815. ordered->truncated_len = new_len;
  7816. spin_unlock_irq(&tree->lock);
  7817. if (btrfs_dec_test_ordered_pending(inode, &ordered,
  7818. start,
  7819. end - start + 1, 1))
  7820. btrfs_finish_ordered_io(ordered);
  7821. }
  7822. btrfs_put_ordered_extent(ordered);
  7823. if (!inode_evicting) {
  7824. cached_state = NULL;
  7825. lock_extent_bits(tree, start, end,
  7826. &cached_state);
  7827. }
  7828. start = end + 1;
  7829. if (start < page_end)
  7830. goto again;
  7831. }
  7832. /*
  7833. * Qgroup reserved space handler
  7834. * Page here will be either
  7835. * 1) Already written to disk
  7836. * In this case, its reserved space is released from data rsv map
  7837. * and will be freed by delayed_ref handler finally.
  7838. * So even we call qgroup_free_data(), it won't decrease reserved
  7839. * space.
  7840. * 2) Not written to disk
  7841. * This means the reserved space should be freed here. However,
  7842. * if a truncate invalidates the page (by clearing PageDirty)
  7843. * and the page is accounted for while allocating extent
  7844. * in btrfs_check_data_free_space() we let delayed_ref to
  7845. * free the entire extent.
  7846. */
  7847. if (PageDirty(page))
  7848. btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
  7849. if (!inode_evicting) {
  7850. clear_extent_bit(tree, page_start, page_end,
  7851. EXTENT_LOCKED | EXTENT_DIRTY |
  7852. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  7853. EXTENT_DEFRAG, 1, 1,
  7854. &cached_state, GFP_NOFS);
  7855. __btrfs_releasepage(page, GFP_NOFS);
  7856. }
  7857. ClearPageChecked(page);
  7858. if (PagePrivate(page)) {
  7859. ClearPagePrivate(page);
  7860. set_page_private(page, 0);
  7861. put_page(page);
  7862. }
  7863. }
  7864. /*
  7865. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  7866. * called from a page fault handler when a page is first dirtied. Hence we must
  7867. * be careful to check for EOF conditions here. We set the page up correctly
  7868. * for a written page which means we get ENOSPC checking when writing into
  7869. * holes and correct delalloc and unwritten extent mapping on filesystems that
  7870. * support these features.
  7871. *
  7872. * We are not allowed to take the i_mutex here so we have to play games to
  7873. * protect against truncate races as the page could now be beyond EOF. Because
  7874. * vmtruncate() writes the inode size before removing pages, once we have the
  7875. * page lock we can determine safely if the page is beyond EOF. If it is not
  7876. * beyond EOF, then the page is guaranteed safe against truncation until we
  7877. * unlock the page.
  7878. */
  7879. int btrfs_page_mkwrite(struct vm_fault *vmf)
  7880. {
  7881. struct page *page = vmf->page;
  7882. struct inode *inode = file_inode(vmf->vma->vm_file);
  7883. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7884. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  7885. struct btrfs_ordered_extent *ordered;
  7886. struct extent_state *cached_state = NULL;
  7887. char *kaddr;
  7888. unsigned long zero_start;
  7889. loff_t size;
  7890. int ret;
  7891. int reserved = 0;
  7892. u64 reserved_space;
  7893. u64 page_start;
  7894. u64 page_end;
  7895. u64 end;
  7896. reserved_space = PAGE_SIZE;
  7897. sb_start_pagefault(inode->i_sb);
  7898. page_start = page_offset(page);
  7899. page_end = page_start + PAGE_SIZE - 1;
  7900. end = page_end;
  7901. /*
  7902. * Reserving delalloc space after obtaining the page lock can lead to
  7903. * deadlock. For example, if a dirty page is locked by this function
  7904. * and the call to btrfs_delalloc_reserve_space() ends up triggering
  7905. * dirty page write out, then the btrfs_writepage() function could
  7906. * end up waiting indefinitely to get a lock on the page currently
  7907. * being processed by btrfs_page_mkwrite() function.
  7908. */
  7909. ret = btrfs_delalloc_reserve_space(inode, page_start,
  7910. reserved_space);
  7911. if (!ret) {
  7912. ret = file_update_time(vmf->vma->vm_file);
  7913. reserved = 1;
  7914. }
  7915. if (ret) {
  7916. if (ret == -ENOMEM)
  7917. ret = VM_FAULT_OOM;
  7918. else /* -ENOSPC, -EIO, etc */
  7919. ret = VM_FAULT_SIGBUS;
  7920. if (reserved)
  7921. goto out;
  7922. goto out_noreserve;
  7923. }
  7924. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  7925. again:
  7926. lock_page(page);
  7927. size = i_size_read(inode);
  7928. if ((page->mapping != inode->i_mapping) ||
  7929. (page_start >= size)) {
  7930. /* page got truncated out from underneath us */
  7931. goto out_unlock;
  7932. }
  7933. wait_on_page_writeback(page);
  7934. lock_extent_bits(io_tree, page_start, page_end, &cached_state);
  7935. set_page_extent_mapped(page);
  7936. /*
  7937. * we can't set the delalloc bits if there are pending ordered
  7938. * extents. Drop our locks and wait for them to finish
  7939. */
  7940. ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
  7941. if (ordered) {
  7942. unlock_extent_cached(io_tree, page_start, page_end,
  7943. &cached_state, GFP_NOFS);
  7944. unlock_page(page);
  7945. btrfs_start_ordered_extent(inode, ordered, 1);
  7946. btrfs_put_ordered_extent(ordered);
  7947. goto again;
  7948. }
  7949. if (page->index == ((size - 1) >> PAGE_SHIFT)) {
  7950. reserved_space = round_up(size - page_start,
  7951. fs_info->sectorsize);
  7952. if (reserved_space < PAGE_SIZE) {
  7953. end = page_start + reserved_space - 1;
  7954. spin_lock(&BTRFS_I(inode)->lock);
  7955. BTRFS_I(inode)->outstanding_extents++;
  7956. spin_unlock(&BTRFS_I(inode)->lock);
  7957. btrfs_delalloc_release_space(inode, page_start,
  7958. PAGE_SIZE - reserved_space);
  7959. }
  7960. }
  7961. /*
  7962. * XXX - page_mkwrite gets called every time the page is dirtied, even
  7963. * if it was already dirty, so for space accounting reasons we need to
  7964. * clear any delalloc bits for the range we are fixing to save. There
  7965. * is probably a better way to do this, but for now keep consistent with
  7966. * prepare_pages in the normal write path.
  7967. */
  7968. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
  7969. EXTENT_DIRTY | EXTENT_DELALLOC |
  7970. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  7971. 0, 0, &cached_state, GFP_NOFS);
  7972. ret = btrfs_set_extent_delalloc(inode, page_start, end,
  7973. &cached_state, 0);
  7974. if (ret) {
  7975. unlock_extent_cached(io_tree, page_start, page_end,
  7976. &cached_state, GFP_NOFS);
  7977. ret = VM_FAULT_SIGBUS;
  7978. goto out_unlock;
  7979. }
  7980. ret = 0;
  7981. /* page is wholly or partially inside EOF */
  7982. if (page_start + PAGE_SIZE > size)
  7983. zero_start = size & ~PAGE_MASK;
  7984. else
  7985. zero_start = PAGE_SIZE;
  7986. if (zero_start != PAGE_SIZE) {
  7987. kaddr = kmap(page);
  7988. memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
  7989. flush_dcache_page(page);
  7990. kunmap(page);
  7991. }
  7992. ClearPageChecked(page);
  7993. set_page_dirty(page);
  7994. SetPageUptodate(page);
  7995. BTRFS_I(inode)->last_trans = fs_info->generation;
  7996. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  7997. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  7998. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  7999. out_unlock:
  8000. if (!ret) {
  8001. sb_end_pagefault(inode->i_sb);
  8002. return VM_FAULT_LOCKED;
  8003. }
  8004. unlock_page(page);
  8005. out:
  8006. btrfs_delalloc_release_space(inode, page_start, reserved_space);
  8007. out_noreserve:
  8008. sb_end_pagefault(inode->i_sb);
  8009. return ret;
  8010. }
  8011. static int btrfs_truncate(struct inode *inode)
  8012. {
  8013. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  8014. struct btrfs_root *root = BTRFS_I(inode)->root;
  8015. struct btrfs_block_rsv *rsv;
  8016. int ret = 0;
  8017. int err = 0;
  8018. struct btrfs_trans_handle *trans;
  8019. u64 mask = fs_info->sectorsize - 1;
  8020. u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
  8021. ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
  8022. (u64)-1);
  8023. if (ret)
  8024. return ret;
  8025. /*
  8026. * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
  8027. * 3 things going on here
  8028. *
  8029. * 1) We need to reserve space for our orphan item and the space to
  8030. * delete our orphan item. Lord knows we don't want to have a dangling
  8031. * orphan item because we didn't reserve space to remove it.
  8032. *
  8033. * 2) We need to reserve space to update our inode.
  8034. *
  8035. * 3) We need to have something to cache all the space that is going to
  8036. * be free'd up by the truncate operation, but also have some slack
  8037. * space reserved in case it uses space during the truncate (thank you
  8038. * very much snapshotting).
  8039. *
  8040. * And we need these to all be separate. The fact is we can use a lot of
  8041. * space doing the truncate, and we have no earthly idea how much space
  8042. * we will use, so we need the truncate reservation to be separate so it
  8043. * doesn't end up using space reserved for updating the inode or
  8044. * removing the orphan item. We also need to be able to stop the
  8045. * transaction and start a new one, which means we need to be able to
  8046. * update the inode several times, and we have no idea of knowing how
  8047. * many times that will be, so we can't just reserve 1 item for the
  8048. * entirety of the operation, so that has to be done separately as well.
  8049. * Then there is the orphan item, which does indeed need to be held on
  8050. * to for the whole operation, and we need nobody to touch this reserved
  8051. * space except the orphan code.
  8052. *
  8053. * So that leaves us with
  8054. *
  8055. * 1) root->orphan_block_rsv - for the orphan deletion.
  8056. * 2) rsv - for the truncate reservation, which we will steal from the
  8057. * transaction reservation.
  8058. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  8059. * updating the inode.
  8060. */
  8061. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  8062. if (!rsv)
  8063. return -ENOMEM;
  8064. rsv->size = min_size;
  8065. rsv->failfast = 1;
  8066. /*
  8067. * 1 for the truncate slack space
  8068. * 1 for updating the inode.
  8069. */
  8070. trans = btrfs_start_transaction(root, 2);
  8071. if (IS_ERR(trans)) {
  8072. err = PTR_ERR(trans);
  8073. goto out;
  8074. }
  8075. /* Migrate the slack space for the truncate to our reserve */
  8076. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
  8077. min_size, 0);
  8078. BUG_ON(ret);
  8079. /*
  8080. * So if we truncate and then write and fsync we normally would just
  8081. * write the extents that changed, which is a problem if we need to
  8082. * first truncate that entire inode. So set this flag so we write out
  8083. * all of the extents in the inode to the sync log so we're completely
  8084. * safe.
  8085. */
  8086. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  8087. trans->block_rsv = rsv;
  8088. while (1) {
  8089. ret = btrfs_truncate_inode_items(trans, root, inode,
  8090. inode->i_size,
  8091. BTRFS_EXTENT_DATA_KEY);
  8092. if (ret != -ENOSPC && ret != -EAGAIN) {
  8093. err = ret;
  8094. break;
  8095. }
  8096. trans->block_rsv = &fs_info->trans_block_rsv;
  8097. ret = btrfs_update_inode(trans, root, inode);
  8098. if (ret) {
  8099. err = ret;
  8100. break;
  8101. }
  8102. btrfs_end_transaction(trans);
  8103. btrfs_btree_balance_dirty(fs_info);
  8104. trans = btrfs_start_transaction(root, 2);
  8105. if (IS_ERR(trans)) {
  8106. ret = err = PTR_ERR(trans);
  8107. trans = NULL;
  8108. break;
  8109. }
  8110. btrfs_block_rsv_release(fs_info, rsv, -1);
  8111. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
  8112. rsv, min_size, 0);
  8113. BUG_ON(ret); /* shouldn't happen */
  8114. trans->block_rsv = rsv;
  8115. }
  8116. if (ret == 0 && inode->i_nlink > 0) {
  8117. trans->block_rsv = root->orphan_block_rsv;
  8118. ret = btrfs_orphan_del(trans, inode);
  8119. if (ret)
  8120. err = ret;
  8121. }
  8122. if (trans) {
  8123. trans->block_rsv = &fs_info->trans_block_rsv;
  8124. ret = btrfs_update_inode(trans, root, inode);
  8125. if (ret && !err)
  8126. err = ret;
  8127. ret = btrfs_end_transaction(trans);
  8128. btrfs_btree_balance_dirty(fs_info);
  8129. }
  8130. out:
  8131. btrfs_free_block_rsv(fs_info, rsv);
  8132. if (ret && !err)
  8133. err = ret;
  8134. return err;
  8135. }
  8136. /*
  8137. * create a new subvolume directory/inode (helper for the ioctl).
  8138. */
  8139. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  8140. struct btrfs_root *new_root,
  8141. struct btrfs_root *parent_root,
  8142. u64 new_dirid)
  8143. {
  8144. struct inode *inode;
  8145. int err;
  8146. u64 index = 0;
  8147. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  8148. new_dirid, new_dirid,
  8149. S_IFDIR | (~current_umask() & S_IRWXUGO),
  8150. &index);
  8151. if (IS_ERR(inode))
  8152. return PTR_ERR(inode);
  8153. inode->i_op = &btrfs_dir_inode_operations;
  8154. inode->i_fop = &btrfs_dir_file_operations;
  8155. set_nlink(inode, 1);
  8156. btrfs_i_size_write(inode, 0);
  8157. unlock_new_inode(inode);
  8158. err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
  8159. if (err)
  8160. btrfs_err(new_root->fs_info,
  8161. "error inheriting subvolume %llu properties: %d",
  8162. new_root->root_key.objectid, err);
  8163. err = btrfs_update_inode(trans, new_root, inode);
  8164. iput(inode);
  8165. return err;
  8166. }
  8167. struct inode *btrfs_alloc_inode(struct super_block *sb)
  8168. {
  8169. struct btrfs_inode *ei;
  8170. struct inode *inode;
  8171. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  8172. if (!ei)
  8173. return NULL;
  8174. ei->root = NULL;
  8175. ei->generation = 0;
  8176. ei->last_trans = 0;
  8177. ei->last_sub_trans = 0;
  8178. ei->logged_trans = 0;
  8179. ei->delalloc_bytes = 0;
  8180. ei->defrag_bytes = 0;
  8181. ei->disk_i_size = 0;
  8182. ei->flags = 0;
  8183. ei->csum_bytes = 0;
  8184. ei->index_cnt = (u64)-1;
  8185. ei->dir_index = 0;
  8186. ei->last_unlink_trans = 0;
  8187. ei->last_log_commit = 0;
  8188. ei->delayed_iput_count = 0;
  8189. spin_lock_init(&ei->lock);
  8190. ei->outstanding_extents = 0;
  8191. ei->reserved_extents = 0;
  8192. ei->runtime_flags = 0;
  8193. ei->force_compress = BTRFS_COMPRESS_NONE;
  8194. ei->delayed_node = NULL;
  8195. ei->i_otime.tv_sec = 0;
  8196. ei->i_otime.tv_nsec = 0;
  8197. inode = &ei->vfs_inode;
  8198. extent_map_tree_init(&ei->extent_tree);
  8199. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  8200. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  8201. ei->io_tree.track_uptodate = 1;
  8202. ei->io_failure_tree.track_uptodate = 1;
  8203. atomic_set(&ei->sync_writers, 0);
  8204. mutex_init(&ei->log_mutex);
  8205. mutex_init(&ei->delalloc_mutex);
  8206. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  8207. INIT_LIST_HEAD(&ei->delalloc_inodes);
  8208. INIT_LIST_HEAD(&ei->delayed_iput);
  8209. RB_CLEAR_NODE(&ei->rb_node);
  8210. init_rwsem(&ei->dio_sem);
  8211. return inode;
  8212. }
  8213. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  8214. void btrfs_test_destroy_inode(struct inode *inode)
  8215. {
  8216. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  8217. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  8218. }
  8219. #endif
  8220. static void btrfs_i_callback(struct rcu_head *head)
  8221. {
  8222. struct inode *inode = container_of(head, struct inode, i_rcu);
  8223. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  8224. }
  8225. void btrfs_destroy_inode(struct inode *inode)
  8226. {
  8227. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  8228. struct btrfs_ordered_extent *ordered;
  8229. struct btrfs_root *root = BTRFS_I(inode)->root;
  8230. WARN_ON(!hlist_empty(&inode->i_dentry));
  8231. WARN_ON(inode->i_data.nrpages);
  8232. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  8233. WARN_ON(BTRFS_I(inode)->reserved_extents);
  8234. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  8235. WARN_ON(BTRFS_I(inode)->csum_bytes);
  8236. WARN_ON(BTRFS_I(inode)->defrag_bytes);
  8237. /*
  8238. * This can happen where we create an inode, but somebody else also
  8239. * created the same inode and we need to destroy the one we already
  8240. * created.
  8241. */
  8242. if (!root)
  8243. goto free;
  8244. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  8245. &BTRFS_I(inode)->runtime_flags)) {
  8246. btrfs_info(fs_info, "inode %llu still on the orphan list",
  8247. btrfs_ino(inode));
  8248. atomic_dec(&root->orphan_inodes);
  8249. }
  8250. while (1) {
  8251. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  8252. if (!ordered)
  8253. break;
  8254. else {
  8255. btrfs_err(fs_info,
  8256. "found ordered extent %llu %llu on inode cleanup",
  8257. ordered->file_offset, ordered->len);
  8258. btrfs_remove_ordered_extent(inode, ordered);
  8259. btrfs_put_ordered_extent(ordered);
  8260. btrfs_put_ordered_extent(ordered);
  8261. }
  8262. }
  8263. btrfs_qgroup_check_reserved_leak(inode);
  8264. inode_tree_del(inode);
  8265. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  8266. free:
  8267. call_rcu(&inode->i_rcu, btrfs_i_callback);
  8268. }
  8269. int btrfs_drop_inode(struct inode *inode)
  8270. {
  8271. struct btrfs_root *root = BTRFS_I(inode)->root;
  8272. if (root == NULL)
  8273. return 1;
  8274. /* the snap/subvol tree is on deleting */
  8275. if (btrfs_root_refs(&root->root_item) == 0)
  8276. return 1;
  8277. else
  8278. return generic_drop_inode(inode);
  8279. }
  8280. static void init_once(void *foo)
  8281. {
  8282. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  8283. inode_init_once(&ei->vfs_inode);
  8284. }
  8285. void btrfs_destroy_cachep(void)
  8286. {
  8287. /*
  8288. * Make sure all delayed rcu free inodes are flushed before we
  8289. * destroy cache.
  8290. */
  8291. rcu_barrier();
  8292. kmem_cache_destroy(btrfs_inode_cachep);
  8293. kmem_cache_destroy(btrfs_trans_handle_cachep);
  8294. kmem_cache_destroy(btrfs_transaction_cachep);
  8295. kmem_cache_destroy(btrfs_path_cachep);
  8296. kmem_cache_destroy(btrfs_free_space_cachep);
  8297. }
  8298. int btrfs_init_cachep(void)
  8299. {
  8300. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  8301. sizeof(struct btrfs_inode), 0,
  8302. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
  8303. init_once);
  8304. if (!btrfs_inode_cachep)
  8305. goto fail;
  8306. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  8307. sizeof(struct btrfs_trans_handle), 0,
  8308. SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  8309. if (!btrfs_trans_handle_cachep)
  8310. goto fail;
  8311. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  8312. sizeof(struct btrfs_transaction), 0,
  8313. SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  8314. if (!btrfs_transaction_cachep)
  8315. goto fail;
  8316. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  8317. sizeof(struct btrfs_path), 0,
  8318. SLAB_MEM_SPREAD, NULL);
  8319. if (!btrfs_path_cachep)
  8320. goto fail;
  8321. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  8322. sizeof(struct btrfs_free_space), 0,
  8323. SLAB_MEM_SPREAD, NULL);
  8324. if (!btrfs_free_space_cachep)
  8325. goto fail;
  8326. return 0;
  8327. fail:
  8328. btrfs_destroy_cachep();
  8329. return -ENOMEM;
  8330. }
  8331. static int btrfs_getattr(struct vfsmount *mnt,
  8332. struct dentry *dentry, struct kstat *stat)
  8333. {
  8334. u64 delalloc_bytes;
  8335. struct inode *inode = d_inode(dentry);
  8336. u32 blocksize = inode->i_sb->s_blocksize;
  8337. generic_fillattr(inode, stat);
  8338. stat->dev = BTRFS_I(inode)->root->anon_dev;
  8339. spin_lock(&BTRFS_I(inode)->lock);
  8340. delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
  8341. spin_unlock(&BTRFS_I(inode)->lock);
  8342. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  8343. ALIGN(delalloc_bytes, blocksize)) >> 9;
  8344. return 0;
  8345. }
  8346. static int btrfs_rename_exchange(struct inode *old_dir,
  8347. struct dentry *old_dentry,
  8348. struct inode *new_dir,
  8349. struct dentry *new_dentry)
  8350. {
  8351. struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
  8352. struct btrfs_trans_handle *trans;
  8353. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  8354. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  8355. struct inode *new_inode = new_dentry->d_inode;
  8356. struct inode *old_inode = old_dentry->d_inode;
  8357. struct timespec ctime = current_time(old_inode);
  8358. struct dentry *parent;
  8359. u64 old_ino = btrfs_ino(old_inode);
  8360. u64 new_ino = btrfs_ino(new_inode);
  8361. u64 old_idx = 0;
  8362. u64 new_idx = 0;
  8363. u64 root_objectid;
  8364. int ret;
  8365. bool root_log_pinned = false;
  8366. bool dest_log_pinned = false;
  8367. /* we only allow rename subvolume link between subvolumes */
  8368. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8369. return -EXDEV;
  8370. /* close the race window with snapshot create/destroy ioctl */
  8371. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8372. down_read(&fs_info->subvol_sem);
  8373. if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
  8374. down_read(&fs_info->subvol_sem);
  8375. /*
  8376. * We want to reserve the absolute worst case amount of items. So if
  8377. * both inodes are subvols and we need to unlink them then that would
  8378. * require 4 item modifications, but if they are both normal inodes it
  8379. * would require 5 item modifications, so we'll assume their normal
  8380. * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
  8381. * should cover the worst case number of items we'll modify.
  8382. */
  8383. trans = btrfs_start_transaction(root, 12);
  8384. if (IS_ERR(trans)) {
  8385. ret = PTR_ERR(trans);
  8386. goto out_notrans;
  8387. }
  8388. /*
  8389. * We need to find a free sequence number both in the source and
  8390. * in the destination directory for the exchange.
  8391. */
  8392. ret = btrfs_set_inode_index(new_dir, &old_idx);
  8393. if (ret)
  8394. goto out_fail;
  8395. ret = btrfs_set_inode_index(old_dir, &new_idx);
  8396. if (ret)
  8397. goto out_fail;
  8398. BTRFS_I(old_inode)->dir_index = 0ULL;
  8399. BTRFS_I(new_inode)->dir_index = 0ULL;
  8400. /* Reference for the source. */
  8401. if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8402. /* force full log commit if subvolume involved. */
  8403. btrfs_set_log_full_commit(fs_info, trans);
  8404. } else {
  8405. btrfs_pin_log_trans(root);
  8406. root_log_pinned = true;
  8407. ret = btrfs_insert_inode_ref(trans, dest,
  8408. new_dentry->d_name.name,
  8409. new_dentry->d_name.len,
  8410. old_ino,
  8411. btrfs_ino(new_dir), old_idx);
  8412. if (ret)
  8413. goto out_fail;
  8414. }
  8415. /* And now for the dest. */
  8416. if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8417. /* force full log commit if subvolume involved. */
  8418. btrfs_set_log_full_commit(fs_info, trans);
  8419. } else {
  8420. btrfs_pin_log_trans(dest);
  8421. dest_log_pinned = true;
  8422. ret = btrfs_insert_inode_ref(trans, root,
  8423. old_dentry->d_name.name,
  8424. old_dentry->d_name.len,
  8425. new_ino,
  8426. btrfs_ino(old_dir), new_idx);
  8427. if (ret)
  8428. goto out_fail;
  8429. }
  8430. /* Update inode version and ctime/mtime. */
  8431. inode_inc_iversion(old_dir);
  8432. inode_inc_iversion(new_dir);
  8433. inode_inc_iversion(old_inode);
  8434. inode_inc_iversion(new_inode);
  8435. old_dir->i_ctime = old_dir->i_mtime = ctime;
  8436. new_dir->i_ctime = new_dir->i_mtime = ctime;
  8437. old_inode->i_ctime = ctime;
  8438. new_inode->i_ctime = ctime;
  8439. if (old_dentry->d_parent != new_dentry->d_parent) {
  8440. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  8441. btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
  8442. }
  8443. /* src is a subvolume */
  8444. if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8445. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8446. ret = btrfs_unlink_subvol(trans, root, old_dir,
  8447. root_objectid,
  8448. old_dentry->d_name.name,
  8449. old_dentry->d_name.len);
  8450. } else { /* src is an inode */
  8451. ret = __btrfs_unlink_inode(trans, root, old_dir,
  8452. old_dentry->d_inode,
  8453. old_dentry->d_name.name,
  8454. old_dentry->d_name.len);
  8455. if (!ret)
  8456. ret = btrfs_update_inode(trans, root, old_inode);
  8457. }
  8458. if (ret) {
  8459. btrfs_abort_transaction(trans, ret);
  8460. goto out_fail;
  8461. }
  8462. /* dest is a subvolume */
  8463. if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8464. root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
  8465. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8466. root_objectid,
  8467. new_dentry->d_name.name,
  8468. new_dentry->d_name.len);
  8469. } else { /* dest is an inode */
  8470. ret = __btrfs_unlink_inode(trans, dest, new_dir,
  8471. new_dentry->d_inode,
  8472. new_dentry->d_name.name,
  8473. new_dentry->d_name.len);
  8474. if (!ret)
  8475. ret = btrfs_update_inode(trans, dest, new_inode);
  8476. }
  8477. if (ret) {
  8478. btrfs_abort_transaction(trans, ret);
  8479. goto out_fail;
  8480. }
  8481. ret = btrfs_add_link(trans, new_dir, old_inode,
  8482. new_dentry->d_name.name,
  8483. new_dentry->d_name.len, 0, old_idx);
  8484. if (ret) {
  8485. btrfs_abort_transaction(trans, ret);
  8486. goto out_fail;
  8487. }
  8488. ret = btrfs_add_link(trans, old_dir, new_inode,
  8489. old_dentry->d_name.name,
  8490. old_dentry->d_name.len, 0, new_idx);
  8491. if (ret) {
  8492. btrfs_abort_transaction(trans, ret);
  8493. goto out_fail;
  8494. }
  8495. if (old_inode->i_nlink == 1)
  8496. BTRFS_I(old_inode)->dir_index = old_idx;
  8497. if (new_inode->i_nlink == 1)
  8498. BTRFS_I(new_inode)->dir_index = new_idx;
  8499. if (root_log_pinned) {
  8500. parent = new_dentry->d_parent;
  8501. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  8502. btrfs_end_log_trans(root);
  8503. root_log_pinned = false;
  8504. }
  8505. if (dest_log_pinned) {
  8506. parent = old_dentry->d_parent;
  8507. btrfs_log_new_name(trans, new_inode, new_dir, parent);
  8508. btrfs_end_log_trans(dest);
  8509. dest_log_pinned = false;
  8510. }
  8511. out_fail:
  8512. /*
  8513. * If we have pinned a log and an error happened, we unpin tasks
  8514. * trying to sync the log and force them to fallback to a transaction
  8515. * commit if the log currently contains any of the inodes involved in
  8516. * this rename operation (to ensure we do not persist a log with an
  8517. * inconsistent state for any of these inodes or leading to any
  8518. * inconsistencies when replayed). If the transaction was aborted, the
  8519. * abortion reason is propagated to userspace when attempting to commit
  8520. * the transaction. If the log does not contain any of these inodes, we
  8521. * allow the tasks to sync it.
  8522. */
  8523. if (ret && (root_log_pinned || dest_log_pinned)) {
  8524. if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
  8525. btrfs_inode_in_log(new_dir, fs_info->generation) ||
  8526. btrfs_inode_in_log(old_inode, fs_info->generation) ||
  8527. (new_inode &&
  8528. btrfs_inode_in_log(new_inode, fs_info->generation)))
  8529. btrfs_set_log_full_commit(fs_info, trans);
  8530. if (root_log_pinned) {
  8531. btrfs_end_log_trans(root);
  8532. root_log_pinned = false;
  8533. }
  8534. if (dest_log_pinned) {
  8535. btrfs_end_log_trans(dest);
  8536. dest_log_pinned = false;
  8537. }
  8538. }
  8539. ret = btrfs_end_transaction(trans);
  8540. out_notrans:
  8541. if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
  8542. up_read(&fs_info->subvol_sem);
  8543. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8544. up_read(&fs_info->subvol_sem);
  8545. return ret;
  8546. }
  8547. static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
  8548. struct btrfs_root *root,
  8549. struct inode *dir,
  8550. struct dentry *dentry)
  8551. {
  8552. int ret;
  8553. struct inode *inode;
  8554. u64 objectid;
  8555. u64 index;
  8556. ret = btrfs_find_free_ino(root, &objectid);
  8557. if (ret)
  8558. return ret;
  8559. inode = btrfs_new_inode(trans, root, dir,
  8560. dentry->d_name.name,
  8561. dentry->d_name.len,
  8562. btrfs_ino(dir),
  8563. objectid,
  8564. S_IFCHR | WHITEOUT_MODE,
  8565. &index);
  8566. if (IS_ERR(inode)) {
  8567. ret = PTR_ERR(inode);
  8568. return ret;
  8569. }
  8570. inode->i_op = &btrfs_special_inode_operations;
  8571. init_special_inode(inode, inode->i_mode,
  8572. WHITEOUT_DEV);
  8573. ret = btrfs_init_inode_security(trans, inode, dir,
  8574. &dentry->d_name);
  8575. if (ret)
  8576. goto out;
  8577. ret = btrfs_add_nondir(trans, dir, dentry,
  8578. inode, 0, index);
  8579. if (ret)
  8580. goto out;
  8581. ret = btrfs_update_inode(trans, root, inode);
  8582. out:
  8583. unlock_new_inode(inode);
  8584. if (ret)
  8585. inode_dec_link_count(inode);
  8586. iput(inode);
  8587. return ret;
  8588. }
  8589. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  8590. struct inode *new_dir, struct dentry *new_dentry,
  8591. unsigned int flags)
  8592. {
  8593. struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
  8594. struct btrfs_trans_handle *trans;
  8595. unsigned int trans_num_items;
  8596. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  8597. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  8598. struct inode *new_inode = d_inode(new_dentry);
  8599. struct inode *old_inode = d_inode(old_dentry);
  8600. u64 index = 0;
  8601. u64 root_objectid;
  8602. int ret;
  8603. u64 old_ino = btrfs_ino(old_inode);
  8604. bool log_pinned = false;
  8605. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  8606. return -EPERM;
  8607. /* we only allow rename subvolume link between subvolumes */
  8608. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8609. return -EXDEV;
  8610. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  8611. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  8612. return -ENOTEMPTY;
  8613. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  8614. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  8615. return -ENOTEMPTY;
  8616. /* check for collisions, even if the name isn't there */
  8617. ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
  8618. new_dentry->d_name.name,
  8619. new_dentry->d_name.len);
  8620. if (ret) {
  8621. if (ret == -EEXIST) {
  8622. /* we shouldn't get
  8623. * eexist without a new_inode */
  8624. if (WARN_ON(!new_inode)) {
  8625. return ret;
  8626. }
  8627. } else {
  8628. /* maybe -EOVERFLOW */
  8629. return ret;
  8630. }
  8631. }
  8632. ret = 0;
  8633. /*
  8634. * we're using rename to replace one file with another. Start IO on it
  8635. * now so we don't add too much work to the end of the transaction
  8636. */
  8637. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
  8638. filemap_flush(old_inode->i_mapping);
  8639. /* close the racy window with snapshot create/destroy ioctl */
  8640. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8641. down_read(&fs_info->subvol_sem);
  8642. /*
  8643. * We want to reserve the absolute worst case amount of items. So if
  8644. * both inodes are subvols and we need to unlink them then that would
  8645. * require 4 item modifications, but if they are both normal inodes it
  8646. * would require 5 item modifications, so we'll assume they are normal
  8647. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  8648. * should cover the worst case number of items we'll modify.
  8649. * If our rename has the whiteout flag, we need more 5 units for the
  8650. * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
  8651. * when selinux is enabled).
  8652. */
  8653. trans_num_items = 11;
  8654. if (flags & RENAME_WHITEOUT)
  8655. trans_num_items += 5;
  8656. trans = btrfs_start_transaction(root, trans_num_items);
  8657. if (IS_ERR(trans)) {
  8658. ret = PTR_ERR(trans);
  8659. goto out_notrans;
  8660. }
  8661. if (dest != root)
  8662. btrfs_record_root_in_trans(trans, dest);
  8663. ret = btrfs_set_inode_index(new_dir, &index);
  8664. if (ret)
  8665. goto out_fail;
  8666. BTRFS_I(old_inode)->dir_index = 0ULL;
  8667. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8668. /* force full log commit if subvolume involved. */
  8669. btrfs_set_log_full_commit(fs_info, trans);
  8670. } else {
  8671. btrfs_pin_log_trans(root);
  8672. log_pinned = true;
  8673. ret = btrfs_insert_inode_ref(trans, dest,
  8674. new_dentry->d_name.name,
  8675. new_dentry->d_name.len,
  8676. old_ino,
  8677. btrfs_ino(new_dir), index);
  8678. if (ret)
  8679. goto out_fail;
  8680. }
  8681. inode_inc_iversion(old_dir);
  8682. inode_inc_iversion(new_dir);
  8683. inode_inc_iversion(old_inode);
  8684. old_dir->i_ctime = old_dir->i_mtime =
  8685. new_dir->i_ctime = new_dir->i_mtime =
  8686. old_inode->i_ctime = current_time(old_dir);
  8687. if (old_dentry->d_parent != new_dentry->d_parent)
  8688. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  8689. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8690. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8691. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  8692. old_dentry->d_name.name,
  8693. old_dentry->d_name.len);
  8694. } else {
  8695. ret = __btrfs_unlink_inode(trans, root, old_dir,
  8696. d_inode(old_dentry),
  8697. old_dentry->d_name.name,
  8698. old_dentry->d_name.len);
  8699. if (!ret)
  8700. ret = btrfs_update_inode(trans, root, old_inode);
  8701. }
  8702. if (ret) {
  8703. btrfs_abort_transaction(trans, ret);
  8704. goto out_fail;
  8705. }
  8706. if (new_inode) {
  8707. inode_inc_iversion(new_inode);
  8708. new_inode->i_ctime = current_time(new_inode);
  8709. if (unlikely(btrfs_ino(new_inode) ==
  8710. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  8711. root_objectid = BTRFS_I(new_inode)->location.objectid;
  8712. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8713. root_objectid,
  8714. new_dentry->d_name.name,
  8715. new_dentry->d_name.len);
  8716. BUG_ON(new_inode->i_nlink == 0);
  8717. } else {
  8718. ret = btrfs_unlink_inode(trans, dest, new_dir,
  8719. d_inode(new_dentry),
  8720. new_dentry->d_name.name,
  8721. new_dentry->d_name.len);
  8722. }
  8723. if (!ret && new_inode->i_nlink == 0)
  8724. ret = btrfs_orphan_add(trans, d_inode(new_dentry));
  8725. if (ret) {
  8726. btrfs_abort_transaction(trans, ret);
  8727. goto out_fail;
  8728. }
  8729. }
  8730. ret = btrfs_add_link(trans, new_dir, old_inode,
  8731. new_dentry->d_name.name,
  8732. new_dentry->d_name.len, 0, index);
  8733. if (ret) {
  8734. btrfs_abort_transaction(trans, ret);
  8735. goto out_fail;
  8736. }
  8737. if (old_inode->i_nlink == 1)
  8738. BTRFS_I(old_inode)->dir_index = index;
  8739. if (log_pinned) {
  8740. struct dentry *parent = new_dentry->d_parent;
  8741. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  8742. btrfs_end_log_trans(root);
  8743. log_pinned = false;
  8744. }
  8745. if (flags & RENAME_WHITEOUT) {
  8746. ret = btrfs_whiteout_for_rename(trans, root, old_dir,
  8747. old_dentry);
  8748. if (ret) {
  8749. btrfs_abort_transaction(trans, ret);
  8750. goto out_fail;
  8751. }
  8752. }
  8753. out_fail:
  8754. /*
  8755. * If we have pinned the log and an error happened, we unpin tasks
  8756. * trying to sync the log and force them to fallback to a transaction
  8757. * commit if the log currently contains any of the inodes involved in
  8758. * this rename operation (to ensure we do not persist a log with an
  8759. * inconsistent state for any of these inodes or leading to any
  8760. * inconsistencies when replayed). If the transaction was aborted, the
  8761. * abortion reason is propagated to userspace when attempting to commit
  8762. * the transaction. If the log does not contain any of these inodes, we
  8763. * allow the tasks to sync it.
  8764. */
  8765. if (ret && log_pinned) {
  8766. if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
  8767. btrfs_inode_in_log(new_dir, fs_info->generation) ||
  8768. btrfs_inode_in_log(old_inode, fs_info->generation) ||
  8769. (new_inode &&
  8770. btrfs_inode_in_log(new_inode, fs_info->generation)))
  8771. btrfs_set_log_full_commit(fs_info, trans);
  8772. btrfs_end_log_trans(root);
  8773. log_pinned = false;
  8774. }
  8775. btrfs_end_transaction(trans);
  8776. out_notrans:
  8777. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8778. up_read(&fs_info->subvol_sem);
  8779. return ret;
  8780. }
  8781. static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  8782. struct inode *new_dir, struct dentry *new_dentry,
  8783. unsigned int flags)
  8784. {
  8785. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  8786. return -EINVAL;
  8787. if (flags & RENAME_EXCHANGE)
  8788. return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
  8789. new_dentry);
  8790. return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  8791. }
  8792. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  8793. {
  8794. struct btrfs_delalloc_work *delalloc_work;
  8795. struct inode *inode;
  8796. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  8797. work);
  8798. inode = delalloc_work->inode;
  8799. filemap_flush(inode->i_mapping);
  8800. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  8801. &BTRFS_I(inode)->runtime_flags))
  8802. filemap_flush(inode->i_mapping);
  8803. if (delalloc_work->delay_iput)
  8804. btrfs_add_delayed_iput(inode);
  8805. else
  8806. iput(inode);
  8807. complete(&delalloc_work->completion);
  8808. }
  8809. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  8810. int delay_iput)
  8811. {
  8812. struct btrfs_delalloc_work *work;
  8813. work = kmalloc(sizeof(*work), GFP_NOFS);
  8814. if (!work)
  8815. return NULL;
  8816. init_completion(&work->completion);
  8817. INIT_LIST_HEAD(&work->list);
  8818. work->inode = inode;
  8819. work->delay_iput = delay_iput;
  8820. WARN_ON_ONCE(!inode);
  8821. btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
  8822. btrfs_run_delalloc_work, NULL, NULL);
  8823. return work;
  8824. }
  8825. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  8826. {
  8827. wait_for_completion(&work->completion);
  8828. kfree(work);
  8829. }
  8830. /*
  8831. * some fairly slow code that needs optimization. This walks the list
  8832. * of all the inodes with pending delalloc and forces them to disk.
  8833. */
  8834. static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
  8835. int nr)
  8836. {
  8837. struct btrfs_inode *binode;
  8838. struct inode *inode;
  8839. struct btrfs_delalloc_work *work, *next;
  8840. struct list_head works;
  8841. struct list_head splice;
  8842. int ret = 0;
  8843. INIT_LIST_HEAD(&works);
  8844. INIT_LIST_HEAD(&splice);
  8845. mutex_lock(&root->delalloc_mutex);
  8846. spin_lock(&root->delalloc_lock);
  8847. list_splice_init(&root->delalloc_inodes, &splice);
  8848. while (!list_empty(&splice)) {
  8849. binode = list_entry(splice.next, struct btrfs_inode,
  8850. delalloc_inodes);
  8851. list_move_tail(&binode->delalloc_inodes,
  8852. &root->delalloc_inodes);
  8853. inode = igrab(&binode->vfs_inode);
  8854. if (!inode) {
  8855. cond_resched_lock(&root->delalloc_lock);
  8856. continue;
  8857. }
  8858. spin_unlock(&root->delalloc_lock);
  8859. work = btrfs_alloc_delalloc_work(inode, delay_iput);
  8860. if (!work) {
  8861. if (delay_iput)
  8862. btrfs_add_delayed_iput(inode);
  8863. else
  8864. iput(inode);
  8865. ret = -ENOMEM;
  8866. goto out;
  8867. }
  8868. list_add_tail(&work->list, &works);
  8869. btrfs_queue_work(root->fs_info->flush_workers,
  8870. &work->work);
  8871. ret++;
  8872. if (nr != -1 && ret >= nr)
  8873. goto out;
  8874. cond_resched();
  8875. spin_lock(&root->delalloc_lock);
  8876. }
  8877. spin_unlock(&root->delalloc_lock);
  8878. out:
  8879. list_for_each_entry_safe(work, next, &works, list) {
  8880. list_del_init(&work->list);
  8881. btrfs_wait_and_free_delalloc_work(work);
  8882. }
  8883. if (!list_empty_careful(&splice)) {
  8884. spin_lock(&root->delalloc_lock);
  8885. list_splice_tail(&splice, &root->delalloc_inodes);
  8886. spin_unlock(&root->delalloc_lock);
  8887. }
  8888. mutex_unlock(&root->delalloc_mutex);
  8889. return ret;
  8890. }
  8891. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  8892. {
  8893. struct btrfs_fs_info *fs_info = root->fs_info;
  8894. int ret;
  8895. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8896. return -EROFS;
  8897. ret = __start_delalloc_inodes(root, delay_iput, -1);
  8898. if (ret > 0)
  8899. ret = 0;
  8900. /*
  8901. * the filemap_flush will queue IO into the worker threads, but
  8902. * we have to make sure the IO is actually started and that
  8903. * ordered extents get created before we return
  8904. */
  8905. atomic_inc(&fs_info->async_submit_draining);
  8906. while (atomic_read(&fs_info->nr_async_submits) ||
  8907. atomic_read(&fs_info->async_delalloc_pages)) {
  8908. wait_event(fs_info->async_submit_wait,
  8909. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  8910. atomic_read(&fs_info->async_delalloc_pages) == 0));
  8911. }
  8912. atomic_dec(&fs_info->async_submit_draining);
  8913. return ret;
  8914. }
  8915. int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
  8916. int nr)
  8917. {
  8918. struct btrfs_root *root;
  8919. struct list_head splice;
  8920. int ret;
  8921. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8922. return -EROFS;
  8923. INIT_LIST_HEAD(&splice);
  8924. mutex_lock(&fs_info->delalloc_root_mutex);
  8925. spin_lock(&fs_info->delalloc_root_lock);
  8926. list_splice_init(&fs_info->delalloc_roots, &splice);
  8927. while (!list_empty(&splice) && nr) {
  8928. root = list_first_entry(&splice, struct btrfs_root,
  8929. delalloc_root);
  8930. root = btrfs_grab_fs_root(root);
  8931. BUG_ON(!root);
  8932. list_move_tail(&root->delalloc_root,
  8933. &fs_info->delalloc_roots);
  8934. spin_unlock(&fs_info->delalloc_root_lock);
  8935. ret = __start_delalloc_inodes(root, delay_iput, nr);
  8936. btrfs_put_fs_root(root);
  8937. if (ret < 0)
  8938. goto out;
  8939. if (nr != -1) {
  8940. nr -= ret;
  8941. WARN_ON(nr < 0);
  8942. }
  8943. spin_lock(&fs_info->delalloc_root_lock);
  8944. }
  8945. spin_unlock(&fs_info->delalloc_root_lock);
  8946. ret = 0;
  8947. atomic_inc(&fs_info->async_submit_draining);
  8948. while (atomic_read(&fs_info->nr_async_submits) ||
  8949. atomic_read(&fs_info->async_delalloc_pages)) {
  8950. wait_event(fs_info->async_submit_wait,
  8951. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  8952. atomic_read(&fs_info->async_delalloc_pages) == 0));
  8953. }
  8954. atomic_dec(&fs_info->async_submit_draining);
  8955. out:
  8956. if (!list_empty_careful(&splice)) {
  8957. spin_lock(&fs_info->delalloc_root_lock);
  8958. list_splice_tail(&splice, &fs_info->delalloc_roots);
  8959. spin_unlock(&fs_info->delalloc_root_lock);
  8960. }
  8961. mutex_unlock(&fs_info->delalloc_root_mutex);
  8962. return ret;
  8963. }
  8964. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  8965. const char *symname)
  8966. {
  8967. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  8968. struct btrfs_trans_handle *trans;
  8969. struct btrfs_root *root = BTRFS_I(dir)->root;
  8970. struct btrfs_path *path;
  8971. struct btrfs_key key;
  8972. struct inode *inode = NULL;
  8973. int err;
  8974. int drop_inode = 0;
  8975. u64 objectid;
  8976. u64 index = 0;
  8977. int name_len;
  8978. int datasize;
  8979. unsigned long ptr;
  8980. struct btrfs_file_extent_item *ei;
  8981. struct extent_buffer *leaf;
  8982. name_len = strlen(symname);
  8983. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
  8984. return -ENAMETOOLONG;
  8985. /*
  8986. * 2 items for inode item and ref
  8987. * 2 items for dir items
  8988. * 1 item for updating parent inode item
  8989. * 1 item for the inline extent item
  8990. * 1 item for xattr if selinux is on
  8991. */
  8992. trans = btrfs_start_transaction(root, 7);
  8993. if (IS_ERR(trans))
  8994. return PTR_ERR(trans);
  8995. err = btrfs_find_free_ino(root, &objectid);
  8996. if (err)
  8997. goto out_unlock;
  8998. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  8999. dentry->d_name.len, btrfs_ino(dir), objectid,
  9000. S_IFLNK|S_IRWXUGO, &index);
  9001. if (IS_ERR(inode)) {
  9002. err = PTR_ERR(inode);
  9003. goto out_unlock;
  9004. }
  9005. /*
  9006. * If the active LSM wants to access the inode during
  9007. * d_instantiate it needs these. Smack checks to see
  9008. * if the filesystem supports xattrs by looking at the
  9009. * ops vector.
  9010. */
  9011. inode->i_fop = &btrfs_file_operations;
  9012. inode->i_op = &btrfs_file_inode_operations;
  9013. inode->i_mapping->a_ops = &btrfs_aops;
  9014. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  9015. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  9016. if (err)
  9017. goto out_unlock_inode;
  9018. path = btrfs_alloc_path();
  9019. if (!path) {
  9020. err = -ENOMEM;
  9021. goto out_unlock_inode;
  9022. }
  9023. key.objectid = btrfs_ino(inode);
  9024. key.offset = 0;
  9025. key.type = BTRFS_EXTENT_DATA_KEY;
  9026. datasize = btrfs_file_extent_calc_inline_size(name_len);
  9027. err = btrfs_insert_empty_item(trans, root, path, &key,
  9028. datasize);
  9029. if (err) {
  9030. btrfs_free_path(path);
  9031. goto out_unlock_inode;
  9032. }
  9033. leaf = path->nodes[0];
  9034. ei = btrfs_item_ptr(leaf, path->slots[0],
  9035. struct btrfs_file_extent_item);
  9036. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  9037. btrfs_set_file_extent_type(leaf, ei,
  9038. BTRFS_FILE_EXTENT_INLINE);
  9039. btrfs_set_file_extent_encryption(leaf, ei, 0);
  9040. btrfs_set_file_extent_compression(leaf, ei, 0);
  9041. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  9042. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  9043. ptr = btrfs_file_extent_inline_start(ei);
  9044. write_extent_buffer(leaf, symname, ptr, name_len);
  9045. btrfs_mark_buffer_dirty(leaf);
  9046. btrfs_free_path(path);
  9047. inode->i_op = &btrfs_symlink_inode_operations;
  9048. inode_nohighmem(inode);
  9049. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  9050. inode_set_bytes(inode, name_len);
  9051. btrfs_i_size_write(inode, name_len);
  9052. err = btrfs_update_inode(trans, root, inode);
  9053. /*
  9054. * Last step, add directory indexes for our symlink inode. This is the
  9055. * last step to avoid extra cleanup of these indexes if an error happens
  9056. * elsewhere above.
  9057. */
  9058. if (!err)
  9059. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  9060. if (err) {
  9061. drop_inode = 1;
  9062. goto out_unlock_inode;
  9063. }
  9064. unlock_new_inode(inode);
  9065. d_instantiate(dentry, inode);
  9066. out_unlock:
  9067. btrfs_end_transaction(trans);
  9068. if (drop_inode) {
  9069. inode_dec_link_count(inode);
  9070. iput(inode);
  9071. }
  9072. btrfs_btree_balance_dirty(fs_info);
  9073. return err;
  9074. out_unlock_inode:
  9075. drop_inode = 1;
  9076. unlock_new_inode(inode);
  9077. goto out_unlock;
  9078. }
  9079. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  9080. u64 start, u64 num_bytes, u64 min_size,
  9081. loff_t actual_len, u64 *alloc_hint,
  9082. struct btrfs_trans_handle *trans)
  9083. {
  9084. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  9085. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  9086. struct extent_map *em;
  9087. struct btrfs_root *root = BTRFS_I(inode)->root;
  9088. struct btrfs_key ins;
  9089. u64 cur_offset = start;
  9090. u64 i_size;
  9091. u64 cur_bytes;
  9092. u64 last_alloc = (u64)-1;
  9093. int ret = 0;
  9094. bool own_trans = true;
  9095. u64 end = start + num_bytes - 1;
  9096. if (trans)
  9097. own_trans = false;
  9098. while (num_bytes > 0) {
  9099. if (own_trans) {
  9100. trans = btrfs_start_transaction(root, 3);
  9101. if (IS_ERR(trans)) {
  9102. ret = PTR_ERR(trans);
  9103. break;
  9104. }
  9105. }
  9106. cur_bytes = min_t(u64, num_bytes, SZ_256M);
  9107. cur_bytes = max(cur_bytes, min_size);
  9108. /*
  9109. * If we are severely fragmented we could end up with really
  9110. * small allocations, so if the allocator is returning small
  9111. * chunks lets make its job easier by only searching for those
  9112. * sized chunks.
  9113. */
  9114. cur_bytes = min(cur_bytes, last_alloc);
  9115. ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
  9116. min_size, 0, *alloc_hint, &ins, 1, 0);
  9117. if (ret) {
  9118. if (own_trans)
  9119. btrfs_end_transaction(trans);
  9120. break;
  9121. }
  9122. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  9123. last_alloc = ins.offset;
  9124. ret = insert_reserved_file_extent(trans, inode,
  9125. cur_offset, ins.objectid,
  9126. ins.offset, ins.offset,
  9127. ins.offset, 0, 0, 0,
  9128. BTRFS_FILE_EXTENT_PREALLOC);
  9129. if (ret) {
  9130. btrfs_free_reserved_extent(fs_info, ins.objectid,
  9131. ins.offset, 0);
  9132. btrfs_abort_transaction(trans, ret);
  9133. if (own_trans)
  9134. btrfs_end_transaction(trans);
  9135. break;
  9136. }
  9137. btrfs_drop_extent_cache(inode, cur_offset,
  9138. cur_offset + ins.offset -1, 0);
  9139. em = alloc_extent_map();
  9140. if (!em) {
  9141. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  9142. &BTRFS_I(inode)->runtime_flags);
  9143. goto next;
  9144. }
  9145. em->start = cur_offset;
  9146. em->orig_start = cur_offset;
  9147. em->len = ins.offset;
  9148. em->block_start = ins.objectid;
  9149. em->block_len = ins.offset;
  9150. em->orig_block_len = ins.offset;
  9151. em->ram_bytes = ins.offset;
  9152. em->bdev = fs_info->fs_devices->latest_bdev;
  9153. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  9154. em->generation = trans->transid;
  9155. while (1) {
  9156. write_lock(&em_tree->lock);
  9157. ret = add_extent_mapping(em_tree, em, 1);
  9158. write_unlock(&em_tree->lock);
  9159. if (ret != -EEXIST)
  9160. break;
  9161. btrfs_drop_extent_cache(inode, cur_offset,
  9162. cur_offset + ins.offset - 1,
  9163. 0);
  9164. }
  9165. free_extent_map(em);
  9166. next:
  9167. num_bytes -= ins.offset;
  9168. cur_offset += ins.offset;
  9169. *alloc_hint = ins.objectid + ins.offset;
  9170. inode_inc_iversion(inode);
  9171. inode->i_ctime = current_time(inode);
  9172. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  9173. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  9174. (actual_len > inode->i_size) &&
  9175. (cur_offset > inode->i_size)) {
  9176. if (cur_offset > actual_len)
  9177. i_size = actual_len;
  9178. else
  9179. i_size = cur_offset;
  9180. i_size_write(inode, i_size);
  9181. btrfs_ordered_update_i_size(inode, i_size, NULL);
  9182. }
  9183. ret = btrfs_update_inode(trans, root, inode);
  9184. if (ret) {
  9185. btrfs_abort_transaction(trans, ret);
  9186. if (own_trans)
  9187. btrfs_end_transaction(trans);
  9188. break;
  9189. }
  9190. if (own_trans)
  9191. btrfs_end_transaction(trans);
  9192. }
  9193. if (cur_offset < end)
  9194. btrfs_free_reserved_data_space(inode, cur_offset,
  9195. end - cur_offset + 1);
  9196. return ret;
  9197. }
  9198. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  9199. u64 start, u64 num_bytes, u64 min_size,
  9200. loff_t actual_len, u64 *alloc_hint)
  9201. {
  9202. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  9203. min_size, actual_len, alloc_hint,
  9204. NULL);
  9205. }
  9206. int btrfs_prealloc_file_range_trans(struct inode *inode,
  9207. struct btrfs_trans_handle *trans, int mode,
  9208. u64 start, u64 num_bytes, u64 min_size,
  9209. loff_t actual_len, u64 *alloc_hint)
  9210. {
  9211. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  9212. min_size, actual_len, alloc_hint, trans);
  9213. }
  9214. static int btrfs_set_page_dirty(struct page *page)
  9215. {
  9216. return __set_page_dirty_nobuffers(page);
  9217. }
  9218. static int btrfs_permission(struct inode *inode, int mask)
  9219. {
  9220. struct btrfs_root *root = BTRFS_I(inode)->root;
  9221. umode_t mode = inode->i_mode;
  9222. if (mask & MAY_WRITE &&
  9223. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  9224. if (btrfs_root_readonly(root))
  9225. return -EROFS;
  9226. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  9227. return -EACCES;
  9228. }
  9229. return generic_permission(inode, mask);
  9230. }
  9231. static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  9232. {
  9233. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  9234. struct btrfs_trans_handle *trans;
  9235. struct btrfs_root *root = BTRFS_I(dir)->root;
  9236. struct inode *inode = NULL;
  9237. u64 objectid;
  9238. u64 index;
  9239. int ret = 0;
  9240. /*
  9241. * 5 units required for adding orphan entry
  9242. */
  9243. trans = btrfs_start_transaction(root, 5);
  9244. if (IS_ERR(trans))
  9245. return PTR_ERR(trans);
  9246. ret = btrfs_find_free_ino(root, &objectid);
  9247. if (ret)
  9248. goto out;
  9249. inode = btrfs_new_inode(trans, root, dir, NULL, 0,
  9250. btrfs_ino(dir), objectid, mode, &index);
  9251. if (IS_ERR(inode)) {
  9252. ret = PTR_ERR(inode);
  9253. inode = NULL;
  9254. goto out;
  9255. }
  9256. inode->i_fop = &btrfs_file_operations;
  9257. inode->i_op = &btrfs_file_inode_operations;
  9258. inode->i_mapping->a_ops = &btrfs_aops;
  9259. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  9260. ret = btrfs_init_inode_security(trans, inode, dir, NULL);
  9261. if (ret)
  9262. goto out_inode;
  9263. ret = btrfs_update_inode(trans, root, inode);
  9264. if (ret)
  9265. goto out_inode;
  9266. ret = btrfs_orphan_add(trans, inode);
  9267. if (ret)
  9268. goto out_inode;
  9269. /*
  9270. * We set number of links to 0 in btrfs_new_inode(), and here we set
  9271. * it to 1 because d_tmpfile() will issue a warning if the count is 0,
  9272. * through:
  9273. *
  9274. * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
  9275. */
  9276. set_nlink(inode, 1);
  9277. unlock_new_inode(inode);
  9278. d_tmpfile(dentry, inode);
  9279. mark_inode_dirty(inode);
  9280. out:
  9281. btrfs_end_transaction(trans);
  9282. if (ret)
  9283. iput(inode);
  9284. btrfs_balance_delayed_items(fs_info);
  9285. btrfs_btree_balance_dirty(fs_info);
  9286. return ret;
  9287. out_inode:
  9288. unlock_new_inode(inode);
  9289. goto out;
  9290. }
  9291. static const struct inode_operations btrfs_dir_inode_operations = {
  9292. .getattr = btrfs_getattr,
  9293. .lookup = btrfs_lookup,
  9294. .create = btrfs_create,
  9295. .unlink = btrfs_unlink,
  9296. .link = btrfs_link,
  9297. .mkdir = btrfs_mkdir,
  9298. .rmdir = btrfs_rmdir,
  9299. .rename = btrfs_rename2,
  9300. .symlink = btrfs_symlink,
  9301. .setattr = btrfs_setattr,
  9302. .mknod = btrfs_mknod,
  9303. .listxattr = btrfs_listxattr,
  9304. .permission = btrfs_permission,
  9305. .get_acl = btrfs_get_acl,
  9306. .set_acl = btrfs_set_acl,
  9307. .update_time = btrfs_update_time,
  9308. .tmpfile = btrfs_tmpfile,
  9309. };
  9310. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  9311. .lookup = btrfs_lookup,
  9312. .permission = btrfs_permission,
  9313. .update_time = btrfs_update_time,
  9314. };
  9315. static const struct file_operations btrfs_dir_file_operations = {
  9316. .llseek = generic_file_llseek,
  9317. .read = generic_read_dir,
  9318. .iterate_shared = btrfs_real_readdir,
  9319. .unlocked_ioctl = btrfs_ioctl,
  9320. #ifdef CONFIG_COMPAT
  9321. .compat_ioctl = btrfs_compat_ioctl,
  9322. #endif
  9323. .release = btrfs_release_file,
  9324. .fsync = btrfs_sync_file,
  9325. };
  9326. static const struct extent_io_ops btrfs_extent_io_ops = {
  9327. .fill_delalloc = run_delalloc_range,
  9328. .submit_bio_hook = btrfs_submit_bio_hook,
  9329. .merge_bio_hook = btrfs_merge_bio_hook,
  9330. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  9331. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  9332. .writepage_start_hook = btrfs_writepage_start_hook,
  9333. .set_bit_hook = btrfs_set_bit_hook,
  9334. .clear_bit_hook = btrfs_clear_bit_hook,
  9335. .merge_extent_hook = btrfs_merge_extent_hook,
  9336. .split_extent_hook = btrfs_split_extent_hook,
  9337. };
  9338. /*
  9339. * btrfs doesn't support the bmap operation because swapfiles
  9340. * use bmap to make a mapping of extents in the file. They assume
  9341. * these extents won't change over the life of the file and they
  9342. * use the bmap result to do IO directly to the drive.
  9343. *
  9344. * the btrfs bmap call would return logical addresses that aren't
  9345. * suitable for IO and they also will change frequently as COW
  9346. * operations happen. So, swapfile + btrfs == corruption.
  9347. *
  9348. * For now we're avoiding this by dropping bmap.
  9349. */
  9350. static const struct address_space_operations btrfs_aops = {
  9351. .readpage = btrfs_readpage,
  9352. .writepage = btrfs_writepage,
  9353. .writepages = btrfs_writepages,
  9354. .readpages = btrfs_readpages,
  9355. .direct_IO = btrfs_direct_IO,
  9356. .invalidatepage = btrfs_invalidatepage,
  9357. .releasepage = btrfs_releasepage,
  9358. .set_page_dirty = btrfs_set_page_dirty,
  9359. .error_remove_page = generic_error_remove_page,
  9360. };
  9361. static const struct address_space_operations btrfs_symlink_aops = {
  9362. .readpage = btrfs_readpage,
  9363. .writepage = btrfs_writepage,
  9364. .invalidatepage = btrfs_invalidatepage,
  9365. .releasepage = btrfs_releasepage,
  9366. };
  9367. static const struct inode_operations btrfs_file_inode_operations = {
  9368. .getattr = btrfs_getattr,
  9369. .setattr = btrfs_setattr,
  9370. .listxattr = btrfs_listxattr,
  9371. .permission = btrfs_permission,
  9372. .fiemap = btrfs_fiemap,
  9373. .get_acl = btrfs_get_acl,
  9374. .set_acl = btrfs_set_acl,
  9375. .update_time = btrfs_update_time,
  9376. };
  9377. static const struct inode_operations btrfs_special_inode_operations = {
  9378. .getattr = btrfs_getattr,
  9379. .setattr = btrfs_setattr,
  9380. .permission = btrfs_permission,
  9381. .listxattr = btrfs_listxattr,
  9382. .get_acl = btrfs_get_acl,
  9383. .set_acl = btrfs_set_acl,
  9384. .update_time = btrfs_update_time,
  9385. };
  9386. static const struct inode_operations btrfs_symlink_inode_operations = {
  9387. .get_link = page_get_link,
  9388. .getattr = btrfs_getattr,
  9389. .setattr = btrfs_setattr,
  9390. .permission = btrfs_permission,
  9391. .listxattr = btrfs_listxattr,
  9392. .update_time = btrfs_update_time,
  9393. };
  9394. const struct dentry_operations btrfs_dentry_operations = {
  9395. .d_delete = btrfs_dentry_delete,
  9396. .d_release = btrfs_dentry_release,
  9397. };