inode.c 231 KB

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