inode.c 288 KB

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