inode.c 204 KB

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