inode.c 230 KB

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