inode.c 230 KB

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