inode.c 288 KB

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