inode.c 252 KB

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