inode.c 291 KB

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