inode.c 198 KB

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