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