ioctl.c 122 KB

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