inode.c 208 KB

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