ioctl.c 133 KB

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