inode.c 208 KB

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