inode.c 256 KB

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