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