inode.c 266 KB

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