inode.c 231 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755
  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. ret = 0;
  1915. while (1) {
  1916. cond_resched();
  1917. leaf = path->nodes[0];
  1918. slot = path->slots[0];
  1919. if (slot >= btrfs_header_nritems(leaf)) {
  1920. ret = btrfs_next_leaf(root, path);
  1921. if (ret < 0) {
  1922. goto out;
  1923. } else if (ret > 0) {
  1924. ret = 0;
  1925. goto out;
  1926. }
  1927. continue;
  1928. }
  1929. path->slots[0]++;
  1930. btrfs_item_key_to_cpu(leaf, &key, slot);
  1931. if (key.objectid > inum)
  1932. goto out;
  1933. if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
  1934. continue;
  1935. extent = btrfs_item_ptr(leaf, slot,
  1936. struct btrfs_file_extent_item);
  1937. if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
  1938. continue;
  1939. /*
  1940. * 'offset' refers to the exact key.offset,
  1941. * NOT the 'offset' field in btrfs_extent_data_ref, ie.
  1942. * (key.offset - extent_offset).
  1943. */
  1944. if (key.offset != offset)
  1945. continue;
  1946. extent_offset = btrfs_file_extent_offset(leaf, extent);
  1947. num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
  1948. if (extent_offset >= old->extent_offset + old->offset +
  1949. old->len || extent_offset + num_bytes <=
  1950. old->extent_offset + old->offset)
  1951. continue;
  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 btrfs_io_bio *io_bio,
  2496. u64 phy_offset, struct page *page,
  2497. u64 start, u64 end, int mirror)
  2498. {
  2499. size_t offset = start - page_offset(page);
  2500. struct inode *inode = page->mapping->host;
  2501. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2502. char *kaddr;
  2503. struct btrfs_root *root = BTRFS_I(inode)->root;
  2504. u32 csum_expected;
  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. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2521. csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
  2522. kaddr = kmap_atomic(page);
  2523. csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
  2524. btrfs_csum_final(csum, (char *)&csum);
  2525. if (csum != csum_expected)
  2526. goto zeroit;
  2527. kunmap_atomic(kaddr);
  2528. good:
  2529. return 0;
  2530. zeroit:
  2531. if (__ratelimit(&_rs))
  2532. btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
  2533. (unsigned long long)btrfs_ino(page->mapping->host),
  2534. (unsigned long long)start, csum, csum_expected);
  2535. memset(kaddr + offset, 1, end - start + 1);
  2536. flush_dcache_page(page);
  2537. kunmap_atomic(kaddr);
  2538. if (csum_expected == 0)
  2539. return 0;
  2540. return -EIO;
  2541. }
  2542. struct delayed_iput {
  2543. struct list_head list;
  2544. struct inode *inode;
  2545. };
  2546. /* JDM: If this is fs-wide, why can't we add a pointer to
  2547. * btrfs_inode instead and avoid the allocation? */
  2548. void btrfs_add_delayed_iput(struct inode *inode)
  2549. {
  2550. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2551. struct delayed_iput *delayed;
  2552. if (atomic_add_unless(&inode->i_count, -1, 1))
  2553. return;
  2554. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  2555. delayed->inode = inode;
  2556. spin_lock(&fs_info->delayed_iput_lock);
  2557. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  2558. spin_unlock(&fs_info->delayed_iput_lock);
  2559. }
  2560. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  2561. {
  2562. LIST_HEAD(list);
  2563. struct btrfs_fs_info *fs_info = root->fs_info;
  2564. struct delayed_iput *delayed;
  2565. int empty;
  2566. spin_lock(&fs_info->delayed_iput_lock);
  2567. empty = list_empty(&fs_info->delayed_iputs);
  2568. spin_unlock(&fs_info->delayed_iput_lock);
  2569. if (empty)
  2570. return;
  2571. spin_lock(&fs_info->delayed_iput_lock);
  2572. list_splice_init(&fs_info->delayed_iputs, &list);
  2573. spin_unlock(&fs_info->delayed_iput_lock);
  2574. while (!list_empty(&list)) {
  2575. delayed = list_entry(list.next, struct delayed_iput, list);
  2576. list_del(&delayed->list);
  2577. iput(delayed->inode);
  2578. kfree(delayed);
  2579. }
  2580. }
  2581. /*
  2582. * This is called in transaction commit time. If there are no orphan
  2583. * files in the subvolume, it removes orphan item and frees block_rsv
  2584. * structure.
  2585. */
  2586. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  2587. struct btrfs_root *root)
  2588. {
  2589. struct btrfs_block_rsv *block_rsv;
  2590. int ret;
  2591. if (atomic_read(&root->orphan_inodes) ||
  2592. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  2593. return;
  2594. spin_lock(&root->orphan_lock);
  2595. if (atomic_read(&root->orphan_inodes)) {
  2596. spin_unlock(&root->orphan_lock);
  2597. return;
  2598. }
  2599. if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
  2600. spin_unlock(&root->orphan_lock);
  2601. return;
  2602. }
  2603. block_rsv = root->orphan_block_rsv;
  2604. root->orphan_block_rsv = NULL;
  2605. spin_unlock(&root->orphan_lock);
  2606. if (root->orphan_item_inserted &&
  2607. btrfs_root_refs(&root->root_item) > 0) {
  2608. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  2609. root->root_key.objectid);
  2610. BUG_ON(ret);
  2611. root->orphan_item_inserted = 0;
  2612. }
  2613. if (block_rsv) {
  2614. WARN_ON(block_rsv->size > 0);
  2615. btrfs_free_block_rsv(root, block_rsv);
  2616. }
  2617. }
  2618. /*
  2619. * This creates an orphan entry for the given inode in case something goes
  2620. * wrong in the middle of an unlink/truncate.
  2621. *
  2622. * NOTE: caller of this function should reserve 5 units of metadata for
  2623. * this function.
  2624. */
  2625. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  2626. {
  2627. struct btrfs_root *root = BTRFS_I(inode)->root;
  2628. struct btrfs_block_rsv *block_rsv = NULL;
  2629. int reserve = 0;
  2630. int insert = 0;
  2631. int ret;
  2632. if (!root->orphan_block_rsv) {
  2633. block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  2634. if (!block_rsv)
  2635. return -ENOMEM;
  2636. }
  2637. spin_lock(&root->orphan_lock);
  2638. if (!root->orphan_block_rsv) {
  2639. root->orphan_block_rsv = block_rsv;
  2640. } else if (block_rsv) {
  2641. btrfs_free_block_rsv(root, block_rsv);
  2642. block_rsv = NULL;
  2643. }
  2644. if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2645. &BTRFS_I(inode)->runtime_flags)) {
  2646. #if 0
  2647. /*
  2648. * For proper ENOSPC handling, we should do orphan
  2649. * cleanup when mounting. But this introduces backward
  2650. * compatibility issue.
  2651. */
  2652. if (!xchg(&root->orphan_item_inserted, 1))
  2653. insert = 2;
  2654. else
  2655. insert = 1;
  2656. #endif
  2657. insert = 1;
  2658. atomic_inc(&root->orphan_inodes);
  2659. }
  2660. if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2661. &BTRFS_I(inode)->runtime_flags))
  2662. reserve = 1;
  2663. spin_unlock(&root->orphan_lock);
  2664. /* grab metadata reservation from transaction handle */
  2665. if (reserve) {
  2666. ret = btrfs_orphan_reserve_metadata(trans, inode);
  2667. BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
  2668. }
  2669. /* insert an orphan item to track this unlinked/truncated file */
  2670. if (insert >= 1) {
  2671. ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
  2672. if (ret && ret != -EEXIST) {
  2673. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2674. &BTRFS_I(inode)->runtime_flags);
  2675. btrfs_abort_transaction(trans, root, ret);
  2676. return ret;
  2677. }
  2678. ret = 0;
  2679. }
  2680. /* insert an orphan item to track subvolume contains orphan files */
  2681. if (insert >= 2) {
  2682. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  2683. root->root_key.objectid);
  2684. if (ret && ret != -EEXIST) {
  2685. btrfs_abort_transaction(trans, root, ret);
  2686. return ret;
  2687. }
  2688. }
  2689. return 0;
  2690. }
  2691. /*
  2692. * We have done the truncate/delete so we can go ahead and remove the orphan
  2693. * item for this particular inode.
  2694. */
  2695. static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
  2696. struct inode *inode)
  2697. {
  2698. struct btrfs_root *root = BTRFS_I(inode)->root;
  2699. int delete_item = 0;
  2700. int release_rsv = 0;
  2701. int ret = 0;
  2702. spin_lock(&root->orphan_lock);
  2703. if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2704. &BTRFS_I(inode)->runtime_flags))
  2705. delete_item = 1;
  2706. if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2707. &BTRFS_I(inode)->runtime_flags))
  2708. release_rsv = 1;
  2709. spin_unlock(&root->orphan_lock);
  2710. if (trans && delete_item) {
  2711. ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
  2712. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2713. }
  2714. if (release_rsv) {
  2715. btrfs_orphan_release_metadata(inode);
  2716. atomic_dec(&root->orphan_inodes);
  2717. }
  2718. return 0;
  2719. }
  2720. /*
  2721. * this cleans up any orphans that may be left on the list from the last use
  2722. * of this root.
  2723. */
  2724. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2725. {
  2726. struct btrfs_path *path;
  2727. struct extent_buffer *leaf;
  2728. struct btrfs_key key, found_key;
  2729. struct btrfs_trans_handle *trans;
  2730. struct inode *inode;
  2731. u64 last_objectid = 0;
  2732. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2733. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2734. return 0;
  2735. path = btrfs_alloc_path();
  2736. if (!path) {
  2737. ret = -ENOMEM;
  2738. goto out;
  2739. }
  2740. path->reada = -1;
  2741. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2742. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  2743. key.offset = (u64)-1;
  2744. while (1) {
  2745. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2746. if (ret < 0)
  2747. goto out;
  2748. /*
  2749. * if ret == 0 means we found what we were searching for, which
  2750. * is weird, but possible, so only screw with path if we didn't
  2751. * find the key and see if we have stuff that matches
  2752. */
  2753. if (ret > 0) {
  2754. ret = 0;
  2755. if (path->slots[0] == 0)
  2756. break;
  2757. path->slots[0]--;
  2758. }
  2759. /* pull out the item */
  2760. leaf = path->nodes[0];
  2761. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2762. /* make sure the item matches what we want */
  2763. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2764. break;
  2765. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2766. break;
  2767. /* release the path since we're done with it */
  2768. btrfs_release_path(path);
  2769. /*
  2770. * this is where we are basically btrfs_lookup, without the
  2771. * crossing root thing. we store the inode number in the
  2772. * offset of the orphan item.
  2773. */
  2774. if (found_key.offset == last_objectid) {
  2775. btrfs_err(root->fs_info,
  2776. "Error removing orphan entry, stopping orphan cleanup");
  2777. ret = -EINVAL;
  2778. goto out;
  2779. }
  2780. last_objectid = found_key.offset;
  2781. found_key.objectid = found_key.offset;
  2782. found_key.type = BTRFS_INODE_ITEM_KEY;
  2783. found_key.offset = 0;
  2784. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2785. ret = PTR_RET(inode);
  2786. if (ret && ret != -ESTALE)
  2787. goto out;
  2788. if (ret == -ESTALE && root == root->fs_info->tree_root) {
  2789. struct btrfs_root *dead_root;
  2790. struct btrfs_fs_info *fs_info = root->fs_info;
  2791. int is_dead_root = 0;
  2792. /*
  2793. * this is an orphan in the tree root. Currently these
  2794. * could come from 2 sources:
  2795. * a) a snapshot deletion in progress
  2796. * b) a free space cache inode
  2797. * We need to distinguish those two, as the snapshot
  2798. * orphan must not get deleted.
  2799. * find_dead_roots already ran before us, so if this
  2800. * is a snapshot deletion, we should find the root
  2801. * in the dead_roots list
  2802. */
  2803. spin_lock(&fs_info->trans_lock);
  2804. list_for_each_entry(dead_root, &fs_info->dead_roots,
  2805. root_list) {
  2806. if (dead_root->root_key.objectid ==
  2807. found_key.objectid) {
  2808. is_dead_root = 1;
  2809. break;
  2810. }
  2811. }
  2812. spin_unlock(&fs_info->trans_lock);
  2813. if (is_dead_root) {
  2814. /* prevent this orphan from being found again */
  2815. key.offset = found_key.objectid - 1;
  2816. continue;
  2817. }
  2818. }
  2819. /*
  2820. * Inode is already gone but the orphan item is still there,
  2821. * kill the orphan item.
  2822. */
  2823. if (ret == -ESTALE) {
  2824. trans = btrfs_start_transaction(root, 1);
  2825. if (IS_ERR(trans)) {
  2826. ret = PTR_ERR(trans);
  2827. goto out;
  2828. }
  2829. btrfs_debug(root->fs_info, "auto deleting %Lu",
  2830. found_key.objectid);
  2831. ret = btrfs_del_orphan_item(trans, root,
  2832. found_key.objectid);
  2833. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2834. btrfs_end_transaction(trans, root);
  2835. continue;
  2836. }
  2837. /*
  2838. * add this inode to the orphan list so btrfs_orphan_del does
  2839. * the proper thing when we hit it
  2840. */
  2841. set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2842. &BTRFS_I(inode)->runtime_flags);
  2843. atomic_inc(&root->orphan_inodes);
  2844. /* if we have links, this was a truncate, lets do that */
  2845. if (inode->i_nlink) {
  2846. if (!S_ISREG(inode->i_mode)) {
  2847. WARN_ON(1);
  2848. iput(inode);
  2849. continue;
  2850. }
  2851. nr_truncate++;
  2852. /* 1 for the orphan item deletion. */
  2853. trans = btrfs_start_transaction(root, 1);
  2854. if (IS_ERR(trans)) {
  2855. iput(inode);
  2856. ret = PTR_ERR(trans);
  2857. goto out;
  2858. }
  2859. ret = btrfs_orphan_add(trans, inode);
  2860. btrfs_end_transaction(trans, root);
  2861. if (ret) {
  2862. iput(inode);
  2863. goto out;
  2864. }
  2865. ret = btrfs_truncate(inode);
  2866. if (ret)
  2867. btrfs_orphan_del(NULL, inode);
  2868. } else {
  2869. nr_unlink++;
  2870. }
  2871. /* this will do delete_inode and everything for us */
  2872. iput(inode);
  2873. if (ret)
  2874. goto out;
  2875. }
  2876. /* release the path since we're done with it */
  2877. btrfs_release_path(path);
  2878. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2879. if (root->orphan_block_rsv)
  2880. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2881. (u64)-1);
  2882. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2883. trans = btrfs_join_transaction(root);
  2884. if (!IS_ERR(trans))
  2885. btrfs_end_transaction(trans, root);
  2886. }
  2887. if (nr_unlink)
  2888. btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
  2889. if (nr_truncate)
  2890. btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
  2891. out:
  2892. if (ret)
  2893. btrfs_crit(root->fs_info,
  2894. "could not do orphan cleanup %d", ret);
  2895. btrfs_free_path(path);
  2896. return ret;
  2897. }
  2898. /*
  2899. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2900. * don't find any xattrs, we know there can't be any acls.
  2901. *
  2902. * slot is the slot the inode is in, objectid is the objectid of the inode
  2903. */
  2904. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2905. int slot, u64 objectid)
  2906. {
  2907. u32 nritems = btrfs_header_nritems(leaf);
  2908. struct btrfs_key found_key;
  2909. static u64 xattr_access = 0;
  2910. static u64 xattr_default = 0;
  2911. int scanned = 0;
  2912. if (!xattr_access) {
  2913. xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
  2914. strlen(POSIX_ACL_XATTR_ACCESS));
  2915. xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
  2916. strlen(POSIX_ACL_XATTR_DEFAULT));
  2917. }
  2918. slot++;
  2919. while (slot < nritems) {
  2920. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2921. /* we found a different objectid, there must not be acls */
  2922. if (found_key.objectid != objectid)
  2923. return 0;
  2924. /* we found an xattr, assume we've got an acl */
  2925. if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
  2926. if (found_key.offset == xattr_access ||
  2927. found_key.offset == xattr_default)
  2928. return 1;
  2929. }
  2930. /*
  2931. * we found a key greater than an xattr key, there can't
  2932. * be any acls later on
  2933. */
  2934. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2935. return 0;
  2936. slot++;
  2937. scanned++;
  2938. /*
  2939. * it goes inode, inode backrefs, xattrs, extents,
  2940. * so if there are a ton of hard links to an inode there can
  2941. * be a lot of backrefs. Don't waste time searching too hard,
  2942. * this is just an optimization
  2943. */
  2944. if (scanned >= 8)
  2945. break;
  2946. }
  2947. /* we hit the end of the leaf before we found an xattr or
  2948. * something larger than an xattr. We have to assume the inode
  2949. * has acls
  2950. */
  2951. return 1;
  2952. }
  2953. /*
  2954. * read an inode from the btree into the in-memory inode
  2955. */
  2956. static void btrfs_read_locked_inode(struct inode *inode)
  2957. {
  2958. struct btrfs_path *path;
  2959. struct extent_buffer *leaf;
  2960. struct btrfs_inode_item *inode_item;
  2961. struct btrfs_timespec *tspec;
  2962. struct btrfs_root *root = BTRFS_I(inode)->root;
  2963. struct btrfs_key location;
  2964. int maybe_acls;
  2965. u32 rdev;
  2966. int ret;
  2967. bool filled = false;
  2968. ret = btrfs_fill_inode(inode, &rdev);
  2969. if (!ret)
  2970. filled = true;
  2971. path = btrfs_alloc_path();
  2972. if (!path)
  2973. goto make_bad;
  2974. path->leave_spinning = 1;
  2975. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2976. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2977. if (ret)
  2978. goto make_bad;
  2979. leaf = path->nodes[0];
  2980. if (filled)
  2981. goto cache_acl;
  2982. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2983. struct btrfs_inode_item);
  2984. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2985. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  2986. i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
  2987. i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
  2988. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2989. tspec = btrfs_inode_atime(inode_item);
  2990. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2991. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2992. tspec = btrfs_inode_mtime(inode_item);
  2993. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2994. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2995. tspec = btrfs_inode_ctime(inode_item);
  2996. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2997. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2998. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2999. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  3000. BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
  3001. /*
  3002. * If we were modified in the current generation and evicted from memory
  3003. * and then re-read we need to do a full sync since we don't have any
  3004. * idea about which extents were modified before we were evicted from
  3005. * cache.
  3006. */
  3007. if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
  3008. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3009. &BTRFS_I(inode)->runtime_flags);
  3010. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  3011. inode->i_generation = BTRFS_I(inode)->generation;
  3012. inode->i_rdev = 0;
  3013. rdev = btrfs_inode_rdev(leaf, inode_item);
  3014. BTRFS_I(inode)->index_cnt = (u64)-1;
  3015. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  3016. cache_acl:
  3017. /*
  3018. * try to precache a NULL acl entry for files that don't have
  3019. * any xattrs or acls
  3020. */
  3021. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  3022. btrfs_ino(inode));
  3023. if (!maybe_acls)
  3024. cache_no_acl(inode);
  3025. btrfs_free_path(path);
  3026. switch (inode->i_mode & S_IFMT) {
  3027. case S_IFREG:
  3028. inode->i_mapping->a_ops = &btrfs_aops;
  3029. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3030. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3031. inode->i_fop = &btrfs_file_operations;
  3032. inode->i_op = &btrfs_file_inode_operations;
  3033. break;
  3034. case S_IFDIR:
  3035. inode->i_fop = &btrfs_dir_file_operations;
  3036. if (root == root->fs_info->tree_root)
  3037. inode->i_op = &btrfs_dir_ro_inode_operations;
  3038. else
  3039. inode->i_op = &btrfs_dir_inode_operations;
  3040. break;
  3041. case S_IFLNK:
  3042. inode->i_op = &btrfs_symlink_inode_operations;
  3043. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  3044. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3045. break;
  3046. default:
  3047. inode->i_op = &btrfs_special_inode_operations;
  3048. init_special_inode(inode, inode->i_mode, rdev);
  3049. break;
  3050. }
  3051. btrfs_update_iflags(inode);
  3052. return;
  3053. make_bad:
  3054. btrfs_free_path(path);
  3055. make_bad_inode(inode);
  3056. }
  3057. /*
  3058. * given a leaf and an inode, copy the inode fields into the leaf
  3059. */
  3060. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3061. struct extent_buffer *leaf,
  3062. struct btrfs_inode_item *item,
  3063. struct inode *inode)
  3064. {
  3065. struct btrfs_map_token token;
  3066. btrfs_init_map_token(&token);
  3067. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3068. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3069. btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
  3070. &token);
  3071. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3072. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3073. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  3074. inode->i_atime.tv_sec, &token);
  3075. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  3076. inode->i_atime.tv_nsec, &token);
  3077. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  3078. inode->i_mtime.tv_sec, &token);
  3079. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  3080. inode->i_mtime.tv_nsec, &token);
  3081. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  3082. inode->i_ctime.tv_sec, &token);
  3083. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  3084. inode->i_ctime.tv_nsec, &token);
  3085. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3086. &token);
  3087. btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
  3088. &token);
  3089. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3090. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3091. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3092. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3093. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3094. }
  3095. /*
  3096. * copy everything in the in-memory inode into the btree.
  3097. */
  3098. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  3099. struct btrfs_root *root, struct inode *inode)
  3100. {
  3101. struct btrfs_inode_item *inode_item;
  3102. struct btrfs_path *path;
  3103. struct extent_buffer *leaf;
  3104. int ret;
  3105. path = btrfs_alloc_path();
  3106. if (!path)
  3107. return -ENOMEM;
  3108. path->leave_spinning = 1;
  3109. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  3110. 1);
  3111. if (ret) {
  3112. if (ret > 0)
  3113. ret = -ENOENT;
  3114. goto failed;
  3115. }
  3116. btrfs_unlock_up_safe(path, 1);
  3117. leaf = path->nodes[0];
  3118. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3119. struct btrfs_inode_item);
  3120. fill_inode_item(trans, leaf, inode_item, inode);
  3121. btrfs_mark_buffer_dirty(leaf);
  3122. btrfs_set_inode_last_trans(trans, inode);
  3123. ret = 0;
  3124. failed:
  3125. btrfs_free_path(path);
  3126. return ret;
  3127. }
  3128. /*
  3129. * copy everything in the in-memory inode into the btree.
  3130. */
  3131. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  3132. struct btrfs_root *root, struct inode *inode)
  3133. {
  3134. int ret;
  3135. /*
  3136. * If the inode is a free space inode, we can deadlock during commit
  3137. * if we put it into the delayed code.
  3138. *
  3139. * The data relocation inode should also be directly updated
  3140. * without delay
  3141. */
  3142. if (!btrfs_is_free_space_inode(inode)
  3143. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
  3144. btrfs_update_root_times(trans, root);
  3145. ret = btrfs_delayed_update_inode(trans, root, inode);
  3146. if (!ret)
  3147. btrfs_set_inode_last_trans(trans, inode);
  3148. return ret;
  3149. }
  3150. return btrfs_update_inode_item(trans, root, inode);
  3151. }
  3152. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  3153. struct btrfs_root *root,
  3154. struct inode *inode)
  3155. {
  3156. int ret;
  3157. ret = btrfs_update_inode(trans, root, inode);
  3158. if (ret == -ENOSPC)
  3159. return btrfs_update_inode_item(trans, root, inode);
  3160. return ret;
  3161. }
  3162. /*
  3163. * unlink helper that gets used here in inode.c and in the tree logging
  3164. * recovery code. It remove a link in a directory with a given name, and
  3165. * also drops the back refs in the inode to the directory
  3166. */
  3167. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3168. struct btrfs_root *root,
  3169. struct inode *dir, struct inode *inode,
  3170. const char *name, int name_len)
  3171. {
  3172. struct btrfs_path *path;
  3173. int ret = 0;
  3174. struct extent_buffer *leaf;
  3175. struct btrfs_dir_item *di;
  3176. struct btrfs_key key;
  3177. u64 index;
  3178. u64 ino = btrfs_ino(inode);
  3179. u64 dir_ino = btrfs_ino(dir);
  3180. path = btrfs_alloc_path();
  3181. if (!path) {
  3182. ret = -ENOMEM;
  3183. goto out;
  3184. }
  3185. path->leave_spinning = 1;
  3186. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3187. name, name_len, -1);
  3188. if (IS_ERR(di)) {
  3189. ret = PTR_ERR(di);
  3190. goto err;
  3191. }
  3192. if (!di) {
  3193. ret = -ENOENT;
  3194. goto err;
  3195. }
  3196. leaf = path->nodes[0];
  3197. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3198. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3199. if (ret)
  3200. goto err;
  3201. btrfs_release_path(path);
  3202. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  3203. dir_ino, &index);
  3204. if (ret) {
  3205. btrfs_info(root->fs_info,
  3206. "failed to delete reference to %.*s, inode %llu parent %llu",
  3207. name_len, name,
  3208. (unsigned long long)ino, (unsigned long long)dir_ino);
  3209. btrfs_abort_transaction(trans, root, ret);
  3210. goto err;
  3211. }
  3212. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3213. if (ret) {
  3214. btrfs_abort_transaction(trans, root, ret);
  3215. goto err;
  3216. }
  3217. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  3218. inode, dir_ino);
  3219. if (ret != 0 && ret != -ENOENT) {
  3220. btrfs_abort_transaction(trans, root, ret);
  3221. goto err;
  3222. }
  3223. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  3224. dir, index);
  3225. if (ret == -ENOENT)
  3226. ret = 0;
  3227. else if (ret)
  3228. btrfs_abort_transaction(trans, root, ret);
  3229. err:
  3230. btrfs_free_path(path);
  3231. if (ret)
  3232. goto out;
  3233. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3234. inode_inc_iversion(inode);
  3235. inode_inc_iversion(dir);
  3236. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3237. ret = btrfs_update_inode(trans, root, dir);
  3238. out:
  3239. return ret;
  3240. }
  3241. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3242. struct btrfs_root *root,
  3243. struct inode *dir, struct inode *inode,
  3244. const char *name, int name_len)
  3245. {
  3246. int ret;
  3247. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  3248. if (!ret) {
  3249. btrfs_drop_nlink(inode);
  3250. ret = btrfs_update_inode(trans, root, inode);
  3251. }
  3252. return ret;
  3253. }
  3254. /*
  3255. * helper to start transaction for unlink and rmdir.
  3256. *
  3257. * unlink and rmdir are special in btrfs, they do not always free space, so
  3258. * if we cannot make our reservations the normal way try and see if there is
  3259. * plenty of slack room in the global reserve to migrate, otherwise we cannot
  3260. * allow the unlink to occur.
  3261. */
  3262. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
  3263. {
  3264. struct btrfs_trans_handle *trans;
  3265. struct btrfs_root *root = BTRFS_I(dir)->root;
  3266. int ret;
  3267. /*
  3268. * 1 for the possible orphan item
  3269. * 1 for the dir item
  3270. * 1 for the dir index
  3271. * 1 for the inode ref
  3272. * 1 for the inode
  3273. */
  3274. trans = btrfs_start_transaction(root, 5);
  3275. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  3276. return trans;
  3277. if (PTR_ERR(trans) == -ENOSPC) {
  3278. u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
  3279. trans = btrfs_start_transaction(root, 0);
  3280. if (IS_ERR(trans))
  3281. return trans;
  3282. ret = btrfs_cond_migrate_bytes(root->fs_info,
  3283. &root->fs_info->trans_block_rsv,
  3284. num_bytes, 5);
  3285. if (ret) {
  3286. btrfs_end_transaction(trans, root);
  3287. return ERR_PTR(ret);
  3288. }
  3289. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3290. trans->bytes_reserved = num_bytes;
  3291. }
  3292. return trans;
  3293. }
  3294. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  3295. {
  3296. struct btrfs_root *root = BTRFS_I(dir)->root;
  3297. struct btrfs_trans_handle *trans;
  3298. struct inode *inode = dentry->d_inode;
  3299. int ret;
  3300. trans = __unlink_start_trans(dir);
  3301. if (IS_ERR(trans))
  3302. return PTR_ERR(trans);
  3303. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  3304. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  3305. dentry->d_name.name, dentry->d_name.len);
  3306. if (ret)
  3307. goto out;
  3308. if (inode->i_nlink == 0) {
  3309. ret = btrfs_orphan_add(trans, inode);
  3310. if (ret)
  3311. goto out;
  3312. }
  3313. out:
  3314. btrfs_end_transaction(trans, root);
  3315. btrfs_btree_balance_dirty(root);
  3316. return ret;
  3317. }
  3318. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  3319. struct btrfs_root *root,
  3320. struct inode *dir, u64 objectid,
  3321. const char *name, int name_len)
  3322. {
  3323. struct btrfs_path *path;
  3324. struct extent_buffer *leaf;
  3325. struct btrfs_dir_item *di;
  3326. struct btrfs_key key;
  3327. u64 index;
  3328. int ret;
  3329. u64 dir_ino = btrfs_ino(dir);
  3330. path = btrfs_alloc_path();
  3331. if (!path)
  3332. return -ENOMEM;
  3333. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3334. name, name_len, -1);
  3335. if (IS_ERR_OR_NULL(di)) {
  3336. if (!di)
  3337. ret = -ENOENT;
  3338. else
  3339. ret = PTR_ERR(di);
  3340. goto out;
  3341. }
  3342. leaf = path->nodes[0];
  3343. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3344. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3345. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3346. if (ret) {
  3347. btrfs_abort_transaction(trans, root, ret);
  3348. goto out;
  3349. }
  3350. btrfs_release_path(path);
  3351. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  3352. objectid, root->root_key.objectid,
  3353. dir_ino, &index, name, name_len);
  3354. if (ret < 0) {
  3355. if (ret != -ENOENT) {
  3356. btrfs_abort_transaction(trans, root, ret);
  3357. goto out;
  3358. }
  3359. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3360. name, name_len);
  3361. if (IS_ERR_OR_NULL(di)) {
  3362. if (!di)
  3363. ret = -ENOENT;
  3364. else
  3365. ret = PTR_ERR(di);
  3366. btrfs_abort_transaction(trans, root, ret);
  3367. goto out;
  3368. }
  3369. leaf = path->nodes[0];
  3370. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3371. btrfs_release_path(path);
  3372. index = key.offset;
  3373. }
  3374. btrfs_release_path(path);
  3375. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  3376. if (ret) {
  3377. btrfs_abort_transaction(trans, root, ret);
  3378. goto out;
  3379. }
  3380. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3381. inode_inc_iversion(dir);
  3382. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  3383. ret = btrfs_update_inode_fallback(trans, root, dir);
  3384. if (ret)
  3385. btrfs_abort_transaction(trans, root, ret);
  3386. out:
  3387. btrfs_free_path(path);
  3388. return ret;
  3389. }
  3390. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3391. {
  3392. struct inode *inode = dentry->d_inode;
  3393. int err = 0;
  3394. struct btrfs_root *root = BTRFS_I(dir)->root;
  3395. struct btrfs_trans_handle *trans;
  3396. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3397. return -ENOTEMPTY;
  3398. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  3399. return -EPERM;
  3400. trans = __unlink_start_trans(dir);
  3401. if (IS_ERR(trans))
  3402. return PTR_ERR(trans);
  3403. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3404. err = btrfs_unlink_subvol(trans, root, dir,
  3405. BTRFS_I(inode)->location.objectid,
  3406. dentry->d_name.name,
  3407. dentry->d_name.len);
  3408. goto out;
  3409. }
  3410. err = btrfs_orphan_add(trans, inode);
  3411. if (err)
  3412. goto out;
  3413. /* now the directory is empty */
  3414. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  3415. dentry->d_name.name, dentry->d_name.len);
  3416. if (!err)
  3417. btrfs_i_size_write(inode, 0);
  3418. out:
  3419. btrfs_end_transaction(trans, root);
  3420. btrfs_btree_balance_dirty(root);
  3421. return err;
  3422. }
  3423. /*
  3424. * this can truncate away extent items, csum items and directory items.
  3425. * It starts at a high offset and removes keys until it can't find
  3426. * any higher than new_size
  3427. *
  3428. * csum items that cross the new i_size are truncated to the new size
  3429. * as well.
  3430. *
  3431. * min_type is the minimum key type to truncate down to. If set to 0, this
  3432. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3433. */
  3434. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3435. struct btrfs_root *root,
  3436. struct inode *inode,
  3437. u64 new_size, u32 min_type)
  3438. {
  3439. struct btrfs_path *path;
  3440. struct extent_buffer *leaf;
  3441. struct btrfs_file_extent_item *fi;
  3442. struct btrfs_key key;
  3443. struct btrfs_key found_key;
  3444. u64 extent_start = 0;
  3445. u64 extent_num_bytes = 0;
  3446. u64 extent_offset = 0;
  3447. u64 item_end = 0;
  3448. u32 found_type = (u8)-1;
  3449. int found_extent;
  3450. int del_item;
  3451. int pending_del_nr = 0;
  3452. int pending_del_slot = 0;
  3453. int extent_type = -1;
  3454. int ret;
  3455. int err = 0;
  3456. u64 ino = btrfs_ino(inode);
  3457. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  3458. path = btrfs_alloc_path();
  3459. if (!path)
  3460. return -ENOMEM;
  3461. path->reada = -1;
  3462. /*
  3463. * We want to drop from the next block forward in case this new size is
  3464. * not block aligned since we will be keeping the last block of the
  3465. * extent just the way it is.
  3466. */
  3467. if (root->ref_cows || root == root->fs_info->tree_root)
  3468. btrfs_drop_extent_cache(inode, ALIGN(new_size,
  3469. root->sectorsize), (u64)-1, 0);
  3470. /*
  3471. * This function is also used to drop the items in the log tree before
  3472. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  3473. * it is used to drop the loged items. So we shouldn't kill the delayed
  3474. * items.
  3475. */
  3476. if (min_type == 0 && root == BTRFS_I(inode)->root)
  3477. btrfs_kill_delayed_inode_items(inode);
  3478. key.objectid = ino;
  3479. key.offset = (u64)-1;
  3480. key.type = (u8)-1;
  3481. search_again:
  3482. path->leave_spinning = 1;
  3483. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3484. if (ret < 0) {
  3485. err = ret;
  3486. goto out;
  3487. }
  3488. if (ret > 0) {
  3489. /* there are no items in the tree for us to truncate, we're
  3490. * done
  3491. */
  3492. if (path->slots[0] == 0)
  3493. goto out;
  3494. path->slots[0]--;
  3495. }
  3496. while (1) {
  3497. fi = NULL;
  3498. leaf = path->nodes[0];
  3499. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3500. found_type = btrfs_key_type(&found_key);
  3501. if (found_key.objectid != ino)
  3502. break;
  3503. if (found_type < min_type)
  3504. break;
  3505. item_end = found_key.offset;
  3506. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  3507. fi = btrfs_item_ptr(leaf, path->slots[0],
  3508. struct btrfs_file_extent_item);
  3509. extent_type = btrfs_file_extent_type(leaf, fi);
  3510. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3511. item_end +=
  3512. btrfs_file_extent_num_bytes(leaf, fi);
  3513. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3514. item_end += btrfs_file_extent_inline_len(leaf,
  3515. fi);
  3516. }
  3517. item_end--;
  3518. }
  3519. if (found_type > min_type) {
  3520. del_item = 1;
  3521. } else {
  3522. if (item_end < new_size)
  3523. break;
  3524. if (found_key.offset >= new_size)
  3525. del_item = 1;
  3526. else
  3527. del_item = 0;
  3528. }
  3529. found_extent = 0;
  3530. /* FIXME, shrink the extent if the ref count is only 1 */
  3531. if (found_type != BTRFS_EXTENT_DATA_KEY)
  3532. goto delete;
  3533. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3534. u64 num_dec;
  3535. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  3536. if (!del_item) {
  3537. u64 orig_num_bytes =
  3538. btrfs_file_extent_num_bytes(leaf, fi);
  3539. extent_num_bytes = ALIGN(new_size -
  3540. found_key.offset,
  3541. root->sectorsize);
  3542. btrfs_set_file_extent_num_bytes(leaf, fi,
  3543. extent_num_bytes);
  3544. num_dec = (orig_num_bytes -
  3545. extent_num_bytes);
  3546. if (root->ref_cows && extent_start != 0)
  3547. inode_sub_bytes(inode, num_dec);
  3548. btrfs_mark_buffer_dirty(leaf);
  3549. } else {
  3550. extent_num_bytes =
  3551. btrfs_file_extent_disk_num_bytes(leaf,
  3552. fi);
  3553. extent_offset = found_key.offset -
  3554. btrfs_file_extent_offset(leaf, fi);
  3555. /* FIXME blocksize != 4096 */
  3556. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  3557. if (extent_start != 0) {
  3558. found_extent = 1;
  3559. if (root->ref_cows)
  3560. inode_sub_bytes(inode, num_dec);
  3561. }
  3562. }
  3563. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3564. /*
  3565. * we can't truncate inline items that have had
  3566. * special encodings
  3567. */
  3568. if (!del_item &&
  3569. btrfs_file_extent_compression(leaf, fi) == 0 &&
  3570. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  3571. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  3572. u32 size = new_size - found_key.offset;
  3573. if (root->ref_cows) {
  3574. inode_sub_bytes(inode, item_end + 1 -
  3575. new_size);
  3576. }
  3577. size =
  3578. btrfs_file_extent_calc_inline_size(size);
  3579. btrfs_truncate_item(root, path, size, 1);
  3580. } else if (root->ref_cows) {
  3581. inode_sub_bytes(inode, item_end + 1 -
  3582. found_key.offset);
  3583. }
  3584. }
  3585. delete:
  3586. if (del_item) {
  3587. if (!pending_del_nr) {
  3588. /* no pending yet, add ourselves */
  3589. pending_del_slot = path->slots[0];
  3590. pending_del_nr = 1;
  3591. } else if (pending_del_nr &&
  3592. path->slots[0] + 1 == pending_del_slot) {
  3593. /* hop on the pending chunk */
  3594. pending_del_nr++;
  3595. pending_del_slot = path->slots[0];
  3596. } else {
  3597. BUG();
  3598. }
  3599. } else {
  3600. break;
  3601. }
  3602. if (found_extent && (root->ref_cows ||
  3603. root == root->fs_info->tree_root)) {
  3604. btrfs_set_path_blocking(path);
  3605. ret = btrfs_free_extent(trans, root, extent_start,
  3606. extent_num_bytes, 0,
  3607. btrfs_header_owner(leaf),
  3608. ino, extent_offset, 0);
  3609. BUG_ON(ret);
  3610. }
  3611. if (found_type == BTRFS_INODE_ITEM_KEY)
  3612. break;
  3613. if (path->slots[0] == 0 ||
  3614. path->slots[0] != pending_del_slot) {
  3615. if (pending_del_nr) {
  3616. ret = btrfs_del_items(trans, root, path,
  3617. pending_del_slot,
  3618. pending_del_nr);
  3619. if (ret) {
  3620. btrfs_abort_transaction(trans,
  3621. root, ret);
  3622. goto error;
  3623. }
  3624. pending_del_nr = 0;
  3625. }
  3626. btrfs_release_path(path);
  3627. goto search_again;
  3628. } else {
  3629. path->slots[0]--;
  3630. }
  3631. }
  3632. out:
  3633. if (pending_del_nr) {
  3634. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3635. pending_del_nr);
  3636. if (ret)
  3637. btrfs_abort_transaction(trans, root, ret);
  3638. }
  3639. error:
  3640. btrfs_free_path(path);
  3641. return err;
  3642. }
  3643. /*
  3644. * btrfs_truncate_page - read, zero a chunk and write a page
  3645. * @inode - inode that we're zeroing
  3646. * @from - the offset to start zeroing
  3647. * @len - the length to zero, 0 to zero the entire range respective to the
  3648. * offset
  3649. * @front - zero up to the offset instead of from the offset on
  3650. *
  3651. * This will find the page for the "from" offset and cow the page and zero the
  3652. * part we want to zero. This is used with truncate and hole punching.
  3653. */
  3654. int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
  3655. int front)
  3656. {
  3657. struct address_space *mapping = inode->i_mapping;
  3658. struct btrfs_root *root = BTRFS_I(inode)->root;
  3659. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3660. struct btrfs_ordered_extent *ordered;
  3661. struct extent_state *cached_state = NULL;
  3662. char *kaddr;
  3663. u32 blocksize = root->sectorsize;
  3664. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3665. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3666. struct page *page;
  3667. gfp_t mask = btrfs_alloc_write_mask(mapping);
  3668. int ret = 0;
  3669. u64 page_start;
  3670. u64 page_end;
  3671. if ((offset & (blocksize - 1)) == 0 &&
  3672. (!len || ((len & (blocksize - 1)) == 0)))
  3673. goto out;
  3674. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3675. if (ret)
  3676. goto out;
  3677. again:
  3678. page = find_or_create_page(mapping, index, mask);
  3679. if (!page) {
  3680. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3681. ret = -ENOMEM;
  3682. goto out;
  3683. }
  3684. page_start = page_offset(page);
  3685. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3686. if (!PageUptodate(page)) {
  3687. ret = btrfs_readpage(NULL, page);
  3688. lock_page(page);
  3689. if (page->mapping != mapping) {
  3690. unlock_page(page);
  3691. page_cache_release(page);
  3692. goto again;
  3693. }
  3694. if (!PageUptodate(page)) {
  3695. ret = -EIO;
  3696. goto out_unlock;
  3697. }
  3698. }
  3699. wait_on_page_writeback(page);
  3700. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  3701. set_page_extent_mapped(page);
  3702. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3703. if (ordered) {
  3704. unlock_extent_cached(io_tree, page_start, page_end,
  3705. &cached_state, GFP_NOFS);
  3706. unlock_page(page);
  3707. page_cache_release(page);
  3708. btrfs_start_ordered_extent(inode, ordered, 1);
  3709. btrfs_put_ordered_extent(ordered);
  3710. goto again;
  3711. }
  3712. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3713. EXTENT_DIRTY | EXTENT_DELALLOC |
  3714. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  3715. 0, 0, &cached_state, GFP_NOFS);
  3716. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3717. &cached_state);
  3718. if (ret) {
  3719. unlock_extent_cached(io_tree, page_start, page_end,
  3720. &cached_state, GFP_NOFS);
  3721. goto out_unlock;
  3722. }
  3723. if (offset != PAGE_CACHE_SIZE) {
  3724. if (!len)
  3725. len = PAGE_CACHE_SIZE - offset;
  3726. kaddr = kmap(page);
  3727. if (front)
  3728. memset(kaddr, 0, offset);
  3729. else
  3730. memset(kaddr + offset, 0, len);
  3731. flush_dcache_page(page);
  3732. kunmap(page);
  3733. }
  3734. ClearPageChecked(page);
  3735. set_page_dirty(page);
  3736. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3737. GFP_NOFS);
  3738. out_unlock:
  3739. if (ret)
  3740. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3741. unlock_page(page);
  3742. page_cache_release(page);
  3743. out:
  3744. return ret;
  3745. }
  3746. /*
  3747. * This function puts in dummy file extents for the area we're creating a hole
  3748. * for. So if we are truncating this file to a larger size we need to insert
  3749. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3750. * the range between oldsize and size
  3751. */
  3752. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3753. {
  3754. struct btrfs_trans_handle *trans;
  3755. struct btrfs_root *root = BTRFS_I(inode)->root;
  3756. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3757. struct extent_map *em = NULL;
  3758. struct extent_state *cached_state = NULL;
  3759. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3760. u64 hole_start = ALIGN(oldsize, root->sectorsize);
  3761. u64 block_end = ALIGN(size, root->sectorsize);
  3762. u64 last_byte;
  3763. u64 cur_offset;
  3764. u64 hole_size;
  3765. int err = 0;
  3766. /*
  3767. * If our size started in the middle of a page we need to zero out the
  3768. * rest of the page before we expand the i_size, otherwise we could
  3769. * expose stale data.
  3770. */
  3771. err = btrfs_truncate_page(inode, oldsize, 0, 0);
  3772. if (err)
  3773. return err;
  3774. if (size <= hole_start)
  3775. return 0;
  3776. while (1) {
  3777. struct btrfs_ordered_extent *ordered;
  3778. btrfs_wait_ordered_range(inode, hole_start,
  3779. block_end - hole_start);
  3780. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3781. &cached_state);
  3782. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3783. if (!ordered)
  3784. break;
  3785. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3786. &cached_state, GFP_NOFS);
  3787. btrfs_put_ordered_extent(ordered);
  3788. }
  3789. cur_offset = hole_start;
  3790. while (1) {
  3791. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3792. block_end - cur_offset, 0);
  3793. if (IS_ERR(em)) {
  3794. err = PTR_ERR(em);
  3795. em = NULL;
  3796. break;
  3797. }
  3798. last_byte = min(extent_map_end(em), block_end);
  3799. last_byte = ALIGN(last_byte , root->sectorsize);
  3800. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3801. struct extent_map *hole_em;
  3802. hole_size = last_byte - cur_offset;
  3803. trans = btrfs_start_transaction(root, 3);
  3804. if (IS_ERR(trans)) {
  3805. err = PTR_ERR(trans);
  3806. break;
  3807. }
  3808. err = btrfs_drop_extents(trans, root, inode,
  3809. cur_offset,
  3810. cur_offset + hole_size, 1);
  3811. if (err) {
  3812. btrfs_abort_transaction(trans, root, err);
  3813. btrfs_end_transaction(trans, root);
  3814. break;
  3815. }
  3816. err = btrfs_insert_file_extent(trans, root,
  3817. btrfs_ino(inode), cur_offset, 0,
  3818. 0, hole_size, 0, hole_size,
  3819. 0, 0, 0);
  3820. if (err) {
  3821. btrfs_abort_transaction(trans, root, err);
  3822. btrfs_end_transaction(trans, root);
  3823. break;
  3824. }
  3825. btrfs_drop_extent_cache(inode, cur_offset,
  3826. cur_offset + hole_size - 1, 0);
  3827. hole_em = alloc_extent_map();
  3828. if (!hole_em) {
  3829. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3830. &BTRFS_I(inode)->runtime_flags);
  3831. goto next;
  3832. }
  3833. hole_em->start = cur_offset;
  3834. hole_em->len = hole_size;
  3835. hole_em->orig_start = cur_offset;
  3836. hole_em->block_start = EXTENT_MAP_HOLE;
  3837. hole_em->block_len = 0;
  3838. hole_em->orig_block_len = 0;
  3839. hole_em->ram_bytes = hole_size;
  3840. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  3841. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  3842. hole_em->generation = trans->transid;
  3843. while (1) {
  3844. write_lock(&em_tree->lock);
  3845. err = add_extent_mapping(em_tree, hole_em, 1);
  3846. write_unlock(&em_tree->lock);
  3847. if (err != -EEXIST)
  3848. break;
  3849. btrfs_drop_extent_cache(inode, cur_offset,
  3850. cur_offset +
  3851. hole_size - 1, 0);
  3852. }
  3853. free_extent_map(hole_em);
  3854. next:
  3855. btrfs_update_inode(trans, root, inode);
  3856. btrfs_end_transaction(trans, root);
  3857. }
  3858. free_extent_map(em);
  3859. em = NULL;
  3860. cur_offset = last_byte;
  3861. if (cur_offset >= block_end)
  3862. break;
  3863. }
  3864. free_extent_map(em);
  3865. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3866. GFP_NOFS);
  3867. return err;
  3868. }
  3869. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  3870. {
  3871. struct btrfs_root *root = BTRFS_I(inode)->root;
  3872. struct btrfs_trans_handle *trans;
  3873. loff_t oldsize = i_size_read(inode);
  3874. loff_t newsize = attr->ia_size;
  3875. int mask = attr->ia_valid;
  3876. int ret;
  3877. /*
  3878. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  3879. * special case where we need to update the times despite not having
  3880. * these flags set. For all other operations the VFS set these flags
  3881. * explicitly if it wants a timestamp update.
  3882. */
  3883. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
  3884. inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
  3885. if (newsize > oldsize) {
  3886. truncate_pagecache(inode, oldsize, newsize);
  3887. ret = btrfs_cont_expand(inode, oldsize, newsize);
  3888. if (ret)
  3889. return ret;
  3890. trans = btrfs_start_transaction(root, 1);
  3891. if (IS_ERR(trans))
  3892. return PTR_ERR(trans);
  3893. i_size_write(inode, newsize);
  3894. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  3895. ret = btrfs_update_inode(trans, root, inode);
  3896. btrfs_end_transaction(trans, root);
  3897. } else {
  3898. /*
  3899. * We're truncating a file that used to have good data down to
  3900. * zero. Make sure it gets into the ordered flush list so that
  3901. * any new writes get down to disk quickly.
  3902. */
  3903. if (newsize == 0)
  3904. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  3905. &BTRFS_I(inode)->runtime_flags);
  3906. /*
  3907. * 1 for the orphan item we're going to add
  3908. * 1 for the orphan item deletion.
  3909. */
  3910. trans = btrfs_start_transaction(root, 2);
  3911. if (IS_ERR(trans))
  3912. return PTR_ERR(trans);
  3913. /*
  3914. * We need to do this in case we fail at _any_ point during the
  3915. * actual truncate. Once we do the truncate_setsize we could
  3916. * invalidate pages which forces any outstanding ordered io to
  3917. * be instantly completed which will give us extents that need
  3918. * to be truncated. If we fail to get an orphan inode down we
  3919. * could have left over extents that were never meant to live,
  3920. * so we need to garuntee from this point on that everything
  3921. * will be consistent.
  3922. */
  3923. ret = btrfs_orphan_add(trans, inode);
  3924. btrfs_end_transaction(trans, root);
  3925. if (ret)
  3926. return ret;
  3927. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3928. truncate_setsize(inode, newsize);
  3929. /* Disable nonlocked read DIO to avoid the end less truncate */
  3930. btrfs_inode_block_unlocked_dio(inode);
  3931. inode_dio_wait(inode);
  3932. btrfs_inode_resume_unlocked_dio(inode);
  3933. ret = btrfs_truncate(inode);
  3934. if (ret && inode->i_nlink)
  3935. btrfs_orphan_del(NULL, inode);
  3936. }
  3937. return ret;
  3938. }
  3939. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3940. {
  3941. struct inode *inode = dentry->d_inode;
  3942. struct btrfs_root *root = BTRFS_I(inode)->root;
  3943. int err;
  3944. if (btrfs_root_readonly(root))
  3945. return -EROFS;
  3946. err = inode_change_ok(inode, attr);
  3947. if (err)
  3948. return err;
  3949. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3950. err = btrfs_setsize(inode, attr);
  3951. if (err)
  3952. return err;
  3953. }
  3954. if (attr->ia_valid) {
  3955. setattr_copy(inode, attr);
  3956. inode_inc_iversion(inode);
  3957. err = btrfs_dirty_inode(inode);
  3958. if (!err && attr->ia_valid & ATTR_MODE)
  3959. err = btrfs_acl_chmod(inode);
  3960. }
  3961. return err;
  3962. }
  3963. void btrfs_evict_inode(struct inode *inode)
  3964. {
  3965. struct btrfs_trans_handle *trans;
  3966. struct btrfs_root *root = BTRFS_I(inode)->root;
  3967. struct btrfs_block_rsv *rsv, *global_rsv;
  3968. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  3969. int ret;
  3970. trace_btrfs_inode_evict(inode);
  3971. truncate_inode_pages(&inode->i_data, 0);
  3972. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  3973. btrfs_is_free_space_inode(inode)))
  3974. goto no_delete;
  3975. if (is_bad_inode(inode)) {
  3976. btrfs_orphan_del(NULL, inode);
  3977. goto no_delete;
  3978. }
  3979. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  3980. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3981. if (root->fs_info->log_root_recovering) {
  3982. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  3983. &BTRFS_I(inode)->runtime_flags));
  3984. goto no_delete;
  3985. }
  3986. if (inode->i_nlink > 0) {
  3987. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3988. goto no_delete;
  3989. }
  3990. ret = btrfs_commit_inode_delayed_inode(inode);
  3991. if (ret) {
  3992. btrfs_orphan_del(NULL, inode);
  3993. goto no_delete;
  3994. }
  3995. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  3996. if (!rsv) {
  3997. btrfs_orphan_del(NULL, inode);
  3998. goto no_delete;
  3999. }
  4000. rsv->size = min_size;
  4001. rsv->failfast = 1;
  4002. global_rsv = &root->fs_info->global_block_rsv;
  4003. btrfs_i_size_write(inode, 0);
  4004. /*
  4005. * This is a bit simpler than btrfs_truncate since we've already
  4006. * reserved our space for our orphan item in the unlink, so we just
  4007. * need to reserve some slack space in case we add bytes and update
  4008. * inode item when doing the truncate.
  4009. */
  4010. while (1) {
  4011. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4012. BTRFS_RESERVE_FLUSH_LIMIT);
  4013. /*
  4014. * Try and steal from the global reserve since we will
  4015. * likely not use this space anyway, we want to try as
  4016. * hard as possible to get this to work.
  4017. */
  4018. if (ret)
  4019. ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
  4020. if (ret) {
  4021. btrfs_warn(root->fs_info,
  4022. "Could not get space for a delete, will truncate on mount %d",
  4023. ret);
  4024. btrfs_orphan_del(NULL, inode);
  4025. btrfs_free_block_rsv(root, rsv);
  4026. goto no_delete;
  4027. }
  4028. trans = btrfs_join_transaction(root);
  4029. if (IS_ERR(trans)) {
  4030. btrfs_orphan_del(NULL, inode);
  4031. btrfs_free_block_rsv(root, rsv);
  4032. goto no_delete;
  4033. }
  4034. trans->block_rsv = rsv;
  4035. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4036. if (ret != -ENOSPC)
  4037. break;
  4038. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4039. btrfs_end_transaction(trans, root);
  4040. trans = NULL;
  4041. btrfs_btree_balance_dirty(root);
  4042. }
  4043. btrfs_free_block_rsv(root, rsv);
  4044. if (ret == 0) {
  4045. trans->block_rsv = root->orphan_block_rsv;
  4046. ret = btrfs_orphan_del(trans, inode);
  4047. BUG_ON(ret);
  4048. }
  4049. trans->block_rsv = &root->fs_info->trans_block_rsv;
  4050. if (!(root == root->fs_info->tree_root ||
  4051. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4052. btrfs_return_ino(root, btrfs_ino(inode));
  4053. btrfs_end_transaction(trans, root);
  4054. btrfs_btree_balance_dirty(root);
  4055. no_delete:
  4056. btrfs_remove_delayed_node(inode);
  4057. clear_inode(inode);
  4058. return;
  4059. }
  4060. /*
  4061. * this returns the key found in the dir entry in the location pointer.
  4062. * If no dir entries were found, location->objectid is 0.
  4063. */
  4064. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4065. struct btrfs_key *location)
  4066. {
  4067. const char *name = dentry->d_name.name;
  4068. int namelen = dentry->d_name.len;
  4069. struct btrfs_dir_item *di;
  4070. struct btrfs_path *path;
  4071. struct btrfs_root *root = BTRFS_I(dir)->root;
  4072. int ret = 0;
  4073. path = btrfs_alloc_path();
  4074. if (!path)
  4075. return -ENOMEM;
  4076. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  4077. namelen, 0);
  4078. if (IS_ERR(di))
  4079. ret = PTR_ERR(di);
  4080. if (IS_ERR_OR_NULL(di))
  4081. goto out_err;
  4082. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4083. out:
  4084. btrfs_free_path(path);
  4085. return ret;
  4086. out_err:
  4087. location->objectid = 0;
  4088. goto out;
  4089. }
  4090. /*
  4091. * when we hit a tree root in a directory, the btrfs part of the inode
  4092. * needs to be changed to reflect the root directory of the tree root. This
  4093. * is kind of like crossing a mount point.
  4094. */
  4095. static int fixup_tree_root_location(struct btrfs_root *root,
  4096. struct inode *dir,
  4097. struct dentry *dentry,
  4098. struct btrfs_key *location,
  4099. struct btrfs_root **sub_root)
  4100. {
  4101. struct btrfs_path *path;
  4102. struct btrfs_root *new_root;
  4103. struct btrfs_root_ref *ref;
  4104. struct extent_buffer *leaf;
  4105. int ret;
  4106. int err = 0;
  4107. path = btrfs_alloc_path();
  4108. if (!path) {
  4109. err = -ENOMEM;
  4110. goto out;
  4111. }
  4112. err = -ENOENT;
  4113. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  4114. BTRFS_I(dir)->root->root_key.objectid,
  4115. location->objectid);
  4116. if (ret) {
  4117. if (ret < 0)
  4118. err = ret;
  4119. goto out;
  4120. }
  4121. leaf = path->nodes[0];
  4122. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4123. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  4124. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4125. goto out;
  4126. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4127. (unsigned long)(ref + 1),
  4128. dentry->d_name.len);
  4129. if (ret)
  4130. goto out;
  4131. btrfs_release_path(path);
  4132. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  4133. if (IS_ERR(new_root)) {
  4134. err = PTR_ERR(new_root);
  4135. goto out;
  4136. }
  4137. *sub_root = new_root;
  4138. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4139. location->type = BTRFS_INODE_ITEM_KEY;
  4140. location->offset = 0;
  4141. err = 0;
  4142. out:
  4143. btrfs_free_path(path);
  4144. return err;
  4145. }
  4146. static void inode_tree_add(struct inode *inode)
  4147. {
  4148. struct btrfs_root *root = BTRFS_I(inode)->root;
  4149. struct btrfs_inode *entry;
  4150. struct rb_node **p;
  4151. struct rb_node *parent;
  4152. u64 ino = btrfs_ino(inode);
  4153. if (inode_unhashed(inode))
  4154. return;
  4155. again:
  4156. parent = NULL;
  4157. spin_lock(&root->inode_lock);
  4158. p = &root->inode_tree.rb_node;
  4159. while (*p) {
  4160. parent = *p;
  4161. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4162. if (ino < btrfs_ino(&entry->vfs_inode))
  4163. p = &parent->rb_left;
  4164. else if (ino > btrfs_ino(&entry->vfs_inode))
  4165. p = &parent->rb_right;
  4166. else {
  4167. WARN_ON(!(entry->vfs_inode.i_state &
  4168. (I_WILL_FREE | I_FREEING)));
  4169. rb_erase(parent, &root->inode_tree);
  4170. RB_CLEAR_NODE(parent);
  4171. spin_unlock(&root->inode_lock);
  4172. goto again;
  4173. }
  4174. }
  4175. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  4176. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4177. spin_unlock(&root->inode_lock);
  4178. }
  4179. static void inode_tree_del(struct inode *inode)
  4180. {
  4181. struct btrfs_root *root = BTRFS_I(inode)->root;
  4182. int empty = 0;
  4183. spin_lock(&root->inode_lock);
  4184. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4185. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4186. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4187. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4188. }
  4189. spin_unlock(&root->inode_lock);
  4190. /*
  4191. * Free space cache has inodes in the tree root, but the tree root has a
  4192. * root_refs of 0, so this could end up dropping the tree root as a
  4193. * snapshot, so we need the extra !root->fs_info->tree_root check to
  4194. * make sure we don't drop it.
  4195. */
  4196. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  4197. root != root->fs_info->tree_root) {
  4198. synchronize_srcu(&root->fs_info->subvol_srcu);
  4199. spin_lock(&root->inode_lock);
  4200. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4201. spin_unlock(&root->inode_lock);
  4202. if (empty)
  4203. btrfs_add_dead_root(root);
  4204. }
  4205. }
  4206. void btrfs_invalidate_inodes(struct btrfs_root *root)
  4207. {
  4208. struct rb_node *node;
  4209. struct rb_node *prev;
  4210. struct btrfs_inode *entry;
  4211. struct inode *inode;
  4212. u64 objectid = 0;
  4213. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  4214. spin_lock(&root->inode_lock);
  4215. again:
  4216. node = root->inode_tree.rb_node;
  4217. prev = NULL;
  4218. while (node) {
  4219. prev = node;
  4220. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4221. if (objectid < btrfs_ino(&entry->vfs_inode))
  4222. node = node->rb_left;
  4223. else if (objectid > btrfs_ino(&entry->vfs_inode))
  4224. node = node->rb_right;
  4225. else
  4226. break;
  4227. }
  4228. if (!node) {
  4229. while (prev) {
  4230. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  4231. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  4232. node = prev;
  4233. break;
  4234. }
  4235. prev = rb_next(prev);
  4236. }
  4237. }
  4238. while (node) {
  4239. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4240. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  4241. inode = igrab(&entry->vfs_inode);
  4242. if (inode) {
  4243. spin_unlock(&root->inode_lock);
  4244. if (atomic_read(&inode->i_count) > 1)
  4245. d_prune_aliases(inode);
  4246. /*
  4247. * btrfs_drop_inode will have it removed from
  4248. * the inode cache when its usage count
  4249. * hits zero.
  4250. */
  4251. iput(inode);
  4252. cond_resched();
  4253. spin_lock(&root->inode_lock);
  4254. goto again;
  4255. }
  4256. if (cond_resched_lock(&root->inode_lock))
  4257. goto again;
  4258. node = rb_next(node);
  4259. }
  4260. spin_unlock(&root->inode_lock);
  4261. }
  4262. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4263. {
  4264. struct btrfs_iget_args *args = p;
  4265. inode->i_ino = args->ino;
  4266. BTRFS_I(inode)->root = args->root;
  4267. return 0;
  4268. }
  4269. static int btrfs_find_actor(struct inode *inode, void *opaque)
  4270. {
  4271. struct btrfs_iget_args *args = opaque;
  4272. return args->ino == btrfs_ino(inode) &&
  4273. args->root == BTRFS_I(inode)->root;
  4274. }
  4275. static struct inode *btrfs_iget_locked(struct super_block *s,
  4276. u64 objectid,
  4277. struct btrfs_root *root)
  4278. {
  4279. struct inode *inode;
  4280. struct btrfs_iget_args args;
  4281. args.ino = objectid;
  4282. args.root = root;
  4283. inode = iget5_locked(s, objectid, btrfs_find_actor,
  4284. btrfs_init_locked_inode,
  4285. (void *)&args);
  4286. return inode;
  4287. }
  4288. /* Get an inode object given its location and corresponding root.
  4289. * Returns in *is_new if the inode was read from disk
  4290. */
  4291. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  4292. struct btrfs_root *root, int *new)
  4293. {
  4294. struct inode *inode;
  4295. inode = btrfs_iget_locked(s, location->objectid, root);
  4296. if (!inode)
  4297. return ERR_PTR(-ENOMEM);
  4298. if (inode->i_state & I_NEW) {
  4299. BTRFS_I(inode)->root = root;
  4300. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  4301. btrfs_read_locked_inode(inode);
  4302. if (!is_bad_inode(inode)) {
  4303. inode_tree_add(inode);
  4304. unlock_new_inode(inode);
  4305. if (new)
  4306. *new = 1;
  4307. } else {
  4308. unlock_new_inode(inode);
  4309. iput(inode);
  4310. inode = ERR_PTR(-ESTALE);
  4311. }
  4312. }
  4313. return inode;
  4314. }
  4315. static struct inode *new_simple_dir(struct super_block *s,
  4316. struct btrfs_key *key,
  4317. struct btrfs_root *root)
  4318. {
  4319. struct inode *inode = new_inode(s);
  4320. if (!inode)
  4321. return ERR_PTR(-ENOMEM);
  4322. BTRFS_I(inode)->root = root;
  4323. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  4324. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  4325. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  4326. inode->i_op = &btrfs_dir_ro_inode_operations;
  4327. inode->i_fop = &simple_dir_operations;
  4328. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  4329. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4330. return inode;
  4331. }
  4332. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  4333. {
  4334. struct inode *inode;
  4335. struct btrfs_root *root = BTRFS_I(dir)->root;
  4336. struct btrfs_root *sub_root = root;
  4337. struct btrfs_key location;
  4338. int index;
  4339. int ret = 0;
  4340. if (dentry->d_name.len > BTRFS_NAME_LEN)
  4341. return ERR_PTR(-ENAMETOOLONG);
  4342. ret = btrfs_inode_by_name(dir, dentry, &location);
  4343. if (ret < 0)
  4344. return ERR_PTR(ret);
  4345. if (location.objectid == 0)
  4346. return NULL;
  4347. if (location.type == BTRFS_INODE_ITEM_KEY) {
  4348. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  4349. return inode;
  4350. }
  4351. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  4352. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  4353. ret = fixup_tree_root_location(root, dir, dentry,
  4354. &location, &sub_root);
  4355. if (ret < 0) {
  4356. if (ret != -ENOENT)
  4357. inode = ERR_PTR(ret);
  4358. else
  4359. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  4360. } else {
  4361. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  4362. }
  4363. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  4364. if (!IS_ERR(inode) && root != sub_root) {
  4365. down_read(&root->fs_info->cleanup_work_sem);
  4366. if (!(inode->i_sb->s_flags & MS_RDONLY))
  4367. ret = btrfs_orphan_cleanup(sub_root);
  4368. up_read(&root->fs_info->cleanup_work_sem);
  4369. if (ret) {
  4370. iput(inode);
  4371. inode = ERR_PTR(ret);
  4372. }
  4373. }
  4374. return inode;
  4375. }
  4376. static int btrfs_dentry_delete(const struct dentry *dentry)
  4377. {
  4378. struct btrfs_root *root;
  4379. struct inode *inode = dentry->d_inode;
  4380. if (!inode && !IS_ROOT(dentry))
  4381. inode = dentry->d_parent->d_inode;
  4382. if (inode) {
  4383. root = BTRFS_I(inode)->root;
  4384. if (btrfs_root_refs(&root->root_item) == 0)
  4385. return 1;
  4386. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4387. return 1;
  4388. }
  4389. return 0;
  4390. }
  4391. static void btrfs_dentry_release(struct dentry *dentry)
  4392. {
  4393. if (dentry->d_fsdata)
  4394. kfree(dentry->d_fsdata);
  4395. }
  4396. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  4397. unsigned int flags)
  4398. {
  4399. struct dentry *ret;
  4400. ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
  4401. return ret;
  4402. }
  4403. unsigned char btrfs_filetype_table[] = {
  4404. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  4405. };
  4406. static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
  4407. {
  4408. struct inode *inode = file_inode(file);
  4409. struct btrfs_root *root = BTRFS_I(inode)->root;
  4410. struct btrfs_item *item;
  4411. struct btrfs_dir_item *di;
  4412. struct btrfs_key key;
  4413. struct btrfs_key found_key;
  4414. struct btrfs_path *path;
  4415. struct list_head ins_list;
  4416. struct list_head del_list;
  4417. int ret;
  4418. struct extent_buffer *leaf;
  4419. int slot;
  4420. unsigned char d_type;
  4421. int over = 0;
  4422. u32 di_cur;
  4423. u32 di_total;
  4424. u32 di_len;
  4425. int key_type = BTRFS_DIR_INDEX_KEY;
  4426. char tmp_name[32];
  4427. char *name_ptr;
  4428. int name_len;
  4429. int is_curr = 0; /* ctx->pos points to the current index? */
  4430. /* FIXME, use a real flag for deciding about the key type */
  4431. if (root->fs_info->tree_root == root)
  4432. key_type = BTRFS_DIR_ITEM_KEY;
  4433. if (!dir_emit_dots(file, ctx))
  4434. return 0;
  4435. path = btrfs_alloc_path();
  4436. if (!path)
  4437. return -ENOMEM;
  4438. path->reada = 1;
  4439. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4440. INIT_LIST_HEAD(&ins_list);
  4441. INIT_LIST_HEAD(&del_list);
  4442. btrfs_get_delayed_items(inode, &ins_list, &del_list);
  4443. }
  4444. btrfs_set_key_type(&key, key_type);
  4445. key.offset = ctx->pos;
  4446. key.objectid = btrfs_ino(inode);
  4447. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4448. if (ret < 0)
  4449. goto err;
  4450. while (1) {
  4451. leaf = path->nodes[0];
  4452. slot = path->slots[0];
  4453. if (slot >= btrfs_header_nritems(leaf)) {
  4454. ret = btrfs_next_leaf(root, path);
  4455. if (ret < 0)
  4456. goto err;
  4457. else if (ret > 0)
  4458. break;
  4459. continue;
  4460. }
  4461. item = btrfs_item_nr(leaf, slot);
  4462. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4463. if (found_key.objectid != key.objectid)
  4464. break;
  4465. if (btrfs_key_type(&found_key) != key_type)
  4466. break;
  4467. if (found_key.offset < ctx->pos)
  4468. goto next;
  4469. if (key_type == BTRFS_DIR_INDEX_KEY &&
  4470. btrfs_should_delete_dir_index(&del_list,
  4471. found_key.offset))
  4472. goto next;
  4473. ctx->pos = found_key.offset;
  4474. is_curr = 1;
  4475. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  4476. di_cur = 0;
  4477. di_total = btrfs_item_size(leaf, item);
  4478. while (di_cur < di_total) {
  4479. struct btrfs_key location;
  4480. if (verify_dir_item(root, leaf, di))
  4481. break;
  4482. name_len = btrfs_dir_name_len(leaf, di);
  4483. if (name_len <= sizeof(tmp_name)) {
  4484. name_ptr = tmp_name;
  4485. } else {
  4486. name_ptr = kmalloc(name_len, GFP_NOFS);
  4487. if (!name_ptr) {
  4488. ret = -ENOMEM;
  4489. goto err;
  4490. }
  4491. }
  4492. read_extent_buffer(leaf, name_ptr,
  4493. (unsigned long)(di + 1), name_len);
  4494. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  4495. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  4496. /* is this a reference to our own snapshot? If so
  4497. * skip it.
  4498. *
  4499. * In contrast to old kernels, we insert the snapshot's
  4500. * dir item and dir index after it has been created, so
  4501. * we won't find a reference to our own snapshot. We
  4502. * still keep the following code for backward
  4503. * compatibility.
  4504. */
  4505. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  4506. location.objectid == root->root_key.objectid) {
  4507. over = 0;
  4508. goto skip;
  4509. }
  4510. over = !dir_emit(ctx, name_ptr, name_len,
  4511. location.objectid, d_type);
  4512. skip:
  4513. if (name_ptr != tmp_name)
  4514. kfree(name_ptr);
  4515. if (over)
  4516. goto nopos;
  4517. di_len = btrfs_dir_name_len(leaf, di) +
  4518. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  4519. di_cur += di_len;
  4520. di = (struct btrfs_dir_item *)((char *)di + di_len);
  4521. }
  4522. next:
  4523. path->slots[0]++;
  4524. }
  4525. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4526. if (is_curr)
  4527. ctx->pos++;
  4528. ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
  4529. if (ret)
  4530. goto nopos;
  4531. }
  4532. /* Reached end of directory/root. Bump pos past the last item. */
  4533. ctx->pos++;
  4534. /*
  4535. * Stop new entries from being returned after we return the last
  4536. * entry.
  4537. *
  4538. * New directory entries are assigned a strictly increasing
  4539. * offset. This means that new entries created during readdir
  4540. * are *guaranteed* to be seen in the future by that readdir.
  4541. * This has broken buggy programs which operate on names as
  4542. * they're returned by readdir. Until we re-use freed offsets
  4543. * we have this hack to stop new entries from being returned
  4544. * under the assumption that they'll never reach this huge
  4545. * offset.
  4546. *
  4547. * This is being careful not to overflow 32bit loff_t unless the
  4548. * last entry requires it because doing so has broken 32bit apps
  4549. * in the past.
  4550. */
  4551. if (key_type == BTRFS_DIR_INDEX_KEY) {
  4552. if (ctx->pos >= INT_MAX)
  4553. ctx->pos = LLONG_MAX;
  4554. else
  4555. ctx->pos = INT_MAX;
  4556. }
  4557. nopos:
  4558. ret = 0;
  4559. err:
  4560. if (key_type == BTRFS_DIR_INDEX_KEY)
  4561. btrfs_put_delayed_items(&ins_list, &del_list);
  4562. btrfs_free_path(path);
  4563. return ret;
  4564. }
  4565. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  4566. {
  4567. struct btrfs_root *root = BTRFS_I(inode)->root;
  4568. struct btrfs_trans_handle *trans;
  4569. int ret = 0;
  4570. bool nolock = false;
  4571. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4572. return 0;
  4573. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  4574. nolock = true;
  4575. if (wbc->sync_mode == WB_SYNC_ALL) {
  4576. if (nolock)
  4577. trans = btrfs_join_transaction_nolock(root);
  4578. else
  4579. trans = btrfs_join_transaction(root);
  4580. if (IS_ERR(trans))
  4581. return PTR_ERR(trans);
  4582. ret = btrfs_commit_transaction(trans, root);
  4583. }
  4584. return ret;
  4585. }
  4586. /*
  4587. * This is somewhat expensive, updating the tree every time the
  4588. * inode changes. But, it is most likely to find the inode in cache.
  4589. * FIXME, needs more benchmarking...there are no reasons other than performance
  4590. * to keep or drop this code.
  4591. */
  4592. static int btrfs_dirty_inode(struct inode *inode)
  4593. {
  4594. struct btrfs_root *root = BTRFS_I(inode)->root;
  4595. struct btrfs_trans_handle *trans;
  4596. int ret;
  4597. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4598. return 0;
  4599. trans = btrfs_join_transaction(root);
  4600. if (IS_ERR(trans))
  4601. return PTR_ERR(trans);
  4602. ret = btrfs_update_inode(trans, root, inode);
  4603. if (ret && ret == -ENOSPC) {
  4604. /* whoops, lets try again with the full transaction */
  4605. btrfs_end_transaction(trans, root);
  4606. trans = btrfs_start_transaction(root, 1);
  4607. if (IS_ERR(trans))
  4608. return PTR_ERR(trans);
  4609. ret = btrfs_update_inode(trans, root, inode);
  4610. }
  4611. btrfs_end_transaction(trans, root);
  4612. if (BTRFS_I(inode)->delayed_node)
  4613. btrfs_balance_delayed_items(root);
  4614. return ret;
  4615. }
  4616. /*
  4617. * This is a copy of file_update_time. We need this so we can return error on
  4618. * ENOSPC for updating the inode in the case of file write and mmap writes.
  4619. */
  4620. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  4621. int flags)
  4622. {
  4623. struct btrfs_root *root = BTRFS_I(inode)->root;
  4624. if (btrfs_root_readonly(root))
  4625. return -EROFS;
  4626. if (flags & S_VERSION)
  4627. inode_inc_iversion(inode);
  4628. if (flags & S_CTIME)
  4629. inode->i_ctime = *now;
  4630. if (flags & S_MTIME)
  4631. inode->i_mtime = *now;
  4632. if (flags & S_ATIME)
  4633. inode->i_atime = *now;
  4634. return btrfs_dirty_inode(inode);
  4635. }
  4636. /*
  4637. * find the highest existing sequence number in a directory
  4638. * and then set the in-memory index_cnt variable to reflect
  4639. * free sequence numbers
  4640. */
  4641. static int btrfs_set_inode_index_count(struct inode *inode)
  4642. {
  4643. struct btrfs_root *root = BTRFS_I(inode)->root;
  4644. struct btrfs_key key, found_key;
  4645. struct btrfs_path *path;
  4646. struct extent_buffer *leaf;
  4647. int ret;
  4648. key.objectid = btrfs_ino(inode);
  4649. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  4650. key.offset = (u64)-1;
  4651. path = btrfs_alloc_path();
  4652. if (!path)
  4653. return -ENOMEM;
  4654. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4655. if (ret < 0)
  4656. goto out;
  4657. /* FIXME: we should be able to handle this */
  4658. if (ret == 0)
  4659. goto out;
  4660. ret = 0;
  4661. /*
  4662. * MAGIC NUMBER EXPLANATION:
  4663. * since we search a directory based on f_pos we have to start at 2
  4664. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  4665. * else has to start at 2
  4666. */
  4667. if (path->slots[0] == 0) {
  4668. BTRFS_I(inode)->index_cnt = 2;
  4669. goto out;
  4670. }
  4671. path->slots[0]--;
  4672. leaf = path->nodes[0];
  4673. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4674. if (found_key.objectid != btrfs_ino(inode) ||
  4675. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  4676. BTRFS_I(inode)->index_cnt = 2;
  4677. goto out;
  4678. }
  4679. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  4680. out:
  4681. btrfs_free_path(path);
  4682. return ret;
  4683. }
  4684. /*
  4685. * helper to find a free sequence number in a given directory. This current
  4686. * code is very simple, later versions will do smarter things in the btree
  4687. */
  4688. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  4689. {
  4690. int ret = 0;
  4691. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  4692. ret = btrfs_inode_delayed_dir_index_count(dir);
  4693. if (ret) {
  4694. ret = btrfs_set_inode_index_count(dir);
  4695. if (ret)
  4696. return ret;
  4697. }
  4698. }
  4699. *index = BTRFS_I(dir)->index_cnt;
  4700. BTRFS_I(dir)->index_cnt++;
  4701. return ret;
  4702. }
  4703. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  4704. struct btrfs_root *root,
  4705. struct inode *dir,
  4706. const char *name, int name_len,
  4707. u64 ref_objectid, u64 objectid,
  4708. umode_t mode, u64 *index)
  4709. {
  4710. struct inode *inode;
  4711. struct btrfs_inode_item *inode_item;
  4712. struct btrfs_key *location;
  4713. struct btrfs_path *path;
  4714. struct btrfs_inode_ref *ref;
  4715. struct btrfs_key key[2];
  4716. u32 sizes[2];
  4717. unsigned long ptr;
  4718. int ret;
  4719. int owner;
  4720. path = btrfs_alloc_path();
  4721. if (!path)
  4722. return ERR_PTR(-ENOMEM);
  4723. inode = new_inode(root->fs_info->sb);
  4724. if (!inode) {
  4725. btrfs_free_path(path);
  4726. return ERR_PTR(-ENOMEM);
  4727. }
  4728. /*
  4729. * we have to initialize this early, so we can reclaim the inode
  4730. * number if we fail afterwards in this function.
  4731. */
  4732. inode->i_ino = objectid;
  4733. if (dir) {
  4734. trace_btrfs_inode_request(dir);
  4735. ret = btrfs_set_inode_index(dir, index);
  4736. if (ret) {
  4737. btrfs_free_path(path);
  4738. iput(inode);
  4739. return ERR_PTR(ret);
  4740. }
  4741. }
  4742. /*
  4743. * index_cnt is ignored for everything but a dir,
  4744. * btrfs_get_inode_index_count has an explanation for the magic
  4745. * number
  4746. */
  4747. BTRFS_I(inode)->index_cnt = 2;
  4748. BTRFS_I(inode)->root = root;
  4749. BTRFS_I(inode)->generation = trans->transid;
  4750. inode->i_generation = BTRFS_I(inode)->generation;
  4751. /*
  4752. * We could have gotten an inode number from somebody who was fsynced
  4753. * and then removed in this same transaction, so let's just set full
  4754. * sync since it will be a full sync anyway and this will blow away the
  4755. * old info in the log.
  4756. */
  4757. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  4758. if (S_ISDIR(mode))
  4759. owner = 0;
  4760. else
  4761. owner = 1;
  4762. key[0].objectid = objectid;
  4763. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  4764. key[0].offset = 0;
  4765. /*
  4766. * Start new inodes with an inode_ref. This is slightly more
  4767. * efficient for small numbers of hard links since they will
  4768. * be packed into one item. Extended refs will kick in if we
  4769. * add more hard links than can fit in the ref item.
  4770. */
  4771. key[1].objectid = objectid;
  4772. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  4773. key[1].offset = ref_objectid;
  4774. sizes[0] = sizeof(struct btrfs_inode_item);
  4775. sizes[1] = name_len + sizeof(*ref);
  4776. path->leave_spinning = 1;
  4777. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  4778. if (ret != 0)
  4779. goto fail;
  4780. inode_init_owner(inode, dir, mode);
  4781. inode_set_bytes(inode, 0);
  4782. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4783. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4784. struct btrfs_inode_item);
  4785. memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
  4786. sizeof(*inode_item));
  4787. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  4788. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  4789. struct btrfs_inode_ref);
  4790. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  4791. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  4792. ptr = (unsigned long)(ref + 1);
  4793. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  4794. btrfs_mark_buffer_dirty(path->nodes[0]);
  4795. btrfs_free_path(path);
  4796. location = &BTRFS_I(inode)->location;
  4797. location->objectid = objectid;
  4798. location->offset = 0;
  4799. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  4800. btrfs_inherit_iflags(inode, dir);
  4801. if (S_ISREG(mode)) {
  4802. if (btrfs_test_opt(root, NODATASUM))
  4803. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  4804. if (btrfs_test_opt(root, NODATACOW))
  4805. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  4806. BTRFS_INODE_NODATASUM;
  4807. }
  4808. insert_inode_hash(inode);
  4809. inode_tree_add(inode);
  4810. trace_btrfs_inode_new(inode);
  4811. btrfs_set_inode_last_trans(trans, inode);
  4812. btrfs_update_root_times(trans, root);
  4813. return inode;
  4814. fail:
  4815. if (dir)
  4816. BTRFS_I(dir)->index_cnt--;
  4817. btrfs_free_path(path);
  4818. iput(inode);
  4819. return ERR_PTR(ret);
  4820. }
  4821. static inline u8 btrfs_inode_type(struct inode *inode)
  4822. {
  4823. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4824. }
  4825. /*
  4826. * utility function to add 'inode' into 'parent_inode' with
  4827. * a give name and a given sequence number.
  4828. * if 'add_backref' is true, also insert a backref from the
  4829. * inode to the parent directory.
  4830. */
  4831. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4832. struct inode *parent_inode, struct inode *inode,
  4833. const char *name, int name_len, int add_backref, u64 index)
  4834. {
  4835. int ret = 0;
  4836. struct btrfs_key key;
  4837. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4838. u64 ino = btrfs_ino(inode);
  4839. u64 parent_ino = btrfs_ino(parent_inode);
  4840. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4841. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4842. } else {
  4843. key.objectid = ino;
  4844. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4845. key.offset = 0;
  4846. }
  4847. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4848. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4849. key.objectid, root->root_key.objectid,
  4850. parent_ino, index, name, name_len);
  4851. } else if (add_backref) {
  4852. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  4853. parent_ino, index);
  4854. }
  4855. /* Nothing to clean up yet */
  4856. if (ret)
  4857. return ret;
  4858. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4859. parent_inode, &key,
  4860. btrfs_inode_type(inode), index);
  4861. if (ret == -EEXIST || ret == -EOVERFLOW)
  4862. goto fail_dir_item;
  4863. else if (ret) {
  4864. btrfs_abort_transaction(trans, root, ret);
  4865. return ret;
  4866. }
  4867. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4868. name_len * 2);
  4869. inode_inc_iversion(parent_inode);
  4870. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4871. ret = btrfs_update_inode(trans, root, parent_inode);
  4872. if (ret)
  4873. btrfs_abort_transaction(trans, root, ret);
  4874. return ret;
  4875. fail_dir_item:
  4876. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4877. u64 local_index;
  4878. int err;
  4879. err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  4880. key.objectid, root->root_key.objectid,
  4881. parent_ino, &local_index, name, name_len);
  4882. } else if (add_backref) {
  4883. u64 local_index;
  4884. int err;
  4885. err = btrfs_del_inode_ref(trans, root, name, name_len,
  4886. ino, parent_ino, &local_index);
  4887. }
  4888. return ret;
  4889. }
  4890. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4891. struct inode *dir, struct dentry *dentry,
  4892. struct inode *inode, int backref, u64 index)
  4893. {
  4894. int err = btrfs_add_link(trans, dir, inode,
  4895. dentry->d_name.name, dentry->d_name.len,
  4896. backref, index);
  4897. if (err > 0)
  4898. err = -EEXIST;
  4899. return err;
  4900. }
  4901. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4902. umode_t mode, dev_t rdev)
  4903. {
  4904. struct btrfs_trans_handle *trans;
  4905. struct btrfs_root *root = BTRFS_I(dir)->root;
  4906. struct inode *inode = NULL;
  4907. int err;
  4908. int drop_inode = 0;
  4909. u64 objectid;
  4910. u64 index = 0;
  4911. if (!new_valid_dev(rdev))
  4912. return -EINVAL;
  4913. /*
  4914. * 2 for inode item and ref
  4915. * 2 for dir items
  4916. * 1 for xattr if selinux is on
  4917. */
  4918. trans = btrfs_start_transaction(root, 5);
  4919. if (IS_ERR(trans))
  4920. return PTR_ERR(trans);
  4921. err = btrfs_find_free_ino(root, &objectid);
  4922. if (err)
  4923. goto out_unlock;
  4924. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4925. dentry->d_name.len, btrfs_ino(dir), objectid,
  4926. mode, &index);
  4927. if (IS_ERR(inode)) {
  4928. err = PTR_ERR(inode);
  4929. goto out_unlock;
  4930. }
  4931. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4932. if (err) {
  4933. drop_inode = 1;
  4934. goto out_unlock;
  4935. }
  4936. /*
  4937. * If the active LSM wants to access the inode during
  4938. * d_instantiate it needs these. Smack checks to see
  4939. * if the filesystem supports xattrs by looking at the
  4940. * ops vector.
  4941. */
  4942. inode->i_op = &btrfs_special_inode_operations;
  4943. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4944. if (err)
  4945. drop_inode = 1;
  4946. else {
  4947. init_special_inode(inode, inode->i_mode, rdev);
  4948. btrfs_update_inode(trans, root, inode);
  4949. d_instantiate(dentry, inode);
  4950. }
  4951. out_unlock:
  4952. btrfs_end_transaction(trans, root);
  4953. btrfs_btree_balance_dirty(root);
  4954. if (drop_inode) {
  4955. inode_dec_link_count(inode);
  4956. iput(inode);
  4957. }
  4958. return err;
  4959. }
  4960. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4961. umode_t mode, bool excl)
  4962. {
  4963. struct btrfs_trans_handle *trans;
  4964. struct btrfs_root *root = BTRFS_I(dir)->root;
  4965. struct inode *inode = NULL;
  4966. int drop_inode_on_err = 0;
  4967. int err;
  4968. u64 objectid;
  4969. u64 index = 0;
  4970. /*
  4971. * 2 for inode item and ref
  4972. * 2 for dir items
  4973. * 1 for xattr if selinux is on
  4974. */
  4975. trans = btrfs_start_transaction(root, 5);
  4976. if (IS_ERR(trans))
  4977. return PTR_ERR(trans);
  4978. err = btrfs_find_free_ino(root, &objectid);
  4979. if (err)
  4980. goto out_unlock;
  4981. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4982. dentry->d_name.len, btrfs_ino(dir), objectid,
  4983. mode, &index);
  4984. if (IS_ERR(inode)) {
  4985. err = PTR_ERR(inode);
  4986. goto out_unlock;
  4987. }
  4988. drop_inode_on_err = 1;
  4989. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4990. if (err)
  4991. goto out_unlock;
  4992. err = btrfs_update_inode(trans, root, inode);
  4993. if (err)
  4994. goto out_unlock;
  4995. /*
  4996. * If the active LSM wants to access the inode during
  4997. * d_instantiate it needs these. Smack checks to see
  4998. * if the filesystem supports xattrs by looking at the
  4999. * ops vector.
  5000. */
  5001. inode->i_fop = &btrfs_file_operations;
  5002. inode->i_op = &btrfs_file_inode_operations;
  5003. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5004. if (err)
  5005. goto out_unlock;
  5006. inode->i_mapping->a_ops = &btrfs_aops;
  5007. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5008. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5009. d_instantiate(dentry, inode);
  5010. out_unlock:
  5011. btrfs_end_transaction(trans, root);
  5012. if (err && drop_inode_on_err) {
  5013. inode_dec_link_count(inode);
  5014. iput(inode);
  5015. }
  5016. btrfs_btree_balance_dirty(root);
  5017. return err;
  5018. }
  5019. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5020. struct dentry *dentry)
  5021. {
  5022. struct btrfs_trans_handle *trans;
  5023. struct btrfs_root *root = BTRFS_I(dir)->root;
  5024. struct inode *inode = old_dentry->d_inode;
  5025. u64 index;
  5026. int err;
  5027. int drop_inode = 0;
  5028. /* do not allow sys_link's with other subvols of the same device */
  5029. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5030. return -EXDEV;
  5031. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5032. return -EMLINK;
  5033. err = btrfs_set_inode_index(dir, &index);
  5034. if (err)
  5035. goto fail;
  5036. /*
  5037. * 2 items for inode and inode ref
  5038. * 2 items for dir items
  5039. * 1 item for parent inode
  5040. */
  5041. trans = btrfs_start_transaction(root, 5);
  5042. if (IS_ERR(trans)) {
  5043. err = PTR_ERR(trans);
  5044. goto fail;
  5045. }
  5046. btrfs_inc_nlink(inode);
  5047. inode_inc_iversion(inode);
  5048. inode->i_ctime = CURRENT_TIME;
  5049. ihold(inode);
  5050. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5051. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  5052. if (err) {
  5053. drop_inode = 1;
  5054. } else {
  5055. struct dentry *parent = dentry->d_parent;
  5056. err = btrfs_update_inode(trans, root, inode);
  5057. if (err)
  5058. goto fail;
  5059. d_instantiate(dentry, inode);
  5060. btrfs_log_new_name(trans, inode, NULL, parent);
  5061. }
  5062. btrfs_end_transaction(trans, root);
  5063. fail:
  5064. if (drop_inode) {
  5065. inode_dec_link_count(inode);
  5066. iput(inode);
  5067. }
  5068. btrfs_btree_balance_dirty(root);
  5069. return err;
  5070. }
  5071. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5072. {
  5073. struct inode *inode = NULL;
  5074. struct btrfs_trans_handle *trans;
  5075. struct btrfs_root *root = BTRFS_I(dir)->root;
  5076. int err = 0;
  5077. int drop_on_err = 0;
  5078. u64 objectid = 0;
  5079. u64 index = 0;
  5080. /*
  5081. * 2 items for inode and ref
  5082. * 2 items for dir items
  5083. * 1 for xattr if selinux is on
  5084. */
  5085. trans = btrfs_start_transaction(root, 5);
  5086. if (IS_ERR(trans))
  5087. return PTR_ERR(trans);
  5088. err = btrfs_find_free_ino(root, &objectid);
  5089. if (err)
  5090. goto out_fail;
  5091. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5092. dentry->d_name.len, btrfs_ino(dir), objectid,
  5093. S_IFDIR | mode, &index);
  5094. if (IS_ERR(inode)) {
  5095. err = PTR_ERR(inode);
  5096. goto out_fail;
  5097. }
  5098. drop_on_err = 1;
  5099. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5100. if (err)
  5101. goto out_fail;
  5102. inode->i_op = &btrfs_dir_inode_operations;
  5103. inode->i_fop = &btrfs_dir_file_operations;
  5104. btrfs_i_size_write(inode, 0);
  5105. err = btrfs_update_inode(trans, root, inode);
  5106. if (err)
  5107. goto out_fail;
  5108. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  5109. dentry->d_name.len, 0, index);
  5110. if (err)
  5111. goto out_fail;
  5112. d_instantiate(dentry, inode);
  5113. drop_on_err = 0;
  5114. out_fail:
  5115. btrfs_end_transaction(trans, root);
  5116. if (drop_on_err)
  5117. iput(inode);
  5118. btrfs_btree_balance_dirty(root);
  5119. return err;
  5120. }
  5121. /* helper for btfs_get_extent. Given an existing extent in the tree,
  5122. * and an extent that you want to insert, deal with overlap and insert
  5123. * the new extent into the tree.
  5124. */
  5125. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  5126. struct extent_map *existing,
  5127. struct extent_map *em,
  5128. u64 map_start, u64 map_len)
  5129. {
  5130. u64 start_diff;
  5131. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  5132. start_diff = map_start - em->start;
  5133. em->start = map_start;
  5134. em->len = map_len;
  5135. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  5136. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  5137. em->block_start += start_diff;
  5138. em->block_len -= start_diff;
  5139. }
  5140. return add_extent_mapping(em_tree, em, 0);
  5141. }
  5142. static noinline int uncompress_inline(struct btrfs_path *path,
  5143. struct inode *inode, struct page *page,
  5144. size_t pg_offset, u64 extent_offset,
  5145. struct btrfs_file_extent_item *item)
  5146. {
  5147. int ret;
  5148. struct extent_buffer *leaf = path->nodes[0];
  5149. char *tmp;
  5150. size_t max_size;
  5151. unsigned long inline_size;
  5152. unsigned long ptr;
  5153. int compress_type;
  5154. WARN_ON(pg_offset != 0);
  5155. compress_type = btrfs_file_extent_compression(leaf, item);
  5156. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5157. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5158. btrfs_item_nr(leaf, path->slots[0]));
  5159. tmp = kmalloc(inline_size, GFP_NOFS);
  5160. if (!tmp)
  5161. return -ENOMEM;
  5162. ptr = btrfs_file_extent_inline_start(item);
  5163. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5164. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  5165. ret = btrfs_decompress(compress_type, tmp, page,
  5166. extent_offset, inline_size, max_size);
  5167. if (ret) {
  5168. char *kaddr = kmap_atomic(page);
  5169. unsigned long copy_size = min_t(u64,
  5170. PAGE_CACHE_SIZE - pg_offset,
  5171. max_size - extent_offset);
  5172. memset(kaddr + pg_offset, 0, copy_size);
  5173. kunmap_atomic(kaddr);
  5174. }
  5175. kfree(tmp);
  5176. return 0;
  5177. }
  5178. /*
  5179. * a bit scary, this does extent mapping from logical file offset to the disk.
  5180. * the ugly parts come from merging extents from the disk with the in-ram
  5181. * representation. This gets more complex because of the data=ordered code,
  5182. * where the in-ram extents might be locked pending data=ordered completion.
  5183. *
  5184. * This also copies inline extents directly into the page.
  5185. */
  5186. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  5187. size_t pg_offset, u64 start, u64 len,
  5188. int create)
  5189. {
  5190. int ret;
  5191. int err = 0;
  5192. u64 bytenr;
  5193. u64 extent_start = 0;
  5194. u64 extent_end = 0;
  5195. u64 objectid = btrfs_ino(inode);
  5196. u32 found_type;
  5197. struct btrfs_path *path = NULL;
  5198. struct btrfs_root *root = BTRFS_I(inode)->root;
  5199. struct btrfs_file_extent_item *item;
  5200. struct extent_buffer *leaf;
  5201. struct btrfs_key found_key;
  5202. struct extent_map *em = NULL;
  5203. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5204. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5205. struct btrfs_trans_handle *trans = NULL;
  5206. int compress_type;
  5207. again:
  5208. read_lock(&em_tree->lock);
  5209. em = lookup_extent_mapping(em_tree, start, len);
  5210. if (em)
  5211. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5212. read_unlock(&em_tree->lock);
  5213. if (em) {
  5214. if (em->start > start || em->start + em->len <= start)
  5215. free_extent_map(em);
  5216. else if (em->block_start == EXTENT_MAP_INLINE && page)
  5217. free_extent_map(em);
  5218. else
  5219. goto out;
  5220. }
  5221. em = alloc_extent_map();
  5222. if (!em) {
  5223. err = -ENOMEM;
  5224. goto out;
  5225. }
  5226. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5227. em->start = EXTENT_MAP_HOLE;
  5228. em->orig_start = EXTENT_MAP_HOLE;
  5229. em->len = (u64)-1;
  5230. em->block_len = (u64)-1;
  5231. if (!path) {
  5232. path = btrfs_alloc_path();
  5233. if (!path) {
  5234. err = -ENOMEM;
  5235. goto out;
  5236. }
  5237. /*
  5238. * Chances are we'll be called again, so go ahead and do
  5239. * readahead
  5240. */
  5241. path->reada = 1;
  5242. }
  5243. ret = btrfs_lookup_file_extent(trans, root, path,
  5244. objectid, start, trans != NULL);
  5245. if (ret < 0) {
  5246. err = ret;
  5247. goto out;
  5248. }
  5249. if (ret != 0) {
  5250. if (path->slots[0] == 0)
  5251. goto not_found;
  5252. path->slots[0]--;
  5253. }
  5254. leaf = path->nodes[0];
  5255. item = btrfs_item_ptr(leaf, path->slots[0],
  5256. struct btrfs_file_extent_item);
  5257. /* are we inside the extent that was found? */
  5258. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5259. found_type = btrfs_key_type(&found_key);
  5260. if (found_key.objectid != objectid ||
  5261. found_type != BTRFS_EXTENT_DATA_KEY) {
  5262. goto not_found;
  5263. }
  5264. found_type = btrfs_file_extent_type(leaf, item);
  5265. extent_start = found_key.offset;
  5266. compress_type = btrfs_file_extent_compression(leaf, item);
  5267. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5268. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5269. extent_end = extent_start +
  5270. btrfs_file_extent_num_bytes(leaf, item);
  5271. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5272. size_t size;
  5273. size = btrfs_file_extent_inline_len(leaf, item);
  5274. extent_end = ALIGN(extent_start + size, root->sectorsize);
  5275. }
  5276. if (start >= extent_end) {
  5277. path->slots[0]++;
  5278. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  5279. ret = btrfs_next_leaf(root, path);
  5280. if (ret < 0) {
  5281. err = ret;
  5282. goto out;
  5283. }
  5284. if (ret > 0)
  5285. goto not_found;
  5286. leaf = path->nodes[0];
  5287. }
  5288. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5289. if (found_key.objectid != objectid ||
  5290. found_key.type != BTRFS_EXTENT_DATA_KEY)
  5291. goto not_found;
  5292. if (start + len <= found_key.offset)
  5293. goto not_found;
  5294. em->start = start;
  5295. em->orig_start = start;
  5296. em->len = found_key.offset - start;
  5297. goto not_found_em;
  5298. }
  5299. em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
  5300. if (found_type == BTRFS_FILE_EXTENT_REG ||
  5301. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  5302. em->start = extent_start;
  5303. em->len = extent_end - extent_start;
  5304. em->orig_start = extent_start -
  5305. btrfs_file_extent_offset(leaf, item);
  5306. em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
  5307. item);
  5308. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  5309. if (bytenr == 0) {
  5310. em->block_start = EXTENT_MAP_HOLE;
  5311. goto insert;
  5312. }
  5313. if (compress_type != BTRFS_COMPRESS_NONE) {
  5314. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  5315. em->compress_type = compress_type;
  5316. em->block_start = bytenr;
  5317. em->block_len = em->orig_block_len;
  5318. } else {
  5319. bytenr += btrfs_file_extent_offset(leaf, item);
  5320. em->block_start = bytenr;
  5321. em->block_len = em->len;
  5322. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  5323. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  5324. }
  5325. goto insert;
  5326. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  5327. unsigned long ptr;
  5328. char *map;
  5329. size_t size;
  5330. size_t extent_offset;
  5331. size_t copy_size;
  5332. em->block_start = EXTENT_MAP_INLINE;
  5333. if (!page || create) {
  5334. em->start = extent_start;
  5335. em->len = extent_end - extent_start;
  5336. goto out;
  5337. }
  5338. size = btrfs_file_extent_inline_len(leaf, item);
  5339. extent_offset = page_offset(page) + pg_offset - extent_start;
  5340. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  5341. size - extent_offset);
  5342. em->start = extent_start + extent_offset;
  5343. em->len = ALIGN(copy_size, root->sectorsize);
  5344. em->orig_block_len = em->len;
  5345. em->orig_start = em->start;
  5346. if (compress_type) {
  5347. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  5348. em->compress_type = compress_type;
  5349. }
  5350. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  5351. if (create == 0 && !PageUptodate(page)) {
  5352. if (btrfs_file_extent_compression(leaf, item) !=
  5353. BTRFS_COMPRESS_NONE) {
  5354. ret = uncompress_inline(path, inode, page,
  5355. pg_offset,
  5356. extent_offset, item);
  5357. BUG_ON(ret); /* -ENOMEM */
  5358. } else {
  5359. map = kmap(page);
  5360. read_extent_buffer(leaf, map + pg_offset, ptr,
  5361. copy_size);
  5362. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  5363. memset(map + pg_offset + copy_size, 0,
  5364. PAGE_CACHE_SIZE - pg_offset -
  5365. copy_size);
  5366. }
  5367. kunmap(page);
  5368. }
  5369. flush_dcache_page(page);
  5370. } else if (create && PageUptodate(page)) {
  5371. BUG();
  5372. if (!trans) {
  5373. kunmap(page);
  5374. free_extent_map(em);
  5375. em = NULL;
  5376. btrfs_release_path(path);
  5377. trans = btrfs_join_transaction(root);
  5378. if (IS_ERR(trans))
  5379. return ERR_CAST(trans);
  5380. goto again;
  5381. }
  5382. map = kmap(page);
  5383. write_extent_buffer(leaf, map + pg_offset, ptr,
  5384. copy_size);
  5385. kunmap(page);
  5386. btrfs_mark_buffer_dirty(leaf);
  5387. }
  5388. set_extent_uptodate(io_tree, em->start,
  5389. extent_map_end(em) - 1, NULL, GFP_NOFS);
  5390. goto insert;
  5391. } else {
  5392. WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
  5393. }
  5394. not_found:
  5395. em->start = start;
  5396. em->orig_start = start;
  5397. em->len = len;
  5398. not_found_em:
  5399. em->block_start = EXTENT_MAP_HOLE;
  5400. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  5401. insert:
  5402. btrfs_release_path(path);
  5403. if (em->start > start || extent_map_end(em) <= start) {
  5404. btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
  5405. (unsigned long long)em->start,
  5406. (unsigned long long)em->len,
  5407. (unsigned long long)start,
  5408. (unsigned long long)len);
  5409. err = -EIO;
  5410. goto out;
  5411. }
  5412. err = 0;
  5413. write_lock(&em_tree->lock);
  5414. ret = add_extent_mapping(em_tree, em, 0);
  5415. /* it is possible that someone inserted the extent into the tree
  5416. * while we had the lock dropped. It is also possible that
  5417. * an overlapping map exists in the tree
  5418. */
  5419. if (ret == -EEXIST) {
  5420. struct extent_map *existing;
  5421. ret = 0;
  5422. existing = lookup_extent_mapping(em_tree, start, len);
  5423. if (existing && (existing->start > start ||
  5424. existing->start + existing->len <= start)) {
  5425. free_extent_map(existing);
  5426. existing = NULL;
  5427. }
  5428. if (!existing) {
  5429. existing = lookup_extent_mapping(em_tree, em->start,
  5430. em->len);
  5431. if (existing) {
  5432. err = merge_extent_mapping(em_tree, existing,
  5433. em, start,
  5434. root->sectorsize);
  5435. free_extent_map(existing);
  5436. if (err) {
  5437. free_extent_map(em);
  5438. em = NULL;
  5439. }
  5440. } else {
  5441. err = -EIO;
  5442. free_extent_map(em);
  5443. em = NULL;
  5444. }
  5445. } else {
  5446. free_extent_map(em);
  5447. em = existing;
  5448. err = 0;
  5449. }
  5450. }
  5451. write_unlock(&em_tree->lock);
  5452. out:
  5453. if (em)
  5454. trace_btrfs_get_extent(root, em);
  5455. if (path)
  5456. btrfs_free_path(path);
  5457. if (trans) {
  5458. ret = btrfs_end_transaction(trans, root);
  5459. if (!err)
  5460. err = ret;
  5461. }
  5462. if (err) {
  5463. free_extent_map(em);
  5464. return ERR_PTR(err);
  5465. }
  5466. BUG_ON(!em); /* Error is always set */
  5467. return em;
  5468. }
  5469. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  5470. size_t pg_offset, u64 start, u64 len,
  5471. int create)
  5472. {
  5473. struct extent_map *em;
  5474. struct extent_map *hole_em = NULL;
  5475. u64 range_start = start;
  5476. u64 end;
  5477. u64 found;
  5478. u64 found_end;
  5479. int err = 0;
  5480. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  5481. if (IS_ERR(em))
  5482. return em;
  5483. if (em) {
  5484. /*
  5485. * if our em maps to
  5486. * - a hole or
  5487. * - a pre-alloc extent,
  5488. * there might actually be delalloc bytes behind it.
  5489. */
  5490. if (em->block_start != EXTENT_MAP_HOLE &&
  5491. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5492. return em;
  5493. else
  5494. hole_em = em;
  5495. }
  5496. /* check to see if we've wrapped (len == -1 or similar) */
  5497. end = start + len;
  5498. if (end < start)
  5499. end = (u64)-1;
  5500. else
  5501. end -= 1;
  5502. em = NULL;
  5503. /* ok, we didn't find anything, lets look for delalloc */
  5504. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  5505. end, len, EXTENT_DELALLOC, 1);
  5506. found_end = range_start + found;
  5507. if (found_end < range_start)
  5508. found_end = (u64)-1;
  5509. /*
  5510. * we didn't find anything useful, return
  5511. * the original results from get_extent()
  5512. */
  5513. if (range_start > end || found_end <= start) {
  5514. em = hole_em;
  5515. hole_em = NULL;
  5516. goto out;
  5517. }
  5518. /* adjust the range_start to make sure it doesn't
  5519. * go backwards from the start they passed in
  5520. */
  5521. range_start = max(start,range_start);
  5522. found = found_end - range_start;
  5523. if (found > 0) {
  5524. u64 hole_start = start;
  5525. u64 hole_len = len;
  5526. em = alloc_extent_map();
  5527. if (!em) {
  5528. err = -ENOMEM;
  5529. goto out;
  5530. }
  5531. /*
  5532. * when btrfs_get_extent can't find anything it
  5533. * returns one huge hole
  5534. *
  5535. * make sure what it found really fits our range, and
  5536. * adjust to make sure it is based on the start from
  5537. * the caller
  5538. */
  5539. if (hole_em) {
  5540. u64 calc_end = extent_map_end(hole_em);
  5541. if (calc_end <= start || (hole_em->start > end)) {
  5542. free_extent_map(hole_em);
  5543. hole_em = NULL;
  5544. } else {
  5545. hole_start = max(hole_em->start, start);
  5546. hole_len = calc_end - hole_start;
  5547. }
  5548. }
  5549. em->bdev = NULL;
  5550. if (hole_em && range_start > hole_start) {
  5551. /* our hole starts before our delalloc, so we
  5552. * have to return just the parts of the hole
  5553. * that go until the delalloc starts
  5554. */
  5555. em->len = min(hole_len,
  5556. range_start - hole_start);
  5557. em->start = hole_start;
  5558. em->orig_start = hole_start;
  5559. /*
  5560. * don't adjust block start at all,
  5561. * it is fixed at EXTENT_MAP_HOLE
  5562. */
  5563. em->block_start = hole_em->block_start;
  5564. em->block_len = hole_len;
  5565. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  5566. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  5567. } else {
  5568. em->start = range_start;
  5569. em->len = found;
  5570. em->orig_start = range_start;
  5571. em->block_start = EXTENT_MAP_DELALLOC;
  5572. em->block_len = found;
  5573. }
  5574. } else if (hole_em) {
  5575. return hole_em;
  5576. }
  5577. out:
  5578. free_extent_map(hole_em);
  5579. if (err) {
  5580. free_extent_map(em);
  5581. return ERR_PTR(err);
  5582. }
  5583. return em;
  5584. }
  5585. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  5586. u64 start, u64 len)
  5587. {
  5588. struct btrfs_root *root = BTRFS_I(inode)->root;
  5589. struct btrfs_trans_handle *trans;
  5590. struct extent_map *em;
  5591. struct btrfs_key ins;
  5592. u64 alloc_hint;
  5593. int ret;
  5594. trans = btrfs_join_transaction(root);
  5595. if (IS_ERR(trans))
  5596. return ERR_CAST(trans);
  5597. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  5598. alloc_hint = get_extent_allocation_hint(inode, start, len);
  5599. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  5600. alloc_hint, &ins, 1);
  5601. if (ret) {
  5602. em = ERR_PTR(ret);
  5603. goto out;
  5604. }
  5605. em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
  5606. ins.offset, ins.offset, ins.offset, 0);
  5607. if (IS_ERR(em))
  5608. goto out;
  5609. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  5610. ins.offset, ins.offset, 0);
  5611. if (ret) {
  5612. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  5613. em = ERR_PTR(ret);
  5614. }
  5615. out:
  5616. btrfs_end_transaction(trans, root);
  5617. return em;
  5618. }
  5619. /*
  5620. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  5621. * block must be cow'd
  5622. */
  5623. noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
  5624. struct inode *inode, u64 offset, u64 *len,
  5625. u64 *orig_start, u64 *orig_block_len,
  5626. u64 *ram_bytes)
  5627. {
  5628. struct btrfs_path *path;
  5629. int ret;
  5630. struct extent_buffer *leaf;
  5631. struct btrfs_root *root = BTRFS_I(inode)->root;
  5632. struct btrfs_file_extent_item *fi;
  5633. struct btrfs_key key;
  5634. u64 disk_bytenr;
  5635. u64 backref_offset;
  5636. u64 extent_end;
  5637. u64 num_bytes;
  5638. int slot;
  5639. int found_type;
  5640. bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
  5641. path = btrfs_alloc_path();
  5642. if (!path)
  5643. return -ENOMEM;
  5644. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  5645. offset, 0);
  5646. if (ret < 0)
  5647. goto out;
  5648. slot = path->slots[0];
  5649. if (ret == 1) {
  5650. if (slot == 0) {
  5651. /* can't find the item, must cow */
  5652. ret = 0;
  5653. goto out;
  5654. }
  5655. slot--;
  5656. }
  5657. ret = 0;
  5658. leaf = path->nodes[0];
  5659. btrfs_item_key_to_cpu(leaf, &key, slot);
  5660. if (key.objectid != btrfs_ino(inode) ||
  5661. key.type != BTRFS_EXTENT_DATA_KEY) {
  5662. /* not our file or wrong item type, must cow */
  5663. goto out;
  5664. }
  5665. if (key.offset > offset) {
  5666. /* Wrong offset, must cow */
  5667. goto out;
  5668. }
  5669. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  5670. found_type = btrfs_file_extent_type(leaf, fi);
  5671. if (found_type != BTRFS_FILE_EXTENT_REG &&
  5672. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  5673. /* not a regular extent, must cow */
  5674. goto out;
  5675. }
  5676. if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
  5677. goto out;
  5678. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  5679. if (disk_bytenr == 0)
  5680. goto out;
  5681. if (btrfs_file_extent_compression(leaf, fi) ||
  5682. btrfs_file_extent_encryption(leaf, fi) ||
  5683. btrfs_file_extent_other_encoding(leaf, fi))
  5684. goto out;
  5685. backref_offset = btrfs_file_extent_offset(leaf, fi);
  5686. if (orig_start) {
  5687. *orig_start = key.offset - backref_offset;
  5688. *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
  5689. *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  5690. }
  5691. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  5692. if (btrfs_extent_readonly(root, disk_bytenr))
  5693. goto out;
  5694. /*
  5695. * look for other files referencing this extent, if we
  5696. * find any we must cow
  5697. */
  5698. if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  5699. key.offset - backref_offset, disk_bytenr))
  5700. goto out;
  5701. /*
  5702. * adjust disk_bytenr and num_bytes to cover just the bytes
  5703. * in this extent we are about to write. If there
  5704. * are any csums in that range we have to cow in order
  5705. * to keep the csums correct
  5706. */
  5707. disk_bytenr += backref_offset;
  5708. disk_bytenr += offset - key.offset;
  5709. num_bytes = min(offset + *len, extent_end) - offset;
  5710. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  5711. goto out;
  5712. /*
  5713. * all of the above have passed, it is safe to overwrite this extent
  5714. * without cow
  5715. */
  5716. *len = num_bytes;
  5717. ret = 1;
  5718. out:
  5719. btrfs_free_path(path);
  5720. return ret;
  5721. }
  5722. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  5723. struct extent_state **cached_state, int writing)
  5724. {
  5725. struct btrfs_ordered_extent *ordered;
  5726. int ret = 0;
  5727. while (1) {
  5728. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5729. 0, cached_state);
  5730. /*
  5731. * We're concerned with the entire range that we're going to be
  5732. * doing DIO to, so we need to make sure theres no ordered
  5733. * extents in this range.
  5734. */
  5735. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5736. lockend - lockstart + 1);
  5737. /*
  5738. * We need to make sure there are no buffered pages in this
  5739. * range either, we could have raced between the invalidate in
  5740. * generic_file_direct_write and locking the extent. The
  5741. * invalidate needs to happen so that reads after a write do not
  5742. * get stale data.
  5743. */
  5744. if (!ordered && (!writing ||
  5745. !test_range_bit(&BTRFS_I(inode)->io_tree,
  5746. lockstart, lockend, EXTENT_UPTODATE, 0,
  5747. *cached_state)))
  5748. break;
  5749. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5750. cached_state, GFP_NOFS);
  5751. if (ordered) {
  5752. btrfs_start_ordered_extent(inode, ordered, 1);
  5753. btrfs_put_ordered_extent(ordered);
  5754. } else {
  5755. /* Screw you mmap */
  5756. ret = filemap_write_and_wait_range(inode->i_mapping,
  5757. lockstart,
  5758. lockend);
  5759. if (ret)
  5760. break;
  5761. /*
  5762. * If we found a page that couldn't be invalidated just
  5763. * fall back to buffered.
  5764. */
  5765. ret = invalidate_inode_pages2_range(inode->i_mapping,
  5766. lockstart >> PAGE_CACHE_SHIFT,
  5767. lockend >> PAGE_CACHE_SHIFT);
  5768. if (ret)
  5769. break;
  5770. }
  5771. cond_resched();
  5772. }
  5773. return ret;
  5774. }
  5775. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  5776. u64 len, u64 orig_start,
  5777. u64 block_start, u64 block_len,
  5778. u64 orig_block_len, u64 ram_bytes,
  5779. int type)
  5780. {
  5781. struct extent_map_tree *em_tree;
  5782. struct extent_map *em;
  5783. struct btrfs_root *root = BTRFS_I(inode)->root;
  5784. int ret;
  5785. em_tree = &BTRFS_I(inode)->extent_tree;
  5786. em = alloc_extent_map();
  5787. if (!em)
  5788. return ERR_PTR(-ENOMEM);
  5789. em->start = start;
  5790. em->orig_start = orig_start;
  5791. em->mod_start = start;
  5792. em->mod_len = len;
  5793. em->len = len;
  5794. em->block_len = block_len;
  5795. em->block_start = block_start;
  5796. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5797. em->orig_block_len = orig_block_len;
  5798. em->ram_bytes = ram_bytes;
  5799. em->generation = -1;
  5800. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5801. if (type == BTRFS_ORDERED_PREALLOC)
  5802. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  5803. do {
  5804. btrfs_drop_extent_cache(inode, em->start,
  5805. em->start + em->len - 1, 0);
  5806. write_lock(&em_tree->lock);
  5807. ret = add_extent_mapping(em_tree, em, 1);
  5808. write_unlock(&em_tree->lock);
  5809. } while (ret == -EEXIST);
  5810. if (ret) {
  5811. free_extent_map(em);
  5812. return ERR_PTR(ret);
  5813. }
  5814. return em;
  5815. }
  5816. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  5817. struct buffer_head *bh_result, int create)
  5818. {
  5819. struct extent_map *em;
  5820. struct btrfs_root *root = BTRFS_I(inode)->root;
  5821. struct extent_state *cached_state = NULL;
  5822. u64 start = iblock << inode->i_blkbits;
  5823. u64 lockstart, lockend;
  5824. u64 len = bh_result->b_size;
  5825. struct btrfs_trans_handle *trans;
  5826. int unlock_bits = EXTENT_LOCKED;
  5827. int ret = 0;
  5828. if (create)
  5829. unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
  5830. else
  5831. len = min_t(u64, len, root->sectorsize);
  5832. lockstart = start;
  5833. lockend = start + len - 1;
  5834. /*
  5835. * If this errors out it's because we couldn't invalidate pagecache for
  5836. * this range and we need to fallback to buffered.
  5837. */
  5838. if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
  5839. return -ENOTBLK;
  5840. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  5841. if (IS_ERR(em)) {
  5842. ret = PTR_ERR(em);
  5843. goto unlock_err;
  5844. }
  5845. /*
  5846. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  5847. * io. INLINE is special, and we could probably kludge it in here, but
  5848. * it's still buffered so for safety lets just fall back to the generic
  5849. * buffered path.
  5850. *
  5851. * For COMPRESSED we _have_ to read the entire extent in so we can
  5852. * decompress it, so there will be buffering required no matter what we
  5853. * do, so go ahead and fallback to buffered.
  5854. *
  5855. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  5856. * to buffered IO. Don't blame me, this is the price we pay for using
  5857. * the generic code.
  5858. */
  5859. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  5860. em->block_start == EXTENT_MAP_INLINE) {
  5861. free_extent_map(em);
  5862. ret = -ENOTBLK;
  5863. goto unlock_err;
  5864. }
  5865. /* Just a good old fashioned hole, return */
  5866. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  5867. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5868. free_extent_map(em);
  5869. goto unlock_err;
  5870. }
  5871. /*
  5872. * We don't allocate a new extent in the following cases
  5873. *
  5874. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  5875. * existing extent.
  5876. * 2) The extent is marked as PREALLOC. We're good to go here and can
  5877. * just use the extent.
  5878. *
  5879. */
  5880. if (!create) {
  5881. len = min(len, em->len - (start - em->start));
  5882. lockstart = start + len;
  5883. goto unlock;
  5884. }
  5885. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  5886. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  5887. em->block_start != EXTENT_MAP_HOLE)) {
  5888. int type;
  5889. int ret;
  5890. u64 block_start, orig_start, orig_block_len, ram_bytes;
  5891. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5892. type = BTRFS_ORDERED_PREALLOC;
  5893. else
  5894. type = BTRFS_ORDERED_NOCOW;
  5895. len = min(len, em->len - (start - em->start));
  5896. block_start = em->block_start + (start - em->start);
  5897. /*
  5898. * we're not going to log anything, but we do need
  5899. * to make sure the current transaction stays open
  5900. * while we look for nocow cross refs
  5901. */
  5902. trans = btrfs_join_transaction(root);
  5903. if (IS_ERR(trans))
  5904. goto must_cow;
  5905. if (can_nocow_extent(trans, inode, start, &len, &orig_start,
  5906. &orig_block_len, &ram_bytes) == 1) {
  5907. if (type == BTRFS_ORDERED_PREALLOC) {
  5908. free_extent_map(em);
  5909. em = create_pinned_em(inode, start, len,
  5910. orig_start,
  5911. block_start, len,
  5912. orig_block_len,
  5913. ram_bytes, type);
  5914. if (IS_ERR(em)) {
  5915. btrfs_end_transaction(trans, root);
  5916. goto unlock_err;
  5917. }
  5918. }
  5919. ret = btrfs_add_ordered_extent_dio(inode, start,
  5920. block_start, len, len, type);
  5921. btrfs_end_transaction(trans, root);
  5922. if (ret) {
  5923. free_extent_map(em);
  5924. goto unlock_err;
  5925. }
  5926. goto unlock;
  5927. }
  5928. btrfs_end_transaction(trans, root);
  5929. }
  5930. must_cow:
  5931. /*
  5932. * this will cow the extent, reset the len in case we changed
  5933. * it above
  5934. */
  5935. len = bh_result->b_size;
  5936. free_extent_map(em);
  5937. em = btrfs_new_extent_direct(inode, start, len);
  5938. if (IS_ERR(em)) {
  5939. ret = PTR_ERR(em);
  5940. goto unlock_err;
  5941. }
  5942. len = min(len, em->len - (start - em->start));
  5943. unlock:
  5944. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  5945. inode->i_blkbits;
  5946. bh_result->b_size = len;
  5947. bh_result->b_bdev = em->bdev;
  5948. set_buffer_mapped(bh_result);
  5949. if (create) {
  5950. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5951. set_buffer_new(bh_result);
  5952. /*
  5953. * Need to update the i_size under the extent lock so buffered
  5954. * readers will get the updated i_size when we unlock.
  5955. */
  5956. if (start + len > i_size_read(inode))
  5957. i_size_write(inode, start + len);
  5958. spin_lock(&BTRFS_I(inode)->lock);
  5959. BTRFS_I(inode)->outstanding_extents++;
  5960. spin_unlock(&BTRFS_I(inode)->lock);
  5961. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5962. lockstart + len - 1, EXTENT_DELALLOC, NULL,
  5963. &cached_state, GFP_NOFS);
  5964. BUG_ON(ret);
  5965. }
  5966. /*
  5967. * In the case of write we need to clear and unlock the entire range,
  5968. * in the case of read we need to unlock only the end area that we
  5969. * aren't using if there is any left over space.
  5970. */
  5971. if (lockstart < lockend) {
  5972. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5973. lockend, unlock_bits, 1, 0,
  5974. &cached_state, GFP_NOFS);
  5975. } else {
  5976. free_extent_state(cached_state);
  5977. }
  5978. free_extent_map(em);
  5979. return 0;
  5980. unlock_err:
  5981. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5982. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  5983. return ret;
  5984. }
  5985. static void btrfs_endio_direct_read(struct bio *bio, int err)
  5986. {
  5987. struct btrfs_dio_private *dip = bio->bi_private;
  5988. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  5989. struct bio_vec *bvec = bio->bi_io_vec;
  5990. struct inode *inode = dip->inode;
  5991. struct btrfs_root *root = BTRFS_I(inode)->root;
  5992. struct bio *dio_bio;
  5993. u32 *csums = (u32 *)dip->csum;
  5994. int index = 0;
  5995. u64 start;
  5996. start = dip->logical_offset;
  5997. do {
  5998. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  5999. struct page *page = bvec->bv_page;
  6000. char *kaddr;
  6001. u32 csum = ~(u32)0;
  6002. unsigned long flags;
  6003. local_irq_save(flags);
  6004. kaddr = kmap_atomic(page);
  6005. csum = btrfs_csum_data(kaddr + bvec->bv_offset,
  6006. csum, bvec->bv_len);
  6007. btrfs_csum_final(csum, (char *)&csum);
  6008. kunmap_atomic(kaddr);
  6009. local_irq_restore(flags);
  6010. flush_dcache_page(bvec->bv_page);
  6011. if (csum != csums[index]) {
  6012. btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
  6013. (unsigned long long)btrfs_ino(inode),
  6014. (unsigned long long)start,
  6015. csum, csums[index]);
  6016. err = -EIO;
  6017. }
  6018. }
  6019. start += bvec->bv_len;
  6020. bvec++;
  6021. index++;
  6022. } while (bvec <= bvec_end);
  6023. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  6024. dip->logical_offset + dip->bytes - 1);
  6025. dio_bio = dip->dio_bio;
  6026. kfree(dip);
  6027. /* If we had a csum failure make sure to clear the uptodate flag */
  6028. if (err)
  6029. clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
  6030. dio_end_io(dio_bio, err);
  6031. bio_put(bio);
  6032. }
  6033. static void btrfs_endio_direct_write(struct bio *bio, int err)
  6034. {
  6035. struct btrfs_dio_private *dip = bio->bi_private;
  6036. struct inode *inode = dip->inode;
  6037. struct btrfs_root *root = BTRFS_I(inode)->root;
  6038. struct btrfs_ordered_extent *ordered = NULL;
  6039. u64 ordered_offset = dip->logical_offset;
  6040. u64 ordered_bytes = dip->bytes;
  6041. struct bio *dio_bio;
  6042. int ret;
  6043. if (err)
  6044. goto out_done;
  6045. again:
  6046. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  6047. &ordered_offset,
  6048. ordered_bytes, !err);
  6049. if (!ret)
  6050. goto out_test;
  6051. ordered->work.func = finish_ordered_fn;
  6052. ordered->work.flags = 0;
  6053. btrfs_queue_worker(&root->fs_info->endio_write_workers,
  6054. &ordered->work);
  6055. out_test:
  6056. /*
  6057. * our bio might span multiple ordered extents. If we haven't
  6058. * completed the accounting for the whole dio, go back and try again
  6059. */
  6060. if (ordered_offset < dip->logical_offset + dip->bytes) {
  6061. ordered_bytes = dip->logical_offset + dip->bytes -
  6062. ordered_offset;
  6063. ordered = NULL;
  6064. goto again;
  6065. }
  6066. out_done:
  6067. dio_bio = dip->dio_bio;
  6068. kfree(dip);
  6069. /* If we had an error make sure to clear the uptodate flag */
  6070. if (err)
  6071. clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
  6072. dio_end_io(dio_bio, err);
  6073. bio_put(bio);
  6074. }
  6075. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  6076. struct bio *bio, int mirror_num,
  6077. unsigned long bio_flags, u64 offset)
  6078. {
  6079. int ret;
  6080. struct btrfs_root *root = BTRFS_I(inode)->root;
  6081. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  6082. BUG_ON(ret); /* -ENOMEM */
  6083. return 0;
  6084. }
  6085. static void btrfs_end_dio_bio(struct bio *bio, int err)
  6086. {
  6087. struct btrfs_dio_private *dip = bio->bi_private;
  6088. if (err) {
  6089. printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
  6090. "sector %#Lx len %u err no %d\n",
  6091. (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
  6092. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  6093. dip->errors = 1;
  6094. /*
  6095. * before atomic variable goto zero, we must make sure
  6096. * dip->errors is perceived to be set.
  6097. */
  6098. smp_mb__before_atomic_dec();
  6099. }
  6100. /* if there are more bios still pending for this dio, just exit */
  6101. if (!atomic_dec_and_test(&dip->pending_bios))
  6102. goto out;
  6103. if (dip->errors) {
  6104. bio_io_error(dip->orig_bio);
  6105. } else {
  6106. set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
  6107. bio_endio(dip->orig_bio, 0);
  6108. }
  6109. out:
  6110. bio_put(bio);
  6111. }
  6112. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  6113. u64 first_sector, gfp_t gfp_flags)
  6114. {
  6115. int nr_vecs = bio_get_nr_vecs(bdev);
  6116. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  6117. }
  6118. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  6119. int rw, u64 file_offset, int skip_sum,
  6120. int async_submit)
  6121. {
  6122. struct btrfs_dio_private *dip = bio->bi_private;
  6123. int write = rw & REQ_WRITE;
  6124. struct btrfs_root *root = BTRFS_I(inode)->root;
  6125. int ret;
  6126. if (async_submit)
  6127. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  6128. bio_get(bio);
  6129. if (!write) {
  6130. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  6131. if (ret)
  6132. goto err;
  6133. }
  6134. if (skip_sum)
  6135. goto map;
  6136. if (write && async_submit) {
  6137. ret = btrfs_wq_submit_bio(root->fs_info,
  6138. inode, rw, bio, 0, 0,
  6139. file_offset,
  6140. __btrfs_submit_bio_start_direct_io,
  6141. __btrfs_submit_bio_done);
  6142. goto err;
  6143. } else if (write) {
  6144. /*
  6145. * If we aren't doing async submit, calculate the csum of the
  6146. * bio now.
  6147. */
  6148. ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
  6149. if (ret)
  6150. goto err;
  6151. } else if (!skip_sum) {
  6152. ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
  6153. file_offset);
  6154. if (ret)
  6155. goto err;
  6156. }
  6157. map:
  6158. ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
  6159. err:
  6160. bio_put(bio);
  6161. return ret;
  6162. }
  6163. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  6164. int skip_sum)
  6165. {
  6166. struct inode *inode = dip->inode;
  6167. struct btrfs_root *root = BTRFS_I(inode)->root;
  6168. struct bio *bio;
  6169. struct bio *orig_bio = dip->orig_bio;
  6170. struct bio_vec *bvec = orig_bio->bi_io_vec;
  6171. u64 start_sector = orig_bio->bi_sector;
  6172. u64 file_offset = dip->logical_offset;
  6173. u64 submit_len = 0;
  6174. u64 map_length;
  6175. int nr_pages = 0;
  6176. int ret = 0;
  6177. int async_submit = 0;
  6178. map_length = orig_bio->bi_size;
  6179. ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
  6180. &map_length, NULL, 0);
  6181. if (ret) {
  6182. bio_put(orig_bio);
  6183. return -EIO;
  6184. }
  6185. if (map_length >= orig_bio->bi_size) {
  6186. bio = orig_bio;
  6187. goto submit;
  6188. }
  6189. /* async crcs make it difficult to collect full stripe writes. */
  6190. if (btrfs_get_alloc_profile(root, 1) &
  6191. (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
  6192. async_submit = 0;
  6193. else
  6194. async_submit = 1;
  6195. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  6196. if (!bio)
  6197. return -ENOMEM;
  6198. bio->bi_private = dip;
  6199. bio->bi_end_io = btrfs_end_dio_bio;
  6200. atomic_inc(&dip->pending_bios);
  6201. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  6202. if (unlikely(map_length < submit_len + bvec->bv_len ||
  6203. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  6204. bvec->bv_offset) < bvec->bv_len)) {
  6205. /*
  6206. * inc the count before we submit the bio so
  6207. * we know the end IO handler won't happen before
  6208. * we inc the count. Otherwise, the dip might get freed
  6209. * before we're done setting it up
  6210. */
  6211. atomic_inc(&dip->pending_bios);
  6212. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  6213. file_offset, skip_sum,
  6214. async_submit);
  6215. if (ret) {
  6216. bio_put(bio);
  6217. atomic_dec(&dip->pending_bios);
  6218. goto out_err;
  6219. }
  6220. start_sector += submit_len >> 9;
  6221. file_offset += submit_len;
  6222. submit_len = 0;
  6223. nr_pages = 0;
  6224. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  6225. start_sector, GFP_NOFS);
  6226. if (!bio)
  6227. goto out_err;
  6228. bio->bi_private = dip;
  6229. bio->bi_end_io = btrfs_end_dio_bio;
  6230. map_length = orig_bio->bi_size;
  6231. ret = btrfs_map_block(root->fs_info, rw,
  6232. start_sector << 9,
  6233. &map_length, NULL, 0);
  6234. if (ret) {
  6235. bio_put(bio);
  6236. goto out_err;
  6237. }
  6238. } else {
  6239. submit_len += bvec->bv_len;
  6240. nr_pages ++;
  6241. bvec++;
  6242. }
  6243. }
  6244. submit:
  6245. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  6246. async_submit);
  6247. if (!ret)
  6248. return 0;
  6249. bio_put(bio);
  6250. out_err:
  6251. dip->errors = 1;
  6252. /*
  6253. * before atomic variable goto zero, we must
  6254. * make sure dip->errors is perceived to be set.
  6255. */
  6256. smp_mb__before_atomic_dec();
  6257. if (atomic_dec_and_test(&dip->pending_bios))
  6258. bio_io_error(dip->orig_bio);
  6259. /* bio_end_io() will handle error, so we needn't return it */
  6260. return 0;
  6261. }
  6262. static void btrfs_submit_direct(int rw, struct bio *dio_bio,
  6263. struct inode *inode, loff_t file_offset)
  6264. {
  6265. struct btrfs_root *root = BTRFS_I(inode)->root;
  6266. struct btrfs_dio_private *dip;
  6267. struct bio *io_bio;
  6268. int skip_sum;
  6269. int sum_len;
  6270. int write = rw & REQ_WRITE;
  6271. int ret = 0;
  6272. u16 csum_size;
  6273. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  6274. io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
  6275. if (!io_bio) {
  6276. ret = -ENOMEM;
  6277. goto free_ordered;
  6278. }
  6279. if (!skip_sum && !write) {
  6280. csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  6281. sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
  6282. sum_len *= csum_size;
  6283. } else {
  6284. sum_len = 0;
  6285. }
  6286. dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
  6287. if (!dip) {
  6288. ret = -ENOMEM;
  6289. goto free_io_bio;
  6290. }
  6291. dip->private = dio_bio->bi_private;
  6292. dip->inode = inode;
  6293. dip->logical_offset = file_offset;
  6294. dip->bytes = dio_bio->bi_size;
  6295. dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
  6296. io_bio->bi_private = dip;
  6297. dip->errors = 0;
  6298. dip->orig_bio = io_bio;
  6299. dip->dio_bio = dio_bio;
  6300. atomic_set(&dip->pending_bios, 0);
  6301. if (write)
  6302. io_bio->bi_end_io = btrfs_endio_direct_write;
  6303. else
  6304. io_bio->bi_end_io = btrfs_endio_direct_read;
  6305. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  6306. if (!ret)
  6307. return;
  6308. free_io_bio:
  6309. bio_put(io_bio);
  6310. free_ordered:
  6311. /*
  6312. * If this is a write, we need to clean up the reserved space and kill
  6313. * the ordered extent.
  6314. */
  6315. if (write) {
  6316. struct btrfs_ordered_extent *ordered;
  6317. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  6318. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  6319. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  6320. btrfs_free_reserved_extent(root, ordered->start,
  6321. ordered->disk_len);
  6322. btrfs_put_ordered_extent(ordered);
  6323. btrfs_put_ordered_extent(ordered);
  6324. }
  6325. bio_endio(dio_bio, ret);
  6326. }
  6327. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  6328. const struct iovec *iov, loff_t offset,
  6329. unsigned long nr_segs)
  6330. {
  6331. int seg;
  6332. int i;
  6333. size_t size;
  6334. unsigned long addr;
  6335. unsigned blocksize_mask = root->sectorsize - 1;
  6336. ssize_t retval = -EINVAL;
  6337. loff_t end = offset;
  6338. if (offset & blocksize_mask)
  6339. goto out;
  6340. /* Check the memory alignment. Blocks cannot straddle pages */
  6341. for (seg = 0; seg < nr_segs; seg++) {
  6342. addr = (unsigned long)iov[seg].iov_base;
  6343. size = iov[seg].iov_len;
  6344. end += size;
  6345. if ((addr & blocksize_mask) || (size & blocksize_mask))
  6346. goto out;
  6347. /* If this is a write we don't need to check anymore */
  6348. if (rw & WRITE)
  6349. continue;
  6350. /*
  6351. * Check to make sure we don't have duplicate iov_base's in this
  6352. * iovec, if so return EINVAL, otherwise we'll get csum errors
  6353. * when reading back.
  6354. */
  6355. for (i = seg + 1; i < nr_segs; i++) {
  6356. if (iov[seg].iov_base == iov[i].iov_base)
  6357. goto out;
  6358. }
  6359. }
  6360. retval = 0;
  6361. out:
  6362. return retval;
  6363. }
  6364. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  6365. const struct iovec *iov, loff_t offset,
  6366. unsigned long nr_segs)
  6367. {
  6368. struct file *file = iocb->ki_filp;
  6369. struct inode *inode = file->f_mapping->host;
  6370. size_t count = 0;
  6371. int flags = 0;
  6372. bool wakeup = true;
  6373. bool relock = false;
  6374. ssize_t ret;
  6375. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  6376. offset, nr_segs))
  6377. return 0;
  6378. atomic_inc(&inode->i_dio_count);
  6379. smp_mb__after_atomic_inc();
  6380. /*
  6381. * The generic stuff only does filemap_write_and_wait_range, which isn't
  6382. * enough if we've written compressed pages to this area, so we need to
  6383. * call btrfs_wait_ordered_range to make absolutely sure that any
  6384. * outstanding dirty pages are on disk.
  6385. */
  6386. count = iov_length(iov, nr_segs);
  6387. btrfs_wait_ordered_range(inode, offset, count);
  6388. if (rw & WRITE) {
  6389. /*
  6390. * If the write DIO is beyond the EOF, we need update
  6391. * the isize, but it is protected by i_mutex. So we can
  6392. * not unlock the i_mutex at this case.
  6393. */
  6394. if (offset + count <= inode->i_size) {
  6395. mutex_unlock(&inode->i_mutex);
  6396. relock = true;
  6397. }
  6398. ret = btrfs_delalloc_reserve_space(inode, count);
  6399. if (ret)
  6400. goto out;
  6401. } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  6402. &BTRFS_I(inode)->runtime_flags))) {
  6403. inode_dio_done(inode);
  6404. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  6405. wakeup = false;
  6406. }
  6407. ret = __blockdev_direct_IO(rw, iocb, inode,
  6408. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  6409. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  6410. btrfs_submit_direct, flags);
  6411. if (rw & WRITE) {
  6412. if (ret < 0 && ret != -EIOCBQUEUED)
  6413. btrfs_delalloc_release_space(inode, count);
  6414. else if (ret >= 0 && (size_t)ret < count)
  6415. btrfs_delalloc_release_space(inode,
  6416. count - (size_t)ret);
  6417. else
  6418. btrfs_delalloc_release_metadata(inode, 0);
  6419. }
  6420. out:
  6421. if (wakeup)
  6422. inode_dio_done(inode);
  6423. if (relock)
  6424. mutex_lock(&inode->i_mutex);
  6425. return ret;
  6426. }
  6427. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  6428. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  6429. __u64 start, __u64 len)
  6430. {
  6431. int ret;
  6432. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  6433. if (ret)
  6434. return ret;
  6435. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  6436. }
  6437. int btrfs_readpage(struct file *file, struct page *page)
  6438. {
  6439. struct extent_io_tree *tree;
  6440. tree = &BTRFS_I(page->mapping->host)->io_tree;
  6441. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  6442. }
  6443. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  6444. {
  6445. struct extent_io_tree *tree;
  6446. if (current->flags & PF_MEMALLOC) {
  6447. redirty_page_for_writepage(wbc, page);
  6448. unlock_page(page);
  6449. return 0;
  6450. }
  6451. tree = &BTRFS_I(page->mapping->host)->io_tree;
  6452. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  6453. }
  6454. static int btrfs_writepages(struct address_space *mapping,
  6455. struct writeback_control *wbc)
  6456. {
  6457. struct extent_io_tree *tree;
  6458. tree = &BTRFS_I(mapping->host)->io_tree;
  6459. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  6460. }
  6461. static int
  6462. btrfs_readpages(struct file *file, struct address_space *mapping,
  6463. struct list_head *pages, unsigned nr_pages)
  6464. {
  6465. struct extent_io_tree *tree;
  6466. tree = &BTRFS_I(mapping->host)->io_tree;
  6467. return extent_readpages(tree, mapping, pages, nr_pages,
  6468. btrfs_get_extent);
  6469. }
  6470. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  6471. {
  6472. struct extent_io_tree *tree;
  6473. struct extent_map_tree *map;
  6474. int ret;
  6475. tree = &BTRFS_I(page->mapping->host)->io_tree;
  6476. map = &BTRFS_I(page->mapping->host)->extent_tree;
  6477. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  6478. if (ret == 1) {
  6479. ClearPagePrivate(page);
  6480. set_page_private(page, 0);
  6481. page_cache_release(page);
  6482. }
  6483. return ret;
  6484. }
  6485. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  6486. {
  6487. if (PageWriteback(page) || PageDirty(page))
  6488. return 0;
  6489. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  6490. }
  6491. static void btrfs_invalidatepage(struct page *page, unsigned int offset,
  6492. unsigned int length)
  6493. {
  6494. struct inode *inode = page->mapping->host;
  6495. struct extent_io_tree *tree;
  6496. struct btrfs_ordered_extent *ordered;
  6497. struct extent_state *cached_state = NULL;
  6498. u64 page_start = page_offset(page);
  6499. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  6500. /*
  6501. * we have the page locked, so new writeback can't start,
  6502. * and the dirty bit won't be cleared while we are here.
  6503. *
  6504. * Wait for IO on this page so that we can safely clear
  6505. * the PagePrivate2 bit and do ordered accounting
  6506. */
  6507. wait_on_page_writeback(page);
  6508. tree = &BTRFS_I(inode)->io_tree;
  6509. if (offset) {
  6510. btrfs_releasepage(page, GFP_NOFS);
  6511. return;
  6512. }
  6513. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  6514. ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
  6515. if (ordered) {
  6516. /*
  6517. * IO on this page will never be started, so we need
  6518. * to account for any ordered extents now
  6519. */
  6520. clear_extent_bit(tree, page_start, page_end,
  6521. EXTENT_DIRTY | EXTENT_DELALLOC |
  6522. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  6523. EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
  6524. /*
  6525. * whoever cleared the private bit is responsible
  6526. * for the finish_ordered_io
  6527. */
  6528. if (TestClearPagePrivate2(page) &&
  6529. btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
  6530. PAGE_CACHE_SIZE, 1)) {
  6531. btrfs_finish_ordered_io(ordered);
  6532. }
  6533. btrfs_put_ordered_extent(ordered);
  6534. cached_state = NULL;
  6535. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  6536. }
  6537. clear_extent_bit(tree, page_start, page_end,
  6538. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  6539. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
  6540. &cached_state, GFP_NOFS);
  6541. __btrfs_releasepage(page, GFP_NOFS);
  6542. ClearPageChecked(page);
  6543. if (PagePrivate(page)) {
  6544. ClearPagePrivate(page);
  6545. set_page_private(page, 0);
  6546. page_cache_release(page);
  6547. }
  6548. }
  6549. /*
  6550. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  6551. * called from a page fault handler when a page is first dirtied. Hence we must
  6552. * be careful to check for EOF conditions here. We set the page up correctly
  6553. * for a written page which means we get ENOSPC checking when writing into
  6554. * holes and correct delalloc and unwritten extent mapping on filesystems that
  6555. * support these features.
  6556. *
  6557. * We are not allowed to take the i_mutex here so we have to play games to
  6558. * protect against truncate races as the page could now be beyond EOF. Because
  6559. * vmtruncate() writes the inode size before removing pages, once we have the
  6560. * page lock we can determine safely if the page is beyond EOF. If it is not
  6561. * beyond EOF, then the page is guaranteed safe against truncation until we
  6562. * unlock the page.
  6563. */
  6564. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  6565. {
  6566. struct page *page = vmf->page;
  6567. struct inode *inode = file_inode(vma->vm_file);
  6568. struct btrfs_root *root = BTRFS_I(inode)->root;
  6569. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6570. struct btrfs_ordered_extent *ordered;
  6571. struct extent_state *cached_state = NULL;
  6572. char *kaddr;
  6573. unsigned long zero_start;
  6574. loff_t size;
  6575. int ret;
  6576. int reserved = 0;
  6577. u64 page_start;
  6578. u64 page_end;
  6579. sb_start_pagefault(inode->i_sb);
  6580. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  6581. if (!ret) {
  6582. ret = file_update_time(vma->vm_file);
  6583. reserved = 1;
  6584. }
  6585. if (ret) {
  6586. if (ret == -ENOMEM)
  6587. ret = VM_FAULT_OOM;
  6588. else /* -ENOSPC, -EIO, etc */
  6589. ret = VM_FAULT_SIGBUS;
  6590. if (reserved)
  6591. goto out;
  6592. goto out_noreserve;
  6593. }
  6594. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  6595. again:
  6596. lock_page(page);
  6597. size = i_size_read(inode);
  6598. page_start = page_offset(page);
  6599. page_end = page_start + PAGE_CACHE_SIZE - 1;
  6600. if ((page->mapping != inode->i_mapping) ||
  6601. (page_start >= size)) {
  6602. /* page got truncated out from underneath us */
  6603. goto out_unlock;
  6604. }
  6605. wait_on_page_writeback(page);
  6606. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  6607. set_page_extent_mapped(page);
  6608. /*
  6609. * we can't set the delalloc bits if there are pending ordered
  6610. * extents. Drop our locks and wait for them to finish
  6611. */
  6612. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  6613. if (ordered) {
  6614. unlock_extent_cached(io_tree, page_start, page_end,
  6615. &cached_state, GFP_NOFS);
  6616. unlock_page(page);
  6617. btrfs_start_ordered_extent(inode, ordered, 1);
  6618. btrfs_put_ordered_extent(ordered);
  6619. goto again;
  6620. }
  6621. /*
  6622. * XXX - page_mkwrite gets called every time the page is dirtied, even
  6623. * if it was already dirty, so for space accounting reasons we need to
  6624. * clear any delalloc bits for the range we are fixing to save. There
  6625. * is probably a better way to do this, but for now keep consistent with
  6626. * prepare_pages in the normal write path.
  6627. */
  6628. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  6629. EXTENT_DIRTY | EXTENT_DELALLOC |
  6630. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  6631. 0, 0, &cached_state, GFP_NOFS);
  6632. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  6633. &cached_state);
  6634. if (ret) {
  6635. unlock_extent_cached(io_tree, page_start, page_end,
  6636. &cached_state, GFP_NOFS);
  6637. ret = VM_FAULT_SIGBUS;
  6638. goto out_unlock;
  6639. }
  6640. ret = 0;
  6641. /* page is wholly or partially inside EOF */
  6642. if (page_start + PAGE_CACHE_SIZE > size)
  6643. zero_start = size & ~PAGE_CACHE_MASK;
  6644. else
  6645. zero_start = PAGE_CACHE_SIZE;
  6646. if (zero_start != PAGE_CACHE_SIZE) {
  6647. kaddr = kmap(page);
  6648. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  6649. flush_dcache_page(page);
  6650. kunmap(page);
  6651. }
  6652. ClearPageChecked(page);
  6653. set_page_dirty(page);
  6654. SetPageUptodate(page);
  6655. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  6656. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  6657. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  6658. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  6659. out_unlock:
  6660. if (!ret) {
  6661. sb_end_pagefault(inode->i_sb);
  6662. return VM_FAULT_LOCKED;
  6663. }
  6664. unlock_page(page);
  6665. out:
  6666. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  6667. out_noreserve:
  6668. sb_end_pagefault(inode->i_sb);
  6669. return ret;
  6670. }
  6671. static int btrfs_truncate(struct inode *inode)
  6672. {
  6673. struct btrfs_root *root = BTRFS_I(inode)->root;
  6674. struct btrfs_block_rsv *rsv;
  6675. int ret = 0;
  6676. int err = 0;
  6677. struct btrfs_trans_handle *trans;
  6678. u64 mask = root->sectorsize - 1;
  6679. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  6680. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  6681. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  6682. /*
  6683. * Yes ladies and gentelment, this is indeed ugly. The fact is we have
  6684. * 3 things going on here
  6685. *
  6686. * 1) We need to reserve space for our orphan item and the space to
  6687. * delete our orphan item. Lord knows we don't want to have a dangling
  6688. * orphan item because we didn't reserve space to remove it.
  6689. *
  6690. * 2) We need to reserve space to update our inode.
  6691. *
  6692. * 3) We need to have something to cache all the space that is going to
  6693. * be free'd up by the truncate operation, but also have some slack
  6694. * space reserved in case it uses space during the truncate (thank you
  6695. * very much snapshotting).
  6696. *
  6697. * And we need these to all be seperate. The fact is we can use alot of
  6698. * space doing the truncate, and we have no earthly idea how much space
  6699. * we will use, so we need the truncate reservation to be seperate so it
  6700. * doesn't end up using space reserved for updating the inode or
  6701. * removing the orphan item. We also need to be able to stop the
  6702. * transaction and start a new one, which means we need to be able to
  6703. * update the inode several times, and we have no idea of knowing how
  6704. * many times that will be, so we can't just reserve 1 item for the
  6705. * entirety of the opration, so that has to be done seperately as well.
  6706. * Then there is the orphan item, which does indeed need to be held on
  6707. * to for the whole operation, and we need nobody to touch this reserved
  6708. * space except the orphan code.
  6709. *
  6710. * So that leaves us with
  6711. *
  6712. * 1) root->orphan_block_rsv - for the orphan deletion.
  6713. * 2) rsv - for the truncate reservation, which we will steal from the
  6714. * transaction reservation.
  6715. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  6716. * updating the inode.
  6717. */
  6718. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  6719. if (!rsv)
  6720. return -ENOMEM;
  6721. rsv->size = min_size;
  6722. rsv->failfast = 1;
  6723. /*
  6724. * 1 for the truncate slack space
  6725. * 1 for updating the inode.
  6726. */
  6727. trans = btrfs_start_transaction(root, 2);
  6728. if (IS_ERR(trans)) {
  6729. err = PTR_ERR(trans);
  6730. goto out;
  6731. }
  6732. /* Migrate the slack space for the truncate to our reserve */
  6733. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  6734. min_size);
  6735. BUG_ON(ret);
  6736. /*
  6737. * setattr is responsible for setting the ordered_data_close flag,
  6738. * but that is only tested during the last file release. That
  6739. * could happen well after the next commit, leaving a great big
  6740. * window where new writes may get lost if someone chooses to write
  6741. * to this file after truncating to zero
  6742. *
  6743. * The inode doesn't have any dirty data here, and so if we commit
  6744. * this is a noop. If someone immediately starts writing to the inode
  6745. * it is very likely we'll catch some of their writes in this
  6746. * transaction, and the commit will find this file on the ordered
  6747. * data list with good things to send down.
  6748. *
  6749. * This is a best effort solution, there is still a window where
  6750. * using truncate to replace the contents of the file will
  6751. * end up with a zero length file after a crash.
  6752. */
  6753. if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  6754. &BTRFS_I(inode)->runtime_flags))
  6755. btrfs_add_ordered_operation(trans, root, inode);
  6756. /*
  6757. * So if we truncate and then write and fsync we normally would just
  6758. * write the extents that changed, which is a problem if we need to
  6759. * first truncate that entire inode. So set this flag so we write out
  6760. * all of the extents in the inode to the sync log so we're completely
  6761. * safe.
  6762. */
  6763. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  6764. trans->block_rsv = rsv;
  6765. while (1) {
  6766. ret = btrfs_truncate_inode_items(trans, root, inode,
  6767. inode->i_size,
  6768. BTRFS_EXTENT_DATA_KEY);
  6769. if (ret != -ENOSPC) {
  6770. err = ret;
  6771. break;
  6772. }
  6773. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6774. ret = btrfs_update_inode(trans, root, inode);
  6775. if (ret) {
  6776. err = ret;
  6777. break;
  6778. }
  6779. btrfs_end_transaction(trans, root);
  6780. btrfs_btree_balance_dirty(root);
  6781. trans = btrfs_start_transaction(root, 2);
  6782. if (IS_ERR(trans)) {
  6783. ret = err = PTR_ERR(trans);
  6784. trans = NULL;
  6785. break;
  6786. }
  6787. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  6788. rsv, min_size);
  6789. BUG_ON(ret); /* shouldn't happen */
  6790. trans->block_rsv = rsv;
  6791. }
  6792. if (ret == 0 && inode->i_nlink > 0) {
  6793. trans->block_rsv = root->orphan_block_rsv;
  6794. ret = btrfs_orphan_del(trans, inode);
  6795. if (ret)
  6796. err = ret;
  6797. }
  6798. if (trans) {
  6799. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6800. ret = btrfs_update_inode(trans, root, inode);
  6801. if (ret && !err)
  6802. err = ret;
  6803. ret = btrfs_end_transaction(trans, root);
  6804. btrfs_btree_balance_dirty(root);
  6805. }
  6806. out:
  6807. btrfs_free_block_rsv(root, rsv);
  6808. if (ret && !err)
  6809. err = ret;
  6810. return err;
  6811. }
  6812. /*
  6813. * create a new subvolume directory/inode (helper for the ioctl).
  6814. */
  6815. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  6816. struct btrfs_root *new_root, u64 new_dirid)
  6817. {
  6818. struct inode *inode;
  6819. int err;
  6820. u64 index = 0;
  6821. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  6822. new_dirid, new_dirid,
  6823. S_IFDIR | (~current_umask() & S_IRWXUGO),
  6824. &index);
  6825. if (IS_ERR(inode))
  6826. return PTR_ERR(inode);
  6827. inode->i_op = &btrfs_dir_inode_operations;
  6828. inode->i_fop = &btrfs_dir_file_operations;
  6829. set_nlink(inode, 1);
  6830. btrfs_i_size_write(inode, 0);
  6831. err = btrfs_update_inode(trans, new_root, inode);
  6832. iput(inode);
  6833. return err;
  6834. }
  6835. struct inode *btrfs_alloc_inode(struct super_block *sb)
  6836. {
  6837. struct btrfs_inode *ei;
  6838. struct inode *inode;
  6839. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  6840. if (!ei)
  6841. return NULL;
  6842. ei->root = NULL;
  6843. ei->generation = 0;
  6844. ei->last_trans = 0;
  6845. ei->last_sub_trans = 0;
  6846. ei->logged_trans = 0;
  6847. ei->delalloc_bytes = 0;
  6848. ei->disk_i_size = 0;
  6849. ei->flags = 0;
  6850. ei->csum_bytes = 0;
  6851. ei->index_cnt = (u64)-1;
  6852. ei->last_unlink_trans = 0;
  6853. ei->last_log_commit = 0;
  6854. spin_lock_init(&ei->lock);
  6855. ei->outstanding_extents = 0;
  6856. ei->reserved_extents = 0;
  6857. ei->runtime_flags = 0;
  6858. ei->force_compress = BTRFS_COMPRESS_NONE;
  6859. ei->delayed_node = NULL;
  6860. inode = &ei->vfs_inode;
  6861. extent_map_tree_init(&ei->extent_tree);
  6862. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  6863. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  6864. ei->io_tree.track_uptodate = 1;
  6865. ei->io_failure_tree.track_uptodate = 1;
  6866. atomic_set(&ei->sync_writers, 0);
  6867. mutex_init(&ei->log_mutex);
  6868. mutex_init(&ei->delalloc_mutex);
  6869. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  6870. INIT_LIST_HEAD(&ei->delalloc_inodes);
  6871. INIT_LIST_HEAD(&ei->ordered_operations);
  6872. RB_CLEAR_NODE(&ei->rb_node);
  6873. return inode;
  6874. }
  6875. static void btrfs_i_callback(struct rcu_head *head)
  6876. {
  6877. struct inode *inode = container_of(head, struct inode, i_rcu);
  6878. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  6879. }
  6880. void btrfs_destroy_inode(struct inode *inode)
  6881. {
  6882. struct btrfs_ordered_extent *ordered;
  6883. struct btrfs_root *root = BTRFS_I(inode)->root;
  6884. WARN_ON(!hlist_empty(&inode->i_dentry));
  6885. WARN_ON(inode->i_data.nrpages);
  6886. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  6887. WARN_ON(BTRFS_I(inode)->reserved_extents);
  6888. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  6889. WARN_ON(BTRFS_I(inode)->csum_bytes);
  6890. /*
  6891. * This can happen where we create an inode, but somebody else also
  6892. * created the same inode and we need to destroy the one we already
  6893. * created.
  6894. */
  6895. if (!root)
  6896. goto free;
  6897. /*
  6898. * Make sure we're properly removed from the ordered operation
  6899. * lists.
  6900. */
  6901. smp_mb();
  6902. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  6903. spin_lock(&root->fs_info->ordered_root_lock);
  6904. list_del_init(&BTRFS_I(inode)->ordered_operations);
  6905. spin_unlock(&root->fs_info->ordered_root_lock);
  6906. }
  6907. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  6908. &BTRFS_I(inode)->runtime_flags)) {
  6909. btrfs_info(root->fs_info, "inode %llu still on the orphan list",
  6910. (unsigned long long)btrfs_ino(inode));
  6911. atomic_dec(&root->orphan_inodes);
  6912. }
  6913. while (1) {
  6914. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  6915. if (!ordered)
  6916. break;
  6917. else {
  6918. btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
  6919. (unsigned long long)ordered->file_offset,
  6920. (unsigned long long)ordered->len);
  6921. btrfs_remove_ordered_extent(inode, ordered);
  6922. btrfs_put_ordered_extent(ordered);
  6923. btrfs_put_ordered_extent(ordered);
  6924. }
  6925. }
  6926. inode_tree_del(inode);
  6927. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  6928. free:
  6929. call_rcu(&inode->i_rcu, btrfs_i_callback);
  6930. }
  6931. int btrfs_drop_inode(struct inode *inode)
  6932. {
  6933. struct btrfs_root *root = BTRFS_I(inode)->root;
  6934. if (root == NULL)
  6935. return 1;
  6936. /* the snap/subvol tree is on deleting */
  6937. if (btrfs_root_refs(&root->root_item) == 0 &&
  6938. root != root->fs_info->tree_root)
  6939. return 1;
  6940. else
  6941. return generic_drop_inode(inode);
  6942. }
  6943. static void init_once(void *foo)
  6944. {
  6945. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  6946. inode_init_once(&ei->vfs_inode);
  6947. }
  6948. void btrfs_destroy_cachep(void)
  6949. {
  6950. /*
  6951. * Make sure all delayed rcu free inodes are flushed before we
  6952. * destroy cache.
  6953. */
  6954. rcu_barrier();
  6955. if (btrfs_inode_cachep)
  6956. kmem_cache_destroy(btrfs_inode_cachep);
  6957. if (btrfs_trans_handle_cachep)
  6958. kmem_cache_destroy(btrfs_trans_handle_cachep);
  6959. if (btrfs_transaction_cachep)
  6960. kmem_cache_destroy(btrfs_transaction_cachep);
  6961. if (btrfs_path_cachep)
  6962. kmem_cache_destroy(btrfs_path_cachep);
  6963. if (btrfs_free_space_cachep)
  6964. kmem_cache_destroy(btrfs_free_space_cachep);
  6965. if (btrfs_delalloc_work_cachep)
  6966. kmem_cache_destroy(btrfs_delalloc_work_cachep);
  6967. }
  6968. int btrfs_init_cachep(void)
  6969. {
  6970. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  6971. sizeof(struct btrfs_inode), 0,
  6972. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  6973. if (!btrfs_inode_cachep)
  6974. goto fail;
  6975. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  6976. sizeof(struct btrfs_trans_handle), 0,
  6977. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6978. if (!btrfs_trans_handle_cachep)
  6979. goto fail;
  6980. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  6981. sizeof(struct btrfs_transaction), 0,
  6982. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6983. if (!btrfs_transaction_cachep)
  6984. goto fail;
  6985. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  6986. sizeof(struct btrfs_path), 0,
  6987. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6988. if (!btrfs_path_cachep)
  6989. goto fail;
  6990. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  6991. sizeof(struct btrfs_free_space), 0,
  6992. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6993. if (!btrfs_free_space_cachep)
  6994. goto fail;
  6995. btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
  6996. sizeof(struct btrfs_delalloc_work), 0,
  6997. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  6998. NULL);
  6999. if (!btrfs_delalloc_work_cachep)
  7000. goto fail;
  7001. return 0;
  7002. fail:
  7003. btrfs_destroy_cachep();
  7004. return -ENOMEM;
  7005. }
  7006. static int btrfs_getattr(struct vfsmount *mnt,
  7007. struct dentry *dentry, struct kstat *stat)
  7008. {
  7009. u64 delalloc_bytes;
  7010. struct inode *inode = dentry->d_inode;
  7011. u32 blocksize = inode->i_sb->s_blocksize;
  7012. generic_fillattr(inode, stat);
  7013. stat->dev = BTRFS_I(inode)->root->anon_dev;
  7014. stat->blksize = PAGE_CACHE_SIZE;
  7015. spin_lock(&BTRFS_I(inode)->lock);
  7016. delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
  7017. spin_unlock(&BTRFS_I(inode)->lock);
  7018. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  7019. ALIGN(delalloc_bytes, blocksize)) >> 9;
  7020. return 0;
  7021. }
  7022. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  7023. struct inode *new_dir, struct dentry *new_dentry)
  7024. {
  7025. struct btrfs_trans_handle *trans;
  7026. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  7027. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  7028. struct inode *new_inode = new_dentry->d_inode;
  7029. struct inode *old_inode = old_dentry->d_inode;
  7030. struct timespec ctime = CURRENT_TIME;
  7031. u64 index = 0;
  7032. u64 root_objectid;
  7033. int ret;
  7034. u64 old_ino = btrfs_ino(old_inode);
  7035. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  7036. return -EPERM;
  7037. /* we only allow rename subvolume link between subvolumes */
  7038. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  7039. return -EXDEV;
  7040. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  7041. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  7042. return -ENOTEMPTY;
  7043. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  7044. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  7045. return -ENOTEMPTY;
  7046. /* check for collisions, even if the name isn't there */
  7047. ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
  7048. new_dentry->d_name.name,
  7049. new_dentry->d_name.len);
  7050. if (ret) {
  7051. if (ret == -EEXIST) {
  7052. /* we shouldn't get
  7053. * eexist without a new_inode */
  7054. if (!new_inode) {
  7055. WARN_ON(1);
  7056. return ret;
  7057. }
  7058. } else {
  7059. /* maybe -EOVERFLOW */
  7060. return ret;
  7061. }
  7062. }
  7063. ret = 0;
  7064. /*
  7065. * we're using rename to replace one file with another.
  7066. * and the replacement file is large. Start IO on it now so
  7067. * we don't add too much work to the end of the transaction
  7068. */
  7069. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  7070. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  7071. filemap_flush(old_inode->i_mapping);
  7072. /* close the racy window with snapshot create/destroy ioctl */
  7073. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  7074. down_read(&root->fs_info->subvol_sem);
  7075. /*
  7076. * We want to reserve the absolute worst case amount of items. So if
  7077. * both inodes are subvols and we need to unlink them then that would
  7078. * require 4 item modifications, but if they are both normal inodes it
  7079. * would require 5 item modifications, so we'll assume their normal
  7080. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  7081. * should cover the worst case number of items we'll modify.
  7082. */
  7083. trans = btrfs_start_transaction(root, 11);
  7084. if (IS_ERR(trans)) {
  7085. ret = PTR_ERR(trans);
  7086. goto out_notrans;
  7087. }
  7088. if (dest != root)
  7089. btrfs_record_root_in_trans(trans, dest);
  7090. ret = btrfs_set_inode_index(new_dir, &index);
  7091. if (ret)
  7092. goto out_fail;
  7093. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  7094. /* force full log commit if subvolume involved. */
  7095. root->fs_info->last_trans_log_full_commit = trans->transid;
  7096. } else {
  7097. ret = btrfs_insert_inode_ref(trans, dest,
  7098. new_dentry->d_name.name,
  7099. new_dentry->d_name.len,
  7100. old_ino,
  7101. btrfs_ino(new_dir), index);
  7102. if (ret)
  7103. goto out_fail;
  7104. /*
  7105. * this is an ugly little race, but the rename is required
  7106. * to make sure that if we crash, the inode is either at the
  7107. * old name or the new one. pinning the log transaction lets
  7108. * us make sure we don't allow a log commit to come in after
  7109. * we unlink the name but before we add the new name back in.
  7110. */
  7111. btrfs_pin_log_trans(root);
  7112. }
  7113. /*
  7114. * make sure the inode gets flushed if it is replacing
  7115. * something.
  7116. */
  7117. if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
  7118. btrfs_add_ordered_operation(trans, root, old_inode);
  7119. inode_inc_iversion(old_dir);
  7120. inode_inc_iversion(new_dir);
  7121. inode_inc_iversion(old_inode);
  7122. old_dir->i_ctime = old_dir->i_mtime = ctime;
  7123. new_dir->i_ctime = new_dir->i_mtime = ctime;
  7124. old_inode->i_ctime = ctime;
  7125. if (old_dentry->d_parent != new_dentry->d_parent)
  7126. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  7127. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  7128. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  7129. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  7130. old_dentry->d_name.name,
  7131. old_dentry->d_name.len);
  7132. } else {
  7133. ret = __btrfs_unlink_inode(trans, root, old_dir,
  7134. old_dentry->d_inode,
  7135. old_dentry->d_name.name,
  7136. old_dentry->d_name.len);
  7137. if (!ret)
  7138. ret = btrfs_update_inode(trans, root, old_inode);
  7139. }
  7140. if (ret) {
  7141. btrfs_abort_transaction(trans, root, ret);
  7142. goto out_fail;
  7143. }
  7144. if (new_inode) {
  7145. inode_inc_iversion(new_inode);
  7146. new_inode->i_ctime = CURRENT_TIME;
  7147. if (unlikely(btrfs_ino(new_inode) ==
  7148. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  7149. root_objectid = BTRFS_I(new_inode)->location.objectid;
  7150. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  7151. root_objectid,
  7152. new_dentry->d_name.name,
  7153. new_dentry->d_name.len);
  7154. BUG_ON(new_inode->i_nlink == 0);
  7155. } else {
  7156. ret = btrfs_unlink_inode(trans, dest, new_dir,
  7157. new_dentry->d_inode,
  7158. new_dentry->d_name.name,
  7159. new_dentry->d_name.len);
  7160. }
  7161. if (!ret && new_inode->i_nlink == 0) {
  7162. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  7163. BUG_ON(ret);
  7164. }
  7165. if (ret) {
  7166. btrfs_abort_transaction(trans, root, ret);
  7167. goto out_fail;
  7168. }
  7169. }
  7170. ret = btrfs_add_link(trans, new_dir, old_inode,
  7171. new_dentry->d_name.name,
  7172. new_dentry->d_name.len, 0, index);
  7173. if (ret) {
  7174. btrfs_abort_transaction(trans, root, ret);
  7175. goto out_fail;
  7176. }
  7177. if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
  7178. struct dentry *parent = new_dentry->d_parent;
  7179. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  7180. btrfs_end_log_trans(root);
  7181. }
  7182. out_fail:
  7183. btrfs_end_transaction(trans, root);
  7184. out_notrans:
  7185. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  7186. up_read(&root->fs_info->subvol_sem);
  7187. return ret;
  7188. }
  7189. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  7190. {
  7191. struct btrfs_delalloc_work *delalloc_work;
  7192. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  7193. work);
  7194. if (delalloc_work->wait)
  7195. btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
  7196. else
  7197. filemap_flush(delalloc_work->inode->i_mapping);
  7198. if (delalloc_work->delay_iput)
  7199. btrfs_add_delayed_iput(delalloc_work->inode);
  7200. else
  7201. iput(delalloc_work->inode);
  7202. complete(&delalloc_work->completion);
  7203. }
  7204. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  7205. int wait, int delay_iput)
  7206. {
  7207. struct btrfs_delalloc_work *work;
  7208. work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
  7209. if (!work)
  7210. return NULL;
  7211. init_completion(&work->completion);
  7212. INIT_LIST_HEAD(&work->list);
  7213. work->inode = inode;
  7214. work->wait = wait;
  7215. work->delay_iput = delay_iput;
  7216. work->work.func = btrfs_run_delalloc_work;
  7217. return work;
  7218. }
  7219. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  7220. {
  7221. wait_for_completion(&work->completion);
  7222. kmem_cache_free(btrfs_delalloc_work_cachep, work);
  7223. }
  7224. /*
  7225. * some fairly slow code that needs optimization. This walks the list
  7226. * of all the inodes with pending delalloc and forces them to disk.
  7227. */
  7228. static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  7229. {
  7230. struct btrfs_inode *binode;
  7231. struct inode *inode;
  7232. struct btrfs_delalloc_work *work, *next;
  7233. struct list_head works;
  7234. struct list_head splice;
  7235. int ret = 0;
  7236. INIT_LIST_HEAD(&works);
  7237. INIT_LIST_HEAD(&splice);
  7238. spin_lock(&root->delalloc_lock);
  7239. list_splice_init(&root->delalloc_inodes, &splice);
  7240. while (!list_empty(&splice)) {
  7241. binode = list_entry(splice.next, struct btrfs_inode,
  7242. delalloc_inodes);
  7243. list_move_tail(&binode->delalloc_inodes,
  7244. &root->delalloc_inodes);
  7245. inode = igrab(&binode->vfs_inode);
  7246. if (!inode) {
  7247. cond_resched_lock(&root->delalloc_lock);
  7248. continue;
  7249. }
  7250. spin_unlock(&root->delalloc_lock);
  7251. work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
  7252. if (unlikely(!work)) {
  7253. ret = -ENOMEM;
  7254. goto out;
  7255. }
  7256. list_add_tail(&work->list, &works);
  7257. btrfs_queue_worker(&root->fs_info->flush_workers,
  7258. &work->work);
  7259. cond_resched();
  7260. spin_lock(&root->delalloc_lock);
  7261. }
  7262. spin_unlock(&root->delalloc_lock);
  7263. list_for_each_entry_safe(work, next, &works, list) {
  7264. list_del_init(&work->list);
  7265. btrfs_wait_and_free_delalloc_work(work);
  7266. }
  7267. return 0;
  7268. out:
  7269. list_for_each_entry_safe(work, next, &works, list) {
  7270. list_del_init(&work->list);
  7271. btrfs_wait_and_free_delalloc_work(work);
  7272. }
  7273. if (!list_empty_careful(&splice)) {
  7274. spin_lock(&root->delalloc_lock);
  7275. list_splice_tail(&splice, &root->delalloc_inodes);
  7276. spin_unlock(&root->delalloc_lock);
  7277. }
  7278. return ret;
  7279. }
  7280. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  7281. {
  7282. int ret;
  7283. if (root->fs_info->sb->s_flags & MS_RDONLY)
  7284. return -EROFS;
  7285. ret = __start_delalloc_inodes(root, delay_iput);
  7286. /*
  7287. * the filemap_flush will queue IO into the worker threads, but
  7288. * we have to make sure the IO is actually started and that
  7289. * ordered extents get created before we return
  7290. */
  7291. atomic_inc(&root->fs_info->async_submit_draining);
  7292. while (atomic_read(&root->fs_info->nr_async_submits) ||
  7293. atomic_read(&root->fs_info->async_delalloc_pages)) {
  7294. wait_event(root->fs_info->async_submit_wait,
  7295. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  7296. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  7297. }
  7298. atomic_dec(&root->fs_info->async_submit_draining);
  7299. return ret;
  7300. }
  7301. int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
  7302. int delay_iput)
  7303. {
  7304. struct btrfs_root *root;
  7305. struct list_head splice;
  7306. int ret;
  7307. if (fs_info->sb->s_flags & MS_RDONLY)
  7308. return -EROFS;
  7309. INIT_LIST_HEAD(&splice);
  7310. spin_lock(&fs_info->delalloc_root_lock);
  7311. list_splice_init(&fs_info->delalloc_roots, &splice);
  7312. while (!list_empty(&splice)) {
  7313. root = list_first_entry(&splice, struct btrfs_root,
  7314. delalloc_root);
  7315. root = btrfs_grab_fs_root(root);
  7316. BUG_ON(!root);
  7317. list_move_tail(&root->delalloc_root,
  7318. &fs_info->delalloc_roots);
  7319. spin_unlock(&fs_info->delalloc_root_lock);
  7320. ret = __start_delalloc_inodes(root, delay_iput);
  7321. btrfs_put_fs_root(root);
  7322. if (ret)
  7323. goto out;
  7324. spin_lock(&fs_info->delalloc_root_lock);
  7325. }
  7326. spin_unlock(&fs_info->delalloc_root_lock);
  7327. atomic_inc(&fs_info->async_submit_draining);
  7328. while (atomic_read(&fs_info->nr_async_submits) ||
  7329. atomic_read(&fs_info->async_delalloc_pages)) {
  7330. wait_event(fs_info->async_submit_wait,
  7331. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  7332. atomic_read(&fs_info->async_delalloc_pages) == 0));
  7333. }
  7334. atomic_dec(&fs_info->async_submit_draining);
  7335. return 0;
  7336. out:
  7337. if (!list_empty_careful(&splice)) {
  7338. spin_lock(&fs_info->delalloc_root_lock);
  7339. list_splice_tail(&splice, &fs_info->delalloc_roots);
  7340. spin_unlock(&fs_info->delalloc_root_lock);
  7341. }
  7342. return ret;
  7343. }
  7344. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  7345. const char *symname)
  7346. {
  7347. struct btrfs_trans_handle *trans;
  7348. struct btrfs_root *root = BTRFS_I(dir)->root;
  7349. struct btrfs_path *path;
  7350. struct btrfs_key key;
  7351. struct inode *inode = NULL;
  7352. int err;
  7353. int drop_inode = 0;
  7354. u64 objectid;
  7355. u64 index = 0 ;
  7356. int name_len;
  7357. int datasize;
  7358. unsigned long ptr;
  7359. struct btrfs_file_extent_item *ei;
  7360. struct extent_buffer *leaf;
  7361. name_len = strlen(symname) + 1;
  7362. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  7363. return -ENAMETOOLONG;
  7364. /*
  7365. * 2 items for inode item and ref
  7366. * 2 items for dir items
  7367. * 1 item for xattr if selinux is on
  7368. */
  7369. trans = btrfs_start_transaction(root, 5);
  7370. if (IS_ERR(trans))
  7371. return PTR_ERR(trans);
  7372. err = btrfs_find_free_ino(root, &objectid);
  7373. if (err)
  7374. goto out_unlock;
  7375. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  7376. dentry->d_name.len, btrfs_ino(dir), objectid,
  7377. S_IFLNK|S_IRWXUGO, &index);
  7378. if (IS_ERR(inode)) {
  7379. err = PTR_ERR(inode);
  7380. goto out_unlock;
  7381. }
  7382. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  7383. if (err) {
  7384. drop_inode = 1;
  7385. goto out_unlock;
  7386. }
  7387. /*
  7388. * If the active LSM wants to access the inode during
  7389. * d_instantiate it needs these. Smack checks to see
  7390. * if the filesystem supports xattrs by looking at the
  7391. * ops vector.
  7392. */
  7393. inode->i_fop = &btrfs_file_operations;
  7394. inode->i_op = &btrfs_file_inode_operations;
  7395. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  7396. if (err)
  7397. drop_inode = 1;
  7398. else {
  7399. inode->i_mapping->a_ops = &btrfs_aops;
  7400. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  7401. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  7402. }
  7403. if (drop_inode)
  7404. goto out_unlock;
  7405. path = btrfs_alloc_path();
  7406. if (!path) {
  7407. err = -ENOMEM;
  7408. drop_inode = 1;
  7409. goto out_unlock;
  7410. }
  7411. key.objectid = btrfs_ino(inode);
  7412. key.offset = 0;
  7413. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  7414. datasize = btrfs_file_extent_calc_inline_size(name_len);
  7415. err = btrfs_insert_empty_item(trans, root, path, &key,
  7416. datasize);
  7417. if (err) {
  7418. drop_inode = 1;
  7419. btrfs_free_path(path);
  7420. goto out_unlock;
  7421. }
  7422. leaf = path->nodes[0];
  7423. ei = btrfs_item_ptr(leaf, path->slots[0],
  7424. struct btrfs_file_extent_item);
  7425. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  7426. btrfs_set_file_extent_type(leaf, ei,
  7427. BTRFS_FILE_EXTENT_INLINE);
  7428. btrfs_set_file_extent_encryption(leaf, ei, 0);
  7429. btrfs_set_file_extent_compression(leaf, ei, 0);
  7430. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  7431. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  7432. ptr = btrfs_file_extent_inline_start(ei);
  7433. write_extent_buffer(leaf, symname, ptr, name_len);
  7434. btrfs_mark_buffer_dirty(leaf);
  7435. btrfs_free_path(path);
  7436. inode->i_op = &btrfs_symlink_inode_operations;
  7437. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  7438. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  7439. inode_set_bytes(inode, name_len);
  7440. btrfs_i_size_write(inode, name_len - 1);
  7441. err = btrfs_update_inode(trans, root, inode);
  7442. if (err)
  7443. drop_inode = 1;
  7444. out_unlock:
  7445. if (!err)
  7446. d_instantiate(dentry, inode);
  7447. btrfs_end_transaction(trans, root);
  7448. if (drop_inode) {
  7449. inode_dec_link_count(inode);
  7450. iput(inode);
  7451. }
  7452. btrfs_btree_balance_dirty(root);
  7453. return err;
  7454. }
  7455. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  7456. u64 start, u64 num_bytes, u64 min_size,
  7457. loff_t actual_len, u64 *alloc_hint,
  7458. struct btrfs_trans_handle *trans)
  7459. {
  7460. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  7461. struct extent_map *em;
  7462. struct btrfs_root *root = BTRFS_I(inode)->root;
  7463. struct btrfs_key ins;
  7464. u64 cur_offset = start;
  7465. u64 i_size;
  7466. u64 cur_bytes;
  7467. int ret = 0;
  7468. bool own_trans = true;
  7469. if (trans)
  7470. own_trans = false;
  7471. while (num_bytes > 0) {
  7472. if (own_trans) {
  7473. trans = btrfs_start_transaction(root, 3);
  7474. if (IS_ERR(trans)) {
  7475. ret = PTR_ERR(trans);
  7476. break;
  7477. }
  7478. }
  7479. cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
  7480. cur_bytes = max(cur_bytes, min_size);
  7481. ret = btrfs_reserve_extent(trans, root, cur_bytes,
  7482. min_size, 0, *alloc_hint, &ins, 1);
  7483. if (ret) {
  7484. if (own_trans)
  7485. btrfs_end_transaction(trans, root);
  7486. break;
  7487. }
  7488. ret = insert_reserved_file_extent(trans, inode,
  7489. cur_offset, ins.objectid,
  7490. ins.offset, ins.offset,
  7491. ins.offset, 0, 0, 0,
  7492. BTRFS_FILE_EXTENT_PREALLOC);
  7493. if (ret) {
  7494. btrfs_abort_transaction(trans, root, ret);
  7495. if (own_trans)
  7496. btrfs_end_transaction(trans, root);
  7497. break;
  7498. }
  7499. btrfs_drop_extent_cache(inode, cur_offset,
  7500. cur_offset + ins.offset -1, 0);
  7501. em = alloc_extent_map();
  7502. if (!em) {
  7503. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  7504. &BTRFS_I(inode)->runtime_flags);
  7505. goto next;
  7506. }
  7507. em->start = cur_offset;
  7508. em->orig_start = cur_offset;
  7509. em->len = ins.offset;
  7510. em->block_start = ins.objectid;
  7511. em->block_len = ins.offset;
  7512. em->orig_block_len = ins.offset;
  7513. em->ram_bytes = ins.offset;
  7514. em->bdev = root->fs_info->fs_devices->latest_bdev;
  7515. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  7516. em->generation = trans->transid;
  7517. while (1) {
  7518. write_lock(&em_tree->lock);
  7519. ret = add_extent_mapping(em_tree, em, 1);
  7520. write_unlock(&em_tree->lock);
  7521. if (ret != -EEXIST)
  7522. break;
  7523. btrfs_drop_extent_cache(inode, cur_offset,
  7524. cur_offset + ins.offset - 1,
  7525. 0);
  7526. }
  7527. free_extent_map(em);
  7528. next:
  7529. num_bytes -= ins.offset;
  7530. cur_offset += ins.offset;
  7531. *alloc_hint = ins.objectid + ins.offset;
  7532. inode_inc_iversion(inode);
  7533. inode->i_ctime = CURRENT_TIME;
  7534. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  7535. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  7536. (actual_len > inode->i_size) &&
  7537. (cur_offset > inode->i_size)) {
  7538. if (cur_offset > actual_len)
  7539. i_size = actual_len;
  7540. else
  7541. i_size = cur_offset;
  7542. i_size_write(inode, i_size);
  7543. btrfs_ordered_update_i_size(inode, i_size, NULL);
  7544. }
  7545. ret = btrfs_update_inode(trans, root, inode);
  7546. if (ret) {
  7547. btrfs_abort_transaction(trans, root, ret);
  7548. if (own_trans)
  7549. btrfs_end_transaction(trans, root);
  7550. break;
  7551. }
  7552. if (own_trans)
  7553. btrfs_end_transaction(trans, root);
  7554. }
  7555. return ret;
  7556. }
  7557. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  7558. u64 start, u64 num_bytes, u64 min_size,
  7559. loff_t actual_len, u64 *alloc_hint)
  7560. {
  7561. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  7562. min_size, actual_len, alloc_hint,
  7563. NULL);
  7564. }
  7565. int btrfs_prealloc_file_range_trans(struct inode *inode,
  7566. struct btrfs_trans_handle *trans, int mode,
  7567. u64 start, u64 num_bytes, u64 min_size,
  7568. loff_t actual_len, u64 *alloc_hint)
  7569. {
  7570. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  7571. min_size, actual_len, alloc_hint, trans);
  7572. }
  7573. static int btrfs_set_page_dirty(struct page *page)
  7574. {
  7575. return __set_page_dirty_nobuffers(page);
  7576. }
  7577. static int btrfs_permission(struct inode *inode, int mask)
  7578. {
  7579. struct btrfs_root *root = BTRFS_I(inode)->root;
  7580. umode_t mode = inode->i_mode;
  7581. if (mask & MAY_WRITE &&
  7582. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  7583. if (btrfs_root_readonly(root))
  7584. return -EROFS;
  7585. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  7586. return -EACCES;
  7587. }
  7588. return generic_permission(inode, mask);
  7589. }
  7590. static const struct inode_operations btrfs_dir_inode_operations = {
  7591. .getattr = btrfs_getattr,
  7592. .lookup = btrfs_lookup,
  7593. .create = btrfs_create,
  7594. .unlink = btrfs_unlink,
  7595. .link = btrfs_link,
  7596. .mkdir = btrfs_mkdir,
  7597. .rmdir = btrfs_rmdir,
  7598. .rename = btrfs_rename,
  7599. .symlink = btrfs_symlink,
  7600. .setattr = btrfs_setattr,
  7601. .mknod = btrfs_mknod,
  7602. .setxattr = btrfs_setxattr,
  7603. .getxattr = btrfs_getxattr,
  7604. .listxattr = btrfs_listxattr,
  7605. .removexattr = btrfs_removexattr,
  7606. .permission = btrfs_permission,
  7607. .get_acl = btrfs_get_acl,
  7608. };
  7609. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  7610. .lookup = btrfs_lookup,
  7611. .permission = btrfs_permission,
  7612. .get_acl = btrfs_get_acl,
  7613. };
  7614. static const struct file_operations btrfs_dir_file_operations = {
  7615. .llseek = generic_file_llseek,
  7616. .read = generic_read_dir,
  7617. .iterate = btrfs_real_readdir,
  7618. .unlocked_ioctl = btrfs_ioctl,
  7619. #ifdef CONFIG_COMPAT
  7620. .compat_ioctl = btrfs_ioctl,
  7621. #endif
  7622. .release = btrfs_release_file,
  7623. .fsync = btrfs_sync_file,
  7624. };
  7625. static struct extent_io_ops btrfs_extent_io_ops = {
  7626. .fill_delalloc = run_delalloc_range,
  7627. .submit_bio_hook = btrfs_submit_bio_hook,
  7628. .merge_bio_hook = btrfs_merge_bio_hook,
  7629. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  7630. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  7631. .writepage_start_hook = btrfs_writepage_start_hook,
  7632. .set_bit_hook = btrfs_set_bit_hook,
  7633. .clear_bit_hook = btrfs_clear_bit_hook,
  7634. .merge_extent_hook = btrfs_merge_extent_hook,
  7635. .split_extent_hook = btrfs_split_extent_hook,
  7636. };
  7637. /*
  7638. * btrfs doesn't support the bmap operation because swapfiles
  7639. * use bmap to make a mapping of extents in the file. They assume
  7640. * these extents won't change over the life of the file and they
  7641. * use the bmap result to do IO directly to the drive.
  7642. *
  7643. * the btrfs bmap call would return logical addresses that aren't
  7644. * suitable for IO and they also will change frequently as COW
  7645. * operations happen. So, swapfile + btrfs == corruption.
  7646. *
  7647. * For now we're avoiding this by dropping bmap.
  7648. */
  7649. static const struct address_space_operations btrfs_aops = {
  7650. .readpage = btrfs_readpage,
  7651. .writepage = btrfs_writepage,
  7652. .writepages = btrfs_writepages,
  7653. .readpages = btrfs_readpages,
  7654. .direct_IO = btrfs_direct_IO,
  7655. .invalidatepage = btrfs_invalidatepage,
  7656. .releasepage = btrfs_releasepage,
  7657. .set_page_dirty = btrfs_set_page_dirty,
  7658. .error_remove_page = generic_error_remove_page,
  7659. };
  7660. static const struct address_space_operations btrfs_symlink_aops = {
  7661. .readpage = btrfs_readpage,
  7662. .writepage = btrfs_writepage,
  7663. .invalidatepage = btrfs_invalidatepage,
  7664. .releasepage = btrfs_releasepage,
  7665. };
  7666. static const struct inode_operations btrfs_file_inode_operations = {
  7667. .getattr = btrfs_getattr,
  7668. .setattr = btrfs_setattr,
  7669. .setxattr = btrfs_setxattr,
  7670. .getxattr = btrfs_getxattr,
  7671. .listxattr = btrfs_listxattr,
  7672. .removexattr = btrfs_removexattr,
  7673. .permission = btrfs_permission,
  7674. .fiemap = btrfs_fiemap,
  7675. .get_acl = btrfs_get_acl,
  7676. .update_time = btrfs_update_time,
  7677. };
  7678. static const struct inode_operations btrfs_special_inode_operations = {
  7679. .getattr = btrfs_getattr,
  7680. .setattr = btrfs_setattr,
  7681. .permission = btrfs_permission,
  7682. .setxattr = btrfs_setxattr,
  7683. .getxattr = btrfs_getxattr,
  7684. .listxattr = btrfs_listxattr,
  7685. .removexattr = btrfs_removexattr,
  7686. .get_acl = btrfs_get_acl,
  7687. .update_time = btrfs_update_time,
  7688. };
  7689. static const struct inode_operations btrfs_symlink_inode_operations = {
  7690. .readlink = generic_readlink,
  7691. .follow_link = page_follow_link_light,
  7692. .put_link = page_put_link,
  7693. .getattr = btrfs_getattr,
  7694. .setattr = btrfs_setattr,
  7695. .permission = btrfs_permission,
  7696. .setxattr = btrfs_setxattr,
  7697. .getxattr = btrfs_getxattr,
  7698. .listxattr = btrfs_listxattr,
  7699. .removexattr = btrfs_removexattr,
  7700. .get_acl = btrfs_get_acl,
  7701. .update_time = btrfs_update_time,
  7702. };
  7703. const struct dentry_operations btrfs_dentry_operations = {
  7704. .d_delete = btrfs_dentry_delete,
  7705. .d_release = btrfs_dentry_release,
  7706. };