inode.c 285 KB

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