inode.c 231 KB

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