ioctl.c 145 KB

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