inode.c 195 KB

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