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