inode.c 231 KB

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