inode.c 207 KB

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