ioctl.c 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/bio.h>
  7. #include <linux/buffer_head.h>
  8. #include <linux/file.h>
  9. #include <linux/fs.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/highmem.h>
  13. #include <linux/time.h>
  14. #include <linux/init.h>
  15. #include <linux/string.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/mount.h>
  18. #include <linux/mpage.h>
  19. #include <linux/namei.h>
  20. #include <linux/swap.h>
  21. #include <linux/writeback.h>
  22. #include <linux/compat.h>
  23. #include <linux/bit_spinlock.h>
  24. #include <linux/security.h>
  25. #include <linux/xattr.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/uuid.h>
  30. #include <linux/btrfs.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/iversion.h>
  33. #include "ctree.h"
  34. #include "disk-io.h"
  35. #include "transaction.h"
  36. #include "btrfs_inode.h"
  37. #include "print-tree.h"
  38. #include "volumes.h"
  39. #include "locking.h"
  40. #include "inode-map.h"
  41. #include "backref.h"
  42. #include "rcu-string.h"
  43. #include "send.h"
  44. #include "dev-replace.h"
  45. #include "props.h"
  46. #include "sysfs.h"
  47. #include "qgroup.h"
  48. #include "tree-log.h"
  49. #include "compression.h"
  50. #ifdef CONFIG_64BIT
  51. /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
  52. * structures are incorrect, as the timespec structure from userspace
  53. * is 4 bytes too small. We define these alternatives here to teach
  54. * the kernel about the 32-bit struct packing.
  55. */
  56. struct btrfs_ioctl_timespec_32 {
  57. __u64 sec;
  58. __u32 nsec;
  59. } __attribute__ ((__packed__));
  60. struct btrfs_ioctl_received_subvol_args_32 {
  61. char uuid[BTRFS_UUID_SIZE]; /* in */
  62. __u64 stransid; /* in */
  63. __u64 rtransid; /* out */
  64. struct btrfs_ioctl_timespec_32 stime; /* in */
  65. struct btrfs_ioctl_timespec_32 rtime; /* out */
  66. __u64 flags; /* in */
  67. __u64 reserved[16]; /* in */
  68. } __attribute__ ((__packed__));
  69. #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
  70. struct btrfs_ioctl_received_subvol_args_32)
  71. #endif
  72. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  73. struct btrfs_ioctl_send_args_32 {
  74. __s64 send_fd; /* in */
  75. __u64 clone_sources_count; /* in */
  76. compat_uptr_t clone_sources; /* in */
  77. __u64 parent_root; /* in */
  78. __u64 flags; /* in */
  79. __u64 reserved[4]; /* in */
  80. } __attribute__ ((__packed__));
  81. #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
  82. struct btrfs_ioctl_send_args_32)
  83. #endif
  84. static int btrfs_clone(struct inode *src, struct inode *inode,
  85. u64 off, u64 olen, u64 olen_aligned, u64 destoff,
  86. int no_time_update);
  87. /* Mask out flags that are inappropriate for the given type of inode. */
  88. static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
  89. unsigned int flags)
  90. {
  91. if (S_ISDIR(inode->i_mode))
  92. return flags;
  93. else if (S_ISREG(inode->i_mode))
  94. return flags & ~FS_DIRSYNC_FL;
  95. else
  96. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  97. }
  98. /*
  99. * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
  100. * ioctl.
  101. */
  102. static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
  103. {
  104. unsigned int iflags = 0;
  105. if (flags & BTRFS_INODE_SYNC)
  106. iflags |= FS_SYNC_FL;
  107. if (flags & BTRFS_INODE_IMMUTABLE)
  108. iflags |= FS_IMMUTABLE_FL;
  109. if (flags & BTRFS_INODE_APPEND)
  110. iflags |= FS_APPEND_FL;
  111. if (flags & BTRFS_INODE_NODUMP)
  112. iflags |= FS_NODUMP_FL;
  113. if (flags & BTRFS_INODE_NOATIME)
  114. iflags |= FS_NOATIME_FL;
  115. if (flags & BTRFS_INODE_DIRSYNC)
  116. iflags |= FS_DIRSYNC_FL;
  117. if (flags & BTRFS_INODE_NODATACOW)
  118. iflags |= FS_NOCOW_FL;
  119. if (flags & BTRFS_INODE_NOCOMPRESS)
  120. iflags |= FS_NOCOMP_FL;
  121. else if (flags & BTRFS_INODE_COMPRESS)
  122. iflags |= FS_COMPR_FL;
  123. return iflags;
  124. }
  125. /*
  126. * Update inode->i_flags based on the btrfs internal flags.
  127. */
  128. void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
  129. {
  130. struct btrfs_inode *binode = BTRFS_I(inode);
  131. unsigned int new_fl = 0;
  132. if (binode->flags & BTRFS_INODE_SYNC)
  133. new_fl |= S_SYNC;
  134. if (binode->flags & BTRFS_INODE_IMMUTABLE)
  135. new_fl |= S_IMMUTABLE;
  136. if (binode->flags & BTRFS_INODE_APPEND)
  137. new_fl |= S_APPEND;
  138. if (binode->flags & BTRFS_INODE_NOATIME)
  139. new_fl |= S_NOATIME;
  140. if (binode->flags & BTRFS_INODE_DIRSYNC)
  141. new_fl |= S_DIRSYNC;
  142. set_mask_bits(&inode->i_flags,
  143. S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
  144. new_fl);
  145. }
  146. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  147. {
  148. struct btrfs_inode *binode = BTRFS_I(file_inode(file));
  149. unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
  150. if (copy_to_user(arg, &flags, sizeof(flags)))
  151. return -EFAULT;
  152. return 0;
  153. }
  154. /* Check if @flags are a supported and valid set of FS_*_FL flags */
  155. static int check_fsflags(unsigned int flags)
  156. {
  157. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  158. FS_NOATIME_FL | FS_NODUMP_FL | \
  159. FS_SYNC_FL | FS_DIRSYNC_FL | \
  160. FS_NOCOMP_FL | FS_COMPR_FL |
  161. FS_NOCOW_FL))
  162. return -EOPNOTSUPP;
  163. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  164. return -EINVAL;
  165. return 0;
  166. }
  167. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  168. {
  169. struct inode *inode = file_inode(file);
  170. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  171. struct btrfs_inode *binode = BTRFS_I(inode);
  172. struct btrfs_root *root = binode->root;
  173. struct btrfs_trans_handle *trans;
  174. unsigned int fsflags, old_fsflags;
  175. int ret;
  176. u64 old_flags;
  177. unsigned int old_i_flags;
  178. umode_t mode;
  179. if (!inode_owner_or_capable(inode))
  180. return -EPERM;
  181. if (btrfs_root_readonly(root))
  182. return -EROFS;
  183. if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
  184. return -EFAULT;
  185. ret = check_fsflags(fsflags);
  186. if (ret)
  187. return ret;
  188. ret = mnt_want_write_file(file);
  189. if (ret)
  190. return ret;
  191. inode_lock(inode);
  192. old_flags = binode->flags;
  193. old_i_flags = inode->i_flags;
  194. mode = inode->i_mode;
  195. fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
  196. old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
  197. if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  198. if (!capable(CAP_LINUX_IMMUTABLE)) {
  199. ret = -EPERM;
  200. goto out_unlock;
  201. }
  202. }
  203. if (fsflags & FS_SYNC_FL)
  204. binode->flags |= BTRFS_INODE_SYNC;
  205. else
  206. binode->flags &= ~BTRFS_INODE_SYNC;
  207. if (fsflags & FS_IMMUTABLE_FL)
  208. binode->flags |= BTRFS_INODE_IMMUTABLE;
  209. else
  210. binode->flags &= ~BTRFS_INODE_IMMUTABLE;
  211. if (fsflags & FS_APPEND_FL)
  212. binode->flags |= BTRFS_INODE_APPEND;
  213. else
  214. binode->flags &= ~BTRFS_INODE_APPEND;
  215. if (fsflags & FS_NODUMP_FL)
  216. binode->flags |= BTRFS_INODE_NODUMP;
  217. else
  218. binode->flags &= ~BTRFS_INODE_NODUMP;
  219. if (fsflags & FS_NOATIME_FL)
  220. binode->flags |= BTRFS_INODE_NOATIME;
  221. else
  222. binode->flags &= ~BTRFS_INODE_NOATIME;
  223. if (fsflags & FS_DIRSYNC_FL)
  224. binode->flags |= BTRFS_INODE_DIRSYNC;
  225. else
  226. binode->flags &= ~BTRFS_INODE_DIRSYNC;
  227. if (fsflags & FS_NOCOW_FL) {
  228. if (S_ISREG(mode)) {
  229. /*
  230. * It's safe to turn csums off here, no extents exist.
  231. * Otherwise we want the flag to reflect the real COW
  232. * status of the file and will not set it.
  233. */
  234. if (inode->i_size == 0)
  235. binode->flags |= BTRFS_INODE_NODATACOW
  236. | BTRFS_INODE_NODATASUM;
  237. } else {
  238. binode->flags |= BTRFS_INODE_NODATACOW;
  239. }
  240. } else {
  241. /*
  242. * Revert back under same assumptions as above
  243. */
  244. if (S_ISREG(mode)) {
  245. if (inode->i_size == 0)
  246. binode->flags &= ~(BTRFS_INODE_NODATACOW
  247. | BTRFS_INODE_NODATASUM);
  248. } else {
  249. binode->flags &= ~BTRFS_INODE_NODATACOW;
  250. }
  251. }
  252. /*
  253. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  254. * flag may be changed automatically if compression code won't make
  255. * things smaller.
  256. */
  257. if (fsflags & FS_NOCOMP_FL) {
  258. binode->flags &= ~BTRFS_INODE_COMPRESS;
  259. binode->flags |= BTRFS_INODE_NOCOMPRESS;
  260. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  261. if (ret && ret != -ENODATA)
  262. goto out_drop;
  263. } else if (fsflags & FS_COMPR_FL) {
  264. const char *comp;
  265. binode->flags |= BTRFS_INODE_COMPRESS;
  266. binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
  267. comp = btrfs_compress_type2str(fs_info->compress_type);
  268. if (!comp || comp[0] == 0)
  269. comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
  270. ret = btrfs_set_prop(inode, "btrfs.compression",
  271. comp, strlen(comp), 0);
  272. if (ret)
  273. goto out_drop;
  274. } else {
  275. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  276. if (ret && ret != -ENODATA)
  277. goto out_drop;
  278. binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  279. }
  280. trans = btrfs_start_transaction(root, 1);
  281. if (IS_ERR(trans)) {
  282. ret = PTR_ERR(trans);
  283. goto out_drop;
  284. }
  285. btrfs_sync_inode_flags_to_i_flags(inode);
  286. inode_inc_iversion(inode);
  287. inode->i_ctime = current_time(inode);
  288. ret = btrfs_update_inode(trans, root, inode);
  289. btrfs_end_transaction(trans);
  290. out_drop:
  291. if (ret) {
  292. binode->flags = old_flags;
  293. inode->i_flags = old_i_flags;
  294. }
  295. out_unlock:
  296. inode_unlock(inode);
  297. mnt_drop_write_file(file);
  298. return ret;
  299. }
  300. /*
  301. * Translate btrfs internal inode flags to xflags as expected by the
  302. * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
  303. * silently dropped.
  304. */
  305. static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
  306. {
  307. unsigned int xflags = 0;
  308. if (flags & BTRFS_INODE_APPEND)
  309. xflags |= FS_XFLAG_APPEND;
  310. if (flags & BTRFS_INODE_IMMUTABLE)
  311. xflags |= FS_XFLAG_IMMUTABLE;
  312. if (flags & BTRFS_INODE_NOATIME)
  313. xflags |= FS_XFLAG_NOATIME;
  314. if (flags & BTRFS_INODE_NODUMP)
  315. xflags |= FS_XFLAG_NODUMP;
  316. if (flags & BTRFS_INODE_SYNC)
  317. xflags |= FS_XFLAG_SYNC;
  318. return xflags;
  319. }
  320. /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
  321. static int check_xflags(unsigned int flags)
  322. {
  323. if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
  324. FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
  325. return -EOPNOTSUPP;
  326. return 0;
  327. }
  328. /*
  329. * Set the xflags from the internal inode flags. The remaining items of fsxattr
  330. * are zeroed.
  331. */
  332. static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
  333. {
  334. struct btrfs_inode *binode = BTRFS_I(file_inode(file));
  335. struct fsxattr fa;
  336. memset(&fa, 0, sizeof(fa));
  337. fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
  338. if (copy_to_user(arg, &fa, sizeof(fa)))
  339. return -EFAULT;
  340. return 0;
  341. }
  342. static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
  343. {
  344. struct inode *inode = file_inode(file);
  345. struct btrfs_inode *binode = BTRFS_I(inode);
  346. struct btrfs_root *root = binode->root;
  347. struct btrfs_trans_handle *trans;
  348. struct fsxattr fa;
  349. unsigned old_flags;
  350. unsigned old_i_flags;
  351. int ret = 0;
  352. if (!inode_owner_or_capable(inode))
  353. return -EPERM;
  354. if (btrfs_root_readonly(root))
  355. return -EROFS;
  356. memset(&fa, 0, sizeof(fa));
  357. if (copy_from_user(&fa, arg, sizeof(fa)))
  358. return -EFAULT;
  359. ret = check_xflags(fa.fsx_xflags);
  360. if (ret)
  361. return ret;
  362. if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
  363. return -EOPNOTSUPP;
  364. ret = mnt_want_write_file(file);
  365. if (ret)
  366. return ret;
  367. inode_lock(inode);
  368. old_flags = binode->flags;
  369. old_i_flags = inode->i_flags;
  370. /* We need the capabilities to change append-only or immutable inode */
  371. if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
  372. (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
  373. !capable(CAP_LINUX_IMMUTABLE)) {
  374. ret = -EPERM;
  375. goto out_unlock;
  376. }
  377. if (fa.fsx_xflags & FS_XFLAG_SYNC)
  378. binode->flags |= BTRFS_INODE_SYNC;
  379. else
  380. binode->flags &= ~BTRFS_INODE_SYNC;
  381. if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
  382. binode->flags |= BTRFS_INODE_IMMUTABLE;
  383. else
  384. binode->flags &= ~BTRFS_INODE_IMMUTABLE;
  385. if (fa.fsx_xflags & FS_XFLAG_APPEND)
  386. binode->flags |= BTRFS_INODE_APPEND;
  387. else
  388. binode->flags &= ~BTRFS_INODE_APPEND;
  389. if (fa.fsx_xflags & FS_XFLAG_NODUMP)
  390. binode->flags |= BTRFS_INODE_NODUMP;
  391. else
  392. binode->flags &= ~BTRFS_INODE_NODUMP;
  393. if (fa.fsx_xflags & FS_XFLAG_NOATIME)
  394. binode->flags |= BTRFS_INODE_NOATIME;
  395. else
  396. binode->flags &= ~BTRFS_INODE_NOATIME;
  397. /* 1 item for the inode */
  398. trans = btrfs_start_transaction(root, 1);
  399. if (IS_ERR(trans)) {
  400. ret = PTR_ERR(trans);
  401. goto out_unlock;
  402. }
  403. btrfs_sync_inode_flags_to_i_flags(inode);
  404. inode_inc_iversion(inode);
  405. inode->i_ctime = current_time(inode);
  406. ret = btrfs_update_inode(trans, root, inode);
  407. btrfs_end_transaction(trans);
  408. out_unlock:
  409. if (ret) {
  410. binode->flags = old_flags;
  411. inode->i_flags = old_i_flags;
  412. }
  413. inode_unlock(inode);
  414. mnt_drop_write_file(file);
  415. return ret;
  416. }
  417. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  418. {
  419. struct inode *inode = file_inode(file);
  420. return put_user(inode->i_generation, arg);
  421. }
  422. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  423. {
  424. struct inode *inode = file_inode(file);
  425. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  426. struct btrfs_device *device;
  427. struct request_queue *q;
  428. struct fstrim_range range;
  429. u64 minlen = ULLONG_MAX;
  430. u64 num_devices = 0;
  431. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  432. int ret;
  433. if (!capable(CAP_SYS_ADMIN))
  434. return -EPERM;
  435. rcu_read_lock();
  436. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  437. dev_list) {
  438. if (!device->bdev)
  439. continue;
  440. q = bdev_get_queue(device->bdev);
  441. if (blk_queue_discard(q)) {
  442. num_devices++;
  443. minlen = min_t(u64, q->limits.discard_granularity,
  444. minlen);
  445. }
  446. }
  447. rcu_read_unlock();
  448. if (!num_devices)
  449. return -EOPNOTSUPP;
  450. if (copy_from_user(&range, arg, sizeof(range)))
  451. return -EFAULT;
  452. if (range.start > total_bytes ||
  453. range.len < fs_info->sb->s_blocksize)
  454. return -EINVAL;
  455. range.len = min(range.len, total_bytes - range.start);
  456. range.minlen = max(range.minlen, minlen);
  457. ret = btrfs_trim_fs(fs_info, &range);
  458. if (ret < 0)
  459. return ret;
  460. if (copy_to_user(arg, &range, sizeof(range)))
  461. return -EFAULT;
  462. return 0;
  463. }
  464. int btrfs_is_empty_uuid(u8 *uuid)
  465. {
  466. int i;
  467. for (i = 0; i < BTRFS_UUID_SIZE; i++) {
  468. if (uuid[i])
  469. return 0;
  470. }
  471. return 1;
  472. }
  473. static noinline int create_subvol(struct inode *dir,
  474. struct dentry *dentry,
  475. const char *name, int namelen,
  476. u64 *async_transid,
  477. struct btrfs_qgroup_inherit *inherit)
  478. {
  479. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  480. struct btrfs_trans_handle *trans;
  481. struct btrfs_key key;
  482. struct btrfs_root_item *root_item;
  483. struct btrfs_inode_item *inode_item;
  484. struct extent_buffer *leaf;
  485. struct btrfs_root *root = BTRFS_I(dir)->root;
  486. struct btrfs_root *new_root;
  487. struct btrfs_block_rsv block_rsv;
  488. struct timespec64 cur_time = current_time(dir);
  489. struct inode *inode;
  490. int ret;
  491. int err;
  492. u64 objectid;
  493. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  494. u64 index = 0;
  495. uuid_le new_uuid;
  496. root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
  497. if (!root_item)
  498. return -ENOMEM;
  499. ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
  500. if (ret)
  501. goto fail_free;
  502. /*
  503. * Don't create subvolume whose level is not zero. Or qgroup will be
  504. * screwed up since it assumes subvolume qgroup's level to be 0.
  505. */
  506. if (btrfs_qgroup_level(objectid)) {
  507. ret = -ENOSPC;
  508. goto fail_free;
  509. }
  510. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  511. /*
  512. * The same as the snapshot creation, please see the comment
  513. * of create_snapshot().
  514. */
  515. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
  516. if (ret)
  517. goto fail_free;
  518. trans = btrfs_start_transaction(root, 0);
  519. if (IS_ERR(trans)) {
  520. ret = PTR_ERR(trans);
  521. btrfs_subvolume_release_metadata(fs_info, &block_rsv);
  522. goto fail_free;
  523. }
  524. trans->block_rsv = &block_rsv;
  525. trans->bytes_reserved = block_rsv.size;
  526. ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
  527. if (ret)
  528. goto fail;
  529. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  530. if (IS_ERR(leaf)) {
  531. ret = PTR_ERR(leaf);
  532. goto fail;
  533. }
  534. memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
  535. btrfs_set_header_bytenr(leaf, leaf->start);
  536. btrfs_set_header_generation(leaf, trans->transid);
  537. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  538. btrfs_set_header_owner(leaf, objectid);
  539. write_extent_buffer_fsid(leaf, fs_info->fsid);
  540. write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
  541. btrfs_mark_buffer_dirty(leaf);
  542. inode_item = &root_item->inode;
  543. btrfs_set_stack_inode_generation(inode_item, 1);
  544. btrfs_set_stack_inode_size(inode_item, 3);
  545. btrfs_set_stack_inode_nlink(inode_item, 1);
  546. btrfs_set_stack_inode_nbytes(inode_item,
  547. fs_info->nodesize);
  548. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  549. btrfs_set_root_flags(root_item, 0);
  550. btrfs_set_root_limit(root_item, 0);
  551. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  552. btrfs_set_root_bytenr(root_item, leaf->start);
  553. btrfs_set_root_generation(root_item, trans->transid);
  554. btrfs_set_root_level(root_item, 0);
  555. btrfs_set_root_refs(root_item, 1);
  556. btrfs_set_root_used(root_item, leaf->len);
  557. btrfs_set_root_last_snapshot(root_item, 0);
  558. btrfs_set_root_generation_v2(root_item,
  559. btrfs_root_generation(root_item));
  560. uuid_le_gen(&new_uuid);
  561. memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  562. btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
  563. btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
  564. root_item->ctime = root_item->otime;
  565. btrfs_set_root_ctransid(root_item, trans->transid);
  566. btrfs_set_root_otransid(root_item, trans->transid);
  567. btrfs_tree_unlock(leaf);
  568. free_extent_buffer(leaf);
  569. leaf = NULL;
  570. btrfs_set_root_dirid(root_item, new_dirid);
  571. key.objectid = objectid;
  572. key.offset = 0;
  573. key.type = BTRFS_ROOT_ITEM_KEY;
  574. ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
  575. root_item);
  576. if (ret)
  577. goto fail;
  578. key.offset = (u64)-1;
  579. new_root = btrfs_read_fs_root_no_name(fs_info, &key);
  580. if (IS_ERR(new_root)) {
  581. ret = PTR_ERR(new_root);
  582. btrfs_abort_transaction(trans, ret);
  583. goto fail;
  584. }
  585. btrfs_record_root_in_trans(trans, new_root);
  586. ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
  587. if (ret) {
  588. /* We potentially lose an unused inode item here */
  589. btrfs_abort_transaction(trans, ret);
  590. goto fail;
  591. }
  592. mutex_lock(&new_root->objectid_mutex);
  593. new_root->highest_objectid = new_dirid;
  594. mutex_unlock(&new_root->objectid_mutex);
  595. /*
  596. * insert the directory item
  597. */
  598. ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
  599. if (ret) {
  600. btrfs_abort_transaction(trans, ret);
  601. goto fail;
  602. }
  603. ret = btrfs_insert_dir_item(trans, root,
  604. name, namelen, BTRFS_I(dir), &key,
  605. BTRFS_FT_DIR, index);
  606. if (ret) {
  607. btrfs_abort_transaction(trans, ret);
  608. goto fail;
  609. }
  610. btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
  611. ret = btrfs_update_inode(trans, root, dir);
  612. BUG_ON(ret);
  613. ret = btrfs_add_root_ref(trans, fs_info,
  614. objectid, root->root_key.objectid,
  615. btrfs_ino(BTRFS_I(dir)), index, name, namelen);
  616. BUG_ON(ret);
  617. ret = btrfs_uuid_tree_add(trans, root_item->uuid,
  618. BTRFS_UUID_KEY_SUBVOL, objectid);
  619. if (ret)
  620. btrfs_abort_transaction(trans, ret);
  621. fail:
  622. kfree(root_item);
  623. trans->block_rsv = NULL;
  624. trans->bytes_reserved = 0;
  625. btrfs_subvolume_release_metadata(fs_info, &block_rsv);
  626. if (async_transid) {
  627. *async_transid = trans->transid;
  628. err = btrfs_commit_transaction_async(trans, 1);
  629. if (err)
  630. err = btrfs_commit_transaction(trans);
  631. } else {
  632. err = btrfs_commit_transaction(trans);
  633. }
  634. if (err && !ret)
  635. ret = err;
  636. if (!ret) {
  637. inode = btrfs_lookup_dentry(dir, dentry);
  638. if (IS_ERR(inode))
  639. return PTR_ERR(inode);
  640. d_instantiate(dentry, inode);
  641. }
  642. return ret;
  643. fail_free:
  644. kfree(root_item);
  645. return ret;
  646. }
  647. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  648. struct dentry *dentry,
  649. u64 *async_transid, bool readonly,
  650. struct btrfs_qgroup_inherit *inherit)
  651. {
  652. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  653. struct inode *inode;
  654. struct btrfs_pending_snapshot *pending_snapshot;
  655. struct btrfs_trans_handle *trans;
  656. int ret;
  657. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  658. return -EINVAL;
  659. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
  660. if (!pending_snapshot)
  661. return -ENOMEM;
  662. pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
  663. GFP_KERNEL);
  664. pending_snapshot->path = btrfs_alloc_path();
  665. if (!pending_snapshot->root_item || !pending_snapshot->path) {
  666. ret = -ENOMEM;
  667. goto free_pending;
  668. }
  669. atomic_inc(&root->will_be_snapshotted);
  670. smp_mb__after_atomic();
  671. /* wait for no snapshot writes */
  672. wait_event(root->subv_writers->wait,
  673. percpu_counter_sum(&root->subv_writers->counter) == 0);
  674. ret = btrfs_start_delalloc_inodes(root);
  675. if (ret)
  676. goto dec_and_free;
  677. btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
  678. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  679. BTRFS_BLOCK_RSV_TEMP);
  680. /*
  681. * 1 - parent dir inode
  682. * 2 - dir entries
  683. * 1 - root item
  684. * 2 - root ref/backref
  685. * 1 - root of snapshot
  686. * 1 - UUID item
  687. */
  688. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
  689. &pending_snapshot->block_rsv, 8,
  690. false);
  691. if (ret)
  692. goto dec_and_free;
  693. pending_snapshot->dentry = dentry;
  694. pending_snapshot->root = root;
  695. pending_snapshot->readonly = readonly;
  696. pending_snapshot->dir = dir;
  697. pending_snapshot->inherit = inherit;
  698. trans = btrfs_start_transaction(root, 0);
  699. if (IS_ERR(trans)) {
  700. ret = PTR_ERR(trans);
  701. goto fail;
  702. }
  703. spin_lock(&fs_info->trans_lock);
  704. list_add(&pending_snapshot->list,
  705. &trans->transaction->pending_snapshots);
  706. spin_unlock(&fs_info->trans_lock);
  707. if (async_transid) {
  708. *async_transid = trans->transid;
  709. ret = btrfs_commit_transaction_async(trans, 1);
  710. if (ret)
  711. ret = btrfs_commit_transaction(trans);
  712. } else {
  713. ret = btrfs_commit_transaction(trans);
  714. }
  715. if (ret)
  716. goto fail;
  717. ret = pending_snapshot->error;
  718. if (ret)
  719. goto fail;
  720. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  721. if (ret)
  722. goto fail;
  723. inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
  724. if (IS_ERR(inode)) {
  725. ret = PTR_ERR(inode);
  726. goto fail;
  727. }
  728. d_instantiate(dentry, inode);
  729. ret = 0;
  730. fail:
  731. btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
  732. dec_and_free:
  733. if (atomic_dec_and_test(&root->will_be_snapshotted))
  734. wake_up_var(&root->will_be_snapshotted);
  735. free_pending:
  736. kfree(pending_snapshot->root_item);
  737. btrfs_free_path(pending_snapshot->path);
  738. kfree(pending_snapshot);
  739. return ret;
  740. }
  741. /* copy of may_delete in fs/namei.c()
  742. * Check whether we can remove a link victim from directory dir, check
  743. * whether the type of victim is right.
  744. * 1. We can't do it if dir is read-only (done in permission())
  745. * 2. We should have write and exec permissions on dir
  746. * 3. We can't remove anything from append-only dir
  747. * 4. We can't do anything with immutable dir (done in permission())
  748. * 5. If the sticky bit on dir is set we should either
  749. * a. be owner of dir, or
  750. * b. be owner of victim, or
  751. * c. have CAP_FOWNER capability
  752. * 6. If the victim is append-only or immutable we can't do anything with
  753. * links pointing to it.
  754. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  755. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  756. * 9. We can't remove a root or mountpoint.
  757. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  758. * nfs_async_unlink().
  759. */
  760. static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
  761. {
  762. int error;
  763. if (d_really_is_negative(victim))
  764. return -ENOENT;
  765. BUG_ON(d_inode(victim->d_parent) != dir);
  766. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  767. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  768. if (error)
  769. return error;
  770. if (IS_APPEND(dir))
  771. return -EPERM;
  772. if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
  773. IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
  774. return -EPERM;
  775. if (isdir) {
  776. if (!d_is_dir(victim))
  777. return -ENOTDIR;
  778. if (IS_ROOT(victim))
  779. return -EBUSY;
  780. } else if (d_is_dir(victim))
  781. return -EISDIR;
  782. if (IS_DEADDIR(dir))
  783. return -ENOENT;
  784. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  785. return -EBUSY;
  786. return 0;
  787. }
  788. /* copy of may_create in fs/namei.c() */
  789. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  790. {
  791. if (d_really_is_positive(child))
  792. return -EEXIST;
  793. if (IS_DEADDIR(dir))
  794. return -ENOENT;
  795. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  796. }
  797. /*
  798. * Create a new subvolume below @parent. This is largely modeled after
  799. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  800. * inside this filesystem so it's quite a bit simpler.
  801. */
  802. static noinline int btrfs_mksubvol(const struct path *parent,
  803. const char *name, int namelen,
  804. struct btrfs_root *snap_src,
  805. u64 *async_transid, bool readonly,
  806. struct btrfs_qgroup_inherit *inherit)
  807. {
  808. struct inode *dir = d_inode(parent->dentry);
  809. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  810. struct dentry *dentry;
  811. int error;
  812. error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
  813. if (error == -EINTR)
  814. return error;
  815. dentry = lookup_one_len(name, parent->dentry, namelen);
  816. error = PTR_ERR(dentry);
  817. if (IS_ERR(dentry))
  818. goto out_unlock;
  819. error = btrfs_may_create(dir, dentry);
  820. if (error)
  821. goto out_dput;
  822. /*
  823. * even if this name doesn't exist, we may get hash collisions.
  824. * check for them now when we can safely fail
  825. */
  826. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  827. dir->i_ino, name,
  828. namelen);
  829. if (error)
  830. goto out_dput;
  831. down_read(&fs_info->subvol_sem);
  832. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  833. goto out_up_read;
  834. if (snap_src) {
  835. error = create_snapshot(snap_src, dir, dentry,
  836. async_transid, readonly, inherit);
  837. } else {
  838. error = create_subvol(dir, dentry, name, namelen,
  839. async_transid, inherit);
  840. }
  841. if (!error)
  842. fsnotify_mkdir(dir, dentry);
  843. out_up_read:
  844. up_read(&fs_info->subvol_sem);
  845. out_dput:
  846. dput(dentry);
  847. out_unlock:
  848. inode_unlock(dir);
  849. return error;
  850. }
  851. /*
  852. * When we're defragging a range, we don't want to kick it off again
  853. * if it is really just waiting for delalloc to send it down.
  854. * If we find a nice big extent or delalloc range for the bytes in the
  855. * file you want to defrag, we return 0 to let you know to skip this
  856. * part of the file
  857. */
  858. static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
  859. {
  860. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  861. struct extent_map *em = NULL;
  862. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  863. u64 end;
  864. read_lock(&em_tree->lock);
  865. em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
  866. read_unlock(&em_tree->lock);
  867. if (em) {
  868. end = extent_map_end(em);
  869. free_extent_map(em);
  870. if (end - offset > thresh)
  871. return 0;
  872. }
  873. /* if we already have a nice delalloc here, just stop */
  874. thresh /= 2;
  875. end = count_range_bits(io_tree, &offset, offset + thresh,
  876. thresh, EXTENT_DELALLOC, 1);
  877. if (end >= thresh)
  878. return 0;
  879. return 1;
  880. }
  881. /*
  882. * helper function to walk through a file and find extents
  883. * newer than a specific transid, and smaller than thresh.
  884. *
  885. * This is used by the defragging code to find new and small
  886. * extents
  887. */
  888. static int find_new_extents(struct btrfs_root *root,
  889. struct inode *inode, u64 newer_than,
  890. u64 *off, u32 thresh)
  891. {
  892. struct btrfs_path *path;
  893. struct btrfs_key min_key;
  894. struct extent_buffer *leaf;
  895. struct btrfs_file_extent_item *extent;
  896. int type;
  897. int ret;
  898. u64 ino = btrfs_ino(BTRFS_I(inode));
  899. path = btrfs_alloc_path();
  900. if (!path)
  901. return -ENOMEM;
  902. min_key.objectid = ino;
  903. min_key.type = BTRFS_EXTENT_DATA_KEY;
  904. min_key.offset = *off;
  905. while (1) {
  906. ret = btrfs_search_forward(root, &min_key, path, newer_than);
  907. if (ret != 0)
  908. goto none;
  909. process_slot:
  910. if (min_key.objectid != ino)
  911. goto none;
  912. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  913. goto none;
  914. leaf = path->nodes[0];
  915. extent = btrfs_item_ptr(leaf, path->slots[0],
  916. struct btrfs_file_extent_item);
  917. type = btrfs_file_extent_type(leaf, extent);
  918. if (type == BTRFS_FILE_EXTENT_REG &&
  919. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  920. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  921. *off = min_key.offset;
  922. btrfs_free_path(path);
  923. return 0;
  924. }
  925. path->slots[0]++;
  926. if (path->slots[0] < btrfs_header_nritems(leaf)) {
  927. btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
  928. goto process_slot;
  929. }
  930. if (min_key.offset == (u64)-1)
  931. goto none;
  932. min_key.offset++;
  933. btrfs_release_path(path);
  934. }
  935. none:
  936. btrfs_free_path(path);
  937. return -ENOENT;
  938. }
  939. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  940. {
  941. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  942. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  943. struct extent_map *em;
  944. u64 len = PAGE_SIZE;
  945. /*
  946. * hopefully we have this extent in the tree already, try without
  947. * the full extent lock
  948. */
  949. read_lock(&em_tree->lock);
  950. em = lookup_extent_mapping(em_tree, start, len);
  951. read_unlock(&em_tree->lock);
  952. if (!em) {
  953. struct extent_state *cached = NULL;
  954. u64 end = start + len - 1;
  955. /* get the big lock and read metadata off disk */
  956. lock_extent_bits(io_tree, start, end, &cached);
  957. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
  958. unlock_extent_cached(io_tree, start, end, &cached);
  959. if (IS_ERR(em))
  960. return NULL;
  961. }
  962. return em;
  963. }
  964. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  965. {
  966. struct extent_map *next;
  967. bool ret = true;
  968. /* this is the last extent */
  969. if (em->start + em->len >= i_size_read(inode))
  970. return false;
  971. next = defrag_lookup_extent(inode, em->start + em->len);
  972. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  973. ret = false;
  974. else if ((em->block_start + em->block_len == next->block_start) &&
  975. (em->block_len > SZ_128K && next->block_len > SZ_128K))
  976. ret = false;
  977. free_extent_map(next);
  978. return ret;
  979. }
  980. static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
  981. u64 *last_len, u64 *skip, u64 *defrag_end,
  982. int compress)
  983. {
  984. struct extent_map *em;
  985. int ret = 1;
  986. bool next_mergeable = true;
  987. bool prev_mergeable = true;
  988. /*
  989. * make sure that once we start defragging an extent, we keep on
  990. * defragging it
  991. */
  992. if (start < *defrag_end)
  993. return 1;
  994. *skip = 0;
  995. em = defrag_lookup_extent(inode, start);
  996. if (!em)
  997. return 0;
  998. /* this will cover holes, and inline extents */
  999. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1000. ret = 0;
  1001. goto out;
  1002. }
  1003. if (!*defrag_end)
  1004. prev_mergeable = false;
  1005. next_mergeable = defrag_check_next_extent(inode, em);
  1006. /*
  1007. * we hit a real extent, if it is big or the next extent is not a
  1008. * real extent, don't bother defragging it
  1009. */
  1010. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  1011. (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
  1012. ret = 0;
  1013. out:
  1014. /*
  1015. * last_len ends up being a counter of how many bytes we've defragged.
  1016. * every time we choose not to defrag an extent, we reset *last_len
  1017. * so that the next tiny extent will force a defrag.
  1018. *
  1019. * The end result of this is that tiny extents before a single big
  1020. * extent will force at least part of that big extent to be defragged.
  1021. */
  1022. if (ret) {
  1023. *defrag_end = extent_map_end(em);
  1024. } else {
  1025. *last_len = 0;
  1026. *skip = extent_map_end(em);
  1027. *defrag_end = 0;
  1028. }
  1029. free_extent_map(em);
  1030. return ret;
  1031. }
  1032. /*
  1033. * it doesn't do much good to defrag one or two pages
  1034. * at a time. This pulls in a nice chunk of pages
  1035. * to COW and defrag.
  1036. *
  1037. * It also makes sure the delalloc code has enough
  1038. * dirty data to avoid making new small extents as part
  1039. * of the defrag
  1040. *
  1041. * It's a good idea to start RA on this range
  1042. * before calling this.
  1043. */
  1044. static int cluster_pages_for_defrag(struct inode *inode,
  1045. struct page **pages,
  1046. unsigned long start_index,
  1047. unsigned long num_pages)
  1048. {
  1049. unsigned long file_end;
  1050. u64 isize = i_size_read(inode);
  1051. u64 page_start;
  1052. u64 page_end;
  1053. u64 page_cnt;
  1054. int ret;
  1055. int i;
  1056. int i_done;
  1057. struct btrfs_ordered_extent *ordered;
  1058. struct extent_state *cached_state = NULL;
  1059. struct extent_io_tree *tree;
  1060. struct extent_changeset *data_reserved = NULL;
  1061. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1062. file_end = (isize - 1) >> PAGE_SHIFT;
  1063. if (!isize || start_index > file_end)
  1064. return 0;
  1065. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  1066. ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
  1067. start_index << PAGE_SHIFT,
  1068. page_cnt << PAGE_SHIFT);
  1069. if (ret)
  1070. return ret;
  1071. i_done = 0;
  1072. tree = &BTRFS_I(inode)->io_tree;
  1073. /* step one, lock all the pages */
  1074. for (i = 0; i < page_cnt; i++) {
  1075. struct page *page;
  1076. again:
  1077. page = find_or_create_page(inode->i_mapping,
  1078. start_index + i, mask);
  1079. if (!page)
  1080. break;
  1081. page_start = page_offset(page);
  1082. page_end = page_start + PAGE_SIZE - 1;
  1083. while (1) {
  1084. lock_extent_bits(tree, page_start, page_end,
  1085. &cached_state);
  1086. ordered = btrfs_lookup_ordered_extent(inode,
  1087. page_start);
  1088. unlock_extent_cached(tree, page_start, page_end,
  1089. &cached_state);
  1090. if (!ordered)
  1091. break;
  1092. unlock_page(page);
  1093. btrfs_start_ordered_extent(inode, ordered, 1);
  1094. btrfs_put_ordered_extent(ordered);
  1095. lock_page(page);
  1096. /*
  1097. * we unlocked the page above, so we need check if
  1098. * it was released or not.
  1099. */
  1100. if (page->mapping != inode->i_mapping) {
  1101. unlock_page(page);
  1102. put_page(page);
  1103. goto again;
  1104. }
  1105. }
  1106. if (!PageUptodate(page)) {
  1107. btrfs_readpage(NULL, page);
  1108. lock_page(page);
  1109. if (!PageUptodate(page)) {
  1110. unlock_page(page);
  1111. put_page(page);
  1112. ret = -EIO;
  1113. break;
  1114. }
  1115. }
  1116. if (page->mapping != inode->i_mapping) {
  1117. unlock_page(page);
  1118. put_page(page);
  1119. goto again;
  1120. }
  1121. pages[i] = page;
  1122. i_done++;
  1123. }
  1124. if (!i_done || ret)
  1125. goto out;
  1126. if (!(inode->i_sb->s_flags & SB_ACTIVE))
  1127. goto out;
  1128. /*
  1129. * so now we have a nice long stream of locked
  1130. * and up to date pages, lets wait on them
  1131. */
  1132. for (i = 0; i < i_done; i++)
  1133. wait_on_page_writeback(pages[i]);
  1134. page_start = page_offset(pages[0]);
  1135. page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
  1136. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1137. page_start, page_end - 1, &cached_state);
  1138. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  1139. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1140. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  1141. &cached_state);
  1142. if (i_done != page_cnt) {
  1143. spin_lock(&BTRFS_I(inode)->lock);
  1144. BTRFS_I(inode)->outstanding_extents++;
  1145. spin_unlock(&BTRFS_I(inode)->lock);
  1146. btrfs_delalloc_release_space(inode, data_reserved,
  1147. start_index << PAGE_SHIFT,
  1148. (page_cnt - i_done) << PAGE_SHIFT, true);
  1149. }
  1150. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  1151. &cached_state);
  1152. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1153. page_start, page_end - 1, &cached_state);
  1154. for (i = 0; i < i_done; i++) {
  1155. clear_page_dirty_for_io(pages[i]);
  1156. ClearPageChecked(pages[i]);
  1157. set_page_extent_mapped(pages[i]);
  1158. set_page_dirty(pages[i]);
  1159. unlock_page(pages[i]);
  1160. put_page(pages[i]);
  1161. }
  1162. btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
  1163. false);
  1164. extent_changeset_free(data_reserved);
  1165. return i_done;
  1166. out:
  1167. for (i = 0; i < i_done; i++) {
  1168. unlock_page(pages[i]);
  1169. put_page(pages[i]);
  1170. }
  1171. btrfs_delalloc_release_space(inode, data_reserved,
  1172. start_index << PAGE_SHIFT,
  1173. page_cnt << PAGE_SHIFT, true);
  1174. btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
  1175. true);
  1176. extent_changeset_free(data_reserved);
  1177. return ret;
  1178. }
  1179. int btrfs_defrag_file(struct inode *inode, struct file *file,
  1180. struct btrfs_ioctl_defrag_range_args *range,
  1181. u64 newer_than, unsigned long max_to_defrag)
  1182. {
  1183. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1184. struct btrfs_root *root = BTRFS_I(inode)->root;
  1185. struct file_ra_state *ra = NULL;
  1186. unsigned long last_index;
  1187. u64 isize = i_size_read(inode);
  1188. u64 last_len = 0;
  1189. u64 skip = 0;
  1190. u64 defrag_end = 0;
  1191. u64 newer_off = range->start;
  1192. unsigned long i;
  1193. unsigned long ra_index = 0;
  1194. int ret;
  1195. int defrag_count = 0;
  1196. int compress_type = BTRFS_COMPRESS_ZLIB;
  1197. u32 extent_thresh = range->extent_thresh;
  1198. unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
  1199. unsigned long cluster = max_cluster;
  1200. u64 new_align = ~((u64)SZ_128K - 1);
  1201. struct page **pages = NULL;
  1202. bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
  1203. if (isize == 0)
  1204. return 0;
  1205. if (range->start >= isize)
  1206. return -EINVAL;
  1207. if (do_compress) {
  1208. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  1209. return -EINVAL;
  1210. if (range->compress_type)
  1211. compress_type = range->compress_type;
  1212. }
  1213. if (extent_thresh == 0)
  1214. extent_thresh = SZ_256K;
  1215. /*
  1216. * If we were not given a file, allocate a readahead context. As
  1217. * readahead is just an optimization, defrag will work without it so
  1218. * we don't error out.
  1219. */
  1220. if (!file) {
  1221. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  1222. if (ra)
  1223. file_ra_state_init(ra, inode->i_mapping);
  1224. } else {
  1225. ra = &file->f_ra;
  1226. }
  1227. pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
  1228. if (!pages) {
  1229. ret = -ENOMEM;
  1230. goto out_ra;
  1231. }
  1232. /* find the last page to defrag */
  1233. if (range->start + range->len > range->start) {
  1234. last_index = min_t(u64, isize - 1,
  1235. range->start + range->len - 1) >> PAGE_SHIFT;
  1236. } else {
  1237. last_index = (isize - 1) >> PAGE_SHIFT;
  1238. }
  1239. if (newer_than) {
  1240. ret = find_new_extents(root, inode, newer_than,
  1241. &newer_off, SZ_64K);
  1242. if (!ret) {
  1243. range->start = newer_off;
  1244. /*
  1245. * we always align our defrag to help keep
  1246. * the extents in the file evenly spaced
  1247. */
  1248. i = (newer_off & new_align) >> PAGE_SHIFT;
  1249. } else
  1250. goto out_ra;
  1251. } else {
  1252. i = range->start >> PAGE_SHIFT;
  1253. }
  1254. if (!max_to_defrag)
  1255. max_to_defrag = last_index - i + 1;
  1256. /*
  1257. * make writeback starts from i, so the defrag range can be
  1258. * written sequentially.
  1259. */
  1260. if (i < inode->i_mapping->writeback_index)
  1261. inode->i_mapping->writeback_index = i;
  1262. while (i <= last_index && defrag_count < max_to_defrag &&
  1263. (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
  1264. /*
  1265. * make sure we stop running if someone unmounts
  1266. * the FS
  1267. */
  1268. if (!(inode->i_sb->s_flags & SB_ACTIVE))
  1269. break;
  1270. if (btrfs_defrag_cancelled(fs_info)) {
  1271. btrfs_debug(fs_info, "defrag_file cancelled");
  1272. ret = -EAGAIN;
  1273. break;
  1274. }
  1275. if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
  1276. extent_thresh, &last_len, &skip,
  1277. &defrag_end, do_compress)){
  1278. unsigned long next;
  1279. /*
  1280. * the should_defrag function tells us how much to skip
  1281. * bump our counter by the suggested amount
  1282. */
  1283. next = DIV_ROUND_UP(skip, PAGE_SIZE);
  1284. i = max(i + 1, next);
  1285. continue;
  1286. }
  1287. if (!newer_than) {
  1288. cluster = (PAGE_ALIGN(defrag_end) >>
  1289. PAGE_SHIFT) - i;
  1290. cluster = min(cluster, max_cluster);
  1291. } else {
  1292. cluster = max_cluster;
  1293. }
  1294. if (i + cluster > ra_index) {
  1295. ra_index = max(i, ra_index);
  1296. if (ra)
  1297. page_cache_sync_readahead(inode->i_mapping, ra,
  1298. file, ra_index, cluster);
  1299. ra_index += cluster;
  1300. }
  1301. inode_lock(inode);
  1302. if (do_compress)
  1303. BTRFS_I(inode)->defrag_compress = compress_type;
  1304. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1305. if (ret < 0) {
  1306. inode_unlock(inode);
  1307. goto out_ra;
  1308. }
  1309. defrag_count += ret;
  1310. balance_dirty_pages_ratelimited(inode->i_mapping);
  1311. inode_unlock(inode);
  1312. if (newer_than) {
  1313. if (newer_off == (u64)-1)
  1314. break;
  1315. if (ret > 0)
  1316. i += ret;
  1317. newer_off = max(newer_off + 1,
  1318. (u64)i << PAGE_SHIFT);
  1319. ret = find_new_extents(root, inode, newer_than,
  1320. &newer_off, SZ_64K);
  1321. if (!ret) {
  1322. range->start = newer_off;
  1323. i = (newer_off & new_align) >> PAGE_SHIFT;
  1324. } else {
  1325. break;
  1326. }
  1327. } else {
  1328. if (ret > 0) {
  1329. i += ret;
  1330. last_len += ret << PAGE_SHIFT;
  1331. } else {
  1332. i++;
  1333. last_len = 0;
  1334. }
  1335. }
  1336. }
  1337. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
  1338. filemap_flush(inode->i_mapping);
  1339. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1340. &BTRFS_I(inode)->runtime_flags))
  1341. filemap_flush(inode->i_mapping);
  1342. }
  1343. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1344. btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
  1345. } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
  1346. btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
  1347. }
  1348. ret = defrag_count;
  1349. out_ra:
  1350. if (do_compress) {
  1351. inode_lock(inode);
  1352. BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
  1353. inode_unlock(inode);
  1354. }
  1355. if (!file)
  1356. kfree(ra);
  1357. kfree(pages);
  1358. return ret;
  1359. }
  1360. static noinline int btrfs_ioctl_resize(struct file *file,
  1361. void __user *arg)
  1362. {
  1363. struct inode *inode = file_inode(file);
  1364. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1365. u64 new_size;
  1366. u64 old_size;
  1367. u64 devid = 1;
  1368. struct btrfs_root *root = BTRFS_I(inode)->root;
  1369. struct btrfs_ioctl_vol_args *vol_args;
  1370. struct btrfs_trans_handle *trans;
  1371. struct btrfs_device *device = NULL;
  1372. char *sizestr;
  1373. char *retptr;
  1374. char *devstr = NULL;
  1375. int ret = 0;
  1376. int mod = 0;
  1377. if (!capable(CAP_SYS_ADMIN))
  1378. return -EPERM;
  1379. ret = mnt_want_write_file(file);
  1380. if (ret)
  1381. return ret;
  1382. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  1383. mnt_drop_write_file(file);
  1384. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  1385. }
  1386. vol_args = memdup_user(arg, sizeof(*vol_args));
  1387. if (IS_ERR(vol_args)) {
  1388. ret = PTR_ERR(vol_args);
  1389. goto out;
  1390. }
  1391. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1392. sizestr = vol_args->name;
  1393. devstr = strchr(sizestr, ':');
  1394. if (devstr) {
  1395. sizestr = devstr + 1;
  1396. *devstr = '\0';
  1397. devstr = vol_args->name;
  1398. ret = kstrtoull(devstr, 10, &devid);
  1399. if (ret)
  1400. goto out_free;
  1401. if (!devid) {
  1402. ret = -EINVAL;
  1403. goto out_free;
  1404. }
  1405. btrfs_info(fs_info, "resizing devid %llu", devid);
  1406. }
  1407. device = btrfs_find_device(fs_info, devid, NULL, NULL);
  1408. if (!device) {
  1409. btrfs_info(fs_info, "resizer unable to find device %llu",
  1410. devid);
  1411. ret = -ENODEV;
  1412. goto out_free;
  1413. }
  1414. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  1415. btrfs_info(fs_info,
  1416. "resizer unable to apply on readonly device %llu",
  1417. devid);
  1418. ret = -EPERM;
  1419. goto out_free;
  1420. }
  1421. if (!strcmp(sizestr, "max"))
  1422. new_size = device->bdev->bd_inode->i_size;
  1423. else {
  1424. if (sizestr[0] == '-') {
  1425. mod = -1;
  1426. sizestr++;
  1427. } else if (sizestr[0] == '+') {
  1428. mod = 1;
  1429. sizestr++;
  1430. }
  1431. new_size = memparse(sizestr, &retptr);
  1432. if (*retptr != '\0' || new_size == 0) {
  1433. ret = -EINVAL;
  1434. goto out_free;
  1435. }
  1436. }
  1437. if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  1438. ret = -EPERM;
  1439. goto out_free;
  1440. }
  1441. old_size = btrfs_device_get_total_bytes(device);
  1442. if (mod < 0) {
  1443. if (new_size > old_size) {
  1444. ret = -EINVAL;
  1445. goto out_free;
  1446. }
  1447. new_size = old_size - new_size;
  1448. } else if (mod > 0) {
  1449. if (new_size > ULLONG_MAX - old_size) {
  1450. ret = -ERANGE;
  1451. goto out_free;
  1452. }
  1453. new_size = old_size + new_size;
  1454. }
  1455. if (new_size < SZ_256M) {
  1456. ret = -EINVAL;
  1457. goto out_free;
  1458. }
  1459. if (new_size > device->bdev->bd_inode->i_size) {
  1460. ret = -EFBIG;
  1461. goto out_free;
  1462. }
  1463. new_size = round_down(new_size, fs_info->sectorsize);
  1464. btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
  1465. rcu_str_deref(device->name), new_size);
  1466. if (new_size > old_size) {
  1467. trans = btrfs_start_transaction(root, 0);
  1468. if (IS_ERR(trans)) {
  1469. ret = PTR_ERR(trans);
  1470. goto out_free;
  1471. }
  1472. ret = btrfs_grow_device(trans, device, new_size);
  1473. btrfs_commit_transaction(trans);
  1474. } else if (new_size < old_size) {
  1475. ret = btrfs_shrink_device(device, new_size);
  1476. } /* equal, nothing need to do */
  1477. out_free:
  1478. kfree(vol_args);
  1479. out:
  1480. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  1481. mnt_drop_write_file(file);
  1482. return ret;
  1483. }
  1484. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1485. const char *name, unsigned long fd, int subvol,
  1486. u64 *transid, bool readonly,
  1487. struct btrfs_qgroup_inherit *inherit)
  1488. {
  1489. int namelen;
  1490. int ret = 0;
  1491. if (!S_ISDIR(file_inode(file)->i_mode))
  1492. return -ENOTDIR;
  1493. ret = mnt_want_write_file(file);
  1494. if (ret)
  1495. goto out;
  1496. namelen = strlen(name);
  1497. if (strchr(name, '/')) {
  1498. ret = -EINVAL;
  1499. goto out_drop_write;
  1500. }
  1501. if (name[0] == '.' &&
  1502. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1503. ret = -EEXIST;
  1504. goto out_drop_write;
  1505. }
  1506. if (subvol) {
  1507. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1508. NULL, transid, readonly, inherit);
  1509. } else {
  1510. struct fd src = fdget(fd);
  1511. struct inode *src_inode;
  1512. if (!src.file) {
  1513. ret = -EINVAL;
  1514. goto out_drop_write;
  1515. }
  1516. src_inode = file_inode(src.file);
  1517. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1518. btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
  1519. "Snapshot src from another FS");
  1520. ret = -EXDEV;
  1521. } else if (!inode_owner_or_capable(src_inode)) {
  1522. /*
  1523. * Subvolume creation is not restricted, but snapshots
  1524. * are limited to own subvolumes only
  1525. */
  1526. ret = -EPERM;
  1527. } else {
  1528. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1529. BTRFS_I(src_inode)->root,
  1530. transid, readonly, inherit);
  1531. }
  1532. fdput(src);
  1533. }
  1534. out_drop_write:
  1535. mnt_drop_write_file(file);
  1536. out:
  1537. return ret;
  1538. }
  1539. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1540. void __user *arg, int subvol)
  1541. {
  1542. struct btrfs_ioctl_vol_args *vol_args;
  1543. int ret;
  1544. if (!S_ISDIR(file_inode(file)->i_mode))
  1545. return -ENOTDIR;
  1546. vol_args = memdup_user(arg, sizeof(*vol_args));
  1547. if (IS_ERR(vol_args))
  1548. return PTR_ERR(vol_args);
  1549. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1550. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1551. vol_args->fd, subvol,
  1552. NULL, false, NULL);
  1553. kfree(vol_args);
  1554. return ret;
  1555. }
  1556. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1557. void __user *arg, int subvol)
  1558. {
  1559. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1560. int ret;
  1561. u64 transid = 0;
  1562. u64 *ptr = NULL;
  1563. bool readonly = false;
  1564. struct btrfs_qgroup_inherit *inherit = NULL;
  1565. if (!S_ISDIR(file_inode(file)->i_mode))
  1566. return -ENOTDIR;
  1567. vol_args = memdup_user(arg, sizeof(*vol_args));
  1568. if (IS_ERR(vol_args))
  1569. return PTR_ERR(vol_args);
  1570. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1571. if (vol_args->flags &
  1572. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1573. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1574. ret = -EOPNOTSUPP;
  1575. goto free_args;
  1576. }
  1577. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1578. ptr = &transid;
  1579. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1580. readonly = true;
  1581. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1582. if (vol_args->size > PAGE_SIZE) {
  1583. ret = -EINVAL;
  1584. goto free_args;
  1585. }
  1586. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1587. if (IS_ERR(inherit)) {
  1588. ret = PTR_ERR(inherit);
  1589. goto free_args;
  1590. }
  1591. }
  1592. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1593. vol_args->fd, subvol, ptr,
  1594. readonly, inherit);
  1595. if (ret)
  1596. goto free_inherit;
  1597. if (ptr && copy_to_user(arg +
  1598. offsetof(struct btrfs_ioctl_vol_args_v2,
  1599. transid),
  1600. ptr, sizeof(*ptr)))
  1601. ret = -EFAULT;
  1602. free_inherit:
  1603. kfree(inherit);
  1604. free_args:
  1605. kfree(vol_args);
  1606. return ret;
  1607. }
  1608. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1609. void __user *arg)
  1610. {
  1611. struct inode *inode = file_inode(file);
  1612. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1613. struct btrfs_root *root = BTRFS_I(inode)->root;
  1614. int ret = 0;
  1615. u64 flags = 0;
  1616. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
  1617. return -EINVAL;
  1618. down_read(&fs_info->subvol_sem);
  1619. if (btrfs_root_readonly(root))
  1620. flags |= BTRFS_SUBVOL_RDONLY;
  1621. up_read(&fs_info->subvol_sem);
  1622. if (copy_to_user(arg, &flags, sizeof(flags)))
  1623. ret = -EFAULT;
  1624. return ret;
  1625. }
  1626. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1627. void __user *arg)
  1628. {
  1629. struct inode *inode = file_inode(file);
  1630. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1631. struct btrfs_root *root = BTRFS_I(inode)->root;
  1632. struct btrfs_trans_handle *trans;
  1633. u64 root_flags;
  1634. u64 flags;
  1635. int ret = 0;
  1636. if (!inode_owner_or_capable(inode))
  1637. return -EPERM;
  1638. ret = mnt_want_write_file(file);
  1639. if (ret)
  1640. goto out;
  1641. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  1642. ret = -EINVAL;
  1643. goto out_drop_write;
  1644. }
  1645. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1646. ret = -EFAULT;
  1647. goto out_drop_write;
  1648. }
  1649. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1650. ret = -EINVAL;
  1651. goto out_drop_write;
  1652. }
  1653. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1654. ret = -EOPNOTSUPP;
  1655. goto out_drop_write;
  1656. }
  1657. down_write(&fs_info->subvol_sem);
  1658. /* nothing to do */
  1659. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1660. goto out_drop_sem;
  1661. root_flags = btrfs_root_flags(&root->root_item);
  1662. if (flags & BTRFS_SUBVOL_RDONLY) {
  1663. btrfs_set_root_flags(&root->root_item,
  1664. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1665. } else {
  1666. /*
  1667. * Block RO -> RW transition if this subvolume is involved in
  1668. * send
  1669. */
  1670. spin_lock(&root->root_item_lock);
  1671. if (root->send_in_progress == 0) {
  1672. btrfs_set_root_flags(&root->root_item,
  1673. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1674. spin_unlock(&root->root_item_lock);
  1675. } else {
  1676. spin_unlock(&root->root_item_lock);
  1677. btrfs_warn(fs_info,
  1678. "Attempt to set subvolume %llu read-write during send",
  1679. root->root_key.objectid);
  1680. ret = -EPERM;
  1681. goto out_drop_sem;
  1682. }
  1683. }
  1684. trans = btrfs_start_transaction(root, 1);
  1685. if (IS_ERR(trans)) {
  1686. ret = PTR_ERR(trans);
  1687. goto out_reset;
  1688. }
  1689. ret = btrfs_update_root(trans, fs_info->tree_root,
  1690. &root->root_key, &root->root_item);
  1691. if (ret < 0) {
  1692. btrfs_end_transaction(trans);
  1693. goto out_reset;
  1694. }
  1695. ret = btrfs_commit_transaction(trans);
  1696. out_reset:
  1697. if (ret)
  1698. btrfs_set_root_flags(&root->root_item, root_flags);
  1699. out_drop_sem:
  1700. up_write(&fs_info->subvol_sem);
  1701. out_drop_write:
  1702. mnt_drop_write_file(file);
  1703. out:
  1704. return ret;
  1705. }
  1706. static noinline int key_in_sk(struct btrfs_key *key,
  1707. struct btrfs_ioctl_search_key *sk)
  1708. {
  1709. struct btrfs_key test;
  1710. int ret;
  1711. test.objectid = sk->min_objectid;
  1712. test.type = sk->min_type;
  1713. test.offset = sk->min_offset;
  1714. ret = btrfs_comp_cpu_keys(key, &test);
  1715. if (ret < 0)
  1716. return 0;
  1717. test.objectid = sk->max_objectid;
  1718. test.type = sk->max_type;
  1719. test.offset = sk->max_offset;
  1720. ret = btrfs_comp_cpu_keys(key, &test);
  1721. if (ret > 0)
  1722. return 0;
  1723. return 1;
  1724. }
  1725. static noinline int copy_to_sk(struct btrfs_path *path,
  1726. struct btrfs_key *key,
  1727. struct btrfs_ioctl_search_key *sk,
  1728. size_t *buf_size,
  1729. char __user *ubuf,
  1730. unsigned long *sk_offset,
  1731. int *num_found)
  1732. {
  1733. u64 found_transid;
  1734. struct extent_buffer *leaf;
  1735. struct btrfs_ioctl_search_header sh;
  1736. struct btrfs_key test;
  1737. unsigned long item_off;
  1738. unsigned long item_len;
  1739. int nritems;
  1740. int i;
  1741. int slot;
  1742. int ret = 0;
  1743. leaf = path->nodes[0];
  1744. slot = path->slots[0];
  1745. nritems = btrfs_header_nritems(leaf);
  1746. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1747. i = nritems;
  1748. goto advance_key;
  1749. }
  1750. found_transid = btrfs_header_generation(leaf);
  1751. for (i = slot; i < nritems; i++) {
  1752. item_off = btrfs_item_ptr_offset(leaf, i);
  1753. item_len = btrfs_item_size_nr(leaf, i);
  1754. btrfs_item_key_to_cpu(leaf, key, i);
  1755. if (!key_in_sk(key, sk))
  1756. continue;
  1757. if (sizeof(sh) + item_len > *buf_size) {
  1758. if (*num_found) {
  1759. ret = 1;
  1760. goto out;
  1761. }
  1762. /*
  1763. * return one empty item back for v1, which does not
  1764. * handle -EOVERFLOW
  1765. */
  1766. *buf_size = sizeof(sh) + item_len;
  1767. item_len = 0;
  1768. ret = -EOVERFLOW;
  1769. }
  1770. if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
  1771. ret = 1;
  1772. goto out;
  1773. }
  1774. sh.objectid = key->objectid;
  1775. sh.offset = key->offset;
  1776. sh.type = key->type;
  1777. sh.len = item_len;
  1778. sh.transid = found_transid;
  1779. /* copy search result header */
  1780. if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
  1781. ret = -EFAULT;
  1782. goto out;
  1783. }
  1784. *sk_offset += sizeof(sh);
  1785. if (item_len) {
  1786. char __user *up = ubuf + *sk_offset;
  1787. /* copy the item */
  1788. if (read_extent_buffer_to_user(leaf, up,
  1789. item_off, item_len)) {
  1790. ret = -EFAULT;
  1791. goto out;
  1792. }
  1793. *sk_offset += item_len;
  1794. }
  1795. (*num_found)++;
  1796. if (ret) /* -EOVERFLOW from above */
  1797. goto out;
  1798. if (*num_found >= sk->nr_items) {
  1799. ret = 1;
  1800. goto out;
  1801. }
  1802. }
  1803. advance_key:
  1804. ret = 0;
  1805. test.objectid = sk->max_objectid;
  1806. test.type = sk->max_type;
  1807. test.offset = sk->max_offset;
  1808. if (btrfs_comp_cpu_keys(key, &test) >= 0)
  1809. ret = 1;
  1810. else if (key->offset < (u64)-1)
  1811. key->offset++;
  1812. else if (key->type < (u8)-1) {
  1813. key->offset = 0;
  1814. key->type++;
  1815. } else if (key->objectid < (u64)-1) {
  1816. key->offset = 0;
  1817. key->type = 0;
  1818. key->objectid++;
  1819. } else
  1820. ret = 1;
  1821. out:
  1822. /*
  1823. * 0: all items from this leaf copied, continue with next
  1824. * 1: * more items can be copied, but unused buffer is too small
  1825. * * all items were found
  1826. * Either way, it will stops the loop which iterates to the next
  1827. * leaf
  1828. * -EOVERFLOW: item was to large for buffer
  1829. * -EFAULT: could not copy extent buffer back to userspace
  1830. */
  1831. return ret;
  1832. }
  1833. static noinline int search_ioctl(struct inode *inode,
  1834. struct btrfs_ioctl_search_key *sk,
  1835. size_t *buf_size,
  1836. char __user *ubuf)
  1837. {
  1838. struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
  1839. struct btrfs_root *root;
  1840. struct btrfs_key key;
  1841. struct btrfs_path *path;
  1842. int ret;
  1843. int num_found = 0;
  1844. unsigned long sk_offset = 0;
  1845. if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
  1846. *buf_size = sizeof(struct btrfs_ioctl_search_header);
  1847. return -EOVERFLOW;
  1848. }
  1849. path = btrfs_alloc_path();
  1850. if (!path)
  1851. return -ENOMEM;
  1852. if (sk->tree_id == 0) {
  1853. /* search the root of the inode that was passed */
  1854. root = BTRFS_I(inode)->root;
  1855. } else {
  1856. key.objectid = sk->tree_id;
  1857. key.type = BTRFS_ROOT_ITEM_KEY;
  1858. key.offset = (u64)-1;
  1859. root = btrfs_read_fs_root_no_name(info, &key);
  1860. if (IS_ERR(root)) {
  1861. btrfs_free_path(path);
  1862. return PTR_ERR(root);
  1863. }
  1864. }
  1865. key.objectid = sk->min_objectid;
  1866. key.type = sk->min_type;
  1867. key.offset = sk->min_offset;
  1868. while (1) {
  1869. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1870. if (ret != 0) {
  1871. if (ret > 0)
  1872. ret = 0;
  1873. goto err;
  1874. }
  1875. ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
  1876. &sk_offset, &num_found);
  1877. btrfs_release_path(path);
  1878. if (ret)
  1879. break;
  1880. }
  1881. if (ret > 0)
  1882. ret = 0;
  1883. err:
  1884. sk->nr_items = num_found;
  1885. btrfs_free_path(path);
  1886. return ret;
  1887. }
  1888. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1889. void __user *argp)
  1890. {
  1891. struct btrfs_ioctl_search_args __user *uargs;
  1892. struct btrfs_ioctl_search_key sk;
  1893. struct inode *inode;
  1894. int ret;
  1895. size_t buf_size;
  1896. if (!capable(CAP_SYS_ADMIN))
  1897. return -EPERM;
  1898. uargs = (struct btrfs_ioctl_search_args __user *)argp;
  1899. if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
  1900. return -EFAULT;
  1901. buf_size = sizeof(uargs->buf);
  1902. inode = file_inode(file);
  1903. ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
  1904. /*
  1905. * In the origin implementation an overflow is handled by returning a
  1906. * search header with a len of zero, so reset ret.
  1907. */
  1908. if (ret == -EOVERFLOW)
  1909. ret = 0;
  1910. if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
  1911. ret = -EFAULT;
  1912. return ret;
  1913. }
  1914. static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
  1915. void __user *argp)
  1916. {
  1917. struct btrfs_ioctl_search_args_v2 __user *uarg;
  1918. struct btrfs_ioctl_search_args_v2 args;
  1919. struct inode *inode;
  1920. int ret;
  1921. size_t buf_size;
  1922. const size_t buf_limit = SZ_16M;
  1923. if (!capable(CAP_SYS_ADMIN))
  1924. return -EPERM;
  1925. /* copy search header and buffer size */
  1926. uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
  1927. if (copy_from_user(&args, uarg, sizeof(args)))
  1928. return -EFAULT;
  1929. buf_size = args.buf_size;
  1930. /* limit result size to 16MB */
  1931. if (buf_size > buf_limit)
  1932. buf_size = buf_limit;
  1933. inode = file_inode(file);
  1934. ret = search_ioctl(inode, &args.key, &buf_size,
  1935. (char __user *)(&uarg->buf[0]));
  1936. if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
  1937. ret = -EFAULT;
  1938. else if (ret == -EOVERFLOW &&
  1939. copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
  1940. ret = -EFAULT;
  1941. return ret;
  1942. }
  1943. /*
  1944. * Search INODE_REFs to identify path name of 'dirid' directory
  1945. * in a 'tree_id' tree. and sets path name to 'name'.
  1946. */
  1947. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1948. u64 tree_id, u64 dirid, char *name)
  1949. {
  1950. struct btrfs_root *root;
  1951. struct btrfs_key key;
  1952. char *ptr;
  1953. int ret = -1;
  1954. int slot;
  1955. int len;
  1956. int total_len = 0;
  1957. struct btrfs_inode_ref *iref;
  1958. struct extent_buffer *l;
  1959. struct btrfs_path *path;
  1960. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1961. name[0]='\0';
  1962. return 0;
  1963. }
  1964. path = btrfs_alloc_path();
  1965. if (!path)
  1966. return -ENOMEM;
  1967. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
  1968. key.objectid = tree_id;
  1969. key.type = BTRFS_ROOT_ITEM_KEY;
  1970. key.offset = (u64)-1;
  1971. root = btrfs_read_fs_root_no_name(info, &key);
  1972. if (IS_ERR(root)) {
  1973. ret = PTR_ERR(root);
  1974. goto out;
  1975. }
  1976. key.objectid = dirid;
  1977. key.type = BTRFS_INODE_REF_KEY;
  1978. key.offset = (u64)-1;
  1979. while (1) {
  1980. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1981. if (ret < 0)
  1982. goto out;
  1983. else if (ret > 0) {
  1984. ret = btrfs_previous_item(root, path, dirid,
  1985. BTRFS_INODE_REF_KEY);
  1986. if (ret < 0)
  1987. goto out;
  1988. else if (ret > 0) {
  1989. ret = -ENOENT;
  1990. goto out;
  1991. }
  1992. }
  1993. l = path->nodes[0];
  1994. slot = path->slots[0];
  1995. btrfs_item_key_to_cpu(l, &key, slot);
  1996. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1997. len = btrfs_inode_ref_name_len(l, iref);
  1998. ptr -= len + 1;
  1999. total_len += len + 1;
  2000. if (ptr < name) {
  2001. ret = -ENAMETOOLONG;
  2002. goto out;
  2003. }
  2004. *(ptr + len) = '/';
  2005. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  2006. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  2007. break;
  2008. btrfs_release_path(path);
  2009. key.objectid = key.offset;
  2010. key.offset = (u64)-1;
  2011. dirid = key.objectid;
  2012. }
  2013. memmove(name, ptr, total_len);
  2014. name[total_len] = '\0';
  2015. ret = 0;
  2016. out:
  2017. btrfs_free_path(path);
  2018. return ret;
  2019. }
  2020. static int btrfs_search_path_in_tree_user(struct inode *inode,
  2021. struct btrfs_ioctl_ino_lookup_user_args *args)
  2022. {
  2023. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2024. struct super_block *sb = inode->i_sb;
  2025. struct btrfs_key upper_limit = BTRFS_I(inode)->location;
  2026. u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
  2027. u64 dirid = args->dirid;
  2028. unsigned long item_off;
  2029. unsigned long item_len;
  2030. struct btrfs_inode_ref *iref;
  2031. struct btrfs_root_ref *rref;
  2032. struct btrfs_root *root;
  2033. struct btrfs_path *path;
  2034. struct btrfs_key key, key2;
  2035. struct extent_buffer *leaf;
  2036. struct inode *temp_inode;
  2037. char *ptr;
  2038. int slot;
  2039. int len;
  2040. int total_len = 0;
  2041. int ret;
  2042. path = btrfs_alloc_path();
  2043. if (!path)
  2044. return -ENOMEM;
  2045. /*
  2046. * If the bottom subvolume does not exist directly under upper_limit,
  2047. * construct the path in from the bottom up.
  2048. */
  2049. if (dirid != upper_limit.objectid) {
  2050. ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
  2051. key.objectid = treeid;
  2052. key.type = BTRFS_ROOT_ITEM_KEY;
  2053. key.offset = (u64)-1;
  2054. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2055. if (IS_ERR(root)) {
  2056. ret = PTR_ERR(root);
  2057. goto out;
  2058. }
  2059. key.objectid = dirid;
  2060. key.type = BTRFS_INODE_REF_KEY;
  2061. key.offset = (u64)-1;
  2062. while (1) {
  2063. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2064. if (ret < 0) {
  2065. goto out;
  2066. } else if (ret > 0) {
  2067. ret = btrfs_previous_item(root, path, dirid,
  2068. BTRFS_INODE_REF_KEY);
  2069. if (ret < 0) {
  2070. goto out;
  2071. } else if (ret > 0) {
  2072. ret = -ENOENT;
  2073. goto out;
  2074. }
  2075. }
  2076. leaf = path->nodes[0];
  2077. slot = path->slots[0];
  2078. btrfs_item_key_to_cpu(leaf, &key, slot);
  2079. iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
  2080. len = btrfs_inode_ref_name_len(leaf, iref);
  2081. ptr -= len + 1;
  2082. total_len += len + 1;
  2083. if (ptr < args->path) {
  2084. ret = -ENAMETOOLONG;
  2085. goto out;
  2086. }
  2087. *(ptr + len) = '/';
  2088. read_extent_buffer(leaf, ptr,
  2089. (unsigned long)(iref + 1), len);
  2090. /* Check the read+exec permission of this directory */
  2091. ret = btrfs_previous_item(root, path, dirid,
  2092. BTRFS_INODE_ITEM_KEY);
  2093. if (ret < 0) {
  2094. goto out;
  2095. } else if (ret > 0) {
  2096. ret = -ENOENT;
  2097. goto out;
  2098. }
  2099. leaf = path->nodes[0];
  2100. slot = path->slots[0];
  2101. btrfs_item_key_to_cpu(leaf, &key2, slot);
  2102. if (key2.objectid != dirid) {
  2103. ret = -ENOENT;
  2104. goto out;
  2105. }
  2106. temp_inode = btrfs_iget(sb, &key2, root, NULL);
  2107. if (IS_ERR(temp_inode)) {
  2108. ret = PTR_ERR(temp_inode);
  2109. goto out;
  2110. }
  2111. ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
  2112. iput(temp_inode);
  2113. if (ret) {
  2114. ret = -EACCES;
  2115. goto out;
  2116. }
  2117. if (key.offset == upper_limit.objectid)
  2118. break;
  2119. if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
  2120. ret = -EACCES;
  2121. goto out;
  2122. }
  2123. btrfs_release_path(path);
  2124. key.objectid = key.offset;
  2125. key.offset = (u64)-1;
  2126. dirid = key.objectid;
  2127. }
  2128. memmove(args->path, ptr, total_len);
  2129. args->path[total_len] = '\0';
  2130. btrfs_release_path(path);
  2131. }
  2132. /* Get the bottom subvolume's name from ROOT_REF */
  2133. root = fs_info->tree_root;
  2134. key.objectid = treeid;
  2135. key.type = BTRFS_ROOT_REF_KEY;
  2136. key.offset = args->treeid;
  2137. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2138. if (ret < 0) {
  2139. goto out;
  2140. } else if (ret > 0) {
  2141. ret = -ENOENT;
  2142. goto out;
  2143. }
  2144. leaf = path->nodes[0];
  2145. slot = path->slots[0];
  2146. btrfs_item_key_to_cpu(leaf, &key, slot);
  2147. item_off = btrfs_item_ptr_offset(leaf, slot);
  2148. item_len = btrfs_item_size_nr(leaf, slot);
  2149. /* Check if dirid in ROOT_REF corresponds to passed dirid */
  2150. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  2151. if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
  2152. ret = -EINVAL;
  2153. goto out;
  2154. }
  2155. /* Copy subvolume's name */
  2156. item_off += sizeof(struct btrfs_root_ref);
  2157. item_len -= sizeof(struct btrfs_root_ref);
  2158. read_extent_buffer(leaf, args->name, item_off, item_len);
  2159. args->name[item_len] = 0;
  2160. out:
  2161. btrfs_free_path(path);
  2162. return ret;
  2163. }
  2164. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  2165. void __user *argp)
  2166. {
  2167. struct btrfs_ioctl_ino_lookup_args *args;
  2168. struct inode *inode;
  2169. int ret = 0;
  2170. args = memdup_user(argp, sizeof(*args));
  2171. if (IS_ERR(args))
  2172. return PTR_ERR(args);
  2173. inode = file_inode(file);
  2174. /*
  2175. * Unprivileged query to obtain the containing subvolume root id. The
  2176. * path is reset so it's consistent with btrfs_search_path_in_tree.
  2177. */
  2178. if (args->treeid == 0)
  2179. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  2180. if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
  2181. args->name[0] = 0;
  2182. goto out;
  2183. }
  2184. if (!capable(CAP_SYS_ADMIN)) {
  2185. ret = -EPERM;
  2186. goto out;
  2187. }
  2188. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  2189. args->treeid, args->objectid,
  2190. args->name);
  2191. out:
  2192. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  2193. ret = -EFAULT;
  2194. kfree(args);
  2195. return ret;
  2196. }
  2197. /*
  2198. * Version of ino_lookup ioctl (unprivileged)
  2199. *
  2200. * The main differences from ino_lookup ioctl are:
  2201. *
  2202. * 1. Read + Exec permission will be checked using inode_permission() during
  2203. * path construction. -EACCES will be returned in case of failure.
  2204. * 2. Path construction will be stopped at the inode number which corresponds
  2205. * to the fd with which this ioctl is called. If constructed path does not
  2206. * exist under fd's inode, -EACCES will be returned.
  2207. * 3. The name of bottom subvolume is also searched and filled.
  2208. */
  2209. static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
  2210. {
  2211. struct btrfs_ioctl_ino_lookup_user_args *args;
  2212. struct inode *inode;
  2213. int ret;
  2214. args = memdup_user(argp, sizeof(*args));
  2215. if (IS_ERR(args))
  2216. return PTR_ERR(args);
  2217. inode = file_inode(file);
  2218. if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
  2219. BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
  2220. /*
  2221. * The subvolume does not exist under fd with which this is
  2222. * called
  2223. */
  2224. kfree(args);
  2225. return -EACCES;
  2226. }
  2227. ret = btrfs_search_path_in_tree_user(inode, args);
  2228. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  2229. ret = -EFAULT;
  2230. kfree(args);
  2231. return ret;
  2232. }
  2233. /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
  2234. static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
  2235. {
  2236. struct btrfs_ioctl_get_subvol_info_args *subvol_info;
  2237. struct btrfs_fs_info *fs_info;
  2238. struct btrfs_root *root;
  2239. struct btrfs_path *path;
  2240. struct btrfs_key key;
  2241. struct btrfs_root_item *root_item;
  2242. struct btrfs_root_ref *rref;
  2243. struct extent_buffer *leaf;
  2244. unsigned long item_off;
  2245. unsigned long item_len;
  2246. struct inode *inode;
  2247. int slot;
  2248. int ret = 0;
  2249. path = btrfs_alloc_path();
  2250. if (!path)
  2251. return -ENOMEM;
  2252. subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
  2253. if (!subvol_info) {
  2254. btrfs_free_path(path);
  2255. return -ENOMEM;
  2256. }
  2257. inode = file_inode(file);
  2258. fs_info = BTRFS_I(inode)->root->fs_info;
  2259. /* Get root_item of inode's subvolume */
  2260. key.objectid = BTRFS_I(inode)->root->root_key.objectid;
  2261. key.type = BTRFS_ROOT_ITEM_KEY;
  2262. key.offset = (u64)-1;
  2263. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2264. if (IS_ERR(root)) {
  2265. ret = PTR_ERR(root);
  2266. goto out;
  2267. }
  2268. root_item = &root->root_item;
  2269. subvol_info->treeid = key.objectid;
  2270. subvol_info->generation = btrfs_root_generation(root_item);
  2271. subvol_info->flags = btrfs_root_flags(root_item);
  2272. memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
  2273. memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
  2274. BTRFS_UUID_SIZE);
  2275. memcpy(subvol_info->received_uuid, root_item->received_uuid,
  2276. BTRFS_UUID_SIZE);
  2277. subvol_info->ctransid = btrfs_root_ctransid(root_item);
  2278. subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
  2279. subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
  2280. subvol_info->otransid = btrfs_root_otransid(root_item);
  2281. subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
  2282. subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
  2283. subvol_info->stransid = btrfs_root_stransid(root_item);
  2284. subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
  2285. subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
  2286. subvol_info->rtransid = btrfs_root_rtransid(root_item);
  2287. subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
  2288. subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
  2289. if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
  2290. /* Search root tree for ROOT_BACKREF of this subvolume */
  2291. root = fs_info->tree_root;
  2292. key.type = BTRFS_ROOT_BACKREF_KEY;
  2293. key.offset = 0;
  2294. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2295. if (ret < 0) {
  2296. goto out;
  2297. } else if (path->slots[0] >=
  2298. btrfs_header_nritems(path->nodes[0])) {
  2299. ret = btrfs_next_leaf(root, path);
  2300. if (ret < 0) {
  2301. goto out;
  2302. } else if (ret > 0) {
  2303. ret = -EUCLEAN;
  2304. goto out;
  2305. }
  2306. }
  2307. leaf = path->nodes[0];
  2308. slot = path->slots[0];
  2309. btrfs_item_key_to_cpu(leaf, &key, slot);
  2310. if (key.objectid == subvol_info->treeid &&
  2311. key.type == BTRFS_ROOT_BACKREF_KEY) {
  2312. subvol_info->parent_id = key.offset;
  2313. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  2314. subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
  2315. item_off = btrfs_item_ptr_offset(leaf, slot)
  2316. + sizeof(struct btrfs_root_ref);
  2317. item_len = btrfs_item_size_nr(leaf, slot)
  2318. - sizeof(struct btrfs_root_ref);
  2319. read_extent_buffer(leaf, subvol_info->name,
  2320. item_off, item_len);
  2321. } else {
  2322. ret = -ENOENT;
  2323. goto out;
  2324. }
  2325. }
  2326. if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
  2327. ret = -EFAULT;
  2328. out:
  2329. btrfs_free_path(path);
  2330. kzfree(subvol_info);
  2331. return ret;
  2332. }
  2333. /*
  2334. * Return ROOT_REF information of the subvolume containing this inode
  2335. * except the subvolume name.
  2336. */
  2337. static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
  2338. {
  2339. struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
  2340. struct btrfs_root_ref *rref;
  2341. struct btrfs_root *root;
  2342. struct btrfs_path *path;
  2343. struct btrfs_key key;
  2344. struct extent_buffer *leaf;
  2345. struct inode *inode;
  2346. u64 objectid;
  2347. int slot;
  2348. int ret;
  2349. u8 found;
  2350. path = btrfs_alloc_path();
  2351. if (!path)
  2352. return -ENOMEM;
  2353. rootrefs = memdup_user(argp, sizeof(*rootrefs));
  2354. if (IS_ERR(rootrefs)) {
  2355. btrfs_free_path(path);
  2356. return PTR_ERR(rootrefs);
  2357. }
  2358. inode = file_inode(file);
  2359. root = BTRFS_I(inode)->root->fs_info->tree_root;
  2360. objectid = BTRFS_I(inode)->root->root_key.objectid;
  2361. key.objectid = objectid;
  2362. key.type = BTRFS_ROOT_REF_KEY;
  2363. key.offset = rootrefs->min_treeid;
  2364. found = 0;
  2365. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2366. if (ret < 0) {
  2367. goto out;
  2368. } else if (path->slots[0] >=
  2369. btrfs_header_nritems(path->nodes[0])) {
  2370. ret = btrfs_next_leaf(root, path);
  2371. if (ret < 0) {
  2372. goto out;
  2373. } else if (ret > 0) {
  2374. ret = -EUCLEAN;
  2375. goto out;
  2376. }
  2377. }
  2378. while (1) {
  2379. leaf = path->nodes[0];
  2380. slot = path->slots[0];
  2381. btrfs_item_key_to_cpu(leaf, &key, slot);
  2382. if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
  2383. ret = 0;
  2384. goto out;
  2385. }
  2386. if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
  2387. ret = -EOVERFLOW;
  2388. goto out;
  2389. }
  2390. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  2391. rootrefs->rootref[found].treeid = key.offset;
  2392. rootrefs->rootref[found].dirid =
  2393. btrfs_root_ref_dirid(leaf, rref);
  2394. found++;
  2395. ret = btrfs_next_item(root, path);
  2396. if (ret < 0) {
  2397. goto out;
  2398. } else if (ret > 0) {
  2399. ret = -EUCLEAN;
  2400. goto out;
  2401. }
  2402. }
  2403. out:
  2404. if (!ret || ret == -EOVERFLOW) {
  2405. rootrefs->num_items = found;
  2406. /* update min_treeid for next search */
  2407. if (found)
  2408. rootrefs->min_treeid =
  2409. rootrefs->rootref[found - 1].treeid + 1;
  2410. if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
  2411. ret = -EFAULT;
  2412. }
  2413. kfree(rootrefs);
  2414. btrfs_free_path(path);
  2415. return ret;
  2416. }
  2417. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  2418. void __user *arg)
  2419. {
  2420. struct dentry *parent = file->f_path.dentry;
  2421. struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
  2422. struct dentry *dentry;
  2423. struct inode *dir = d_inode(parent);
  2424. struct inode *inode;
  2425. struct btrfs_root *root = BTRFS_I(dir)->root;
  2426. struct btrfs_root *dest = NULL;
  2427. struct btrfs_ioctl_vol_args *vol_args;
  2428. int namelen;
  2429. int err = 0;
  2430. if (!S_ISDIR(dir->i_mode))
  2431. return -ENOTDIR;
  2432. vol_args = memdup_user(arg, sizeof(*vol_args));
  2433. if (IS_ERR(vol_args))
  2434. return PTR_ERR(vol_args);
  2435. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2436. namelen = strlen(vol_args->name);
  2437. if (strchr(vol_args->name, '/') ||
  2438. strncmp(vol_args->name, "..", namelen) == 0) {
  2439. err = -EINVAL;
  2440. goto out;
  2441. }
  2442. err = mnt_want_write_file(file);
  2443. if (err)
  2444. goto out;
  2445. err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
  2446. if (err == -EINTR)
  2447. goto out_drop_write;
  2448. dentry = lookup_one_len(vol_args->name, parent, namelen);
  2449. if (IS_ERR(dentry)) {
  2450. err = PTR_ERR(dentry);
  2451. goto out_unlock_dir;
  2452. }
  2453. if (d_really_is_negative(dentry)) {
  2454. err = -ENOENT;
  2455. goto out_dput;
  2456. }
  2457. inode = d_inode(dentry);
  2458. dest = BTRFS_I(inode)->root;
  2459. if (!capable(CAP_SYS_ADMIN)) {
  2460. /*
  2461. * Regular user. Only allow this with a special mount
  2462. * option, when the user has write+exec access to the
  2463. * subvol root, and when rmdir(2) would have been
  2464. * allowed.
  2465. *
  2466. * Note that this is _not_ check that the subvol is
  2467. * empty or doesn't contain data that we wouldn't
  2468. * otherwise be able to delete.
  2469. *
  2470. * Users who want to delete empty subvols should try
  2471. * rmdir(2).
  2472. */
  2473. err = -EPERM;
  2474. if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
  2475. goto out_dput;
  2476. /*
  2477. * Do not allow deletion if the parent dir is the same
  2478. * as the dir to be deleted. That means the ioctl
  2479. * must be called on the dentry referencing the root
  2480. * of the subvol, not a random directory contained
  2481. * within it.
  2482. */
  2483. err = -EINVAL;
  2484. if (root == dest)
  2485. goto out_dput;
  2486. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  2487. if (err)
  2488. goto out_dput;
  2489. }
  2490. /* check if subvolume may be deleted by a user */
  2491. err = btrfs_may_delete(dir, dentry, 1);
  2492. if (err)
  2493. goto out_dput;
  2494. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  2495. err = -EINVAL;
  2496. goto out_dput;
  2497. }
  2498. inode_lock(inode);
  2499. err = btrfs_delete_subvolume(dir, dentry);
  2500. inode_unlock(inode);
  2501. if (!err)
  2502. d_delete(dentry);
  2503. out_dput:
  2504. dput(dentry);
  2505. out_unlock_dir:
  2506. inode_unlock(dir);
  2507. out_drop_write:
  2508. mnt_drop_write_file(file);
  2509. out:
  2510. kfree(vol_args);
  2511. return err;
  2512. }
  2513. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2514. {
  2515. struct inode *inode = file_inode(file);
  2516. struct btrfs_root *root = BTRFS_I(inode)->root;
  2517. struct btrfs_ioctl_defrag_range_args *range;
  2518. int ret;
  2519. ret = mnt_want_write_file(file);
  2520. if (ret)
  2521. return ret;
  2522. if (btrfs_root_readonly(root)) {
  2523. ret = -EROFS;
  2524. goto out;
  2525. }
  2526. switch (inode->i_mode & S_IFMT) {
  2527. case S_IFDIR:
  2528. if (!capable(CAP_SYS_ADMIN)) {
  2529. ret = -EPERM;
  2530. goto out;
  2531. }
  2532. ret = btrfs_defrag_root(root);
  2533. break;
  2534. case S_IFREG:
  2535. if (!(file->f_mode & FMODE_WRITE)) {
  2536. ret = -EINVAL;
  2537. goto out;
  2538. }
  2539. range = kzalloc(sizeof(*range), GFP_KERNEL);
  2540. if (!range) {
  2541. ret = -ENOMEM;
  2542. goto out;
  2543. }
  2544. if (argp) {
  2545. if (copy_from_user(range, argp,
  2546. sizeof(*range))) {
  2547. ret = -EFAULT;
  2548. kfree(range);
  2549. goto out;
  2550. }
  2551. /* compression requires us to start the IO */
  2552. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2553. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2554. range->extent_thresh = (u32)-1;
  2555. }
  2556. } else {
  2557. /* the rest are all set to zero by kzalloc */
  2558. range->len = (u64)-1;
  2559. }
  2560. ret = btrfs_defrag_file(file_inode(file), file,
  2561. range, BTRFS_OLDEST_GENERATION, 0);
  2562. if (ret > 0)
  2563. ret = 0;
  2564. kfree(range);
  2565. break;
  2566. default:
  2567. ret = -EINVAL;
  2568. }
  2569. out:
  2570. mnt_drop_write_file(file);
  2571. return ret;
  2572. }
  2573. static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
  2574. {
  2575. struct btrfs_ioctl_vol_args *vol_args;
  2576. int ret;
  2577. if (!capable(CAP_SYS_ADMIN))
  2578. return -EPERM;
  2579. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
  2580. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2581. vol_args = memdup_user(arg, sizeof(*vol_args));
  2582. if (IS_ERR(vol_args)) {
  2583. ret = PTR_ERR(vol_args);
  2584. goto out;
  2585. }
  2586. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2587. ret = btrfs_init_new_device(fs_info, vol_args->name);
  2588. if (!ret)
  2589. btrfs_info(fs_info, "disk added %s", vol_args->name);
  2590. kfree(vol_args);
  2591. out:
  2592. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  2593. return ret;
  2594. }
  2595. static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
  2596. {
  2597. struct inode *inode = file_inode(file);
  2598. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2599. struct btrfs_ioctl_vol_args_v2 *vol_args;
  2600. int ret;
  2601. if (!capable(CAP_SYS_ADMIN))
  2602. return -EPERM;
  2603. ret = mnt_want_write_file(file);
  2604. if (ret)
  2605. return ret;
  2606. vol_args = memdup_user(arg, sizeof(*vol_args));
  2607. if (IS_ERR(vol_args)) {
  2608. ret = PTR_ERR(vol_args);
  2609. goto err_drop;
  2610. }
  2611. /* Check for compatibility reject unknown flags */
  2612. if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
  2613. ret = -EOPNOTSUPP;
  2614. goto out;
  2615. }
  2616. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  2617. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2618. goto out;
  2619. }
  2620. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
  2621. ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
  2622. } else {
  2623. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  2624. ret = btrfs_rm_device(fs_info, vol_args->name, 0);
  2625. }
  2626. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  2627. if (!ret) {
  2628. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
  2629. btrfs_info(fs_info, "device deleted: id %llu",
  2630. vol_args->devid);
  2631. else
  2632. btrfs_info(fs_info, "device deleted: %s",
  2633. vol_args->name);
  2634. }
  2635. out:
  2636. kfree(vol_args);
  2637. err_drop:
  2638. mnt_drop_write_file(file);
  2639. return ret;
  2640. }
  2641. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2642. {
  2643. struct inode *inode = file_inode(file);
  2644. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2645. struct btrfs_ioctl_vol_args *vol_args;
  2646. int ret;
  2647. if (!capable(CAP_SYS_ADMIN))
  2648. return -EPERM;
  2649. ret = mnt_want_write_file(file);
  2650. if (ret)
  2651. return ret;
  2652. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  2653. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2654. goto out_drop_write;
  2655. }
  2656. vol_args = memdup_user(arg, sizeof(*vol_args));
  2657. if (IS_ERR(vol_args)) {
  2658. ret = PTR_ERR(vol_args);
  2659. goto out;
  2660. }
  2661. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2662. ret = btrfs_rm_device(fs_info, vol_args->name, 0);
  2663. if (!ret)
  2664. btrfs_info(fs_info, "disk deleted %s", vol_args->name);
  2665. kfree(vol_args);
  2666. out:
  2667. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  2668. out_drop_write:
  2669. mnt_drop_write_file(file);
  2670. return ret;
  2671. }
  2672. static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
  2673. void __user *arg)
  2674. {
  2675. struct btrfs_ioctl_fs_info_args *fi_args;
  2676. struct btrfs_device *device;
  2677. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2678. int ret = 0;
  2679. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2680. if (!fi_args)
  2681. return -ENOMEM;
  2682. rcu_read_lock();
  2683. fi_args->num_devices = fs_devices->num_devices;
  2684. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  2685. if (device->devid > fi_args->max_id)
  2686. fi_args->max_id = device->devid;
  2687. }
  2688. rcu_read_unlock();
  2689. memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
  2690. fi_args->nodesize = fs_info->nodesize;
  2691. fi_args->sectorsize = fs_info->sectorsize;
  2692. fi_args->clone_alignment = fs_info->sectorsize;
  2693. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2694. ret = -EFAULT;
  2695. kfree(fi_args);
  2696. return ret;
  2697. }
  2698. static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
  2699. void __user *arg)
  2700. {
  2701. struct btrfs_ioctl_dev_info_args *di_args;
  2702. struct btrfs_device *dev;
  2703. int ret = 0;
  2704. char *s_uuid = NULL;
  2705. di_args = memdup_user(arg, sizeof(*di_args));
  2706. if (IS_ERR(di_args))
  2707. return PTR_ERR(di_args);
  2708. if (!btrfs_is_empty_uuid(di_args->uuid))
  2709. s_uuid = di_args->uuid;
  2710. rcu_read_lock();
  2711. dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
  2712. if (!dev) {
  2713. ret = -ENODEV;
  2714. goto out;
  2715. }
  2716. di_args->devid = dev->devid;
  2717. di_args->bytes_used = btrfs_device_get_bytes_used(dev);
  2718. di_args->total_bytes = btrfs_device_get_total_bytes(dev);
  2719. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2720. if (dev->name) {
  2721. struct rcu_string *name;
  2722. name = rcu_dereference(dev->name);
  2723. strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
  2724. di_args->path[sizeof(di_args->path) - 1] = 0;
  2725. } else {
  2726. di_args->path[0] = '\0';
  2727. }
  2728. out:
  2729. rcu_read_unlock();
  2730. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2731. ret = -EFAULT;
  2732. kfree(di_args);
  2733. return ret;
  2734. }
  2735. static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
  2736. {
  2737. struct page *page;
  2738. page = grab_cache_page(inode->i_mapping, index);
  2739. if (!page)
  2740. return ERR_PTR(-ENOMEM);
  2741. if (!PageUptodate(page)) {
  2742. int ret;
  2743. ret = btrfs_readpage(NULL, page);
  2744. if (ret)
  2745. return ERR_PTR(ret);
  2746. lock_page(page);
  2747. if (!PageUptodate(page)) {
  2748. unlock_page(page);
  2749. put_page(page);
  2750. return ERR_PTR(-EIO);
  2751. }
  2752. if (page->mapping != inode->i_mapping) {
  2753. unlock_page(page);
  2754. put_page(page);
  2755. return ERR_PTR(-EAGAIN);
  2756. }
  2757. }
  2758. return page;
  2759. }
  2760. static int gather_extent_pages(struct inode *inode, struct page **pages,
  2761. int num_pages, u64 off)
  2762. {
  2763. int i;
  2764. pgoff_t index = off >> PAGE_SHIFT;
  2765. for (i = 0; i < num_pages; i++) {
  2766. again:
  2767. pages[i] = extent_same_get_page(inode, index + i);
  2768. if (IS_ERR(pages[i])) {
  2769. int err = PTR_ERR(pages[i]);
  2770. if (err == -EAGAIN)
  2771. goto again;
  2772. pages[i] = NULL;
  2773. return err;
  2774. }
  2775. }
  2776. return 0;
  2777. }
  2778. static int lock_extent_range(struct inode *inode, u64 off, u64 len,
  2779. bool retry_range_locking)
  2780. {
  2781. /*
  2782. * Do any pending delalloc/csum calculations on inode, one way or
  2783. * another, and lock file content.
  2784. * The locking order is:
  2785. *
  2786. * 1) pages
  2787. * 2) range in the inode's io tree
  2788. */
  2789. while (1) {
  2790. struct btrfs_ordered_extent *ordered;
  2791. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2792. ordered = btrfs_lookup_first_ordered_extent(inode,
  2793. off + len - 1);
  2794. if ((!ordered ||
  2795. ordered->file_offset + ordered->len <= off ||
  2796. ordered->file_offset >= off + len) &&
  2797. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2798. off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
  2799. if (ordered)
  2800. btrfs_put_ordered_extent(ordered);
  2801. break;
  2802. }
  2803. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2804. if (ordered)
  2805. btrfs_put_ordered_extent(ordered);
  2806. if (!retry_range_locking)
  2807. return -EAGAIN;
  2808. btrfs_wait_ordered_range(inode, off, len);
  2809. }
  2810. return 0;
  2811. }
  2812. static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
  2813. {
  2814. inode_unlock(inode1);
  2815. inode_unlock(inode2);
  2816. }
  2817. static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
  2818. {
  2819. if (inode1 < inode2)
  2820. swap(inode1, inode2);
  2821. inode_lock_nested(inode1, I_MUTEX_PARENT);
  2822. inode_lock_nested(inode2, I_MUTEX_CHILD);
  2823. }
  2824. static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
  2825. struct inode *inode2, u64 loff2, u64 len)
  2826. {
  2827. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2828. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2829. }
  2830. static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
  2831. struct inode *inode2, u64 loff2, u64 len,
  2832. bool retry_range_locking)
  2833. {
  2834. int ret;
  2835. if (inode1 < inode2) {
  2836. swap(inode1, inode2);
  2837. swap(loff1, loff2);
  2838. }
  2839. ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
  2840. if (ret)
  2841. return ret;
  2842. ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
  2843. if (ret)
  2844. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
  2845. loff1 + len - 1);
  2846. return ret;
  2847. }
  2848. struct cmp_pages {
  2849. int num_pages;
  2850. struct page **src_pages;
  2851. struct page **dst_pages;
  2852. };
  2853. static void btrfs_cmp_data_free(struct cmp_pages *cmp)
  2854. {
  2855. int i;
  2856. struct page *pg;
  2857. for (i = 0; i < cmp->num_pages; i++) {
  2858. pg = cmp->src_pages[i];
  2859. if (pg) {
  2860. unlock_page(pg);
  2861. put_page(pg);
  2862. }
  2863. pg = cmp->dst_pages[i];
  2864. if (pg) {
  2865. unlock_page(pg);
  2866. put_page(pg);
  2867. }
  2868. }
  2869. }
  2870. static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
  2871. struct inode *dst, u64 dst_loff,
  2872. u64 len, struct cmp_pages *cmp)
  2873. {
  2874. int ret;
  2875. int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
  2876. cmp->num_pages = num_pages;
  2877. ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
  2878. if (ret)
  2879. goto out;
  2880. ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
  2881. out:
  2882. if (ret)
  2883. btrfs_cmp_data_free(cmp);
  2884. return ret;
  2885. }
  2886. static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
  2887. {
  2888. int ret = 0;
  2889. int i;
  2890. struct page *src_page, *dst_page;
  2891. unsigned int cmp_len = PAGE_SIZE;
  2892. void *addr, *dst_addr;
  2893. i = 0;
  2894. while (len) {
  2895. if (len < PAGE_SIZE)
  2896. cmp_len = len;
  2897. BUG_ON(i >= cmp->num_pages);
  2898. src_page = cmp->src_pages[i];
  2899. dst_page = cmp->dst_pages[i];
  2900. ASSERT(PageLocked(src_page));
  2901. ASSERT(PageLocked(dst_page));
  2902. addr = kmap_atomic(src_page);
  2903. dst_addr = kmap_atomic(dst_page);
  2904. flush_dcache_page(src_page);
  2905. flush_dcache_page(dst_page);
  2906. if (memcmp(addr, dst_addr, cmp_len))
  2907. ret = -EBADE;
  2908. kunmap_atomic(addr);
  2909. kunmap_atomic(dst_addr);
  2910. if (ret)
  2911. break;
  2912. len -= cmp_len;
  2913. i++;
  2914. }
  2915. return ret;
  2916. }
  2917. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
  2918. u64 olen)
  2919. {
  2920. u64 len = *plen;
  2921. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2922. if (off + olen > inode->i_size || off + olen < off)
  2923. return -EINVAL;
  2924. /* if we extend to eof, continue to block boundary */
  2925. if (off + len == inode->i_size)
  2926. *plen = len = ALIGN(inode->i_size, bs) - off;
  2927. /* Check that we are block aligned - btrfs_clone() requires this */
  2928. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2929. return -EINVAL;
  2930. return 0;
  2931. }
  2932. static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
  2933. struct inode *dst, u64 dst_loff,
  2934. struct cmp_pages *cmp)
  2935. {
  2936. int ret;
  2937. u64 len = olen;
  2938. bool same_inode = (src == dst);
  2939. u64 same_lock_start = 0;
  2940. u64 same_lock_len = 0;
  2941. ret = extent_same_check_offsets(src, loff, &len, olen);
  2942. if (ret)
  2943. return ret;
  2944. ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
  2945. if (ret)
  2946. return ret;
  2947. if (same_inode) {
  2948. /*
  2949. * Single inode case wants the same checks, except we
  2950. * don't want our length pushed out past i_size as
  2951. * comparing that data range makes no sense.
  2952. *
  2953. * extent_same_check_offsets() will do this for an
  2954. * unaligned length at i_size, so catch it here and
  2955. * reject the request.
  2956. *
  2957. * This effectively means we require aligned extents
  2958. * for the single-inode case, whereas the other cases
  2959. * allow an unaligned length so long as it ends at
  2960. * i_size.
  2961. */
  2962. if (len != olen)
  2963. return -EINVAL;
  2964. /* Check for overlapping ranges */
  2965. if (dst_loff + len > loff && dst_loff < loff + len)
  2966. return -EINVAL;
  2967. same_lock_start = min_t(u64, loff, dst_loff);
  2968. same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
  2969. }
  2970. again:
  2971. ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
  2972. if (ret)
  2973. return ret;
  2974. if (same_inode)
  2975. ret = lock_extent_range(src, same_lock_start, same_lock_len,
  2976. false);
  2977. else
  2978. ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
  2979. false);
  2980. /*
  2981. * If one of the inodes has dirty pages in the respective range or
  2982. * ordered extents, we need to flush dellaloc and wait for all ordered
  2983. * extents in the range. We must unlock the pages and the ranges in the
  2984. * io trees to avoid deadlocks when flushing delalloc (requires locking
  2985. * pages) and when waiting for ordered extents to complete (they require
  2986. * range locking).
  2987. */
  2988. if (ret == -EAGAIN) {
  2989. /*
  2990. * Ranges in the io trees already unlocked. Now unlock all
  2991. * pages before waiting for all IO to complete.
  2992. */
  2993. btrfs_cmp_data_free(cmp);
  2994. if (same_inode) {
  2995. btrfs_wait_ordered_range(src, same_lock_start,
  2996. same_lock_len);
  2997. } else {
  2998. btrfs_wait_ordered_range(src, loff, len);
  2999. btrfs_wait_ordered_range(dst, dst_loff, len);
  3000. }
  3001. goto again;
  3002. }
  3003. ASSERT(ret == 0);
  3004. if (WARN_ON(ret)) {
  3005. /* ranges in the io trees already unlocked */
  3006. btrfs_cmp_data_free(cmp);
  3007. return ret;
  3008. }
  3009. /* pass original length for comparison so we stay within i_size */
  3010. ret = btrfs_cmp_data(olen, cmp);
  3011. if (ret == 0)
  3012. ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
  3013. if (same_inode)
  3014. unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
  3015. same_lock_start + same_lock_len - 1);
  3016. else
  3017. btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
  3018. btrfs_cmp_data_free(cmp);
  3019. return ret;
  3020. }
  3021. #define BTRFS_MAX_DEDUPE_LEN SZ_16M
  3022. static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
  3023. struct inode *dst, u64 dst_loff)
  3024. {
  3025. int ret;
  3026. struct cmp_pages cmp;
  3027. int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
  3028. bool same_inode = (src == dst);
  3029. u64 i, tail_len, chunk_count;
  3030. if (olen == 0)
  3031. return 0;
  3032. if (same_inode)
  3033. inode_lock(src);
  3034. else
  3035. btrfs_double_inode_lock(src, dst);
  3036. /* don't make the dst file partly checksummed */
  3037. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3038. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  3039. ret = -EINVAL;
  3040. goto out_unlock;
  3041. }
  3042. tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
  3043. chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
  3044. if (chunk_count == 0)
  3045. num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
  3046. /*
  3047. * If deduping ranges in the same inode, locking rules make it
  3048. * mandatory to always lock pages in ascending order to avoid deadlocks
  3049. * with concurrent tasks (such as starting writeback/delalloc).
  3050. */
  3051. if (same_inode && dst_loff < loff)
  3052. swap(loff, dst_loff);
  3053. /*
  3054. * We must gather up all the pages before we initiate our extent
  3055. * locking. We use an array for the page pointers. Size of the array is
  3056. * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
  3057. */
  3058. cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
  3059. GFP_KERNEL | __GFP_ZERO);
  3060. cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
  3061. GFP_KERNEL | __GFP_ZERO);
  3062. if (!cmp.src_pages || !cmp.dst_pages) {
  3063. ret = -ENOMEM;
  3064. goto out_free;
  3065. }
  3066. for (i = 0; i < chunk_count; i++) {
  3067. ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
  3068. dst, dst_loff, &cmp);
  3069. if (ret)
  3070. goto out_unlock;
  3071. loff += BTRFS_MAX_DEDUPE_LEN;
  3072. dst_loff += BTRFS_MAX_DEDUPE_LEN;
  3073. }
  3074. if (tail_len > 0)
  3075. ret = btrfs_extent_same_range(src, loff, tail_len, dst,
  3076. dst_loff, &cmp);
  3077. out_unlock:
  3078. if (same_inode)
  3079. inode_unlock(src);
  3080. else
  3081. btrfs_double_inode_unlock(src, dst);
  3082. out_free:
  3083. kvfree(cmp.src_pages);
  3084. kvfree(cmp.dst_pages);
  3085. return ret;
  3086. }
  3087. ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
  3088. struct file *dst_file, u64 dst_loff)
  3089. {
  3090. struct inode *src = file_inode(src_file);
  3091. struct inode *dst = file_inode(dst_file);
  3092. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  3093. ssize_t res;
  3094. if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
  3095. /*
  3096. * Btrfs does not support blocksize < page_size. As a
  3097. * result, btrfs_cmp_data() won't correctly handle
  3098. * this situation without an update.
  3099. */
  3100. return -EINVAL;
  3101. }
  3102. res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
  3103. if (res)
  3104. return res;
  3105. return olen;
  3106. }
  3107. static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
  3108. struct inode *inode,
  3109. u64 endoff,
  3110. const u64 destoff,
  3111. const u64 olen,
  3112. int no_time_update)
  3113. {
  3114. struct btrfs_root *root = BTRFS_I(inode)->root;
  3115. int ret;
  3116. inode_inc_iversion(inode);
  3117. if (!no_time_update)
  3118. inode->i_mtime = inode->i_ctime = current_time(inode);
  3119. /*
  3120. * We round up to the block size at eof when determining which
  3121. * extents to clone above, but shouldn't round up the file size.
  3122. */
  3123. if (endoff > destoff + olen)
  3124. endoff = destoff + olen;
  3125. if (endoff > inode->i_size)
  3126. btrfs_i_size_write(BTRFS_I(inode), endoff);
  3127. ret = btrfs_update_inode(trans, root, inode);
  3128. if (ret) {
  3129. btrfs_abort_transaction(trans, ret);
  3130. btrfs_end_transaction(trans);
  3131. goto out;
  3132. }
  3133. ret = btrfs_end_transaction(trans);
  3134. out:
  3135. return ret;
  3136. }
  3137. static void clone_update_extent_map(struct btrfs_inode *inode,
  3138. const struct btrfs_trans_handle *trans,
  3139. const struct btrfs_path *path,
  3140. const u64 hole_offset,
  3141. const u64 hole_len)
  3142. {
  3143. struct extent_map_tree *em_tree = &inode->extent_tree;
  3144. struct extent_map *em;
  3145. int ret;
  3146. em = alloc_extent_map();
  3147. if (!em) {
  3148. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
  3149. return;
  3150. }
  3151. if (path) {
  3152. struct btrfs_file_extent_item *fi;
  3153. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3154. struct btrfs_file_extent_item);
  3155. btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
  3156. em->generation = -1;
  3157. if (btrfs_file_extent_type(path->nodes[0], fi) ==
  3158. BTRFS_FILE_EXTENT_INLINE)
  3159. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3160. &inode->runtime_flags);
  3161. } else {
  3162. em->start = hole_offset;
  3163. em->len = hole_len;
  3164. em->ram_bytes = em->len;
  3165. em->orig_start = hole_offset;
  3166. em->block_start = EXTENT_MAP_HOLE;
  3167. em->block_len = 0;
  3168. em->orig_block_len = 0;
  3169. em->compress_type = BTRFS_COMPRESS_NONE;
  3170. em->generation = trans->transid;
  3171. }
  3172. while (1) {
  3173. write_lock(&em_tree->lock);
  3174. ret = add_extent_mapping(em_tree, em, 1);
  3175. write_unlock(&em_tree->lock);
  3176. if (ret != -EEXIST) {
  3177. free_extent_map(em);
  3178. break;
  3179. }
  3180. btrfs_drop_extent_cache(inode, em->start,
  3181. em->start + em->len - 1, 0);
  3182. }
  3183. if (ret)
  3184. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
  3185. }
  3186. /*
  3187. * Make sure we do not end up inserting an inline extent into a file that has
  3188. * already other (non-inline) extents. If a file has an inline extent it can
  3189. * not have any other extents and the (single) inline extent must start at the
  3190. * file offset 0. Failing to respect these rules will lead to file corruption,
  3191. * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
  3192. *
  3193. * We can have extents that have been already written to disk or we can have
  3194. * dirty ranges still in delalloc, in which case the extent maps and items are
  3195. * created only when we run delalloc, and the delalloc ranges might fall outside
  3196. * the range we are currently locking in the inode's io tree. So we check the
  3197. * inode's i_size because of that (i_size updates are done while holding the
  3198. * i_mutex, which we are holding here).
  3199. * We also check to see if the inode has a size not greater than "datal" but has
  3200. * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
  3201. * protected against such concurrent fallocate calls by the i_mutex).
  3202. *
  3203. * If the file has no extents but a size greater than datal, do not allow the
  3204. * copy because we would need turn the inline extent into a non-inline one (even
  3205. * with NO_HOLES enabled). If we find our destination inode only has one inline
  3206. * extent, just overwrite it with the source inline extent if its size is less
  3207. * than the source extent's size, or we could copy the source inline extent's
  3208. * data into the destination inode's inline extent if the later is greater then
  3209. * the former.
  3210. */
  3211. static int clone_copy_inline_extent(struct inode *dst,
  3212. struct btrfs_trans_handle *trans,
  3213. struct btrfs_path *path,
  3214. struct btrfs_key *new_key,
  3215. const u64 drop_start,
  3216. const u64 datal,
  3217. const u64 skip,
  3218. const u64 size,
  3219. char *inline_data)
  3220. {
  3221. struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
  3222. struct btrfs_root *root = BTRFS_I(dst)->root;
  3223. const u64 aligned_end = ALIGN(new_key->offset + datal,
  3224. fs_info->sectorsize);
  3225. int ret;
  3226. struct btrfs_key key;
  3227. if (new_key->offset > 0)
  3228. return -EOPNOTSUPP;
  3229. key.objectid = btrfs_ino(BTRFS_I(dst));
  3230. key.type = BTRFS_EXTENT_DATA_KEY;
  3231. key.offset = 0;
  3232. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3233. if (ret < 0) {
  3234. return ret;
  3235. } else if (ret > 0) {
  3236. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  3237. ret = btrfs_next_leaf(root, path);
  3238. if (ret < 0)
  3239. return ret;
  3240. else if (ret > 0)
  3241. goto copy_inline_extent;
  3242. }
  3243. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  3244. if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
  3245. key.type == BTRFS_EXTENT_DATA_KEY) {
  3246. ASSERT(key.offset > 0);
  3247. return -EOPNOTSUPP;
  3248. }
  3249. } else if (i_size_read(dst) <= datal) {
  3250. struct btrfs_file_extent_item *ei;
  3251. u64 ext_len;
  3252. /*
  3253. * If the file size is <= datal, make sure there are no other
  3254. * extents following (can happen do to an fallocate call with
  3255. * the flag FALLOC_FL_KEEP_SIZE).
  3256. */
  3257. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3258. struct btrfs_file_extent_item);
  3259. /*
  3260. * If it's an inline extent, it can not have other extents
  3261. * following it.
  3262. */
  3263. if (btrfs_file_extent_type(path->nodes[0], ei) ==
  3264. BTRFS_FILE_EXTENT_INLINE)
  3265. goto copy_inline_extent;
  3266. ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3267. if (ext_len > aligned_end)
  3268. return -EOPNOTSUPP;
  3269. ret = btrfs_next_item(root, path);
  3270. if (ret < 0) {
  3271. return ret;
  3272. } else if (ret == 0) {
  3273. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3274. path->slots[0]);
  3275. if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
  3276. key.type == BTRFS_EXTENT_DATA_KEY)
  3277. return -EOPNOTSUPP;
  3278. }
  3279. }
  3280. copy_inline_extent:
  3281. /*
  3282. * We have no extent items, or we have an extent at offset 0 which may
  3283. * or may not be inlined. All these cases are dealt the same way.
  3284. */
  3285. if (i_size_read(dst) > datal) {
  3286. /*
  3287. * If the destination inode has an inline extent...
  3288. * This would require copying the data from the source inline
  3289. * extent into the beginning of the destination's inline extent.
  3290. * But this is really complex, both extents can be compressed
  3291. * or just one of them, which would require decompressing and
  3292. * re-compressing data (which could increase the new compressed
  3293. * size, not allowing the compressed data to fit anymore in an
  3294. * inline extent).
  3295. * So just don't support this case for now (it should be rare,
  3296. * we are not really saving space when cloning inline extents).
  3297. */
  3298. return -EOPNOTSUPP;
  3299. }
  3300. btrfs_release_path(path);
  3301. ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
  3302. if (ret)
  3303. return ret;
  3304. ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
  3305. if (ret)
  3306. return ret;
  3307. if (skip) {
  3308. const u32 start = btrfs_file_extent_calc_inline_size(0);
  3309. memmove(inline_data + start, inline_data + start + skip, datal);
  3310. }
  3311. write_extent_buffer(path->nodes[0], inline_data,
  3312. btrfs_item_ptr_offset(path->nodes[0],
  3313. path->slots[0]),
  3314. size);
  3315. inode_add_bytes(dst, datal);
  3316. return 0;
  3317. }
  3318. /**
  3319. * btrfs_clone() - clone a range from inode file to another
  3320. *
  3321. * @src: Inode to clone from
  3322. * @inode: Inode to clone to
  3323. * @off: Offset within source to start clone from
  3324. * @olen: Original length, passed by user, of range to clone
  3325. * @olen_aligned: Block-aligned value of olen
  3326. * @destoff: Offset within @inode to start clone
  3327. * @no_time_update: Whether to update mtime/ctime on the target inode
  3328. */
  3329. static int btrfs_clone(struct inode *src, struct inode *inode,
  3330. const u64 off, const u64 olen, const u64 olen_aligned,
  3331. const u64 destoff, int no_time_update)
  3332. {
  3333. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3334. struct btrfs_root *root = BTRFS_I(inode)->root;
  3335. struct btrfs_path *path = NULL;
  3336. struct extent_buffer *leaf;
  3337. struct btrfs_trans_handle *trans;
  3338. char *buf = NULL;
  3339. struct btrfs_key key;
  3340. u32 nritems;
  3341. int slot;
  3342. int ret;
  3343. const u64 len = olen_aligned;
  3344. u64 last_dest_end = destoff;
  3345. ret = -ENOMEM;
  3346. buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
  3347. if (!buf)
  3348. return ret;
  3349. path = btrfs_alloc_path();
  3350. if (!path) {
  3351. kvfree(buf);
  3352. return ret;
  3353. }
  3354. path->reada = READA_FORWARD;
  3355. /* clone data */
  3356. key.objectid = btrfs_ino(BTRFS_I(src));
  3357. key.type = BTRFS_EXTENT_DATA_KEY;
  3358. key.offset = off;
  3359. while (1) {
  3360. u64 next_key_min_offset = key.offset + 1;
  3361. /*
  3362. * note the key will change type as we walk through the
  3363. * tree.
  3364. */
  3365. path->leave_spinning = 1;
  3366. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  3367. 0, 0);
  3368. if (ret < 0)
  3369. goto out;
  3370. /*
  3371. * First search, if no extent item that starts at offset off was
  3372. * found but the previous item is an extent item, it's possible
  3373. * it might overlap our target range, therefore process it.
  3374. */
  3375. if (key.offset == off && ret > 0 && path->slots[0] > 0) {
  3376. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3377. path->slots[0] - 1);
  3378. if (key.type == BTRFS_EXTENT_DATA_KEY)
  3379. path->slots[0]--;
  3380. }
  3381. nritems = btrfs_header_nritems(path->nodes[0]);
  3382. process_slot:
  3383. if (path->slots[0] >= nritems) {
  3384. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  3385. if (ret < 0)
  3386. goto out;
  3387. if (ret > 0)
  3388. break;
  3389. nritems = btrfs_header_nritems(path->nodes[0]);
  3390. }
  3391. leaf = path->nodes[0];
  3392. slot = path->slots[0];
  3393. btrfs_item_key_to_cpu(leaf, &key, slot);
  3394. if (key.type > BTRFS_EXTENT_DATA_KEY ||
  3395. key.objectid != btrfs_ino(BTRFS_I(src)))
  3396. break;
  3397. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3398. struct btrfs_file_extent_item *extent;
  3399. int type;
  3400. u32 size;
  3401. struct btrfs_key new_key;
  3402. u64 disko = 0, diskl = 0;
  3403. u64 datao = 0, datal = 0;
  3404. u8 comp;
  3405. u64 drop_start;
  3406. extent = btrfs_item_ptr(leaf, slot,
  3407. struct btrfs_file_extent_item);
  3408. comp = btrfs_file_extent_compression(leaf, extent);
  3409. type = btrfs_file_extent_type(leaf, extent);
  3410. if (type == BTRFS_FILE_EXTENT_REG ||
  3411. type == BTRFS_FILE_EXTENT_PREALLOC) {
  3412. disko = btrfs_file_extent_disk_bytenr(leaf,
  3413. extent);
  3414. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  3415. extent);
  3416. datao = btrfs_file_extent_offset(leaf, extent);
  3417. datal = btrfs_file_extent_num_bytes(leaf,
  3418. extent);
  3419. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  3420. /* take upper bound, may be compressed */
  3421. datal = btrfs_file_extent_ram_bytes(leaf,
  3422. extent);
  3423. }
  3424. /*
  3425. * The first search might have left us at an extent
  3426. * item that ends before our target range's start, can
  3427. * happen if we have holes and NO_HOLES feature enabled.
  3428. */
  3429. if (key.offset + datal <= off) {
  3430. path->slots[0]++;
  3431. goto process_slot;
  3432. } else if (key.offset >= off + len) {
  3433. break;
  3434. }
  3435. next_key_min_offset = key.offset + datal;
  3436. size = btrfs_item_size_nr(leaf, slot);
  3437. read_extent_buffer(leaf, buf,
  3438. btrfs_item_ptr_offset(leaf, slot),
  3439. size);
  3440. btrfs_release_path(path);
  3441. path->leave_spinning = 0;
  3442. memcpy(&new_key, &key, sizeof(new_key));
  3443. new_key.objectid = btrfs_ino(BTRFS_I(inode));
  3444. if (off <= key.offset)
  3445. new_key.offset = key.offset + destoff - off;
  3446. else
  3447. new_key.offset = destoff;
  3448. /*
  3449. * Deal with a hole that doesn't have an extent item
  3450. * that represents it (NO_HOLES feature enabled).
  3451. * This hole is either in the middle of the cloning
  3452. * range or at the beginning (fully overlaps it or
  3453. * partially overlaps it).
  3454. */
  3455. if (new_key.offset != last_dest_end)
  3456. drop_start = last_dest_end;
  3457. else
  3458. drop_start = new_key.offset;
  3459. /*
  3460. * 1 - adjusting old extent (we may have to split it)
  3461. * 1 - add new extent
  3462. * 1 - inode update
  3463. */
  3464. trans = btrfs_start_transaction(root, 3);
  3465. if (IS_ERR(trans)) {
  3466. ret = PTR_ERR(trans);
  3467. goto out;
  3468. }
  3469. if (type == BTRFS_FILE_EXTENT_REG ||
  3470. type == BTRFS_FILE_EXTENT_PREALLOC) {
  3471. /*
  3472. * a | --- range to clone ---| b
  3473. * | ------------- extent ------------- |
  3474. */
  3475. /* subtract range b */
  3476. if (key.offset + datal > off + len)
  3477. datal = off + len - key.offset;
  3478. /* subtract range a */
  3479. if (off > key.offset) {
  3480. datao += off - key.offset;
  3481. datal -= off - key.offset;
  3482. }
  3483. ret = btrfs_drop_extents(trans, root, inode,
  3484. drop_start,
  3485. new_key.offset + datal,
  3486. 1);
  3487. if (ret) {
  3488. if (ret != -EOPNOTSUPP)
  3489. btrfs_abort_transaction(trans,
  3490. ret);
  3491. btrfs_end_transaction(trans);
  3492. goto out;
  3493. }
  3494. ret = btrfs_insert_empty_item(trans, root, path,
  3495. &new_key, size);
  3496. if (ret) {
  3497. btrfs_abort_transaction(trans, ret);
  3498. btrfs_end_transaction(trans);
  3499. goto out;
  3500. }
  3501. leaf = path->nodes[0];
  3502. slot = path->slots[0];
  3503. write_extent_buffer(leaf, buf,
  3504. btrfs_item_ptr_offset(leaf, slot),
  3505. size);
  3506. extent = btrfs_item_ptr(leaf, slot,
  3507. struct btrfs_file_extent_item);
  3508. /* disko == 0 means it's a hole */
  3509. if (!disko)
  3510. datao = 0;
  3511. btrfs_set_file_extent_offset(leaf, extent,
  3512. datao);
  3513. btrfs_set_file_extent_num_bytes(leaf, extent,
  3514. datal);
  3515. if (disko) {
  3516. inode_add_bytes(inode, datal);
  3517. ret = btrfs_inc_extent_ref(trans,
  3518. root,
  3519. disko, diskl, 0,
  3520. root->root_key.objectid,
  3521. btrfs_ino(BTRFS_I(inode)),
  3522. new_key.offset - datao);
  3523. if (ret) {
  3524. btrfs_abort_transaction(trans,
  3525. ret);
  3526. btrfs_end_transaction(trans);
  3527. goto out;
  3528. }
  3529. }
  3530. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  3531. u64 skip = 0;
  3532. u64 trim = 0;
  3533. if (off > key.offset) {
  3534. skip = off - key.offset;
  3535. new_key.offset += skip;
  3536. }
  3537. if (key.offset + datal > off + len)
  3538. trim = key.offset + datal - (off + len);
  3539. if (comp && (skip || trim)) {
  3540. ret = -EINVAL;
  3541. btrfs_end_transaction(trans);
  3542. goto out;
  3543. }
  3544. size -= skip + trim;
  3545. datal -= skip + trim;
  3546. ret = clone_copy_inline_extent(inode,
  3547. trans, path,
  3548. &new_key,
  3549. drop_start,
  3550. datal,
  3551. skip, size, buf);
  3552. if (ret) {
  3553. if (ret != -EOPNOTSUPP)
  3554. btrfs_abort_transaction(trans,
  3555. ret);
  3556. btrfs_end_transaction(trans);
  3557. goto out;
  3558. }
  3559. leaf = path->nodes[0];
  3560. slot = path->slots[0];
  3561. }
  3562. /* If we have an implicit hole (NO_HOLES feature). */
  3563. if (drop_start < new_key.offset)
  3564. clone_update_extent_map(BTRFS_I(inode), trans,
  3565. NULL, drop_start,
  3566. new_key.offset - drop_start);
  3567. clone_update_extent_map(BTRFS_I(inode), trans,
  3568. path, 0, 0);
  3569. btrfs_mark_buffer_dirty(leaf);
  3570. btrfs_release_path(path);
  3571. last_dest_end = ALIGN(new_key.offset + datal,
  3572. fs_info->sectorsize);
  3573. ret = clone_finish_inode_update(trans, inode,
  3574. last_dest_end,
  3575. destoff, olen,
  3576. no_time_update);
  3577. if (ret)
  3578. goto out;
  3579. if (new_key.offset + datal >= destoff + len)
  3580. break;
  3581. }
  3582. btrfs_release_path(path);
  3583. key.offset = next_key_min_offset;
  3584. if (fatal_signal_pending(current)) {
  3585. ret = -EINTR;
  3586. goto out;
  3587. }
  3588. }
  3589. ret = 0;
  3590. if (last_dest_end < destoff + len) {
  3591. /*
  3592. * We have an implicit hole (NO_HOLES feature is enabled) that
  3593. * fully or partially overlaps our cloning range at its end.
  3594. */
  3595. btrfs_release_path(path);
  3596. /*
  3597. * 1 - remove extent(s)
  3598. * 1 - inode update
  3599. */
  3600. trans = btrfs_start_transaction(root, 2);
  3601. if (IS_ERR(trans)) {
  3602. ret = PTR_ERR(trans);
  3603. goto out;
  3604. }
  3605. ret = btrfs_drop_extents(trans, root, inode,
  3606. last_dest_end, destoff + len, 1);
  3607. if (ret) {
  3608. if (ret != -EOPNOTSUPP)
  3609. btrfs_abort_transaction(trans, ret);
  3610. btrfs_end_transaction(trans);
  3611. goto out;
  3612. }
  3613. clone_update_extent_map(BTRFS_I(inode), trans, NULL,
  3614. last_dest_end,
  3615. destoff + len - last_dest_end);
  3616. ret = clone_finish_inode_update(trans, inode, destoff + len,
  3617. destoff, olen, no_time_update);
  3618. }
  3619. out:
  3620. btrfs_free_path(path);
  3621. kvfree(buf);
  3622. return ret;
  3623. }
  3624. static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  3625. u64 off, u64 olen, u64 destoff)
  3626. {
  3627. struct inode *inode = file_inode(file);
  3628. struct inode *src = file_inode(file_src);
  3629. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3630. struct btrfs_root *root = BTRFS_I(inode)->root;
  3631. int ret;
  3632. u64 len = olen;
  3633. u64 bs = fs_info->sb->s_blocksize;
  3634. int same_inode = src == inode;
  3635. /*
  3636. * TODO:
  3637. * - split compressed inline extents. annoying: we need to
  3638. * decompress into destination's address_space (the file offset
  3639. * may change, so source mapping won't do), then recompress (or
  3640. * otherwise reinsert) a subrange.
  3641. *
  3642. * - split destination inode's inline extents. The inline extents can
  3643. * be either compressed or non-compressed.
  3644. */
  3645. if (btrfs_root_readonly(root))
  3646. return -EROFS;
  3647. if (file_src->f_path.mnt != file->f_path.mnt ||
  3648. src->i_sb != inode->i_sb)
  3649. return -EXDEV;
  3650. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  3651. return -EISDIR;
  3652. if (!same_inode) {
  3653. btrfs_double_inode_lock(src, inode);
  3654. } else {
  3655. inode_lock(src);
  3656. }
  3657. /* don't make the dst file partly checksummed */
  3658. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3659. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  3660. ret = -EINVAL;
  3661. goto out_unlock;
  3662. }
  3663. /* determine range to clone */
  3664. ret = -EINVAL;
  3665. if (off + len > src->i_size || off + len < off)
  3666. goto out_unlock;
  3667. if (len == 0)
  3668. olen = len = src->i_size - off;
  3669. /* if we extend to eof, continue to block boundary */
  3670. if (off + len == src->i_size)
  3671. len = ALIGN(src->i_size, bs) - off;
  3672. if (len == 0) {
  3673. ret = 0;
  3674. goto out_unlock;
  3675. }
  3676. /* verify the end result is block aligned */
  3677. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  3678. !IS_ALIGNED(destoff, bs))
  3679. goto out_unlock;
  3680. /* verify if ranges are overlapped within the same file */
  3681. if (same_inode) {
  3682. if (destoff + len > off && destoff < off + len)
  3683. goto out_unlock;
  3684. }
  3685. if (destoff > inode->i_size) {
  3686. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  3687. if (ret)
  3688. goto out_unlock;
  3689. }
  3690. /*
  3691. * Lock the target range too. Right after we replace the file extent
  3692. * items in the fs tree (which now point to the cloned data), we might
  3693. * have a worker replace them with extent items relative to a write
  3694. * operation that was issued before this clone operation (i.e. confront
  3695. * with inode.c:btrfs_finish_ordered_io).
  3696. */
  3697. if (same_inode) {
  3698. u64 lock_start = min_t(u64, off, destoff);
  3699. u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
  3700. ret = lock_extent_range(src, lock_start, lock_len, true);
  3701. } else {
  3702. ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
  3703. true);
  3704. }
  3705. ASSERT(ret == 0);
  3706. if (WARN_ON(ret)) {
  3707. /* ranges in the io trees already unlocked */
  3708. goto out_unlock;
  3709. }
  3710. ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
  3711. if (same_inode) {
  3712. u64 lock_start = min_t(u64, off, destoff);
  3713. u64 lock_end = max_t(u64, off, destoff) + len - 1;
  3714. unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
  3715. } else {
  3716. btrfs_double_extent_unlock(src, off, inode, destoff, len);
  3717. }
  3718. /*
  3719. * Truncate page cache pages so that future reads will see the cloned
  3720. * data immediately and not the previous data.
  3721. */
  3722. truncate_inode_pages_range(&inode->i_data,
  3723. round_down(destoff, PAGE_SIZE),
  3724. round_up(destoff + len, PAGE_SIZE) - 1);
  3725. out_unlock:
  3726. if (!same_inode)
  3727. btrfs_double_inode_unlock(src, inode);
  3728. else
  3729. inode_unlock(src);
  3730. return ret;
  3731. }
  3732. int btrfs_clone_file_range(struct file *src_file, loff_t off,
  3733. struct file *dst_file, loff_t destoff, u64 len)
  3734. {
  3735. return btrfs_clone_files(dst_file, src_file, off, len, destoff);
  3736. }
  3737. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  3738. {
  3739. struct inode *inode = file_inode(file);
  3740. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3741. struct btrfs_root *root = BTRFS_I(inode)->root;
  3742. struct btrfs_root *new_root;
  3743. struct btrfs_dir_item *di;
  3744. struct btrfs_trans_handle *trans;
  3745. struct btrfs_path *path;
  3746. struct btrfs_key location;
  3747. struct btrfs_disk_key disk_key;
  3748. u64 objectid = 0;
  3749. u64 dir_id;
  3750. int ret;
  3751. if (!capable(CAP_SYS_ADMIN))
  3752. return -EPERM;
  3753. ret = mnt_want_write_file(file);
  3754. if (ret)
  3755. return ret;
  3756. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  3757. ret = -EFAULT;
  3758. goto out;
  3759. }
  3760. if (!objectid)
  3761. objectid = BTRFS_FS_TREE_OBJECTID;
  3762. location.objectid = objectid;
  3763. location.type = BTRFS_ROOT_ITEM_KEY;
  3764. location.offset = (u64)-1;
  3765. new_root = btrfs_read_fs_root_no_name(fs_info, &location);
  3766. if (IS_ERR(new_root)) {
  3767. ret = PTR_ERR(new_root);
  3768. goto out;
  3769. }
  3770. if (!is_fstree(new_root->objectid)) {
  3771. ret = -ENOENT;
  3772. goto out;
  3773. }
  3774. path = btrfs_alloc_path();
  3775. if (!path) {
  3776. ret = -ENOMEM;
  3777. goto out;
  3778. }
  3779. path->leave_spinning = 1;
  3780. trans = btrfs_start_transaction(root, 1);
  3781. if (IS_ERR(trans)) {
  3782. btrfs_free_path(path);
  3783. ret = PTR_ERR(trans);
  3784. goto out;
  3785. }
  3786. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  3787. di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
  3788. dir_id, "default", 7, 1);
  3789. if (IS_ERR_OR_NULL(di)) {
  3790. btrfs_free_path(path);
  3791. btrfs_end_transaction(trans);
  3792. btrfs_err(fs_info,
  3793. "Umm, you don't have the default diritem, this isn't going to work");
  3794. ret = -ENOENT;
  3795. goto out;
  3796. }
  3797. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  3798. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  3799. btrfs_mark_buffer_dirty(path->nodes[0]);
  3800. btrfs_free_path(path);
  3801. btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
  3802. btrfs_end_transaction(trans);
  3803. out:
  3804. mnt_drop_write_file(file);
  3805. return ret;
  3806. }
  3807. static void get_block_group_info(struct list_head *groups_list,
  3808. struct btrfs_ioctl_space_info *space)
  3809. {
  3810. struct btrfs_block_group_cache *block_group;
  3811. space->total_bytes = 0;
  3812. space->used_bytes = 0;
  3813. space->flags = 0;
  3814. list_for_each_entry(block_group, groups_list, list) {
  3815. space->flags = block_group->flags;
  3816. space->total_bytes += block_group->key.offset;
  3817. space->used_bytes +=
  3818. btrfs_block_group_used(&block_group->item);
  3819. }
  3820. }
  3821. static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
  3822. void __user *arg)
  3823. {
  3824. struct btrfs_ioctl_space_args space_args;
  3825. struct btrfs_ioctl_space_info space;
  3826. struct btrfs_ioctl_space_info *dest;
  3827. struct btrfs_ioctl_space_info *dest_orig;
  3828. struct btrfs_ioctl_space_info __user *user_dest;
  3829. struct btrfs_space_info *info;
  3830. static const u64 types[] = {
  3831. BTRFS_BLOCK_GROUP_DATA,
  3832. BTRFS_BLOCK_GROUP_SYSTEM,
  3833. BTRFS_BLOCK_GROUP_METADATA,
  3834. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
  3835. };
  3836. int num_types = 4;
  3837. int alloc_size;
  3838. int ret = 0;
  3839. u64 slot_count = 0;
  3840. int i, c;
  3841. if (copy_from_user(&space_args,
  3842. (struct btrfs_ioctl_space_args __user *)arg,
  3843. sizeof(space_args)))
  3844. return -EFAULT;
  3845. for (i = 0; i < num_types; i++) {
  3846. struct btrfs_space_info *tmp;
  3847. info = NULL;
  3848. rcu_read_lock();
  3849. list_for_each_entry_rcu(tmp, &fs_info->space_info,
  3850. list) {
  3851. if (tmp->flags == types[i]) {
  3852. info = tmp;
  3853. break;
  3854. }
  3855. }
  3856. rcu_read_unlock();
  3857. if (!info)
  3858. continue;
  3859. down_read(&info->groups_sem);
  3860. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3861. if (!list_empty(&info->block_groups[c]))
  3862. slot_count++;
  3863. }
  3864. up_read(&info->groups_sem);
  3865. }
  3866. /*
  3867. * Global block reserve, exported as a space_info
  3868. */
  3869. slot_count++;
  3870. /* space_slots == 0 means they are asking for a count */
  3871. if (space_args.space_slots == 0) {
  3872. space_args.total_spaces = slot_count;
  3873. goto out;
  3874. }
  3875. slot_count = min_t(u64, space_args.space_slots, slot_count);
  3876. alloc_size = sizeof(*dest) * slot_count;
  3877. /* we generally have at most 6 or so space infos, one for each raid
  3878. * level. So, a whole page should be more than enough for everyone
  3879. */
  3880. if (alloc_size > PAGE_SIZE)
  3881. return -ENOMEM;
  3882. space_args.total_spaces = 0;
  3883. dest = kmalloc(alloc_size, GFP_KERNEL);
  3884. if (!dest)
  3885. return -ENOMEM;
  3886. dest_orig = dest;
  3887. /* now we have a buffer to copy into */
  3888. for (i = 0; i < num_types; i++) {
  3889. struct btrfs_space_info *tmp;
  3890. if (!slot_count)
  3891. break;
  3892. info = NULL;
  3893. rcu_read_lock();
  3894. list_for_each_entry_rcu(tmp, &fs_info->space_info,
  3895. list) {
  3896. if (tmp->flags == types[i]) {
  3897. info = tmp;
  3898. break;
  3899. }
  3900. }
  3901. rcu_read_unlock();
  3902. if (!info)
  3903. continue;
  3904. down_read(&info->groups_sem);
  3905. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3906. if (!list_empty(&info->block_groups[c])) {
  3907. get_block_group_info(&info->block_groups[c],
  3908. &space);
  3909. memcpy(dest, &space, sizeof(space));
  3910. dest++;
  3911. space_args.total_spaces++;
  3912. slot_count--;
  3913. }
  3914. if (!slot_count)
  3915. break;
  3916. }
  3917. up_read(&info->groups_sem);
  3918. }
  3919. /*
  3920. * Add global block reserve
  3921. */
  3922. if (slot_count) {
  3923. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  3924. spin_lock(&block_rsv->lock);
  3925. space.total_bytes = block_rsv->size;
  3926. space.used_bytes = block_rsv->size - block_rsv->reserved;
  3927. spin_unlock(&block_rsv->lock);
  3928. space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
  3929. memcpy(dest, &space, sizeof(space));
  3930. space_args.total_spaces++;
  3931. }
  3932. user_dest = (struct btrfs_ioctl_space_info __user *)
  3933. (arg + sizeof(struct btrfs_ioctl_space_args));
  3934. if (copy_to_user(user_dest, dest_orig, alloc_size))
  3935. ret = -EFAULT;
  3936. kfree(dest_orig);
  3937. out:
  3938. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  3939. ret = -EFAULT;
  3940. return ret;
  3941. }
  3942. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  3943. void __user *argp)
  3944. {
  3945. struct btrfs_trans_handle *trans;
  3946. u64 transid;
  3947. int ret;
  3948. trans = btrfs_attach_transaction_barrier(root);
  3949. if (IS_ERR(trans)) {
  3950. if (PTR_ERR(trans) != -ENOENT)
  3951. return PTR_ERR(trans);
  3952. /* No running transaction, don't bother */
  3953. transid = root->fs_info->last_trans_committed;
  3954. goto out;
  3955. }
  3956. transid = trans->transid;
  3957. ret = btrfs_commit_transaction_async(trans, 0);
  3958. if (ret) {
  3959. btrfs_end_transaction(trans);
  3960. return ret;
  3961. }
  3962. out:
  3963. if (argp)
  3964. if (copy_to_user(argp, &transid, sizeof(transid)))
  3965. return -EFAULT;
  3966. return 0;
  3967. }
  3968. static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
  3969. void __user *argp)
  3970. {
  3971. u64 transid;
  3972. if (argp) {
  3973. if (copy_from_user(&transid, argp, sizeof(transid)))
  3974. return -EFAULT;
  3975. } else {
  3976. transid = 0; /* current trans */
  3977. }
  3978. return btrfs_wait_for_commit(fs_info, transid);
  3979. }
  3980. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  3981. {
  3982. struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
  3983. struct btrfs_ioctl_scrub_args *sa;
  3984. int ret;
  3985. if (!capable(CAP_SYS_ADMIN))
  3986. return -EPERM;
  3987. sa = memdup_user(arg, sizeof(*sa));
  3988. if (IS_ERR(sa))
  3989. return PTR_ERR(sa);
  3990. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  3991. ret = mnt_want_write_file(file);
  3992. if (ret)
  3993. goto out;
  3994. }
  3995. ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
  3996. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  3997. 0);
  3998. if (copy_to_user(arg, sa, sizeof(*sa)))
  3999. ret = -EFAULT;
  4000. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  4001. mnt_drop_write_file(file);
  4002. out:
  4003. kfree(sa);
  4004. return ret;
  4005. }
  4006. static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
  4007. {
  4008. if (!capable(CAP_SYS_ADMIN))
  4009. return -EPERM;
  4010. return btrfs_scrub_cancel(fs_info);
  4011. }
  4012. static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
  4013. void __user *arg)
  4014. {
  4015. struct btrfs_ioctl_scrub_args *sa;
  4016. int ret;
  4017. if (!capable(CAP_SYS_ADMIN))
  4018. return -EPERM;
  4019. sa = memdup_user(arg, sizeof(*sa));
  4020. if (IS_ERR(sa))
  4021. return PTR_ERR(sa);
  4022. ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
  4023. if (copy_to_user(arg, sa, sizeof(*sa)))
  4024. ret = -EFAULT;
  4025. kfree(sa);
  4026. return ret;
  4027. }
  4028. static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
  4029. void __user *arg)
  4030. {
  4031. struct btrfs_ioctl_get_dev_stats *sa;
  4032. int ret;
  4033. sa = memdup_user(arg, sizeof(*sa));
  4034. if (IS_ERR(sa))
  4035. return PTR_ERR(sa);
  4036. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  4037. kfree(sa);
  4038. return -EPERM;
  4039. }
  4040. ret = btrfs_get_dev_stats(fs_info, sa);
  4041. if (copy_to_user(arg, sa, sizeof(*sa)))
  4042. ret = -EFAULT;
  4043. kfree(sa);
  4044. return ret;
  4045. }
  4046. static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
  4047. void __user *arg)
  4048. {
  4049. struct btrfs_ioctl_dev_replace_args *p;
  4050. int ret;
  4051. if (!capable(CAP_SYS_ADMIN))
  4052. return -EPERM;
  4053. p = memdup_user(arg, sizeof(*p));
  4054. if (IS_ERR(p))
  4055. return PTR_ERR(p);
  4056. switch (p->cmd) {
  4057. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  4058. if (sb_rdonly(fs_info->sb)) {
  4059. ret = -EROFS;
  4060. goto out;
  4061. }
  4062. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  4063. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  4064. } else {
  4065. ret = btrfs_dev_replace_by_ioctl(fs_info, p);
  4066. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  4067. }
  4068. break;
  4069. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  4070. btrfs_dev_replace_status(fs_info, p);
  4071. ret = 0;
  4072. break;
  4073. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  4074. p->result = btrfs_dev_replace_cancel(fs_info);
  4075. ret = 0;
  4076. break;
  4077. default:
  4078. ret = -EINVAL;
  4079. break;
  4080. }
  4081. if (copy_to_user(arg, p, sizeof(*p)))
  4082. ret = -EFAULT;
  4083. out:
  4084. kfree(p);
  4085. return ret;
  4086. }
  4087. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  4088. {
  4089. int ret = 0;
  4090. int i;
  4091. u64 rel_ptr;
  4092. int size;
  4093. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  4094. struct inode_fs_paths *ipath = NULL;
  4095. struct btrfs_path *path;
  4096. if (!capable(CAP_DAC_READ_SEARCH))
  4097. return -EPERM;
  4098. path = btrfs_alloc_path();
  4099. if (!path) {
  4100. ret = -ENOMEM;
  4101. goto out;
  4102. }
  4103. ipa = memdup_user(arg, sizeof(*ipa));
  4104. if (IS_ERR(ipa)) {
  4105. ret = PTR_ERR(ipa);
  4106. ipa = NULL;
  4107. goto out;
  4108. }
  4109. size = min_t(u32, ipa->size, 4096);
  4110. ipath = init_ipath(size, root, path);
  4111. if (IS_ERR(ipath)) {
  4112. ret = PTR_ERR(ipath);
  4113. ipath = NULL;
  4114. goto out;
  4115. }
  4116. ret = paths_from_inode(ipa->inum, ipath);
  4117. if (ret < 0)
  4118. goto out;
  4119. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  4120. rel_ptr = ipath->fspath->val[i] -
  4121. (u64)(unsigned long)ipath->fspath->val;
  4122. ipath->fspath->val[i] = rel_ptr;
  4123. }
  4124. ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
  4125. ipath->fspath, size);
  4126. if (ret) {
  4127. ret = -EFAULT;
  4128. goto out;
  4129. }
  4130. out:
  4131. btrfs_free_path(path);
  4132. free_ipath(ipath);
  4133. kfree(ipa);
  4134. return ret;
  4135. }
  4136. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  4137. {
  4138. struct btrfs_data_container *inodes = ctx;
  4139. const size_t c = 3 * sizeof(u64);
  4140. if (inodes->bytes_left >= c) {
  4141. inodes->bytes_left -= c;
  4142. inodes->val[inodes->elem_cnt] = inum;
  4143. inodes->val[inodes->elem_cnt + 1] = offset;
  4144. inodes->val[inodes->elem_cnt + 2] = root;
  4145. inodes->elem_cnt += 3;
  4146. } else {
  4147. inodes->bytes_missing += c - inodes->bytes_left;
  4148. inodes->bytes_left = 0;
  4149. inodes->elem_missed += 3;
  4150. }
  4151. return 0;
  4152. }
  4153. static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
  4154. void __user *arg, int version)
  4155. {
  4156. int ret = 0;
  4157. int size;
  4158. struct btrfs_ioctl_logical_ino_args *loi;
  4159. struct btrfs_data_container *inodes = NULL;
  4160. struct btrfs_path *path = NULL;
  4161. bool ignore_offset;
  4162. if (!capable(CAP_SYS_ADMIN))
  4163. return -EPERM;
  4164. loi = memdup_user(arg, sizeof(*loi));
  4165. if (IS_ERR(loi))
  4166. return PTR_ERR(loi);
  4167. if (version == 1) {
  4168. ignore_offset = false;
  4169. size = min_t(u32, loi->size, SZ_64K);
  4170. } else {
  4171. /* All reserved bits must be 0 for now */
  4172. if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
  4173. ret = -EINVAL;
  4174. goto out_loi;
  4175. }
  4176. /* Only accept flags we have defined so far */
  4177. if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
  4178. ret = -EINVAL;
  4179. goto out_loi;
  4180. }
  4181. ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
  4182. size = min_t(u32, loi->size, SZ_16M);
  4183. }
  4184. path = btrfs_alloc_path();
  4185. if (!path) {
  4186. ret = -ENOMEM;
  4187. goto out;
  4188. }
  4189. inodes = init_data_container(size);
  4190. if (IS_ERR(inodes)) {
  4191. ret = PTR_ERR(inodes);
  4192. inodes = NULL;
  4193. goto out;
  4194. }
  4195. ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
  4196. build_ino_list, inodes, ignore_offset);
  4197. if (ret == -EINVAL)
  4198. ret = -ENOENT;
  4199. if (ret < 0)
  4200. goto out;
  4201. ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
  4202. size);
  4203. if (ret)
  4204. ret = -EFAULT;
  4205. out:
  4206. btrfs_free_path(path);
  4207. kvfree(inodes);
  4208. out_loi:
  4209. kfree(loi);
  4210. return ret;
  4211. }
  4212. void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  4213. struct btrfs_ioctl_balance_args *bargs)
  4214. {
  4215. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  4216. bargs->flags = bctl->flags;
  4217. if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
  4218. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  4219. if (atomic_read(&fs_info->balance_pause_req))
  4220. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  4221. if (atomic_read(&fs_info->balance_cancel_req))
  4222. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  4223. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  4224. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  4225. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  4226. spin_lock(&fs_info->balance_lock);
  4227. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  4228. spin_unlock(&fs_info->balance_lock);
  4229. }
  4230. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  4231. {
  4232. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4233. struct btrfs_fs_info *fs_info = root->fs_info;
  4234. struct btrfs_ioctl_balance_args *bargs;
  4235. struct btrfs_balance_control *bctl;
  4236. bool need_unlock; /* for mut. excl. ops lock */
  4237. int ret;
  4238. if (!capable(CAP_SYS_ADMIN))
  4239. return -EPERM;
  4240. ret = mnt_want_write_file(file);
  4241. if (ret)
  4242. return ret;
  4243. again:
  4244. if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  4245. mutex_lock(&fs_info->balance_mutex);
  4246. need_unlock = true;
  4247. goto locked;
  4248. }
  4249. /*
  4250. * mut. excl. ops lock is locked. Three possibilities:
  4251. * (1) some other op is running
  4252. * (2) balance is running
  4253. * (3) balance is paused -- special case (think resume)
  4254. */
  4255. mutex_lock(&fs_info->balance_mutex);
  4256. if (fs_info->balance_ctl) {
  4257. /* this is either (2) or (3) */
  4258. if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  4259. mutex_unlock(&fs_info->balance_mutex);
  4260. /*
  4261. * Lock released to allow other waiters to continue,
  4262. * we'll reexamine the status again.
  4263. */
  4264. mutex_lock(&fs_info->balance_mutex);
  4265. if (fs_info->balance_ctl &&
  4266. !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  4267. /* this is (3) */
  4268. need_unlock = false;
  4269. goto locked;
  4270. }
  4271. mutex_unlock(&fs_info->balance_mutex);
  4272. goto again;
  4273. } else {
  4274. /* this is (2) */
  4275. mutex_unlock(&fs_info->balance_mutex);
  4276. ret = -EINPROGRESS;
  4277. goto out;
  4278. }
  4279. } else {
  4280. /* this is (1) */
  4281. mutex_unlock(&fs_info->balance_mutex);
  4282. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  4283. goto out;
  4284. }
  4285. locked:
  4286. BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
  4287. if (arg) {
  4288. bargs = memdup_user(arg, sizeof(*bargs));
  4289. if (IS_ERR(bargs)) {
  4290. ret = PTR_ERR(bargs);
  4291. goto out_unlock;
  4292. }
  4293. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  4294. if (!fs_info->balance_ctl) {
  4295. ret = -ENOTCONN;
  4296. goto out_bargs;
  4297. }
  4298. bctl = fs_info->balance_ctl;
  4299. spin_lock(&fs_info->balance_lock);
  4300. bctl->flags |= BTRFS_BALANCE_RESUME;
  4301. spin_unlock(&fs_info->balance_lock);
  4302. goto do_balance;
  4303. }
  4304. } else {
  4305. bargs = NULL;
  4306. }
  4307. if (fs_info->balance_ctl) {
  4308. ret = -EINPROGRESS;
  4309. goto out_bargs;
  4310. }
  4311. bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
  4312. if (!bctl) {
  4313. ret = -ENOMEM;
  4314. goto out_bargs;
  4315. }
  4316. if (arg) {
  4317. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  4318. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  4319. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  4320. bctl->flags = bargs->flags;
  4321. } else {
  4322. /* balance everything - no filters */
  4323. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  4324. }
  4325. if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
  4326. ret = -EINVAL;
  4327. goto out_bctl;
  4328. }
  4329. do_balance:
  4330. /*
  4331. * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
  4332. * btrfs_balance. bctl is freed in reset_balance_state, or, if
  4333. * restriper was paused all the way until unmount, in free_fs_info.
  4334. * The flag should be cleared after reset_balance_state.
  4335. */
  4336. need_unlock = false;
  4337. ret = btrfs_balance(fs_info, bctl, bargs);
  4338. bctl = NULL;
  4339. if (arg) {
  4340. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  4341. ret = -EFAULT;
  4342. }
  4343. out_bctl:
  4344. kfree(bctl);
  4345. out_bargs:
  4346. kfree(bargs);
  4347. out_unlock:
  4348. mutex_unlock(&fs_info->balance_mutex);
  4349. if (need_unlock)
  4350. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  4351. out:
  4352. mnt_drop_write_file(file);
  4353. return ret;
  4354. }
  4355. static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
  4356. {
  4357. if (!capable(CAP_SYS_ADMIN))
  4358. return -EPERM;
  4359. switch (cmd) {
  4360. case BTRFS_BALANCE_CTL_PAUSE:
  4361. return btrfs_pause_balance(fs_info);
  4362. case BTRFS_BALANCE_CTL_CANCEL:
  4363. return btrfs_cancel_balance(fs_info);
  4364. }
  4365. return -EINVAL;
  4366. }
  4367. static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
  4368. void __user *arg)
  4369. {
  4370. struct btrfs_ioctl_balance_args *bargs;
  4371. int ret = 0;
  4372. if (!capable(CAP_SYS_ADMIN))
  4373. return -EPERM;
  4374. mutex_lock(&fs_info->balance_mutex);
  4375. if (!fs_info->balance_ctl) {
  4376. ret = -ENOTCONN;
  4377. goto out;
  4378. }
  4379. bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
  4380. if (!bargs) {
  4381. ret = -ENOMEM;
  4382. goto out;
  4383. }
  4384. btrfs_update_ioctl_balance_args(fs_info, bargs);
  4385. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  4386. ret = -EFAULT;
  4387. kfree(bargs);
  4388. out:
  4389. mutex_unlock(&fs_info->balance_mutex);
  4390. return ret;
  4391. }
  4392. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  4393. {
  4394. struct inode *inode = file_inode(file);
  4395. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4396. struct btrfs_ioctl_quota_ctl_args *sa;
  4397. struct btrfs_trans_handle *trans = NULL;
  4398. int ret;
  4399. int err;
  4400. if (!capable(CAP_SYS_ADMIN))
  4401. return -EPERM;
  4402. ret = mnt_want_write_file(file);
  4403. if (ret)
  4404. return ret;
  4405. sa = memdup_user(arg, sizeof(*sa));
  4406. if (IS_ERR(sa)) {
  4407. ret = PTR_ERR(sa);
  4408. goto drop_write;
  4409. }
  4410. down_write(&fs_info->subvol_sem);
  4411. trans = btrfs_start_transaction(fs_info->tree_root, 2);
  4412. if (IS_ERR(trans)) {
  4413. ret = PTR_ERR(trans);
  4414. goto out;
  4415. }
  4416. switch (sa->cmd) {
  4417. case BTRFS_QUOTA_CTL_ENABLE:
  4418. ret = btrfs_quota_enable(trans, fs_info);
  4419. break;
  4420. case BTRFS_QUOTA_CTL_DISABLE:
  4421. ret = btrfs_quota_disable(trans, fs_info);
  4422. break;
  4423. default:
  4424. ret = -EINVAL;
  4425. break;
  4426. }
  4427. err = btrfs_commit_transaction(trans);
  4428. if (err && !ret)
  4429. ret = err;
  4430. out:
  4431. kfree(sa);
  4432. up_write(&fs_info->subvol_sem);
  4433. drop_write:
  4434. mnt_drop_write_file(file);
  4435. return ret;
  4436. }
  4437. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  4438. {
  4439. struct inode *inode = file_inode(file);
  4440. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4441. struct btrfs_root *root = BTRFS_I(inode)->root;
  4442. struct btrfs_ioctl_qgroup_assign_args *sa;
  4443. struct btrfs_trans_handle *trans;
  4444. int ret;
  4445. int err;
  4446. if (!capable(CAP_SYS_ADMIN))
  4447. return -EPERM;
  4448. ret = mnt_want_write_file(file);
  4449. if (ret)
  4450. return ret;
  4451. sa = memdup_user(arg, sizeof(*sa));
  4452. if (IS_ERR(sa)) {
  4453. ret = PTR_ERR(sa);
  4454. goto drop_write;
  4455. }
  4456. trans = btrfs_join_transaction(root);
  4457. if (IS_ERR(trans)) {
  4458. ret = PTR_ERR(trans);
  4459. goto out;
  4460. }
  4461. if (sa->assign) {
  4462. ret = btrfs_add_qgroup_relation(trans, fs_info,
  4463. sa->src, sa->dst);
  4464. } else {
  4465. ret = btrfs_del_qgroup_relation(trans, fs_info,
  4466. sa->src, sa->dst);
  4467. }
  4468. /* update qgroup status and info */
  4469. err = btrfs_run_qgroups(trans, fs_info);
  4470. if (err < 0)
  4471. btrfs_handle_fs_error(fs_info, err,
  4472. "failed to update qgroup status and info");
  4473. err = btrfs_end_transaction(trans);
  4474. if (err && !ret)
  4475. ret = err;
  4476. out:
  4477. kfree(sa);
  4478. drop_write:
  4479. mnt_drop_write_file(file);
  4480. return ret;
  4481. }
  4482. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  4483. {
  4484. struct inode *inode = file_inode(file);
  4485. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4486. struct btrfs_root *root = BTRFS_I(inode)->root;
  4487. struct btrfs_ioctl_qgroup_create_args *sa;
  4488. struct btrfs_trans_handle *trans;
  4489. int ret;
  4490. int err;
  4491. if (!capable(CAP_SYS_ADMIN))
  4492. return -EPERM;
  4493. ret = mnt_want_write_file(file);
  4494. if (ret)
  4495. return ret;
  4496. sa = memdup_user(arg, sizeof(*sa));
  4497. if (IS_ERR(sa)) {
  4498. ret = PTR_ERR(sa);
  4499. goto drop_write;
  4500. }
  4501. if (!sa->qgroupid) {
  4502. ret = -EINVAL;
  4503. goto out;
  4504. }
  4505. trans = btrfs_join_transaction(root);
  4506. if (IS_ERR(trans)) {
  4507. ret = PTR_ERR(trans);
  4508. goto out;
  4509. }
  4510. if (sa->create) {
  4511. ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
  4512. } else {
  4513. ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
  4514. }
  4515. err = btrfs_end_transaction(trans);
  4516. if (err && !ret)
  4517. ret = err;
  4518. out:
  4519. kfree(sa);
  4520. drop_write:
  4521. mnt_drop_write_file(file);
  4522. return ret;
  4523. }
  4524. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  4525. {
  4526. struct inode *inode = file_inode(file);
  4527. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4528. struct btrfs_root *root = BTRFS_I(inode)->root;
  4529. struct btrfs_ioctl_qgroup_limit_args *sa;
  4530. struct btrfs_trans_handle *trans;
  4531. int ret;
  4532. int err;
  4533. u64 qgroupid;
  4534. if (!capable(CAP_SYS_ADMIN))
  4535. return -EPERM;
  4536. ret = mnt_want_write_file(file);
  4537. if (ret)
  4538. return ret;
  4539. sa = memdup_user(arg, sizeof(*sa));
  4540. if (IS_ERR(sa)) {
  4541. ret = PTR_ERR(sa);
  4542. goto drop_write;
  4543. }
  4544. trans = btrfs_join_transaction(root);
  4545. if (IS_ERR(trans)) {
  4546. ret = PTR_ERR(trans);
  4547. goto out;
  4548. }
  4549. qgroupid = sa->qgroupid;
  4550. if (!qgroupid) {
  4551. /* take the current subvol as qgroup */
  4552. qgroupid = root->root_key.objectid;
  4553. }
  4554. ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
  4555. err = btrfs_end_transaction(trans);
  4556. if (err && !ret)
  4557. ret = err;
  4558. out:
  4559. kfree(sa);
  4560. drop_write:
  4561. mnt_drop_write_file(file);
  4562. return ret;
  4563. }
  4564. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  4565. {
  4566. struct inode *inode = file_inode(file);
  4567. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4568. struct btrfs_ioctl_quota_rescan_args *qsa;
  4569. int ret;
  4570. if (!capable(CAP_SYS_ADMIN))
  4571. return -EPERM;
  4572. ret = mnt_want_write_file(file);
  4573. if (ret)
  4574. return ret;
  4575. qsa = memdup_user(arg, sizeof(*qsa));
  4576. if (IS_ERR(qsa)) {
  4577. ret = PTR_ERR(qsa);
  4578. goto drop_write;
  4579. }
  4580. if (qsa->flags) {
  4581. ret = -EINVAL;
  4582. goto out;
  4583. }
  4584. ret = btrfs_qgroup_rescan(fs_info);
  4585. out:
  4586. kfree(qsa);
  4587. drop_write:
  4588. mnt_drop_write_file(file);
  4589. return ret;
  4590. }
  4591. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  4592. {
  4593. struct inode *inode = file_inode(file);
  4594. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4595. struct btrfs_ioctl_quota_rescan_args *qsa;
  4596. int ret = 0;
  4597. if (!capable(CAP_SYS_ADMIN))
  4598. return -EPERM;
  4599. qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
  4600. if (!qsa)
  4601. return -ENOMEM;
  4602. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  4603. qsa->flags = 1;
  4604. qsa->progress = fs_info->qgroup_rescan_progress.objectid;
  4605. }
  4606. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  4607. ret = -EFAULT;
  4608. kfree(qsa);
  4609. return ret;
  4610. }
  4611. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  4612. {
  4613. struct inode *inode = file_inode(file);
  4614. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4615. if (!capable(CAP_SYS_ADMIN))
  4616. return -EPERM;
  4617. return btrfs_qgroup_wait_for_completion(fs_info, true);
  4618. }
  4619. static long _btrfs_ioctl_set_received_subvol(struct file *file,
  4620. struct btrfs_ioctl_received_subvol_args *sa)
  4621. {
  4622. struct inode *inode = file_inode(file);
  4623. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4624. struct btrfs_root *root = BTRFS_I(inode)->root;
  4625. struct btrfs_root_item *root_item = &root->root_item;
  4626. struct btrfs_trans_handle *trans;
  4627. struct timespec64 ct = current_time(inode);
  4628. int ret = 0;
  4629. int received_uuid_changed;
  4630. if (!inode_owner_or_capable(inode))
  4631. return -EPERM;
  4632. ret = mnt_want_write_file(file);
  4633. if (ret < 0)
  4634. return ret;
  4635. down_write(&fs_info->subvol_sem);
  4636. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  4637. ret = -EINVAL;
  4638. goto out;
  4639. }
  4640. if (btrfs_root_readonly(root)) {
  4641. ret = -EROFS;
  4642. goto out;
  4643. }
  4644. /*
  4645. * 1 - root item
  4646. * 2 - uuid items (received uuid + subvol uuid)
  4647. */
  4648. trans = btrfs_start_transaction(root, 3);
  4649. if (IS_ERR(trans)) {
  4650. ret = PTR_ERR(trans);
  4651. trans = NULL;
  4652. goto out;
  4653. }
  4654. sa->rtransid = trans->transid;
  4655. sa->rtime.sec = ct.tv_sec;
  4656. sa->rtime.nsec = ct.tv_nsec;
  4657. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  4658. BTRFS_UUID_SIZE);
  4659. if (received_uuid_changed &&
  4660. !btrfs_is_empty_uuid(root_item->received_uuid)) {
  4661. ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
  4662. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4663. root->root_key.objectid);
  4664. if (ret && ret != -ENOENT) {
  4665. btrfs_abort_transaction(trans, ret);
  4666. btrfs_end_transaction(trans);
  4667. goto out;
  4668. }
  4669. }
  4670. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  4671. btrfs_set_root_stransid(root_item, sa->stransid);
  4672. btrfs_set_root_rtransid(root_item, sa->rtransid);
  4673. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  4674. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  4675. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  4676. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  4677. ret = btrfs_update_root(trans, fs_info->tree_root,
  4678. &root->root_key, &root->root_item);
  4679. if (ret < 0) {
  4680. btrfs_end_transaction(trans);
  4681. goto out;
  4682. }
  4683. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  4684. ret = btrfs_uuid_tree_add(trans, sa->uuid,
  4685. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4686. root->root_key.objectid);
  4687. if (ret < 0 && ret != -EEXIST) {
  4688. btrfs_abort_transaction(trans, ret);
  4689. btrfs_end_transaction(trans);
  4690. goto out;
  4691. }
  4692. }
  4693. ret = btrfs_commit_transaction(trans);
  4694. out:
  4695. up_write(&fs_info->subvol_sem);
  4696. mnt_drop_write_file(file);
  4697. return ret;
  4698. }
  4699. #ifdef CONFIG_64BIT
  4700. static long btrfs_ioctl_set_received_subvol_32(struct file *file,
  4701. void __user *arg)
  4702. {
  4703. struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
  4704. struct btrfs_ioctl_received_subvol_args *args64 = NULL;
  4705. int ret = 0;
  4706. args32 = memdup_user(arg, sizeof(*args32));
  4707. if (IS_ERR(args32))
  4708. return PTR_ERR(args32);
  4709. args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
  4710. if (!args64) {
  4711. ret = -ENOMEM;
  4712. goto out;
  4713. }
  4714. memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
  4715. args64->stransid = args32->stransid;
  4716. args64->rtransid = args32->rtransid;
  4717. args64->stime.sec = args32->stime.sec;
  4718. args64->stime.nsec = args32->stime.nsec;
  4719. args64->rtime.sec = args32->rtime.sec;
  4720. args64->rtime.nsec = args32->rtime.nsec;
  4721. args64->flags = args32->flags;
  4722. ret = _btrfs_ioctl_set_received_subvol(file, args64);
  4723. if (ret)
  4724. goto out;
  4725. memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
  4726. args32->stransid = args64->stransid;
  4727. args32->rtransid = args64->rtransid;
  4728. args32->stime.sec = args64->stime.sec;
  4729. args32->stime.nsec = args64->stime.nsec;
  4730. args32->rtime.sec = args64->rtime.sec;
  4731. args32->rtime.nsec = args64->rtime.nsec;
  4732. args32->flags = args64->flags;
  4733. ret = copy_to_user(arg, args32, sizeof(*args32));
  4734. if (ret)
  4735. ret = -EFAULT;
  4736. out:
  4737. kfree(args32);
  4738. kfree(args64);
  4739. return ret;
  4740. }
  4741. #endif
  4742. static long btrfs_ioctl_set_received_subvol(struct file *file,
  4743. void __user *arg)
  4744. {
  4745. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  4746. int ret = 0;
  4747. sa = memdup_user(arg, sizeof(*sa));
  4748. if (IS_ERR(sa))
  4749. return PTR_ERR(sa);
  4750. ret = _btrfs_ioctl_set_received_subvol(file, sa);
  4751. if (ret)
  4752. goto out;
  4753. ret = copy_to_user(arg, sa, sizeof(*sa));
  4754. if (ret)
  4755. ret = -EFAULT;
  4756. out:
  4757. kfree(sa);
  4758. return ret;
  4759. }
  4760. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  4761. {
  4762. struct inode *inode = file_inode(file);
  4763. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4764. size_t len;
  4765. int ret;
  4766. char label[BTRFS_LABEL_SIZE];
  4767. spin_lock(&fs_info->super_lock);
  4768. memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  4769. spin_unlock(&fs_info->super_lock);
  4770. len = strnlen(label, BTRFS_LABEL_SIZE);
  4771. if (len == BTRFS_LABEL_SIZE) {
  4772. btrfs_warn(fs_info,
  4773. "label is too long, return the first %zu bytes",
  4774. --len);
  4775. }
  4776. ret = copy_to_user(arg, label, len);
  4777. return ret ? -EFAULT : 0;
  4778. }
  4779. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  4780. {
  4781. struct inode *inode = file_inode(file);
  4782. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4783. struct btrfs_root *root = BTRFS_I(inode)->root;
  4784. struct btrfs_super_block *super_block = fs_info->super_copy;
  4785. struct btrfs_trans_handle *trans;
  4786. char label[BTRFS_LABEL_SIZE];
  4787. int ret;
  4788. if (!capable(CAP_SYS_ADMIN))
  4789. return -EPERM;
  4790. if (copy_from_user(label, arg, sizeof(label)))
  4791. return -EFAULT;
  4792. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  4793. btrfs_err(fs_info,
  4794. "unable to set label with more than %d bytes",
  4795. BTRFS_LABEL_SIZE - 1);
  4796. return -EINVAL;
  4797. }
  4798. ret = mnt_want_write_file(file);
  4799. if (ret)
  4800. return ret;
  4801. trans = btrfs_start_transaction(root, 0);
  4802. if (IS_ERR(trans)) {
  4803. ret = PTR_ERR(trans);
  4804. goto out_unlock;
  4805. }
  4806. spin_lock(&fs_info->super_lock);
  4807. strcpy(super_block->label, label);
  4808. spin_unlock(&fs_info->super_lock);
  4809. ret = btrfs_commit_transaction(trans);
  4810. out_unlock:
  4811. mnt_drop_write_file(file);
  4812. return ret;
  4813. }
  4814. #define INIT_FEATURE_FLAGS(suffix) \
  4815. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  4816. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  4817. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  4818. int btrfs_ioctl_get_supported_features(void __user *arg)
  4819. {
  4820. static const struct btrfs_ioctl_feature_flags features[3] = {
  4821. INIT_FEATURE_FLAGS(SUPP),
  4822. INIT_FEATURE_FLAGS(SAFE_SET),
  4823. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  4824. };
  4825. if (copy_to_user(arg, &features, sizeof(features)))
  4826. return -EFAULT;
  4827. return 0;
  4828. }
  4829. static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
  4830. {
  4831. struct inode *inode = file_inode(file);
  4832. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4833. struct btrfs_super_block *super_block = fs_info->super_copy;
  4834. struct btrfs_ioctl_feature_flags features;
  4835. features.compat_flags = btrfs_super_compat_flags(super_block);
  4836. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  4837. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  4838. if (copy_to_user(arg, &features, sizeof(features)))
  4839. return -EFAULT;
  4840. return 0;
  4841. }
  4842. static int check_feature_bits(struct btrfs_fs_info *fs_info,
  4843. enum btrfs_feature_set set,
  4844. u64 change_mask, u64 flags, u64 supported_flags,
  4845. u64 safe_set, u64 safe_clear)
  4846. {
  4847. const char *type = btrfs_feature_set_names[set];
  4848. char *names;
  4849. u64 disallowed, unsupported;
  4850. u64 set_mask = flags & change_mask;
  4851. u64 clear_mask = ~flags & change_mask;
  4852. unsupported = set_mask & ~supported_flags;
  4853. if (unsupported) {
  4854. names = btrfs_printable_features(set, unsupported);
  4855. if (names) {
  4856. btrfs_warn(fs_info,
  4857. "this kernel does not support the %s feature bit%s",
  4858. names, strchr(names, ',') ? "s" : "");
  4859. kfree(names);
  4860. } else
  4861. btrfs_warn(fs_info,
  4862. "this kernel does not support %s bits 0x%llx",
  4863. type, unsupported);
  4864. return -EOPNOTSUPP;
  4865. }
  4866. disallowed = set_mask & ~safe_set;
  4867. if (disallowed) {
  4868. names = btrfs_printable_features(set, disallowed);
  4869. if (names) {
  4870. btrfs_warn(fs_info,
  4871. "can't set the %s feature bit%s while mounted",
  4872. names, strchr(names, ',') ? "s" : "");
  4873. kfree(names);
  4874. } else
  4875. btrfs_warn(fs_info,
  4876. "can't set %s bits 0x%llx while mounted",
  4877. type, disallowed);
  4878. return -EPERM;
  4879. }
  4880. disallowed = clear_mask & ~safe_clear;
  4881. if (disallowed) {
  4882. names = btrfs_printable_features(set, disallowed);
  4883. if (names) {
  4884. btrfs_warn(fs_info,
  4885. "can't clear the %s feature bit%s while mounted",
  4886. names, strchr(names, ',') ? "s" : "");
  4887. kfree(names);
  4888. } else
  4889. btrfs_warn(fs_info,
  4890. "can't clear %s bits 0x%llx while mounted",
  4891. type, disallowed);
  4892. return -EPERM;
  4893. }
  4894. return 0;
  4895. }
  4896. #define check_feature(fs_info, change_mask, flags, mask_base) \
  4897. check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
  4898. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  4899. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  4900. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  4901. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  4902. {
  4903. struct inode *inode = file_inode(file);
  4904. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4905. struct btrfs_root *root = BTRFS_I(inode)->root;
  4906. struct btrfs_super_block *super_block = fs_info->super_copy;
  4907. struct btrfs_ioctl_feature_flags flags[2];
  4908. struct btrfs_trans_handle *trans;
  4909. u64 newflags;
  4910. int ret;
  4911. if (!capable(CAP_SYS_ADMIN))
  4912. return -EPERM;
  4913. if (copy_from_user(flags, arg, sizeof(flags)))
  4914. return -EFAULT;
  4915. /* Nothing to do */
  4916. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  4917. !flags[0].incompat_flags)
  4918. return 0;
  4919. ret = check_feature(fs_info, flags[0].compat_flags,
  4920. flags[1].compat_flags, COMPAT);
  4921. if (ret)
  4922. return ret;
  4923. ret = check_feature(fs_info, flags[0].compat_ro_flags,
  4924. flags[1].compat_ro_flags, COMPAT_RO);
  4925. if (ret)
  4926. return ret;
  4927. ret = check_feature(fs_info, flags[0].incompat_flags,
  4928. flags[1].incompat_flags, INCOMPAT);
  4929. if (ret)
  4930. return ret;
  4931. ret = mnt_want_write_file(file);
  4932. if (ret)
  4933. return ret;
  4934. trans = btrfs_start_transaction(root, 0);
  4935. if (IS_ERR(trans)) {
  4936. ret = PTR_ERR(trans);
  4937. goto out_drop_write;
  4938. }
  4939. spin_lock(&fs_info->super_lock);
  4940. newflags = btrfs_super_compat_flags(super_block);
  4941. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  4942. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  4943. btrfs_set_super_compat_flags(super_block, newflags);
  4944. newflags = btrfs_super_compat_ro_flags(super_block);
  4945. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  4946. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  4947. btrfs_set_super_compat_ro_flags(super_block, newflags);
  4948. newflags = btrfs_super_incompat_flags(super_block);
  4949. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  4950. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  4951. btrfs_set_super_incompat_flags(super_block, newflags);
  4952. spin_unlock(&fs_info->super_lock);
  4953. ret = btrfs_commit_transaction(trans);
  4954. out_drop_write:
  4955. mnt_drop_write_file(file);
  4956. return ret;
  4957. }
  4958. static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
  4959. {
  4960. struct btrfs_ioctl_send_args *arg;
  4961. int ret;
  4962. if (compat) {
  4963. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4964. struct btrfs_ioctl_send_args_32 args32;
  4965. ret = copy_from_user(&args32, argp, sizeof(args32));
  4966. if (ret)
  4967. return -EFAULT;
  4968. arg = kzalloc(sizeof(*arg), GFP_KERNEL);
  4969. if (!arg)
  4970. return -ENOMEM;
  4971. arg->send_fd = args32.send_fd;
  4972. arg->clone_sources_count = args32.clone_sources_count;
  4973. arg->clone_sources = compat_ptr(args32.clone_sources);
  4974. arg->parent_root = args32.parent_root;
  4975. arg->flags = args32.flags;
  4976. memcpy(arg->reserved, args32.reserved,
  4977. sizeof(args32.reserved));
  4978. #else
  4979. return -ENOTTY;
  4980. #endif
  4981. } else {
  4982. arg = memdup_user(argp, sizeof(*arg));
  4983. if (IS_ERR(arg))
  4984. return PTR_ERR(arg);
  4985. }
  4986. ret = btrfs_ioctl_send(file, arg);
  4987. kfree(arg);
  4988. return ret;
  4989. }
  4990. long btrfs_ioctl(struct file *file, unsigned int
  4991. cmd, unsigned long arg)
  4992. {
  4993. struct inode *inode = file_inode(file);
  4994. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4995. struct btrfs_root *root = BTRFS_I(inode)->root;
  4996. void __user *argp = (void __user *)arg;
  4997. switch (cmd) {
  4998. case FS_IOC_GETFLAGS:
  4999. return btrfs_ioctl_getflags(file, argp);
  5000. case FS_IOC_SETFLAGS:
  5001. return btrfs_ioctl_setflags(file, argp);
  5002. case FS_IOC_GETVERSION:
  5003. return btrfs_ioctl_getversion(file, argp);
  5004. case FITRIM:
  5005. return btrfs_ioctl_fitrim(file, argp);
  5006. case BTRFS_IOC_SNAP_CREATE:
  5007. return btrfs_ioctl_snap_create(file, argp, 0);
  5008. case BTRFS_IOC_SNAP_CREATE_V2:
  5009. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  5010. case BTRFS_IOC_SUBVOL_CREATE:
  5011. return btrfs_ioctl_snap_create(file, argp, 1);
  5012. case BTRFS_IOC_SUBVOL_CREATE_V2:
  5013. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  5014. case BTRFS_IOC_SNAP_DESTROY:
  5015. return btrfs_ioctl_snap_destroy(file, argp);
  5016. case BTRFS_IOC_SUBVOL_GETFLAGS:
  5017. return btrfs_ioctl_subvol_getflags(file, argp);
  5018. case BTRFS_IOC_SUBVOL_SETFLAGS:
  5019. return btrfs_ioctl_subvol_setflags(file, argp);
  5020. case BTRFS_IOC_DEFAULT_SUBVOL:
  5021. return btrfs_ioctl_default_subvol(file, argp);
  5022. case BTRFS_IOC_DEFRAG:
  5023. return btrfs_ioctl_defrag(file, NULL);
  5024. case BTRFS_IOC_DEFRAG_RANGE:
  5025. return btrfs_ioctl_defrag(file, argp);
  5026. case BTRFS_IOC_RESIZE:
  5027. return btrfs_ioctl_resize(file, argp);
  5028. case BTRFS_IOC_ADD_DEV:
  5029. return btrfs_ioctl_add_dev(fs_info, argp);
  5030. case BTRFS_IOC_RM_DEV:
  5031. return btrfs_ioctl_rm_dev(file, argp);
  5032. case BTRFS_IOC_RM_DEV_V2:
  5033. return btrfs_ioctl_rm_dev_v2(file, argp);
  5034. case BTRFS_IOC_FS_INFO:
  5035. return btrfs_ioctl_fs_info(fs_info, argp);
  5036. case BTRFS_IOC_DEV_INFO:
  5037. return btrfs_ioctl_dev_info(fs_info, argp);
  5038. case BTRFS_IOC_BALANCE:
  5039. return btrfs_ioctl_balance(file, NULL);
  5040. case BTRFS_IOC_TREE_SEARCH:
  5041. return btrfs_ioctl_tree_search(file, argp);
  5042. case BTRFS_IOC_TREE_SEARCH_V2:
  5043. return btrfs_ioctl_tree_search_v2(file, argp);
  5044. case BTRFS_IOC_INO_LOOKUP:
  5045. return btrfs_ioctl_ino_lookup(file, argp);
  5046. case BTRFS_IOC_INO_PATHS:
  5047. return btrfs_ioctl_ino_to_path(root, argp);
  5048. case BTRFS_IOC_LOGICAL_INO:
  5049. return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
  5050. case BTRFS_IOC_LOGICAL_INO_V2:
  5051. return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
  5052. case BTRFS_IOC_SPACE_INFO:
  5053. return btrfs_ioctl_space_info(fs_info, argp);
  5054. case BTRFS_IOC_SYNC: {
  5055. int ret;
  5056. ret = btrfs_start_delalloc_roots(fs_info, -1);
  5057. if (ret)
  5058. return ret;
  5059. ret = btrfs_sync_fs(inode->i_sb, 1);
  5060. /*
  5061. * The transaction thread may want to do more work,
  5062. * namely it pokes the cleaner kthread that will start
  5063. * processing uncleaned subvols.
  5064. */
  5065. wake_up_process(fs_info->transaction_kthread);
  5066. return ret;
  5067. }
  5068. case BTRFS_IOC_START_SYNC:
  5069. return btrfs_ioctl_start_sync(root, argp);
  5070. case BTRFS_IOC_WAIT_SYNC:
  5071. return btrfs_ioctl_wait_sync(fs_info, argp);
  5072. case BTRFS_IOC_SCRUB:
  5073. return btrfs_ioctl_scrub(file, argp);
  5074. case BTRFS_IOC_SCRUB_CANCEL:
  5075. return btrfs_ioctl_scrub_cancel(fs_info);
  5076. case BTRFS_IOC_SCRUB_PROGRESS:
  5077. return btrfs_ioctl_scrub_progress(fs_info, argp);
  5078. case BTRFS_IOC_BALANCE_V2:
  5079. return btrfs_ioctl_balance(file, argp);
  5080. case BTRFS_IOC_BALANCE_CTL:
  5081. return btrfs_ioctl_balance_ctl(fs_info, arg);
  5082. case BTRFS_IOC_BALANCE_PROGRESS:
  5083. return btrfs_ioctl_balance_progress(fs_info, argp);
  5084. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  5085. return btrfs_ioctl_set_received_subvol(file, argp);
  5086. #ifdef CONFIG_64BIT
  5087. case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
  5088. return btrfs_ioctl_set_received_subvol_32(file, argp);
  5089. #endif
  5090. case BTRFS_IOC_SEND:
  5091. return _btrfs_ioctl_send(file, argp, false);
  5092. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  5093. case BTRFS_IOC_SEND_32:
  5094. return _btrfs_ioctl_send(file, argp, true);
  5095. #endif
  5096. case BTRFS_IOC_GET_DEV_STATS:
  5097. return btrfs_ioctl_get_dev_stats(fs_info, argp);
  5098. case BTRFS_IOC_QUOTA_CTL:
  5099. return btrfs_ioctl_quota_ctl(file, argp);
  5100. case BTRFS_IOC_QGROUP_ASSIGN:
  5101. return btrfs_ioctl_qgroup_assign(file, argp);
  5102. case BTRFS_IOC_QGROUP_CREATE:
  5103. return btrfs_ioctl_qgroup_create(file, argp);
  5104. case BTRFS_IOC_QGROUP_LIMIT:
  5105. return btrfs_ioctl_qgroup_limit(file, argp);
  5106. case BTRFS_IOC_QUOTA_RESCAN:
  5107. return btrfs_ioctl_quota_rescan(file, argp);
  5108. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  5109. return btrfs_ioctl_quota_rescan_status(file, argp);
  5110. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  5111. return btrfs_ioctl_quota_rescan_wait(file, argp);
  5112. case BTRFS_IOC_DEV_REPLACE:
  5113. return btrfs_ioctl_dev_replace(fs_info, argp);
  5114. case BTRFS_IOC_GET_FSLABEL:
  5115. return btrfs_ioctl_get_fslabel(file, argp);
  5116. case BTRFS_IOC_SET_FSLABEL:
  5117. return btrfs_ioctl_set_fslabel(file, argp);
  5118. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  5119. return btrfs_ioctl_get_supported_features(argp);
  5120. case BTRFS_IOC_GET_FEATURES:
  5121. return btrfs_ioctl_get_features(file, argp);
  5122. case BTRFS_IOC_SET_FEATURES:
  5123. return btrfs_ioctl_set_features(file, argp);
  5124. case FS_IOC_FSGETXATTR:
  5125. return btrfs_ioctl_fsgetxattr(file, argp);
  5126. case FS_IOC_FSSETXATTR:
  5127. return btrfs_ioctl_fssetxattr(file, argp);
  5128. case BTRFS_IOC_GET_SUBVOL_INFO:
  5129. return btrfs_ioctl_get_subvol_info(file, argp);
  5130. case BTRFS_IOC_GET_SUBVOL_ROOTREF:
  5131. return btrfs_ioctl_get_subvol_rootref(file, argp);
  5132. case BTRFS_IOC_INO_LOOKUP_USER:
  5133. return btrfs_ioctl_ino_lookup_user(file, argp);
  5134. }
  5135. return -ENOTTY;
  5136. }
  5137. #ifdef CONFIG_COMPAT
  5138. long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  5139. {
  5140. /*
  5141. * These all access 32-bit values anyway so no further
  5142. * handling is necessary.
  5143. */
  5144. switch (cmd) {
  5145. case FS_IOC32_GETFLAGS:
  5146. cmd = FS_IOC_GETFLAGS;
  5147. break;
  5148. case FS_IOC32_SETFLAGS:
  5149. cmd = FS_IOC_SETFLAGS;
  5150. break;
  5151. case FS_IOC32_GETVERSION:
  5152. cmd = FS_IOC_GETVERSION;
  5153. break;
  5154. }
  5155. return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  5156. }
  5157. #endif