inode.c 293 KB

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