ioctl.c 142 KB

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