inode.c 289 KB

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