inode.c 259 KB

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