inode.c 289 KB

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