inode.c 230 KB

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