inode.c 214 KB

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