inode.c 288 KB

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