inode.c 286 KB

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