inode.c 286 KB

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