ioctl.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020
  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 "compat.h"
  45. #include "ctree.h"
  46. #include "disk-io.h"
  47. #include "transaction.h"
  48. #include "btrfs_inode.h"
  49. #include "ioctl.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. /* Mask out flags that are inappropriate for the given type of inode. */
  59. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  60. {
  61. if (S_ISDIR(mode))
  62. return flags;
  63. else if (S_ISREG(mode))
  64. return flags & ~FS_DIRSYNC_FL;
  65. else
  66. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  67. }
  68. /*
  69. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  70. */
  71. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  72. {
  73. unsigned int iflags = 0;
  74. if (flags & BTRFS_INODE_SYNC)
  75. iflags |= FS_SYNC_FL;
  76. if (flags & BTRFS_INODE_IMMUTABLE)
  77. iflags |= FS_IMMUTABLE_FL;
  78. if (flags & BTRFS_INODE_APPEND)
  79. iflags |= FS_APPEND_FL;
  80. if (flags & BTRFS_INODE_NODUMP)
  81. iflags |= FS_NODUMP_FL;
  82. if (flags & BTRFS_INODE_NOATIME)
  83. iflags |= FS_NOATIME_FL;
  84. if (flags & BTRFS_INODE_DIRSYNC)
  85. iflags |= FS_DIRSYNC_FL;
  86. if (flags & BTRFS_INODE_NODATACOW)
  87. iflags |= FS_NOCOW_FL;
  88. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  89. iflags |= FS_COMPR_FL;
  90. else if (flags & BTRFS_INODE_NOCOMPRESS)
  91. iflags |= FS_NOCOMP_FL;
  92. return iflags;
  93. }
  94. /*
  95. * Update inode->i_flags based on the btrfs internal flags.
  96. */
  97. void btrfs_update_iflags(struct inode *inode)
  98. {
  99. struct btrfs_inode *ip = BTRFS_I(inode);
  100. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  101. if (ip->flags & BTRFS_INODE_SYNC)
  102. inode->i_flags |= S_SYNC;
  103. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  104. inode->i_flags |= S_IMMUTABLE;
  105. if (ip->flags & BTRFS_INODE_APPEND)
  106. inode->i_flags |= S_APPEND;
  107. if (ip->flags & BTRFS_INODE_NOATIME)
  108. inode->i_flags |= S_NOATIME;
  109. if (ip->flags & BTRFS_INODE_DIRSYNC)
  110. inode->i_flags |= S_DIRSYNC;
  111. }
  112. /*
  113. * Inherit flags from the parent inode.
  114. *
  115. * Currently only the compression flags and the cow flags are inherited.
  116. */
  117. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  118. {
  119. unsigned int flags;
  120. if (!dir)
  121. return;
  122. flags = BTRFS_I(dir)->flags;
  123. if (flags & BTRFS_INODE_NOCOMPRESS) {
  124. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  125. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  126. } else if (flags & BTRFS_INODE_COMPRESS) {
  127. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  128. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  129. }
  130. if (flags & BTRFS_INODE_NODATACOW) {
  131. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  132. if (S_ISREG(inode->i_mode))
  133. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  134. }
  135. btrfs_update_iflags(inode);
  136. }
  137. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  138. {
  139. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  140. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  141. if (copy_to_user(arg, &flags, sizeof(flags)))
  142. return -EFAULT;
  143. return 0;
  144. }
  145. static int check_flags(unsigned int flags)
  146. {
  147. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  148. FS_NOATIME_FL | FS_NODUMP_FL | \
  149. FS_SYNC_FL | FS_DIRSYNC_FL | \
  150. FS_NOCOMP_FL | FS_COMPR_FL |
  151. FS_NOCOW_FL))
  152. return -EOPNOTSUPP;
  153. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  154. return -EINVAL;
  155. return 0;
  156. }
  157. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  158. {
  159. struct inode *inode = file->f_path.dentry->d_inode;
  160. struct btrfs_inode *ip = BTRFS_I(inode);
  161. struct btrfs_root *root = ip->root;
  162. struct btrfs_trans_handle *trans;
  163. unsigned int flags, oldflags;
  164. int ret;
  165. u64 ip_oldflags;
  166. unsigned int i_oldflags;
  167. umode_t mode;
  168. if (btrfs_root_readonly(root))
  169. return -EROFS;
  170. if (copy_from_user(&flags, arg, sizeof(flags)))
  171. return -EFAULT;
  172. ret = check_flags(flags);
  173. if (ret)
  174. return ret;
  175. if (!inode_owner_or_capable(inode))
  176. return -EACCES;
  177. ret = mnt_want_write_file(file);
  178. if (ret)
  179. return ret;
  180. mutex_lock(&inode->i_mutex);
  181. ip_oldflags = ip->flags;
  182. i_oldflags = inode->i_flags;
  183. mode = inode->i_mode;
  184. flags = btrfs_mask_flags(inode->i_mode, flags);
  185. oldflags = btrfs_flags_to_ioctl(ip->flags);
  186. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  187. if (!capable(CAP_LINUX_IMMUTABLE)) {
  188. ret = -EPERM;
  189. goto out_unlock;
  190. }
  191. }
  192. if (flags & FS_SYNC_FL)
  193. ip->flags |= BTRFS_INODE_SYNC;
  194. else
  195. ip->flags &= ~BTRFS_INODE_SYNC;
  196. if (flags & FS_IMMUTABLE_FL)
  197. ip->flags |= BTRFS_INODE_IMMUTABLE;
  198. else
  199. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  200. if (flags & FS_APPEND_FL)
  201. ip->flags |= BTRFS_INODE_APPEND;
  202. else
  203. ip->flags &= ~BTRFS_INODE_APPEND;
  204. if (flags & FS_NODUMP_FL)
  205. ip->flags |= BTRFS_INODE_NODUMP;
  206. else
  207. ip->flags &= ~BTRFS_INODE_NODUMP;
  208. if (flags & FS_NOATIME_FL)
  209. ip->flags |= BTRFS_INODE_NOATIME;
  210. else
  211. ip->flags &= ~BTRFS_INODE_NOATIME;
  212. if (flags & FS_DIRSYNC_FL)
  213. ip->flags |= BTRFS_INODE_DIRSYNC;
  214. else
  215. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  216. if (flags & FS_NOCOW_FL) {
  217. if (S_ISREG(mode)) {
  218. /*
  219. * It's safe to turn csums off here, no extents exist.
  220. * Otherwise we want the flag to reflect the real COW
  221. * status of the file and will not set it.
  222. */
  223. if (inode->i_size == 0)
  224. ip->flags |= BTRFS_INODE_NODATACOW
  225. | BTRFS_INODE_NODATASUM;
  226. } else {
  227. ip->flags |= BTRFS_INODE_NODATACOW;
  228. }
  229. } else {
  230. /*
  231. * Revert back under same assuptions as above
  232. */
  233. if (S_ISREG(mode)) {
  234. if (inode->i_size == 0)
  235. ip->flags &= ~(BTRFS_INODE_NODATACOW
  236. | BTRFS_INODE_NODATASUM);
  237. } else {
  238. ip->flags &= ~BTRFS_INODE_NODATACOW;
  239. }
  240. }
  241. /*
  242. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  243. * flag may be changed automatically if compression code won't make
  244. * things smaller.
  245. */
  246. if (flags & FS_NOCOMP_FL) {
  247. ip->flags &= ~BTRFS_INODE_COMPRESS;
  248. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  249. } else if (flags & FS_COMPR_FL) {
  250. ip->flags |= BTRFS_INODE_COMPRESS;
  251. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  252. } else {
  253. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  254. }
  255. trans = btrfs_start_transaction(root, 1);
  256. if (IS_ERR(trans)) {
  257. ret = PTR_ERR(trans);
  258. goto out_drop;
  259. }
  260. btrfs_update_iflags(inode);
  261. inode_inc_iversion(inode);
  262. inode->i_ctime = CURRENT_TIME;
  263. ret = btrfs_update_inode(trans, root, inode);
  264. btrfs_end_transaction(trans, root);
  265. out_drop:
  266. if (ret) {
  267. ip->flags = ip_oldflags;
  268. inode->i_flags = i_oldflags;
  269. }
  270. out_unlock:
  271. mutex_unlock(&inode->i_mutex);
  272. mnt_drop_write_file(file);
  273. return ret;
  274. }
  275. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  276. {
  277. struct inode *inode = file->f_path.dentry->d_inode;
  278. return put_user(inode->i_generation, arg);
  279. }
  280. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  281. {
  282. struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
  283. struct btrfs_device *device;
  284. struct request_queue *q;
  285. struct fstrim_range range;
  286. u64 minlen = ULLONG_MAX;
  287. u64 num_devices = 0;
  288. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  289. int ret;
  290. if (!capable(CAP_SYS_ADMIN))
  291. return -EPERM;
  292. rcu_read_lock();
  293. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  294. dev_list) {
  295. if (!device->bdev)
  296. continue;
  297. q = bdev_get_queue(device->bdev);
  298. if (blk_queue_discard(q)) {
  299. num_devices++;
  300. minlen = min((u64)q->limits.discard_granularity,
  301. minlen);
  302. }
  303. }
  304. rcu_read_unlock();
  305. if (!num_devices)
  306. return -EOPNOTSUPP;
  307. if (copy_from_user(&range, arg, sizeof(range)))
  308. return -EFAULT;
  309. if (range.start > total_bytes ||
  310. range.len < fs_info->sb->s_blocksize)
  311. return -EINVAL;
  312. range.len = min(range.len, total_bytes - range.start);
  313. range.minlen = max(range.minlen, minlen);
  314. ret = btrfs_trim_fs(fs_info->tree_root, &range);
  315. if (ret < 0)
  316. return ret;
  317. if (copy_to_user(arg, &range, sizeof(range)))
  318. return -EFAULT;
  319. return 0;
  320. }
  321. static noinline int create_subvol(struct btrfs_root *root,
  322. struct dentry *dentry,
  323. char *name, int namelen,
  324. u64 *async_transid,
  325. struct btrfs_qgroup_inherit **inherit)
  326. {
  327. struct btrfs_trans_handle *trans;
  328. struct btrfs_key key;
  329. struct btrfs_root_item root_item;
  330. struct btrfs_inode_item *inode_item;
  331. struct extent_buffer *leaf;
  332. struct btrfs_root *new_root;
  333. struct dentry *parent = dentry->d_parent;
  334. struct inode *dir;
  335. struct timespec cur_time = CURRENT_TIME;
  336. int ret;
  337. int err;
  338. u64 objectid;
  339. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  340. u64 index = 0;
  341. uuid_le new_uuid;
  342. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  343. if (ret)
  344. return ret;
  345. dir = parent->d_inode;
  346. /*
  347. * 1 - inode item
  348. * 2 - refs
  349. * 1 - root item
  350. * 2 - dir items
  351. */
  352. trans = btrfs_start_transaction(root, 6);
  353. if (IS_ERR(trans))
  354. return PTR_ERR(trans);
  355. ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
  356. inherit ? *inherit : NULL);
  357. if (ret)
  358. goto fail;
  359. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  360. 0, objectid, NULL, 0, 0, 0);
  361. if (IS_ERR(leaf)) {
  362. ret = PTR_ERR(leaf);
  363. goto fail;
  364. }
  365. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  366. btrfs_set_header_bytenr(leaf, leaf->start);
  367. btrfs_set_header_generation(leaf, trans->transid);
  368. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  369. btrfs_set_header_owner(leaf, objectid);
  370. write_extent_buffer(leaf, root->fs_info->fsid,
  371. (unsigned long)btrfs_header_fsid(leaf),
  372. BTRFS_FSID_SIZE);
  373. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  374. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  375. BTRFS_UUID_SIZE);
  376. btrfs_mark_buffer_dirty(leaf);
  377. memset(&root_item, 0, sizeof(root_item));
  378. inode_item = &root_item.inode;
  379. inode_item->generation = cpu_to_le64(1);
  380. inode_item->size = cpu_to_le64(3);
  381. inode_item->nlink = cpu_to_le32(1);
  382. inode_item->nbytes = cpu_to_le64(root->leafsize);
  383. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  384. root_item.flags = 0;
  385. root_item.byte_limit = 0;
  386. inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
  387. btrfs_set_root_bytenr(&root_item, leaf->start);
  388. btrfs_set_root_generation(&root_item, trans->transid);
  389. btrfs_set_root_level(&root_item, 0);
  390. btrfs_set_root_refs(&root_item, 1);
  391. btrfs_set_root_used(&root_item, leaf->len);
  392. btrfs_set_root_last_snapshot(&root_item, 0);
  393. btrfs_set_root_generation_v2(&root_item,
  394. btrfs_root_generation(&root_item));
  395. uuid_le_gen(&new_uuid);
  396. memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
  397. root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
  398. root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
  399. root_item.ctime = root_item.otime;
  400. btrfs_set_root_ctransid(&root_item, trans->transid);
  401. btrfs_set_root_otransid(&root_item, trans->transid);
  402. btrfs_tree_unlock(leaf);
  403. free_extent_buffer(leaf);
  404. leaf = NULL;
  405. btrfs_set_root_dirid(&root_item, new_dirid);
  406. key.objectid = objectid;
  407. key.offset = 0;
  408. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  409. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  410. &root_item);
  411. if (ret)
  412. goto fail;
  413. key.offset = (u64)-1;
  414. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  415. if (IS_ERR(new_root)) {
  416. btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
  417. ret = PTR_ERR(new_root);
  418. goto fail;
  419. }
  420. btrfs_record_root_in_trans(trans, new_root);
  421. ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
  422. if (ret) {
  423. /* We potentially lose an unused inode item here */
  424. btrfs_abort_transaction(trans, root, ret);
  425. goto fail;
  426. }
  427. /*
  428. * insert the directory item
  429. */
  430. ret = btrfs_set_inode_index(dir, &index);
  431. if (ret) {
  432. btrfs_abort_transaction(trans, root, ret);
  433. goto fail;
  434. }
  435. ret = btrfs_insert_dir_item(trans, root,
  436. name, namelen, dir, &key,
  437. BTRFS_FT_DIR, index);
  438. if (ret) {
  439. btrfs_abort_transaction(trans, root, ret);
  440. goto fail;
  441. }
  442. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  443. ret = btrfs_update_inode(trans, root, dir);
  444. BUG_ON(ret);
  445. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  446. objectid, root->root_key.objectid,
  447. btrfs_ino(dir), index, name, namelen);
  448. BUG_ON(ret);
  449. fail:
  450. if (async_transid) {
  451. *async_transid = trans->transid;
  452. err = btrfs_commit_transaction_async(trans, root, 1);
  453. } else {
  454. err = btrfs_commit_transaction(trans, root);
  455. }
  456. if (err && !ret)
  457. ret = err;
  458. if (!ret)
  459. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  460. return ret;
  461. }
  462. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  463. char *name, int namelen, u64 *async_transid,
  464. bool readonly, struct btrfs_qgroup_inherit **inherit)
  465. {
  466. struct inode *inode;
  467. struct btrfs_pending_snapshot *pending_snapshot;
  468. struct btrfs_trans_handle *trans;
  469. int ret;
  470. if (!root->ref_cows)
  471. return -EINVAL;
  472. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  473. if (!pending_snapshot)
  474. return -ENOMEM;
  475. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  476. BTRFS_BLOCK_RSV_TEMP);
  477. pending_snapshot->dentry = dentry;
  478. pending_snapshot->root = root;
  479. pending_snapshot->readonly = readonly;
  480. if (inherit) {
  481. pending_snapshot->inherit = *inherit;
  482. *inherit = NULL; /* take responsibility to free it */
  483. }
  484. trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
  485. if (IS_ERR(trans)) {
  486. ret = PTR_ERR(trans);
  487. goto fail;
  488. }
  489. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  490. BUG_ON(ret);
  491. spin_lock(&root->fs_info->trans_lock);
  492. list_add(&pending_snapshot->list,
  493. &trans->transaction->pending_snapshots);
  494. spin_unlock(&root->fs_info->trans_lock);
  495. if (async_transid) {
  496. *async_transid = trans->transid;
  497. ret = btrfs_commit_transaction_async(trans,
  498. root->fs_info->extent_root, 1);
  499. } else {
  500. ret = btrfs_commit_transaction(trans,
  501. root->fs_info->extent_root);
  502. }
  503. if (ret) {
  504. /* cleanup_transaction has freed this for us */
  505. if (trans->aborted)
  506. pending_snapshot = NULL;
  507. goto fail;
  508. }
  509. ret = pending_snapshot->error;
  510. if (ret)
  511. goto fail;
  512. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  513. if (ret)
  514. goto fail;
  515. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  516. if (IS_ERR(inode)) {
  517. ret = PTR_ERR(inode);
  518. goto fail;
  519. }
  520. BUG_ON(!inode);
  521. d_instantiate(dentry, inode);
  522. ret = 0;
  523. fail:
  524. kfree(pending_snapshot);
  525. return ret;
  526. }
  527. /* copy of check_sticky in fs/namei.c()
  528. * It's inline, so penalty for filesystems that don't use sticky bit is
  529. * minimal.
  530. */
  531. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  532. {
  533. kuid_t fsuid = current_fsuid();
  534. if (!(dir->i_mode & S_ISVTX))
  535. return 0;
  536. if (uid_eq(inode->i_uid, fsuid))
  537. return 0;
  538. if (uid_eq(dir->i_uid, fsuid))
  539. return 0;
  540. return !capable(CAP_FOWNER);
  541. }
  542. /* copy of may_delete in fs/namei.c()
  543. * Check whether we can remove a link victim from directory dir, check
  544. * whether the type of victim is right.
  545. * 1. We can't do it if dir is read-only (done in permission())
  546. * 2. We should have write and exec permissions on dir
  547. * 3. We can't remove anything from append-only dir
  548. * 4. We can't do anything with immutable dir (done in permission())
  549. * 5. If the sticky bit on dir is set we should either
  550. * a. be owner of dir, or
  551. * b. be owner of victim, or
  552. * c. have CAP_FOWNER capability
  553. * 6. If the victim is append-only or immutable we can't do antyhing with
  554. * links pointing to it.
  555. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  556. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  557. * 9. We can't remove a root or mountpoint.
  558. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  559. * nfs_async_unlink().
  560. */
  561. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  562. {
  563. int error;
  564. if (!victim->d_inode)
  565. return -ENOENT;
  566. BUG_ON(victim->d_parent->d_inode != dir);
  567. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  568. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  569. if (error)
  570. return error;
  571. if (IS_APPEND(dir))
  572. return -EPERM;
  573. if (btrfs_check_sticky(dir, victim->d_inode)||
  574. IS_APPEND(victim->d_inode)||
  575. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  576. return -EPERM;
  577. if (isdir) {
  578. if (!S_ISDIR(victim->d_inode->i_mode))
  579. return -ENOTDIR;
  580. if (IS_ROOT(victim))
  581. return -EBUSY;
  582. } else if (S_ISDIR(victim->d_inode->i_mode))
  583. return -EISDIR;
  584. if (IS_DEADDIR(dir))
  585. return -ENOENT;
  586. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  587. return -EBUSY;
  588. return 0;
  589. }
  590. /* copy of may_create in fs/namei.c() */
  591. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  592. {
  593. if (child->d_inode)
  594. return -EEXIST;
  595. if (IS_DEADDIR(dir))
  596. return -ENOENT;
  597. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  598. }
  599. /*
  600. * Create a new subvolume below @parent. This is largely modeled after
  601. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  602. * inside this filesystem so it's quite a bit simpler.
  603. */
  604. static noinline int btrfs_mksubvol(struct path *parent,
  605. char *name, int namelen,
  606. struct btrfs_root *snap_src,
  607. u64 *async_transid, bool readonly,
  608. struct btrfs_qgroup_inherit **inherit)
  609. {
  610. struct inode *dir = parent->dentry->d_inode;
  611. struct dentry *dentry;
  612. int error;
  613. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  614. dentry = lookup_one_len(name, parent->dentry, namelen);
  615. error = PTR_ERR(dentry);
  616. if (IS_ERR(dentry))
  617. goto out_unlock;
  618. error = -EEXIST;
  619. if (dentry->d_inode)
  620. goto out_dput;
  621. error = btrfs_may_create(dir, dentry);
  622. if (error)
  623. goto out_dput;
  624. /*
  625. * even if this name doesn't exist, we may get hash collisions.
  626. * check for them now when we can safely fail
  627. */
  628. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  629. dir->i_ino, name,
  630. namelen);
  631. if (error)
  632. goto out_dput;
  633. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  634. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  635. goto out_up_read;
  636. if (snap_src) {
  637. error = create_snapshot(snap_src, dentry, name, namelen,
  638. async_transid, readonly, inherit);
  639. } else {
  640. error = create_subvol(BTRFS_I(dir)->root, dentry,
  641. name, namelen, async_transid, inherit);
  642. }
  643. if (!error)
  644. fsnotify_mkdir(dir, dentry);
  645. out_up_read:
  646. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  647. out_dput:
  648. dput(dentry);
  649. out_unlock:
  650. mutex_unlock(&dir->i_mutex);
  651. return error;
  652. }
  653. /*
  654. * When we're defragging a range, we don't want to kick it off again
  655. * if it is really just waiting for delalloc to send it down.
  656. * If we find a nice big extent or delalloc range for the bytes in the
  657. * file you want to defrag, we return 0 to let you know to skip this
  658. * part of the file
  659. */
  660. static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
  661. {
  662. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  663. struct extent_map *em = NULL;
  664. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  665. u64 end;
  666. read_lock(&em_tree->lock);
  667. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  668. read_unlock(&em_tree->lock);
  669. if (em) {
  670. end = extent_map_end(em);
  671. free_extent_map(em);
  672. if (end - offset > thresh)
  673. return 0;
  674. }
  675. /* if we already have a nice delalloc here, just stop */
  676. thresh /= 2;
  677. end = count_range_bits(io_tree, &offset, offset + thresh,
  678. thresh, EXTENT_DELALLOC, 1);
  679. if (end >= thresh)
  680. return 0;
  681. return 1;
  682. }
  683. /*
  684. * helper function to walk through a file and find extents
  685. * newer than a specific transid, and smaller than thresh.
  686. *
  687. * This is used by the defragging code to find new and small
  688. * extents
  689. */
  690. static int find_new_extents(struct btrfs_root *root,
  691. struct inode *inode, u64 newer_than,
  692. u64 *off, int thresh)
  693. {
  694. struct btrfs_path *path;
  695. struct btrfs_key min_key;
  696. struct btrfs_key max_key;
  697. struct extent_buffer *leaf;
  698. struct btrfs_file_extent_item *extent;
  699. int type;
  700. int ret;
  701. u64 ino = btrfs_ino(inode);
  702. path = btrfs_alloc_path();
  703. if (!path)
  704. return -ENOMEM;
  705. min_key.objectid = ino;
  706. min_key.type = BTRFS_EXTENT_DATA_KEY;
  707. min_key.offset = *off;
  708. max_key.objectid = ino;
  709. max_key.type = (u8)-1;
  710. max_key.offset = (u64)-1;
  711. path->keep_locks = 1;
  712. while(1) {
  713. ret = btrfs_search_forward(root, &min_key, &max_key,
  714. path, 0, newer_than);
  715. if (ret != 0)
  716. goto none;
  717. if (min_key.objectid != ino)
  718. goto none;
  719. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  720. goto none;
  721. leaf = path->nodes[0];
  722. extent = btrfs_item_ptr(leaf, path->slots[0],
  723. struct btrfs_file_extent_item);
  724. type = btrfs_file_extent_type(leaf, extent);
  725. if (type == BTRFS_FILE_EXTENT_REG &&
  726. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  727. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  728. *off = min_key.offset;
  729. btrfs_free_path(path);
  730. return 0;
  731. }
  732. if (min_key.offset == (u64)-1)
  733. goto none;
  734. min_key.offset++;
  735. btrfs_release_path(path);
  736. }
  737. none:
  738. btrfs_free_path(path);
  739. return -ENOENT;
  740. }
  741. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  742. {
  743. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  744. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  745. struct extent_map *em;
  746. u64 len = PAGE_CACHE_SIZE;
  747. /*
  748. * hopefully we have this extent in the tree already, try without
  749. * the full extent lock
  750. */
  751. read_lock(&em_tree->lock);
  752. em = lookup_extent_mapping(em_tree, start, len);
  753. read_unlock(&em_tree->lock);
  754. if (!em) {
  755. /* get the big lock and read metadata off disk */
  756. lock_extent(io_tree, start, start + len - 1);
  757. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  758. unlock_extent(io_tree, start, start + len - 1);
  759. if (IS_ERR(em))
  760. return NULL;
  761. }
  762. return em;
  763. }
  764. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  765. {
  766. struct extent_map *next;
  767. bool ret = true;
  768. /* this is the last extent */
  769. if (em->start + em->len >= i_size_read(inode))
  770. return false;
  771. next = defrag_lookup_extent(inode, em->start + em->len);
  772. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  773. ret = false;
  774. free_extent_map(next);
  775. return ret;
  776. }
  777. static int should_defrag_range(struct inode *inode, u64 start, int thresh,
  778. u64 *last_len, u64 *skip, u64 *defrag_end,
  779. int compress)
  780. {
  781. struct extent_map *em;
  782. int ret = 1;
  783. bool next_mergeable = true;
  784. /*
  785. * make sure that once we start defragging an extent, we keep on
  786. * defragging it
  787. */
  788. if (start < *defrag_end)
  789. return 1;
  790. *skip = 0;
  791. em = defrag_lookup_extent(inode, start);
  792. if (!em)
  793. return 0;
  794. /* this will cover holes, and inline extents */
  795. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  796. ret = 0;
  797. goto out;
  798. }
  799. next_mergeable = defrag_check_next_extent(inode, em);
  800. /*
  801. * we hit a real extent, if it is big or the next extent is not a
  802. * real extent, don't bother defragging it
  803. */
  804. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  805. (em->len >= thresh || !next_mergeable))
  806. ret = 0;
  807. out:
  808. /*
  809. * last_len ends up being a counter of how many bytes we've defragged.
  810. * every time we choose not to defrag an extent, we reset *last_len
  811. * so that the next tiny extent will force a defrag.
  812. *
  813. * The end result of this is that tiny extents before a single big
  814. * extent will force at least part of that big extent to be defragged.
  815. */
  816. if (ret) {
  817. *defrag_end = extent_map_end(em);
  818. } else {
  819. *last_len = 0;
  820. *skip = extent_map_end(em);
  821. *defrag_end = 0;
  822. }
  823. free_extent_map(em);
  824. return ret;
  825. }
  826. /*
  827. * it doesn't do much good to defrag one or two pages
  828. * at a time. This pulls in a nice chunk of pages
  829. * to COW and defrag.
  830. *
  831. * It also makes sure the delalloc code has enough
  832. * dirty data to avoid making new small extents as part
  833. * of the defrag
  834. *
  835. * It's a good idea to start RA on this range
  836. * before calling this.
  837. */
  838. static int cluster_pages_for_defrag(struct inode *inode,
  839. struct page **pages,
  840. unsigned long start_index,
  841. int num_pages)
  842. {
  843. unsigned long file_end;
  844. u64 isize = i_size_read(inode);
  845. u64 page_start;
  846. u64 page_end;
  847. u64 page_cnt;
  848. int ret;
  849. int i;
  850. int i_done;
  851. struct btrfs_ordered_extent *ordered;
  852. struct extent_state *cached_state = NULL;
  853. struct extent_io_tree *tree;
  854. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  855. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  856. if (!isize || start_index > file_end)
  857. return 0;
  858. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  859. ret = btrfs_delalloc_reserve_space(inode,
  860. page_cnt << PAGE_CACHE_SHIFT);
  861. if (ret)
  862. return ret;
  863. i_done = 0;
  864. tree = &BTRFS_I(inode)->io_tree;
  865. /* step one, lock all the pages */
  866. for (i = 0; i < page_cnt; i++) {
  867. struct page *page;
  868. again:
  869. page = find_or_create_page(inode->i_mapping,
  870. start_index + i, mask);
  871. if (!page)
  872. break;
  873. page_start = page_offset(page);
  874. page_end = page_start + PAGE_CACHE_SIZE - 1;
  875. while (1) {
  876. lock_extent(tree, page_start, page_end);
  877. ordered = btrfs_lookup_ordered_extent(inode,
  878. page_start);
  879. unlock_extent(tree, page_start, page_end);
  880. if (!ordered)
  881. break;
  882. unlock_page(page);
  883. btrfs_start_ordered_extent(inode, ordered, 1);
  884. btrfs_put_ordered_extent(ordered);
  885. lock_page(page);
  886. /*
  887. * we unlocked the page above, so we need check if
  888. * it was released or not.
  889. */
  890. if (page->mapping != inode->i_mapping) {
  891. unlock_page(page);
  892. page_cache_release(page);
  893. goto again;
  894. }
  895. }
  896. if (!PageUptodate(page)) {
  897. btrfs_readpage(NULL, page);
  898. lock_page(page);
  899. if (!PageUptodate(page)) {
  900. unlock_page(page);
  901. page_cache_release(page);
  902. ret = -EIO;
  903. break;
  904. }
  905. }
  906. if (page->mapping != inode->i_mapping) {
  907. unlock_page(page);
  908. page_cache_release(page);
  909. goto again;
  910. }
  911. pages[i] = page;
  912. i_done++;
  913. }
  914. if (!i_done || ret)
  915. goto out;
  916. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  917. goto out;
  918. /*
  919. * so now we have a nice long stream of locked
  920. * and up to date pages, lets wait on them
  921. */
  922. for (i = 0; i < i_done; i++)
  923. wait_on_page_writeback(pages[i]);
  924. page_start = page_offset(pages[0]);
  925. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  926. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  927. page_start, page_end - 1, 0, &cached_state);
  928. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  929. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  930. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  931. &cached_state, GFP_NOFS);
  932. if (i_done != page_cnt) {
  933. spin_lock(&BTRFS_I(inode)->lock);
  934. BTRFS_I(inode)->outstanding_extents++;
  935. spin_unlock(&BTRFS_I(inode)->lock);
  936. btrfs_delalloc_release_space(inode,
  937. (page_cnt - i_done) << PAGE_CACHE_SHIFT);
  938. }
  939. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  940. &cached_state, GFP_NOFS);
  941. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  942. page_start, page_end - 1, &cached_state,
  943. GFP_NOFS);
  944. for (i = 0; i < i_done; i++) {
  945. clear_page_dirty_for_io(pages[i]);
  946. ClearPageChecked(pages[i]);
  947. set_page_extent_mapped(pages[i]);
  948. set_page_dirty(pages[i]);
  949. unlock_page(pages[i]);
  950. page_cache_release(pages[i]);
  951. }
  952. return i_done;
  953. out:
  954. for (i = 0; i < i_done; i++) {
  955. unlock_page(pages[i]);
  956. page_cache_release(pages[i]);
  957. }
  958. btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
  959. return ret;
  960. }
  961. int btrfs_defrag_file(struct inode *inode, struct file *file,
  962. struct btrfs_ioctl_defrag_range_args *range,
  963. u64 newer_than, unsigned long max_to_defrag)
  964. {
  965. struct btrfs_root *root = BTRFS_I(inode)->root;
  966. struct file_ra_state *ra = NULL;
  967. unsigned long last_index;
  968. u64 isize = i_size_read(inode);
  969. u64 last_len = 0;
  970. u64 skip = 0;
  971. u64 defrag_end = 0;
  972. u64 newer_off = range->start;
  973. unsigned long i;
  974. unsigned long ra_index = 0;
  975. int ret;
  976. int defrag_count = 0;
  977. int compress_type = BTRFS_COMPRESS_ZLIB;
  978. int extent_thresh = range->extent_thresh;
  979. int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  980. int cluster = max_cluster;
  981. u64 new_align = ~((u64)128 * 1024 - 1);
  982. struct page **pages = NULL;
  983. if (extent_thresh == 0)
  984. extent_thresh = 256 * 1024;
  985. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  986. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  987. return -EINVAL;
  988. if (range->compress_type)
  989. compress_type = range->compress_type;
  990. }
  991. if (isize == 0)
  992. return 0;
  993. /*
  994. * if we were not given a file, allocate a readahead
  995. * context
  996. */
  997. if (!file) {
  998. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  999. if (!ra)
  1000. return -ENOMEM;
  1001. file_ra_state_init(ra, inode->i_mapping);
  1002. } else {
  1003. ra = &file->f_ra;
  1004. }
  1005. pages = kmalloc(sizeof(struct page *) * max_cluster,
  1006. GFP_NOFS);
  1007. if (!pages) {
  1008. ret = -ENOMEM;
  1009. goto out_ra;
  1010. }
  1011. /* find the last page to defrag */
  1012. if (range->start + range->len > range->start) {
  1013. last_index = min_t(u64, isize - 1,
  1014. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  1015. } else {
  1016. last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1017. }
  1018. if (newer_than) {
  1019. ret = find_new_extents(root, inode, newer_than,
  1020. &newer_off, 64 * 1024);
  1021. if (!ret) {
  1022. range->start = newer_off;
  1023. /*
  1024. * we always align our defrag to help keep
  1025. * the extents in the file evenly spaced
  1026. */
  1027. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1028. } else
  1029. goto out_ra;
  1030. } else {
  1031. i = range->start >> PAGE_CACHE_SHIFT;
  1032. }
  1033. if (!max_to_defrag)
  1034. max_to_defrag = last_index + 1;
  1035. /*
  1036. * make writeback starts from i, so the defrag range can be
  1037. * written sequentially.
  1038. */
  1039. if (i < inode->i_mapping->writeback_index)
  1040. inode->i_mapping->writeback_index = i;
  1041. while (i <= last_index && defrag_count < max_to_defrag &&
  1042. (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  1043. PAGE_CACHE_SHIFT)) {
  1044. /*
  1045. * make sure we stop running if someone unmounts
  1046. * the FS
  1047. */
  1048. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1049. break;
  1050. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  1051. extent_thresh, &last_len, &skip,
  1052. &defrag_end, range->flags &
  1053. BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1054. unsigned long next;
  1055. /*
  1056. * the should_defrag function tells us how much to skip
  1057. * bump our counter by the suggested amount
  1058. */
  1059. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1060. i = max(i + 1, next);
  1061. continue;
  1062. }
  1063. if (!newer_than) {
  1064. cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
  1065. PAGE_CACHE_SHIFT) - i;
  1066. cluster = min(cluster, max_cluster);
  1067. } else {
  1068. cluster = max_cluster;
  1069. }
  1070. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  1071. BTRFS_I(inode)->force_compress = compress_type;
  1072. if (i + cluster > ra_index) {
  1073. ra_index = max(i, ra_index);
  1074. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  1075. cluster);
  1076. ra_index += max_cluster;
  1077. }
  1078. mutex_lock(&inode->i_mutex);
  1079. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1080. if (ret < 0) {
  1081. mutex_unlock(&inode->i_mutex);
  1082. goto out_ra;
  1083. }
  1084. defrag_count += ret;
  1085. balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
  1086. mutex_unlock(&inode->i_mutex);
  1087. if (newer_than) {
  1088. if (newer_off == (u64)-1)
  1089. break;
  1090. if (ret > 0)
  1091. i += ret;
  1092. newer_off = max(newer_off + 1,
  1093. (u64)i << PAGE_CACHE_SHIFT);
  1094. ret = find_new_extents(root, inode,
  1095. newer_than, &newer_off,
  1096. 64 * 1024);
  1097. if (!ret) {
  1098. range->start = newer_off;
  1099. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1100. } else {
  1101. break;
  1102. }
  1103. } else {
  1104. if (ret > 0) {
  1105. i += ret;
  1106. last_len += ret << PAGE_CACHE_SHIFT;
  1107. } else {
  1108. i++;
  1109. last_len = 0;
  1110. }
  1111. }
  1112. }
  1113. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  1114. filemap_flush(inode->i_mapping);
  1115. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1116. /* the filemap_flush will queue IO into the worker threads, but
  1117. * we have to make sure the IO is actually started and that
  1118. * ordered extents get created before we return
  1119. */
  1120. atomic_inc(&root->fs_info->async_submit_draining);
  1121. while (atomic_read(&root->fs_info->nr_async_submits) ||
  1122. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1123. wait_event(root->fs_info->async_submit_wait,
  1124. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  1125. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  1126. }
  1127. atomic_dec(&root->fs_info->async_submit_draining);
  1128. mutex_lock(&inode->i_mutex);
  1129. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1130. mutex_unlock(&inode->i_mutex);
  1131. }
  1132. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1133. btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
  1134. }
  1135. ret = defrag_count;
  1136. out_ra:
  1137. if (!file)
  1138. kfree(ra);
  1139. kfree(pages);
  1140. return ret;
  1141. }
  1142. static noinline int btrfs_ioctl_resize(struct file *file,
  1143. void __user *arg)
  1144. {
  1145. u64 new_size;
  1146. u64 old_size;
  1147. u64 devid = 1;
  1148. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1149. struct btrfs_ioctl_vol_args *vol_args;
  1150. struct btrfs_trans_handle *trans;
  1151. struct btrfs_device *device = NULL;
  1152. char *sizestr;
  1153. char *devstr = NULL;
  1154. int ret = 0;
  1155. int mod = 0;
  1156. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1157. return -EROFS;
  1158. if (!capable(CAP_SYS_ADMIN))
  1159. return -EPERM;
  1160. ret = mnt_want_write_file(file);
  1161. if (ret)
  1162. return ret;
  1163. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1164. 1)) {
  1165. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1166. mnt_drop_write_file(file);
  1167. return -EINVAL;
  1168. }
  1169. mutex_lock(&root->fs_info->volume_mutex);
  1170. vol_args = memdup_user(arg, sizeof(*vol_args));
  1171. if (IS_ERR(vol_args)) {
  1172. ret = PTR_ERR(vol_args);
  1173. goto out;
  1174. }
  1175. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1176. sizestr = vol_args->name;
  1177. devstr = strchr(sizestr, ':');
  1178. if (devstr) {
  1179. char *end;
  1180. sizestr = devstr + 1;
  1181. *devstr = '\0';
  1182. devstr = vol_args->name;
  1183. devid = simple_strtoull(devstr, &end, 10);
  1184. printk(KERN_INFO "btrfs: resizing devid %llu\n",
  1185. (unsigned long long)devid);
  1186. }
  1187. device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  1188. if (!device) {
  1189. printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
  1190. (unsigned long long)devid);
  1191. ret = -EINVAL;
  1192. goto out_free;
  1193. }
  1194. if (!device->writeable) {
  1195. printk(KERN_INFO "btrfs: resizer unable to apply on "
  1196. "readonly device %llu\n",
  1197. (unsigned long long)devid);
  1198. ret = -EINVAL;
  1199. goto out_free;
  1200. }
  1201. if (!strcmp(sizestr, "max"))
  1202. new_size = device->bdev->bd_inode->i_size;
  1203. else {
  1204. if (sizestr[0] == '-') {
  1205. mod = -1;
  1206. sizestr++;
  1207. } else if (sizestr[0] == '+') {
  1208. mod = 1;
  1209. sizestr++;
  1210. }
  1211. new_size = memparse(sizestr, NULL);
  1212. if (new_size == 0) {
  1213. ret = -EINVAL;
  1214. goto out_free;
  1215. }
  1216. }
  1217. if (device->is_tgtdev_for_dev_replace) {
  1218. ret = -EINVAL;
  1219. goto out_free;
  1220. }
  1221. old_size = device->total_bytes;
  1222. if (mod < 0) {
  1223. if (new_size > old_size) {
  1224. ret = -EINVAL;
  1225. goto out_free;
  1226. }
  1227. new_size = old_size - new_size;
  1228. } else if (mod > 0) {
  1229. new_size = old_size + new_size;
  1230. }
  1231. if (new_size < 256 * 1024 * 1024) {
  1232. ret = -EINVAL;
  1233. goto out_free;
  1234. }
  1235. if (new_size > device->bdev->bd_inode->i_size) {
  1236. ret = -EFBIG;
  1237. goto out_free;
  1238. }
  1239. do_div(new_size, root->sectorsize);
  1240. new_size *= root->sectorsize;
  1241. printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
  1242. rcu_str_deref(device->name),
  1243. (unsigned long long)new_size);
  1244. if (new_size > old_size) {
  1245. trans = btrfs_start_transaction(root, 0);
  1246. if (IS_ERR(trans)) {
  1247. ret = PTR_ERR(trans);
  1248. goto out_free;
  1249. }
  1250. ret = btrfs_grow_device(trans, device, new_size);
  1251. btrfs_commit_transaction(trans, root);
  1252. } else if (new_size < old_size) {
  1253. ret = btrfs_shrink_device(device, new_size);
  1254. } /* equal, nothing need to do */
  1255. out_free:
  1256. kfree(vol_args);
  1257. out:
  1258. mutex_unlock(&root->fs_info->volume_mutex);
  1259. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1260. mnt_drop_write_file(file);
  1261. return ret;
  1262. }
  1263. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1264. char *name, unsigned long fd, int subvol,
  1265. u64 *transid, bool readonly,
  1266. struct btrfs_qgroup_inherit **inherit)
  1267. {
  1268. int namelen;
  1269. int ret = 0;
  1270. ret = mnt_want_write_file(file);
  1271. if (ret)
  1272. goto out;
  1273. namelen = strlen(name);
  1274. if (strchr(name, '/')) {
  1275. ret = -EINVAL;
  1276. goto out_drop_write;
  1277. }
  1278. if (name[0] == '.' &&
  1279. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1280. ret = -EEXIST;
  1281. goto out_drop_write;
  1282. }
  1283. if (subvol) {
  1284. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1285. NULL, transid, readonly, inherit);
  1286. } else {
  1287. struct fd src = fdget(fd);
  1288. struct inode *src_inode;
  1289. if (!src.file) {
  1290. ret = -EINVAL;
  1291. goto out_drop_write;
  1292. }
  1293. src_inode = src.file->f_path.dentry->d_inode;
  1294. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1295. printk(KERN_INFO "btrfs: Snapshot src from "
  1296. "another FS\n");
  1297. ret = -EINVAL;
  1298. } else {
  1299. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1300. BTRFS_I(src_inode)->root,
  1301. transid, readonly, inherit);
  1302. }
  1303. fdput(src);
  1304. }
  1305. out_drop_write:
  1306. mnt_drop_write_file(file);
  1307. out:
  1308. return ret;
  1309. }
  1310. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1311. void __user *arg, int subvol)
  1312. {
  1313. struct btrfs_ioctl_vol_args *vol_args;
  1314. int ret;
  1315. vol_args = memdup_user(arg, sizeof(*vol_args));
  1316. if (IS_ERR(vol_args))
  1317. return PTR_ERR(vol_args);
  1318. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1319. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1320. vol_args->fd, subvol,
  1321. NULL, false, NULL);
  1322. kfree(vol_args);
  1323. return ret;
  1324. }
  1325. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1326. void __user *arg, int subvol)
  1327. {
  1328. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1329. int ret;
  1330. u64 transid = 0;
  1331. u64 *ptr = NULL;
  1332. bool readonly = false;
  1333. struct btrfs_qgroup_inherit *inherit = NULL;
  1334. vol_args = memdup_user(arg, sizeof(*vol_args));
  1335. if (IS_ERR(vol_args))
  1336. return PTR_ERR(vol_args);
  1337. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1338. if (vol_args->flags &
  1339. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1340. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1341. ret = -EOPNOTSUPP;
  1342. goto out;
  1343. }
  1344. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1345. ptr = &transid;
  1346. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1347. readonly = true;
  1348. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1349. if (vol_args->size > PAGE_CACHE_SIZE) {
  1350. ret = -EINVAL;
  1351. goto out;
  1352. }
  1353. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1354. if (IS_ERR(inherit)) {
  1355. ret = PTR_ERR(inherit);
  1356. goto out;
  1357. }
  1358. }
  1359. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1360. vol_args->fd, subvol, ptr,
  1361. readonly, &inherit);
  1362. if (ret == 0 && ptr &&
  1363. copy_to_user(arg +
  1364. offsetof(struct btrfs_ioctl_vol_args_v2,
  1365. transid), ptr, sizeof(*ptr)))
  1366. ret = -EFAULT;
  1367. out:
  1368. kfree(vol_args);
  1369. kfree(inherit);
  1370. return ret;
  1371. }
  1372. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1373. void __user *arg)
  1374. {
  1375. struct inode *inode = fdentry(file)->d_inode;
  1376. struct btrfs_root *root = BTRFS_I(inode)->root;
  1377. int ret = 0;
  1378. u64 flags = 0;
  1379. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1380. return -EINVAL;
  1381. down_read(&root->fs_info->subvol_sem);
  1382. if (btrfs_root_readonly(root))
  1383. flags |= BTRFS_SUBVOL_RDONLY;
  1384. up_read(&root->fs_info->subvol_sem);
  1385. if (copy_to_user(arg, &flags, sizeof(flags)))
  1386. ret = -EFAULT;
  1387. return ret;
  1388. }
  1389. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1390. void __user *arg)
  1391. {
  1392. struct inode *inode = fdentry(file)->d_inode;
  1393. struct btrfs_root *root = BTRFS_I(inode)->root;
  1394. struct btrfs_trans_handle *trans;
  1395. u64 root_flags;
  1396. u64 flags;
  1397. int ret = 0;
  1398. ret = mnt_want_write_file(file);
  1399. if (ret)
  1400. goto out;
  1401. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1402. ret = -EINVAL;
  1403. goto out_drop_write;
  1404. }
  1405. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1406. ret = -EFAULT;
  1407. goto out_drop_write;
  1408. }
  1409. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1410. ret = -EINVAL;
  1411. goto out_drop_write;
  1412. }
  1413. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1414. ret = -EOPNOTSUPP;
  1415. goto out_drop_write;
  1416. }
  1417. if (!inode_owner_or_capable(inode)) {
  1418. ret = -EACCES;
  1419. goto out_drop_write;
  1420. }
  1421. down_write(&root->fs_info->subvol_sem);
  1422. /* nothing to do */
  1423. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1424. goto out_drop_sem;
  1425. root_flags = btrfs_root_flags(&root->root_item);
  1426. if (flags & BTRFS_SUBVOL_RDONLY)
  1427. btrfs_set_root_flags(&root->root_item,
  1428. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1429. else
  1430. btrfs_set_root_flags(&root->root_item,
  1431. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1432. trans = btrfs_start_transaction(root, 1);
  1433. if (IS_ERR(trans)) {
  1434. ret = PTR_ERR(trans);
  1435. goto out_reset;
  1436. }
  1437. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1438. &root->root_key, &root->root_item);
  1439. btrfs_commit_transaction(trans, root);
  1440. out_reset:
  1441. if (ret)
  1442. btrfs_set_root_flags(&root->root_item, root_flags);
  1443. out_drop_sem:
  1444. up_write(&root->fs_info->subvol_sem);
  1445. out_drop_write:
  1446. mnt_drop_write_file(file);
  1447. out:
  1448. return ret;
  1449. }
  1450. /*
  1451. * helper to check if the subvolume references other subvolumes
  1452. */
  1453. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1454. {
  1455. struct btrfs_path *path;
  1456. struct btrfs_key key;
  1457. int ret;
  1458. path = btrfs_alloc_path();
  1459. if (!path)
  1460. return -ENOMEM;
  1461. key.objectid = root->root_key.objectid;
  1462. key.type = BTRFS_ROOT_REF_KEY;
  1463. key.offset = (u64)-1;
  1464. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1465. &key, path, 0, 0);
  1466. if (ret < 0)
  1467. goto out;
  1468. BUG_ON(ret == 0);
  1469. ret = 0;
  1470. if (path->slots[0] > 0) {
  1471. path->slots[0]--;
  1472. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1473. if (key.objectid == root->root_key.objectid &&
  1474. key.type == BTRFS_ROOT_REF_KEY)
  1475. ret = -ENOTEMPTY;
  1476. }
  1477. out:
  1478. btrfs_free_path(path);
  1479. return ret;
  1480. }
  1481. static noinline int key_in_sk(struct btrfs_key *key,
  1482. struct btrfs_ioctl_search_key *sk)
  1483. {
  1484. struct btrfs_key test;
  1485. int ret;
  1486. test.objectid = sk->min_objectid;
  1487. test.type = sk->min_type;
  1488. test.offset = sk->min_offset;
  1489. ret = btrfs_comp_cpu_keys(key, &test);
  1490. if (ret < 0)
  1491. return 0;
  1492. test.objectid = sk->max_objectid;
  1493. test.type = sk->max_type;
  1494. test.offset = sk->max_offset;
  1495. ret = btrfs_comp_cpu_keys(key, &test);
  1496. if (ret > 0)
  1497. return 0;
  1498. return 1;
  1499. }
  1500. static noinline int copy_to_sk(struct btrfs_root *root,
  1501. struct btrfs_path *path,
  1502. struct btrfs_key *key,
  1503. struct btrfs_ioctl_search_key *sk,
  1504. char *buf,
  1505. unsigned long *sk_offset,
  1506. int *num_found)
  1507. {
  1508. u64 found_transid;
  1509. struct extent_buffer *leaf;
  1510. struct btrfs_ioctl_search_header sh;
  1511. unsigned long item_off;
  1512. unsigned long item_len;
  1513. int nritems;
  1514. int i;
  1515. int slot;
  1516. int ret = 0;
  1517. leaf = path->nodes[0];
  1518. slot = path->slots[0];
  1519. nritems = btrfs_header_nritems(leaf);
  1520. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1521. i = nritems;
  1522. goto advance_key;
  1523. }
  1524. found_transid = btrfs_header_generation(leaf);
  1525. for (i = slot; i < nritems; i++) {
  1526. item_off = btrfs_item_ptr_offset(leaf, i);
  1527. item_len = btrfs_item_size_nr(leaf, i);
  1528. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1529. item_len = 0;
  1530. if (sizeof(sh) + item_len + *sk_offset >
  1531. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1532. ret = 1;
  1533. goto overflow;
  1534. }
  1535. btrfs_item_key_to_cpu(leaf, key, i);
  1536. if (!key_in_sk(key, sk))
  1537. continue;
  1538. sh.objectid = key->objectid;
  1539. sh.offset = key->offset;
  1540. sh.type = key->type;
  1541. sh.len = item_len;
  1542. sh.transid = found_transid;
  1543. /* copy search result header */
  1544. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1545. *sk_offset += sizeof(sh);
  1546. if (item_len) {
  1547. char *p = buf + *sk_offset;
  1548. /* copy the item */
  1549. read_extent_buffer(leaf, p,
  1550. item_off, item_len);
  1551. *sk_offset += item_len;
  1552. }
  1553. (*num_found)++;
  1554. if (*num_found >= sk->nr_items)
  1555. break;
  1556. }
  1557. advance_key:
  1558. ret = 0;
  1559. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1560. key->offset++;
  1561. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1562. key->offset = 0;
  1563. key->type++;
  1564. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1565. key->offset = 0;
  1566. key->type = 0;
  1567. key->objectid++;
  1568. } else
  1569. ret = 1;
  1570. overflow:
  1571. return ret;
  1572. }
  1573. static noinline int search_ioctl(struct inode *inode,
  1574. struct btrfs_ioctl_search_args *args)
  1575. {
  1576. struct btrfs_root *root;
  1577. struct btrfs_key key;
  1578. struct btrfs_key max_key;
  1579. struct btrfs_path *path;
  1580. struct btrfs_ioctl_search_key *sk = &args->key;
  1581. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1582. int ret;
  1583. int num_found = 0;
  1584. unsigned long sk_offset = 0;
  1585. path = btrfs_alloc_path();
  1586. if (!path)
  1587. return -ENOMEM;
  1588. if (sk->tree_id == 0) {
  1589. /* search the root of the inode that was passed */
  1590. root = BTRFS_I(inode)->root;
  1591. } else {
  1592. key.objectid = sk->tree_id;
  1593. key.type = BTRFS_ROOT_ITEM_KEY;
  1594. key.offset = (u64)-1;
  1595. root = btrfs_read_fs_root_no_name(info, &key);
  1596. if (IS_ERR(root)) {
  1597. printk(KERN_ERR "could not find root %llu\n",
  1598. sk->tree_id);
  1599. btrfs_free_path(path);
  1600. return -ENOENT;
  1601. }
  1602. }
  1603. key.objectid = sk->min_objectid;
  1604. key.type = sk->min_type;
  1605. key.offset = sk->min_offset;
  1606. max_key.objectid = sk->max_objectid;
  1607. max_key.type = sk->max_type;
  1608. max_key.offset = sk->max_offset;
  1609. path->keep_locks = 1;
  1610. while(1) {
  1611. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1612. sk->min_transid);
  1613. if (ret != 0) {
  1614. if (ret > 0)
  1615. ret = 0;
  1616. goto err;
  1617. }
  1618. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1619. &sk_offset, &num_found);
  1620. btrfs_release_path(path);
  1621. if (ret || num_found >= sk->nr_items)
  1622. break;
  1623. }
  1624. ret = 0;
  1625. err:
  1626. sk->nr_items = num_found;
  1627. btrfs_free_path(path);
  1628. return ret;
  1629. }
  1630. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1631. void __user *argp)
  1632. {
  1633. struct btrfs_ioctl_search_args *args;
  1634. struct inode *inode;
  1635. int ret;
  1636. if (!capable(CAP_SYS_ADMIN))
  1637. return -EPERM;
  1638. args = memdup_user(argp, sizeof(*args));
  1639. if (IS_ERR(args))
  1640. return PTR_ERR(args);
  1641. inode = fdentry(file)->d_inode;
  1642. ret = search_ioctl(inode, args);
  1643. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1644. ret = -EFAULT;
  1645. kfree(args);
  1646. return ret;
  1647. }
  1648. /*
  1649. * Search INODE_REFs to identify path name of 'dirid' directory
  1650. * in a 'tree_id' tree. and sets path name to 'name'.
  1651. */
  1652. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1653. u64 tree_id, u64 dirid, char *name)
  1654. {
  1655. struct btrfs_root *root;
  1656. struct btrfs_key key;
  1657. char *ptr;
  1658. int ret = -1;
  1659. int slot;
  1660. int len;
  1661. int total_len = 0;
  1662. struct btrfs_inode_ref *iref;
  1663. struct extent_buffer *l;
  1664. struct btrfs_path *path;
  1665. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1666. name[0]='\0';
  1667. return 0;
  1668. }
  1669. path = btrfs_alloc_path();
  1670. if (!path)
  1671. return -ENOMEM;
  1672. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1673. key.objectid = tree_id;
  1674. key.type = BTRFS_ROOT_ITEM_KEY;
  1675. key.offset = (u64)-1;
  1676. root = btrfs_read_fs_root_no_name(info, &key);
  1677. if (IS_ERR(root)) {
  1678. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1679. ret = -ENOENT;
  1680. goto out;
  1681. }
  1682. key.objectid = dirid;
  1683. key.type = BTRFS_INODE_REF_KEY;
  1684. key.offset = (u64)-1;
  1685. while(1) {
  1686. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1687. if (ret < 0)
  1688. goto out;
  1689. l = path->nodes[0];
  1690. slot = path->slots[0];
  1691. if (ret > 0 && slot > 0)
  1692. slot--;
  1693. btrfs_item_key_to_cpu(l, &key, slot);
  1694. if (ret > 0 && (key.objectid != dirid ||
  1695. key.type != BTRFS_INODE_REF_KEY)) {
  1696. ret = -ENOENT;
  1697. goto out;
  1698. }
  1699. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1700. len = btrfs_inode_ref_name_len(l, iref);
  1701. ptr -= len + 1;
  1702. total_len += len + 1;
  1703. if (ptr < name)
  1704. goto out;
  1705. *(ptr + len) = '/';
  1706. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1707. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1708. break;
  1709. btrfs_release_path(path);
  1710. key.objectid = key.offset;
  1711. key.offset = (u64)-1;
  1712. dirid = key.objectid;
  1713. }
  1714. if (ptr < name)
  1715. goto out;
  1716. memmove(name, ptr, total_len);
  1717. name[total_len]='\0';
  1718. ret = 0;
  1719. out:
  1720. btrfs_free_path(path);
  1721. return ret;
  1722. }
  1723. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1724. void __user *argp)
  1725. {
  1726. struct btrfs_ioctl_ino_lookup_args *args;
  1727. struct inode *inode;
  1728. int ret;
  1729. if (!capable(CAP_SYS_ADMIN))
  1730. return -EPERM;
  1731. args = memdup_user(argp, sizeof(*args));
  1732. if (IS_ERR(args))
  1733. return PTR_ERR(args);
  1734. inode = fdentry(file)->d_inode;
  1735. if (args->treeid == 0)
  1736. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1737. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1738. args->treeid, args->objectid,
  1739. args->name);
  1740. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1741. ret = -EFAULT;
  1742. kfree(args);
  1743. return ret;
  1744. }
  1745. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1746. void __user *arg)
  1747. {
  1748. struct dentry *parent = fdentry(file);
  1749. struct dentry *dentry;
  1750. struct inode *dir = parent->d_inode;
  1751. struct inode *inode;
  1752. struct btrfs_root *root = BTRFS_I(dir)->root;
  1753. struct btrfs_root *dest = NULL;
  1754. struct btrfs_ioctl_vol_args *vol_args;
  1755. struct btrfs_trans_handle *trans;
  1756. int namelen;
  1757. int ret;
  1758. int err = 0;
  1759. vol_args = memdup_user(arg, sizeof(*vol_args));
  1760. if (IS_ERR(vol_args))
  1761. return PTR_ERR(vol_args);
  1762. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1763. namelen = strlen(vol_args->name);
  1764. if (strchr(vol_args->name, '/') ||
  1765. strncmp(vol_args->name, "..", namelen) == 0) {
  1766. err = -EINVAL;
  1767. goto out;
  1768. }
  1769. err = mnt_want_write_file(file);
  1770. if (err)
  1771. goto out;
  1772. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1773. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1774. if (IS_ERR(dentry)) {
  1775. err = PTR_ERR(dentry);
  1776. goto out_unlock_dir;
  1777. }
  1778. if (!dentry->d_inode) {
  1779. err = -ENOENT;
  1780. goto out_dput;
  1781. }
  1782. inode = dentry->d_inode;
  1783. dest = BTRFS_I(inode)->root;
  1784. if (!capable(CAP_SYS_ADMIN)){
  1785. /*
  1786. * Regular user. Only allow this with a special mount
  1787. * option, when the user has write+exec access to the
  1788. * subvol root, and when rmdir(2) would have been
  1789. * allowed.
  1790. *
  1791. * Note that this is _not_ check that the subvol is
  1792. * empty or doesn't contain data that we wouldn't
  1793. * otherwise be able to delete.
  1794. *
  1795. * Users who want to delete empty subvols should try
  1796. * rmdir(2).
  1797. */
  1798. err = -EPERM;
  1799. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1800. goto out_dput;
  1801. /*
  1802. * Do not allow deletion if the parent dir is the same
  1803. * as the dir to be deleted. That means the ioctl
  1804. * must be called on the dentry referencing the root
  1805. * of the subvol, not a random directory contained
  1806. * within it.
  1807. */
  1808. err = -EINVAL;
  1809. if (root == dest)
  1810. goto out_dput;
  1811. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1812. if (err)
  1813. goto out_dput;
  1814. }
  1815. /* check if subvolume may be deleted by a user */
  1816. err = btrfs_may_delete(dir, dentry, 1);
  1817. if (err)
  1818. goto out_dput;
  1819. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1820. err = -EINVAL;
  1821. goto out_dput;
  1822. }
  1823. mutex_lock(&inode->i_mutex);
  1824. err = d_invalidate(dentry);
  1825. if (err)
  1826. goto out_unlock;
  1827. down_write(&root->fs_info->subvol_sem);
  1828. err = may_destroy_subvol(dest);
  1829. if (err)
  1830. goto out_up_write;
  1831. trans = btrfs_start_transaction(root, 0);
  1832. if (IS_ERR(trans)) {
  1833. err = PTR_ERR(trans);
  1834. goto out_up_write;
  1835. }
  1836. trans->block_rsv = &root->fs_info->global_block_rsv;
  1837. ret = btrfs_unlink_subvol(trans, root, dir,
  1838. dest->root_key.objectid,
  1839. dentry->d_name.name,
  1840. dentry->d_name.len);
  1841. if (ret) {
  1842. err = ret;
  1843. btrfs_abort_transaction(trans, root, ret);
  1844. goto out_end_trans;
  1845. }
  1846. btrfs_record_root_in_trans(trans, dest);
  1847. memset(&dest->root_item.drop_progress, 0,
  1848. sizeof(dest->root_item.drop_progress));
  1849. dest->root_item.drop_level = 0;
  1850. btrfs_set_root_refs(&dest->root_item, 0);
  1851. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1852. ret = btrfs_insert_orphan_item(trans,
  1853. root->fs_info->tree_root,
  1854. dest->root_key.objectid);
  1855. if (ret) {
  1856. btrfs_abort_transaction(trans, root, ret);
  1857. err = ret;
  1858. goto out_end_trans;
  1859. }
  1860. }
  1861. out_end_trans:
  1862. ret = btrfs_end_transaction(trans, root);
  1863. if (ret && !err)
  1864. err = ret;
  1865. inode->i_flags |= S_DEAD;
  1866. out_up_write:
  1867. up_write(&root->fs_info->subvol_sem);
  1868. out_unlock:
  1869. mutex_unlock(&inode->i_mutex);
  1870. if (!err) {
  1871. shrink_dcache_sb(root->fs_info->sb);
  1872. btrfs_invalidate_inodes(dest);
  1873. d_delete(dentry);
  1874. }
  1875. out_dput:
  1876. dput(dentry);
  1877. out_unlock_dir:
  1878. mutex_unlock(&dir->i_mutex);
  1879. mnt_drop_write_file(file);
  1880. out:
  1881. kfree(vol_args);
  1882. return err;
  1883. }
  1884. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1885. {
  1886. struct inode *inode = fdentry(file)->d_inode;
  1887. struct btrfs_root *root = BTRFS_I(inode)->root;
  1888. struct btrfs_ioctl_defrag_range_args *range;
  1889. int ret;
  1890. ret = mnt_want_write_file(file);
  1891. if (ret)
  1892. return ret;
  1893. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1894. 1)) {
  1895. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1896. mnt_drop_write_file(file);
  1897. return -EINVAL;
  1898. }
  1899. if (btrfs_root_readonly(root)) {
  1900. ret = -EROFS;
  1901. goto out;
  1902. }
  1903. switch (inode->i_mode & S_IFMT) {
  1904. case S_IFDIR:
  1905. if (!capable(CAP_SYS_ADMIN)) {
  1906. ret = -EPERM;
  1907. goto out;
  1908. }
  1909. ret = btrfs_defrag_root(root, 0);
  1910. if (ret)
  1911. goto out;
  1912. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1913. break;
  1914. case S_IFREG:
  1915. if (!(file->f_mode & FMODE_WRITE)) {
  1916. ret = -EINVAL;
  1917. goto out;
  1918. }
  1919. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1920. if (!range) {
  1921. ret = -ENOMEM;
  1922. goto out;
  1923. }
  1924. if (argp) {
  1925. if (copy_from_user(range, argp,
  1926. sizeof(*range))) {
  1927. ret = -EFAULT;
  1928. kfree(range);
  1929. goto out;
  1930. }
  1931. /* compression requires us to start the IO */
  1932. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1933. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1934. range->extent_thresh = (u32)-1;
  1935. }
  1936. } else {
  1937. /* the rest are all set to zero by kzalloc */
  1938. range->len = (u64)-1;
  1939. }
  1940. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1941. range, 0, 0);
  1942. if (ret > 0)
  1943. ret = 0;
  1944. kfree(range);
  1945. break;
  1946. default:
  1947. ret = -EINVAL;
  1948. }
  1949. out:
  1950. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1951. mnt_drop_write_file(file);
  1952. return ret;
  1953. }
  1954. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1955. {
  1956. struct btrfs_ioctl_vol_args *vol_args;
  1957. int ret;
  1958. if (!capable(CAP_SYS_ADMIN))
  1959. return -EPERM;
  1960. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1961. 1)) {
  1962. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1963. return -EINVAL;
  1964. }
  1965. mutex_lock(&root->fs_info->volume_mutex);
  1966. vol_args = memdup_user(arg, sizeof(*vol_args));
  1967. if (IS_ERR(vol_args)) {
  1968. ret = PTR_ERR(vol_args);
  1969. goto out;
  1970. }
  1971. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1972. ret = btrfs_init_new_device(root, vol_args->name);
  1973. kfree(vol_args);
  1974. out:
  1975. mutex_unlock(&root->fs_info->volume_mutex);
  1976. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1977. return ret;
  1978. }
  1979. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  1980. {
  1981. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1982. struct btrfs_ioctl_vol_args *vol_args;
  1983. int ret;
  1984. if (!capable(CAP_SYS_ADMIN))
  1985. return -EPERM;
  1986. ret = mnt_want_write_file(file);
  1987. if (ret)
  1988. return ret;
  1989. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1990. 1)) {
  1991. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1992. mnt_drop_write_file(file);
  1993. return -EINVAL;
  1994. }
  1995. mutex_lock(&root->fs_info->volume_mutex);
  1996. vol_args = memdup_user(arg, sizeof(*vol_args));
  1997. if (IS_ERR(vol_args)) {
  1998. ret = PTR_ERR(vol_args);
  1999. goto out;
  2000. }
  2001. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2002. ret = btrfs_rm_device(root, vol_args->name);
  2003. kfree(vol_args);
  2004. out:
  2005. mutex_unlock(&root->fs_info->volume_mutex);
  2006. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2007. mnt_drop_write_file(file);
  2008. return ret;
  2009. }
  2010. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2011. {
  2012. struct btrfs_ioctl_fs_info_args *fi_args;
  2013. struct btrfs_device *device;
  2014. struct btrfs_device *next;
  2015. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2016. int ret = 0;
  2017. if (!capable(CAP_SYS_ADMIN))
  2018. return -EPERM;
  2019. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2020. if (!fi_args)
  2021. return -ENOMEM;
  2022. fi_args->num_devices = fs_devices->num_devices;
  2023. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2024. mutex_lock(&fs_devices->device_list_mutex);
  2025. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2026. if (device->devid > fi_args->max_id)
  2027. fi_args->max_id = device->devid;
  2028. }
  2029. mutex_unlock(&fs_devices->device_list_mutex);
  2030. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2031. ret = -EFAULT;
  2032. kfree(fi_args);
  2033. return ret;
  2034. }
  2035. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2036. {
  2037. struct btrfs_ioctl_dev_info_args *di_args;
  2038. struct btrfs_device *dev;
  2039. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2040. int ret = 0;
  2041. char *s_uuid = NULL;
  2042. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  2043. if (!capable(CAP_SYS_ADMIN))
  2044. return -EPERM;
  2045. di_args = memdup_user(arg, sizeof(*di_args));
  2046. if (IS_ERR(di_args))
  2047. return PTR_ERR(di_args);
  2048. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  2049. s_uuid = di_args->uuid;
  2050. mutex_lock(&fs_devices->device_list_mutex);
  2051. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2052. mutex_unlock(&fs_devices->device_list_mutex);
  2053. if (!dev) {
  2054. ret = -ENODEV;
  2055. goto out;
  2056. }
  2057. di_args->devid = dev->devid;
  2058. di_args->bytes_used = dev->bytes_used;
  2059. di_args->total_bytes = dev->total_bytes;
  2060. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2061. if (dev->name) {
  2062. struct rcu_string *name;
  2063. rcu_read_lock();
  2064. name = rcu_dereference(dev->name);
  2065. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2066. rcu_read_unlock();
  2067. di_args->path[sizeof(di_args->path) - 1] = 0;
  2068. } else {
  2069. di_args->path[0] = '\0';
  2070. }
  2071. out:
  2072. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2073. ret = -EFAULT;
  2074. kfree(di_args);
  2075. return ret;
  2076. }
  2077. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2078. u64 off, u64 olen, u64 destoff)
  2079. {
  2080. struct inode *inode = fdentry(file)->d_inode;
  2081. struct btrfs_root *root = BTRFS_I(inode)->root;
  2082. struct fd src_file;
  2083. struct inode *src;
  2084. struct btrfs_trans_handle *trans;
  2085. struct btrfs_path *path;
  2086. struct extent_buffer *leaf;
  2087. char *buf;
  2088. struct btrfs_key key;
  2089. u32 nritems;
  2090. int slot;
  2091. int ret;
  2092. u64 len = olen;
  2093. u64 bs = root->fs_info->sb->s_blocksize;
  2094. /*
  2095. * TODO:
  2096. * - split compressed inline extents. annoying: we need to
  2097. * decompress into destination's address_space (the file offset
  2098. * may change, so source mapping won't do), then recompress (or
  2099. * otherwise reinsert) a subrange.
  2100. * - allow ranges within the same file to be cloned (provided
  2101. * they don't overlap)?
  2102. */
  2103. /* the destination must be opened for writing */
  2104. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  2105. return -EINVAL;
  2106. if (btrfs_root_readonly(root))
  2107. return -EROFS;
  2108. ret = mnt_want_write_file(file);
  2109. if (ret)
  2110. return ret;
  2111. src_file = fdget(srcfd);
  2112. if (!src_file.file) {
  2113. ret = -EBADF;
  2114. goto out_drop_write;
  2115. }
  2116. ret = -EXDEV;
  2117. if (src_file.file->f_path.mnt != file->f_path.mnt)
  2118. goto out_fput;
  2119. src = src_file.file->f_dentry->d_inode;
  2120. ret = -EINVAL;
  2121. if (src == inode)
  2122. goto out_fput;
  2123. /* the src must be open for reading */
  2124. if (!(src_file.file->f_mode & FMODE_READ))
  2125. goto out_fput;
  2126. /* don't make the dst file partly checksummed */
  2127. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2128. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  2129. goto out_fput;
  2130. ret = -EISDIR;
  2131. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  2132. goto out_fput;
  2133. ret = -EXDEV;
  2134. if (src->i_sb != inode->i_sb)
  2135. goto out_fput;
  2136. ret = -ENOMEM;
  2137. buf = vmalloc(btrfs_level_size(root, 0));
  2138. if (!buf)
  2139. goto out_fput;
  2140. path = btrfs_alloc_path();
  2141. if (!path) {
  2142. vfree(buf);
  2143. goto out_fput;
  2144. }
  2145. path->reada = 2;
  2146. if (inode < src) {
  2147. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2148. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2149. } else {
  2150. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2151. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2152. }
  2153. /* determine range to clone */
  2154. ret = -EINVAL;
  2155. if (off + len > src->i_size || off + len < off)
  2156. goto out_unlock;
  2157. if (len == 0)
  2158. olen = len = src->i_size - off;
  2159. /* if we extend to eof, continue to block boundary */
  2160. if (off + len == src->i_size)
  2161. len = ALIGN(src->i_size, bs) - off;
  2162. /* verify the end result is block aligned */
  2163. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2164. !IS_ALIGNED(destoff, bs))
  2165. goto out_unlock;
  2166. if (destoff > inode->i_size) {
  2167. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2168. if (ret)
  2169. goto out_unlock;
  2170. }
  2171. /* truncate page cache pages from target inode range */
  2172. truncate_inode_pages_range(&inode->i_data, destoff,
  2173. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2174. /* do any pending delalloc/csum calc on src, one way or
  2175. another, and lock file content */
  2176. while (1) {
  2177. struct btrfs_ordered_extent *ordered;
  2178. lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2179. ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
  2180. if (!ordered &&
  2181. !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
  2182. EXTENT_DELALLOC, 0, NULL))
  2183. break;
  2184. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2185. if (ordered)
  2186. btrfs_put_ordered_extent(ordered);
  2187. btrfs_wait_ordered_range(src, off, len);
  2188. }
  2189. /* clone data */
  2190. key.objectid = btrfs_ino(src);
  2191. key.type = BTRFS_EXTENT_DATA_KEY;
  2192. key.offset = 0;
  2193. while (1) {
  2194. /*
  2195. * note the key will change type as we walk through the
  2196. * tree.
  2197. */
  2198. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2199. 0, 0);
  2200. if (ret < 0)
  2201. goto out;
  2202. nritems = btrfs_header_nritems(path->nodes[0]);
  2203. if (path->slots[0] >= nritems) {
  2204. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2205. if (ret < 0)
  2206. goto out;
  2207. if (ret > 0)
  2208. break;
  2209. nritems = btrfs_header_nritems(path->nodes[0]);
  2210. }
  2211. leaf = path->nodes[0];
  2212. slot = path->slots[0];
  2213. btrfs_item_key_to_cpu(leaf, &key, slot);
  2214. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2215. key.objectid != btrfs_ino(src))
  2216. break;
  2217. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2218. struct btrfs_file_extent_item *extent;
  2219. int type;
  2220. u32 size;
  2221. struct btrfs_key new_key;
  2222. u64 disko = 0, diskl = 0;
  2223. u64 datao = 0, datal = 0;
  2224. u8 comp;
  2225. u64 endoff;
  2226. size = btrfs_item_size_nr(leaf, slot);
  2227. read_extent_buffer(leaf, buf,
  2228. btrfs_item_ptr_offset(leaf, slot),
  2229. size);
  2230. extent = btrfs_item_ptr(leaf, slot,
  2231. struct btrfs_file_extent_item);
  2232. comp = btrfs_file_extent_compression(leaf, extent);
  2233. type = btrfs_file_extent_type(leaf, extent);
  2234. if (type == BTRFS_FILE_EXTENT_REG ||
  2235. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2236. disko = btrfs_file_extent_disk_bytenr(leaf,
  2237. extent);
  2238. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2239. extent);
  2240. datao = btrfs_file_extent_offset(leaf, extent);
  2241. datal = btrfs_file_extent_num_bytes(leaf,
  2242. extent);
  2243. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2244. /* take upper bound, may be compressed */
  2245. datal = btrfs_file_extent_ram_bytes(leaf,
  2246. extent);
  2247. }
  2248. btrfs_release_path(path);
  2249. if (key.offset + datal <= off ||
  2250. key.offset >= off + len - 1)
  2251. goto next;
  2252. memcpy(&new_key, &key, sizeof(new_key));
  2253. new_key.objectid = btrfs_ino(inode);
  2254. if (off <= key.offset)
  2255. new_key.offset = key.offset + destoff - off;
  2256. else
  2257. new_key.offset = destoff;
  2258. /*
  2259. * 1 - adjusting old extent (we may have to split it)
  2260. * 1 - add new extent
  2261. * 1 - inode update
  2262. */
  2263. trans = btrfs_start_transaction(root, 3);
  2264. if (IS_ERR(trans)) {
  2265. ret = PTR_ERR(trans);
  2266. goto out;
  2267. }
  2268. if (type == BTRFS_FILE_EXTENT_REG ||
  2269. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2270. /*
  2271. * a | --- range to clone ---| b
  2272. * | ------------- extent ------------- |
  2273. */
  2274. /* substract range b */
  2275. if (key.offset + datal > off + len)
  2276. datal = off + len - key.offset;
  2277. /* substract range a */
  2278. if (off > key.offset) {
  2279. datao += off - key.offset;
  2280. datal -= off - key.offset;
  2281. }
  2282. ret = btrfs_drop_extents(trans, root, inode,
  2283. new_key.offset,
  2284. new_key.offset + datal,
  2285. 1);
  2286. if (ret) {
  2287. btrfs_abort_transaction(trans, root,
  2288. ret);
  2289. btrfs_end_transaction(trans, root);
  2290. goto out;
  2291. }
  2292. ret = btrfs_insert_empty_item(trans, root, path,
  2293. &new_key, size);
  2294. if (ret) {
  2295. btrfs_abort_transaction(trans, root,
  2296. ret);
  2297. btrfs_end_transaction(trans, root);
  2298. goto out;
  2299. }
  2300. leaf = path->nodes[0];
  2301. slot = path->slots[0];
  2302. write_extent_buffer(leaf, buf,
  2303. btrfs_item_ptr_offset(leaf, slot),
  2304. size);
  2305. extent = btrfs_item_ptr(leaf, slot,
  2306. struct btrfs_file_extent_item);
  2307. /* disko == 0 means it's a hole */
  2308. if (!disko)
  2309. datao = 0;
  2310. btrfs_set_file_extent_offset(leaf, extent,
  2311. datao);
  2312. btrfs_set_file_extent_num_bytes(leaf, extent,
  2313. datal);
  2314. if (disko) {
  2315. inode_add_bytes(inode, datal);
  2316. ret = btrfs_inc_extent_ref(trans, root,
  2317. disko, diskl, 0,
  2318. root->root_key.objectid,
  2319. btrfs_ino(inode),
  2320. new_key.offset - datao,
  2321. 0);
  2322. if (ret) {
  2323. btrfs_abort_transaction(trans,
  2324. root,
  2325. ret);
  2326. btrfs_end_transaction(trans,
  2327. root);
  2328. goto out;
  2329. }
  2330. }
  2331. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2332. u64 skip = 0;
  2333. u64 trim = 0;
  2334. if (off > key.offset) {
  2335. skip = off - key.offset;
  2336. new_key.offset += skip;
  2337. }
  2338. if (key.offset + datal > off + len)
  2339. trim = key.offset + datal - (off + len);
  2340. if (comp && (skip || trim)) {
  2341. ret = -EINVAL;
  2342. btrfs_end_transaction(trans, root);
  2343. goto out;
  2344. }
  2345. size -= skip + trim;
  2346. datal -= skip + trim;
  2347. ret = btrfs_drop_extents(trans, root, inode,
  2348. new_key.offset,
  2349. new_key.offset + datal,
  2350. 1);
  2351. if (ret) {
  2352. btrfs_abort_transaction(trans, root,
  2353. ret);
  2354. btrfs_end_transaction(trans, root);
  2355. goto out;
  2356. }
  2357. ret = btrfs_insert_empty_item(trans, root, path,
  2358. &new_key, size);
  2359. if (ret) {
  2360. btrfs_abort_transaction(trans, root,
  2361. ret);
  2362. btrfs_end_transaction(trans, root);
  2363. goto out;
  2364. }
  2365. if (skip) {
  2366. u32 start =
  2367. btrfs_file_extent_calc_inline_size(0);
  2368. memmove(buf+start, buf+start+skip,
  2369. datal);
  2370. }
  2371. leaf = path->nodes[0];
  2372. slot = path->slots[0];
  2373. write_extent_buffer(leaf, buf,
  2374. btrfs_item_ptr_offset(leaf, slot),
  2375. size);
  2376. inode_add_bytes(inode, datal);
  2377. }
  2378. btrfs_mark_buffer_dirty(leaf);
  2379. btrfs_release_path(path);
  2380. inode_inc_iversion(inode);
  2381. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2382. /*
  2383. * we round up to the block size at eof when
  2384. * determining which extents to clone above,
  2385. * but shouldn't round up the file size
  2386. */
  2387. endoff = new_key.offset + datal;
  2388. if (endoff > destoff+olen)
  2389. endoff = destoff+olen;
  2390. if (endoff > inode->i_size)
  2391. btrfs_i_size_write(inode, endoff);
  2392. ret = btrfs_update_inode(trans, root, inode);
  2393. if (ret) {
  2394. btrfs_abort_transaction(trans, root, ret);
  2395. btrfs_end_transaction(trans, root);
  2396. goto out;
  2397. }
  2398. ret = btrfs_end_transaction(trans, root);
  2399. }
  2400. next:
  2401. btrfs_release_path(path);
  2402. key.offset++;
  2403. }
  2404. ret = 0;
  2405. out:
  2406. btrfs_release_path(path);
  2407. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2408. out_unlock:
  2409. mutex_unlock(&src->i_mutex);
  2410. mutex_unlock(&inode->i_mutex);
  2411. vfree(buf);
  2412. btrfs_free_path(path);
  2413. out_fput:
  2414. fdput(src_file);
  2415. out_drop_write:
  2416. mnt_drop_write_file(file);
  2417. return ret;
  2418. }
  2419. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2420. {
  2421. struct btrfs_ioctl_clone_range_args args;
  2422. if (copy_from_user(&args, argp, sizeof(args)))
  2423. return -EFAULT;
  2424. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2425. args.src_length, args.dest_offset);
  2426. }
  2427. /*
  2428. * there are many ways the trans_start and trans_end ioctls can lead
  2429. * to deadlocks. They should only be used by applications that
  2430. * basically own the machine, and have a very in depth understanding
  2431. * of all the possible deadlocks and enospc problems.
  2432. */
  2433. static long btrfs_ioctl_trans_start(struct file *file)
  2434. {
  2435. struct inode *inode = fdentry(file)->d_inode;
  2436. struct btrfs_root *root = BTRFS_I(inode)->root;
  2437. struct btrfs_trans_handle *trans;
  2438. int ret;
  2439. ret = -EPERM;
  2440. if (!capable(CAP_SYS_ADMIN))
  2441. goto out;
  2442. ret = -EINPROGRESS;
  2443. if (file->private_data)
  2444. goto out;
  2445. ret = -EROFS;
  2446. if (btrfs_root_readonly(root))
  2447. goto out;
  2448. ret = mnt_want_write_file(file);
  2449. if (ret)
  2450. goto out;
  2451. atomic_inc(&root->fs_info->open_ioctl_trans);
  2452. ret = -ENOMEM;
  2453. trans = btrfs_start_ioctl_transaction(root);
  2454. if (IS_ERR(trans))
  2455. goto out_drop;
  2456. file->private_data = trans;
  2457. return 0;
  2458. out_drop:
  2459. atomic_dec(&root->fs_info->open_ioctl_trans);
  2460. mnt_drop_write_file(file);
  2461. out:
  2462. return ret;
  2463. }
  2464. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2465. {
  2466. struct inode *inode = fdentry(file)->d_inode;
  2467. struct btrfs_root *root = BTRFS_I(inode)->root;
  2468. struct btrfs_root *new_root;
  2469. struct btrfs_dir_item *di;
  2470. struct btrfs_trans_handle *trans;
  2471. struct btrfs_path *path;
  2472. struct btrfs_key location;
  2473. struct btrfs_disk_key disk_key;
  2474. u64 objectid = 0;
  2475. u64 dir_id;
  2476. int ret;
  2477. if (!capable(CAP_SYS_ADMIN))
  2478. return -EPERM;
  2479. ret = mnt_want_write_file(file);
  2480. if (ret)
  2481. return ret;
  2482. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2483. ret = -EFAULT;
  2484. goto out;
  2485. }
  2486. if (!objectid)
  2487. objectid = root->root_key.objectid;
  2488. location.objectid = objectid;
  2489. location.type = BTRFS_ROOT_ITEM_KEY;
  2490. location.offset = (u64)-1;
  2491. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2492. if (IS_ERR(new_root)) {
  2493. ret = PTR_ERR(new_root);
  2494. goto out;
  2495. }
  2496. if (btrfs_root_refs(&new_root->root_item) == 0) {
  2497. ret = -ENOENT;
  2498. goto out;
  2499. }
  2500. path = btrfs_alloc_path();
  2501. if (!path) {
  2502. ret = -ENOMEM;
  2503. goto out;
  2504. }
  2505. path->leave_spinning = 1;
  2506. trans = btrfs_start_transaction(root, 1);
  2507. if (IS_ERR(trans)) {
  2508. btrfs_free_path(path);
  2509. ret = PTR_ERR(trans);
  2510. goto out;
  2511. }
  2512. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2513. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2514. dir_id, "default", 7, 1);
  2515. if (IS_ERR_OR_NULL(di)) {
  2516. btrfs_free_path(path);
  2517. btrfs_end_transaction(trans, root);
  2518. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2519. "this isn't going to work\n");
  2520. ret = -ENOENT;
  2521. goto out;
  2522. }
  2523. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2524. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2525. btrfs_mark_buffer_dirty(path->nodes[0]);
  2526. btrfs_free_path(path);
  2527. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2528. btrfs_end_transaction(trans, root);
  2529. out:
  2530. mnt_drop_write_file(file);
  2531. return ret;
  2532. }
  2533. void btrfs_get_block_group_info(struct list_head *groups_list,
  2534. struct btrfs_ioctl_space_info *space)
  2535. {
  2536. struct btrfs_block_group_cache *block_group;
  2537. space->total_bytes = 0;
  2538. space->used_bytes = 0;
  2539. space->flags = 0;
  2540. list_for_each_entry(block_group, groups_list, list) {
  2541. space->flags = block_group->flags;
  2542. space->total_bytes += block_group->key.offset;
  2543. space->used_bytes +=
  2544. btrfs_block_group_used(&block_group->item);
  2545. }
  2546. }
  2547. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2548. {
  2549. struct btrfs_ioctl_space_args space_args;
  2550. struct btrfs_ioctl_space_info space;
  2551. struct btrfs_ioctl_space_info *dest;
  2552. struct btrfs_ioctl_space_info *dest_orig;
  2553. struct btrfs_ioctl_space_info __user *user_dest;
  2554. struct btrfs_space_info *info;
  2555. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2556. BTRFS_BLOCK_GROUP_SYSTEM,
  2557. BTRFS_BLOCK_GROUP_METADATA,
  2558. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2559. int num_types = 4;
  2560. int alloc_size;
  2561. int ret = 0;
  2562. u64 slot_count = 0;
  2563. int i, c;
  2564. if (copy_from_user(&space_args,
  2565. (struct btrfs_ioctl_space_args __user *)arg,
  2566. sizeof(space_args)))
  2567. return -EFAULT;
  2568. for (i = 0; i < num_types; i++) {
  2569. struct btrfs_space_info *tmp;
  2570. info = NULL;
  2571. rcu_read_lock();
  2572. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2573. list) {
  2574. if (tmp->flags == types[i]) {
  2575. info = tmp;
  2576. break;
  2577. }
  2578. }
  2579. rcu_read_unlock();
  2580. if (!info)
  2581. continue;
  2582. down_read(&info->groups_sem);
  2583. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2584. if (!list_empty(&info->block_groups[c]))
  2585. slot_count++;
  2586. }
  2587. up_read(&info->groups_sem);
  2588. }
  2589. /* space_slots == 0 means they are asking for a count */
  2590. if (space_args.space_slots == 0) {
  2591. space_args.total_spaces = slot_count;
  2592. goto out;
  2593. }
  2594. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2595. alloc_size = sizeof(*dest) * slot_count;
  2596. /* we generally have at most 6 or so space infos, one for each raid
  2597. * level. So, a whole page should be more than enough for everyone
  2598. */
  2599. if (alloc_size > PAGE_CACHE_SIZE)
  2600. return -ENOMEM;
  2601. space_args.total_spaces = 0;
  2602. dest = kmalloc(alloc_size, GFP_NOFS);
  2603. if (!dest)
  2604. return -ENOMEM;
  2605. dest_orig = dest;
  2606. /* now we have a buffer to copy into */
  2607. for (i = 0; i < num_types; i++) {
  2608. struct btrfs_space_info *tmp;
  2609. if (!slot_count)
  2610. break;
  2611. info = NULL;
  2612. rcu_read_lock();
  2613. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2614. list) {
  2615. if (tmp->flags == types[i]) {
  2616. info = tmp;
  2617. break;
  2618. }
  2619. }
  2620. rcu_read_unlock();
  2621. if (!info)
  2622. continue;
  2623. down_read(&info->groups_sem);
  2624. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2625. if (!list_empty(&info->block_groups[c])) {
  2626. btrfs_get_block_group_info(
  2627. &info->block_groups[c], &space);
  2628. memcpy(dest, &space, sizeof(space));
  2629. dest++;
  2630. space_args.total_spaces++;
  2631. slot_count--;
  2632. }
  2633. if (!slot_count)
  2634. break;
  2635. }
  2636. up_read(&info->groups_sem);
  2637. }
  2638. user_dest = (struct btrfs_ioctl_space_info __user *)
  2639. (arg + sizeof(struct btrfs_ioctl_space_args));
  2640. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2641. ret = -EFAULT;
  2642. kfree(dest_orig);
  2643. out:
  2644. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2645. ret = -EFAULT;
  2646. return ret;
  2647. }
  2648. /*
  2649. * there are many ways the trans_start and trans_end ioctls can lead
  2650. * to deadlocks. They should only be used by applications that
  2651. * basically own the machine, and have a very in depth understanding
  2652. * of all the possible deadlocks and enospc problems.
  2653. */
  2654. long btrfs_ioctl_trans_end(struct file *file)
  2655. {
  2656. struct inode *inode = fdentry(file)->d_inode;
  2657. struct btrfs_root *root = BTRFS_I(inode)->root;
  2658. struct btrfs_trans_handle *trans;
  2659. trans = file->private_data;
  2660. if (!trans)
  2661. return -EINVAL;
  2662. file->private_data = NULL;
  2663. btrfs_end_transaction(trans, root);
  2664. atomic_dec(&root->fs_info->open_ioctl_trans);
  2665. mnt_drop_write_file(file);
  2666. return 0;
  2667. }
  2668. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  2669. void __user *argp)
  2670. {
  2671. struct btrfs_trans_handle *trans;
  2672. u64 transid;
  2673. int ret;
  2674. trans = btrfs_attach_transaction(root);
  2675. if (IS_ERR(trans)) {
  2676. if (PTR_ERR(trans) != -ENOENT)
  2677. return PTR_ERR(trans);
  2678. /* No running transaction, don't bother */
  2679. transid = root->fs_info->last_trans_committed;
  2680. goto out;
  2681. }
  2682. transid = trans->transid;
  2683. ret = btrfs_commit_transaction_async(trans, root, 0);
  2684. if (ret) {
  2685. btrfs_end_transaction(trans, root);
  2686. return ret;
  2687. }
  2688. out:
  2689. if (argp)
  2690. if (copy_to_user(argp, &transid, sizeof(transid)))
  2691. return -EFAULT;
  2692. return 0;
  2693. }
  2694. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  2695. void __user *argp)
  2696. {
  2697. u64 transid;
  2698. if (argp) {
  2699. if (copy_from_user(&transid, argp, sizeof(transid)))
  2700. return -EFAULT;
  2701. } else {
  2702. transid = 0; /* current trans */
  2703. }
  2704. return btrfs_wait_for_commit(root, transid);
  2705. }
  2706. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  2707. {
  2708. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2709. struct btrfs_ioctl_scrub_args *sa;
  2710. int ret;
  2711. if (!capable(CAP_SYS_ADMIN))
  2712. return -EPERM;
  2713. sa = memdup_user(arg, sizeof(*sa));
  2714. if (IS_ERR(sa))
  2715. return PTR_ERR(sa);
  2716. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  2717. ret = mnt_want_write_file(file);
  2718. if (ret)
  2719. goto out;
  2720. }
  2721. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  2722. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  2723. 0);
  2724. if (copy_to_user(arg, sa, sizeof(*sa)))
  2725. ret = -EFAULT;
  2726. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  2727. mnt_drop_write_file(file);
  2728. out:
  2729. kfree(sa);
  2730. return ret;
  2731. }
  2732. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2733. {
  2734. if (!capable(CAP_SYS_ADMIN))
  2735. return -EPERM;
  2736. return btrfs_scrub_cancel(root->fs_info);
  2737. }
  2738. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2739. void __user *arg)
  2740. {
  2741. struct btrfs_ioctl_scrub_args *sa;
  2742. int ret;
  2743. if (!capable(CAP_SYS_ADMIN))
  2744. return -EPERM;
  2745. sa = memdup_user(arg, sizeof(*sa));
  2746. if (IS_ERR(sa))
  2747. return PTR_ERR(sa);
  2748. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2749. if (copy_to_user(arg, sa, sizeof(*sa)))
  2750. ret = -EFAULT;
  2751. kfree(sa);
  2752. return ret;
  2753. }
  2754. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  2755. void __user *arg)
  2756. {
  2757. struct btrfs_ioctl_get_dev_stats *sa;
  2758. int ret;
  2759. sa = memdup_user(arg, sizeof(*sa));
  2760. if (IS_ERR(sa))
  2761. return PTR_ERR(sa);
  2762. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  2763. kfree(sa);
  2764. return -EPERM;
  2765. }
  2766. ret = btrfs_get_dev_stats(root, sa);
  2767. if (copy_to_user(arg, sa, sizeof(*sa)))
  2768. ret = -EFAULT;
  2769. kfree(sa);
  2770. return ret;
  2771. }
  2772. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  2773. {
  2774. struct btrfs_ioctl_dev_replace_args *p;
  2775. int ret;
  2776. if (!capable(CAP_SYS_ADMIN))
  2777. return -EPERM;
  2778. p = memdup_user(arg, sizeof(*p));
  2779. if (IS_ERR(p))
  2780. return PTR_ERR(p);
  2781. switch (p->cmd) {
  2782. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  2783. if (atomic_xchg(
  2784. &root->fs_info->mutually_exclusive_operation_running,
  2785. 1)) {
  2786. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2787. ret = -EINPROGRESS;
  2788. } else {
  2789. ret = btrfs_dev_replace_start(root, p);
  2790. atomic_set(
  2791. &root->fs_info->mutually_exclusive_operation_running,
  2792. 0);
  2793. }
  2794. break;
  2795. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  2796. btrfs_dev_replace_status(root->fs_info, p);
  2797. ret = 0;
  2798. break;
  2799. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  2800. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  2801. break;
  2802. default:
  2803. ret = -EINVAL;
  2804. break;
  2805. }
  2806. if (copy_to_user(arg, p, sizeof(*p)))
  2807. ret = -EFAULT;
  2808. kfree(p);
  2809. return ret;
  2810. }
  2811. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  2812. {
  2813. int ret = 0;
  2814. int i;
  2815. u64 rel_ptr;
  2816. int size;
  2817. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  2818. struct inode_fs_paths *ipath = NULL;
  2819. struct btrfs_path *path;
  2820. if (!capable(CAP_SYS_ADMIN))
  2821. return -EPERM;
  2822. path = btrfs_alloc_path();
  2823. if (!path) {
  2824. ret = -ENOMEM;
  2825. goto out;
  2826. }
  2827. ipa = memdup_user(arg, sizeof(*ipa));
  2828. if (IS_ERR(ipa)) {
  2829. ret = PTR_ERR(ipa);
  2830. ipa = NULL;
  2831. goto out;
  2832. }
  2833. size = min_t(u32, ipa->size, 4096);
  2834. ipath = init_ipath(size, root, path);
  2835. if (IS_ERR(ipath)) {
  2836. ret = PTR_ERR(ipath);
  2837. ipath = NULL;
  2838. goto out;
  2839. }
  2840. ret = paths_from_inode(ipa->inum, ipath);
  2841. if (ret < 0)
  2842. goto out;
  2843. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  2844. rel_ptr = ipath->fspath->val[i] -
  2845. (u64)(unsigned long)ipath->fspath->val;
  2846. ipath->fspath->val[i] = rel_ptr;
  2847. }
  2848. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  2849. (void *)(unsigned long)ipath->fspath, size);
  2850. if (ret) {
  2851. ret = -EFAULT;
  2852. goto out;
  2853. }
  2854. out:
  2855. btrfs_free_path(path);
  2856. free_ipath(ipath);
  2857. kfree(ipa);
  2858. return ret;
  2859. }
  2860. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  2861. {
  2862. struct btrfs_data_container *inodes = ctx;
  2863. const size_t c = 3 * sizeof(u64);
  2864. if (inodes->bytes_left >= c) {
  2865. inodes->bytes_left -= c;
  2866. inodes->val[inodes->elem_cnt] = inum;
  2867. inodes->val[inodes->elem_cnt + 1] = offset;
  2868. inodes->val[inodes->elem_cnt + 2] = root;
  2869. inodes->elem_cnt += 3;
  2870. } else {
  2871. inodes->bytes_missing += c - inodes->bytes_left;
  2872. inodes->bytes_left = 0;
  2873. inodes->elem_missed += 3;
  2874. }
  2875. return 0;
  2876. }
  2877. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  2878. void __user *arg)
  2879. {
  2880. int ret = 0;
  2881. int size;
  2882. struct btrfs_ioctl_logical_ino_args *loi;
  2883. struct btrfs_data_container *inodes = NULL;
  2884. struct btrfs_path *path = NULL;
  2885. if (!capable(CAP_SYS_ADMIN))
  2886. return -EPERM;
  2887. loi = memdup_user(arg, sizeof(*loi));
  2888. if (IS_ERR(loi)) {
  2889. ret = PTR_ERR(loi);
  2890. loi = NULL;
  2891. goto out;
  2892. }
  2893. path = btrfs_alloc_path();
  2894. if (!path) {
  2895. ret = -ENOMEM;
  2896. goto out;
  2897. }
  2898. size = min_t(u32, loi->size, 64 * 1024);
  2899. inodes = init_data_container(size);
  2900. if (IS_ERR(inodes)) {
  2901. ret = PTR_ERR(inodes);
  2902. inodes = NULL;
  2903. goto out;
  2904. }
  2905. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  2906. build_ino_list, inodes);
  2907. if (ret == -EINVAL)
  2908. ret = -ENOENT;
  2909. if (ret < 0)
  2910. goto out;
  2911. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  2912. (void *)(unsigned long)inodes, size);
  2913. if (ret)
  2914. ret = -EFAULT;
  2915. out:
  2916. btrfs_free_path(path);
  2917. vfree(inodes);
  2918. kfree(loi);
  2919. return ret;
  2920. }
  2921. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2922. struct btrfs_ioctl_balance_args *bargs)
  2923. {
  2924. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2925. bargs->flags = bctl->flags;
  2926. if (atomic_read(&fs_info->balance_running))
  2927. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  2928. if (atomic_read(&fs_info->balance_pause_req))
  2929. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  2930. if (atomic_read(&fs_info->balance_cancel_req))
  2931. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  2932. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  2933. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  2934. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  2935. if (lock) {
  2936. spin_lock(&fs_info->balance_lock);
  2937. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2938. spin_unlock(&fs_info->balance_lock);
  2939. } else {
  2940. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2941. }
  2942. }
  2943. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  2944. {
  2945. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2946. struct btrfs_fs_info *fs_info = root->fs_info;
  2947. struct btrfs_ioctl_balance_args *bargs;
  2948. struct btrfs_balance_control *bctl;
  2949. bool need_unlock; /* for mut. excl. ops lock */
  2950. int ret;
  2951. if (!capable(CAP_SYS_ADMIN))
  2952. return -EPERM;
  2953. ret = mnt_want_write_file(file);
  2954. if (ret)
  2955. return ret;
  2956. again:
  2957. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  2958. mutex_lock(&fs_info->volume_mutex);
  2959. mutex_lock(&fs_info->balance_mutex);
  2960. need_unlock = true;
  2961. goto locked;
  2962. }
  2963. /*
  2964. * mut. excl. ops lock is locked. Three possibilites:
  2965. * (1) some other op is running
  2966. * (2) balance is running
  2967. * (3) balance is paused -- special case (think resume)
  2968. */
  2969. mutex_lock(&fs_info->balance_mutex);
  2970. if (fs_info->balance_ctl) {
  2971. /* this is either (2) or (3) */
  2972. if (!atomic_read(&fs_info->balance_running)) {
  2973. mutex_unlock(&fs_info->balance_mutex);
  2974. if (!mutex_trylock(&fs_info->volume_mutex))
  2975. goto again;
  2976. mutex_lock(&fs_info->balance_mutex);
  2977. if (fs_info->balance_ctl &&
  2978. !atomic_read(&fs_info->balance_running)) {
  2979. /* this is (3) */
  2980. need_unlock = false;
  2981. goto locked;
  2982. }
  2983. mutex_unlock(&fs_info->balance_mutex);
  2984. mutex_unlock(&fs_info->volume_mutex);
  2985. goto again;
  2986. } else {
  2987. /* this is (2) */
  2988. mutex_unlock(&fs_info->balance_mutex);
  2989. ret = -EINPROGRESS;
  2990. goto out;
  2991. }
  2992. } else {
  2993. /* this is (1) */
  2994. mutex_unlock(&fs_info->balance_mutex);
  2995. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2996. ret = -EINVAL;
  2997. goto out;
  2998. }
  2999. locked:
  3000. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3001. if (arg) {
  3002. bargs = memdup_user(arg, sizeof(*bargs));
  3003. if (IS_ERR(bargs)) {
  3004. ret = PTR_ERR(bargs);
  3005. goto out_unlock;
  3006. }
  3007. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3008. if (!fs_info->balance_ctl) {
  3009. ret = -ENOTCONN;
  3010. goto out_bargs;
  3011. }
  3012. bctl = fs_info->balance_ctl;
  3013. spin_lock(&fs_info->balance_lock);
  3014. bctl->flags |= BTRFS_BALANCE_RESUME;
  3015. spin_unlock(&fs_info->balance_lock);
  3016. goto do_balance;
  3017. }
  3018. } else {
  3019. bargs = NULL;
  3020. }
  3021. if (fs_info->balance_ctl) {
  3022. ret = -EINPROGRESS;
  3023. goto out_bargs;
  3024. }
  3025. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3026. if (!bctl) {
  3027. ret = -ENOMEM;
  3028. goto out_bargs;
  3029. }
  3030. bctl->fs_info = fs_info;
  3031. if (arg) {
  3032. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3033. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3034. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3035. bctl->flags = bargs->flags;
  3036. } else {
  3037. /* balance everything - no filters */
  3038. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3039. }
  3040. do_balance:
  3041. /*
  3042. * Ownership of bctl and mutually_exclusive_operation_running
  3043. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3044. * or, if restriper was paused all the way until unmount, in
  3045. * free_fs_info. mutually_exclusive_operation_running is
  3046. * cleared in __cancel_balance.
  3047. */
  3048. need_unlock = false;
  3049. ret = btrfs_balance(bctl, bargs);
  3050. if (arg) {
  3051. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3052. ret = -EFAULT;
  3053. }
  3054. out_bargs:
  3055. kfree(bargs);
  3056. out_unlock:
  3057. mutex_unlock(&fs_info->balance_mutex);
  3058. mutex_unlock(&fs_info->volume_mutex);
  3059. if (need_unlock)
  3060. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3061. out:
  3062. mnt_drop_write_file(file);
  3063. return ret;
  3064. }
  3065. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3066. {
  3067. if (!capable(CAP_SYS_ADMIN))
  3068. return -EPERM;
  3069. switch (cmd) {
  3070. case BTRFS_BALANCE_CTL_PAUSE:
  3071. return btrfs_pause_balance(root->fs_info);
  3072. case BTRFS_BALANCE_CTL_CANCEL:
  3073. return btrfs_cancel_balance(root->fs_info);
  3074. }
  3075. return -EINVAL;
  3076. }
  3077. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3078. void __user *arg)
  3079. {
  3080. struct btrfs_fs_info *fs_info = root->fs_info;
  3081. struct btrfs_ioctl_balance_args *bargs;
  3082. int ret = 0;
  3083. if (!capable(CAP_SYS_ADMIN))
  3084. return -EPERM;
  3085. mutex_lock(&fs_info->balance_mutex);
  3086. if (!fs_info->balance_ctl) {
  3087. ret = -ENOTCONN;
  3088. goto out;
  3089. }
  3090. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3091. if (!bargs) {
  3092. ret = -ENOMEM;
  3093. goto out;
  3094. }
  3095. update_ioctl_balance_args(fs_info, 1, bargs);
  3096. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3097. ret = -EFAULT;
  3098. kfree(bargs);
  3099. out:
  3100. mutex_unlock(&fs_info->balance_mutex);
  3101. return ret;
  3102. }
  3103. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3104. {
  3105. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3106. struct btrfs_ioctl_quota_ctl_args *sa;
  3107. struct btrfs_trans_handle *trans = NULL;
  3108. int ret;
  3109. int err;
  3110. if (!capable(CAP_SYS_ADMIN))
  3111. return -EPERM;
  3112. ret = mnt_want_write_file(file);
  3113. if (ret)
  3114. return ret;
  3115. sa = memdup_user(arg, sizeof(*sa));
  3116. if (IS_ERR(sa)) {
  3117. ret = PTR_ERR(sa);
  3118. goto drop_write;
  3119. }
  3120. if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
  3121. trans = btrfs_start_transaction(root, 2);
  3122. if (IS_ERR(trans)) {
  3123. ret = PTR_ERR(trans);
  3124. goto out;
  3125. }
  3126. }
  3127. switch (sa->cmd) {
  3128. case BTRFS_QUOTA_CTL_ENABLE:
  3129. ret = btrfs_quota_enable(trans, root->fs_info);
  3130. break;
  3131. case BTRFS_QUOTA_CTL_DISABLE:
  3132. ret = btrfs_quota_disable(trans, root->fs_info);
  3133. break;
  3134. case BTRFS_QUOTA_CTL_RESCAN:
  3135. ret = btrfs_quota_rescan(root->fs_info);
  3136. break;
  3137. default:
  3138. ret = -EINVAL;
  3139. break;
  3140. }
  3141. if (copy_to_user(arg, sa, sizeof(*sa)))
  3142. ret = -EFAULT;
  3143. if (trans) {
  3144. err = btrfs_commit_transaction(trans, root);
  3145. if (err && !ret)
  3146. ret = err;
  3147. }
  3148. out:
  3149. kfree(sa);
  3150. drop_write:
  3151. mnt_drop_write_file(file);
  3152. return ret;
  3153. }
  3154. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3155. {
  3156. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3157. struct btrfs_ioctl_qgroup_assign_args *sa;
  3158. struct btrfs_trans_handle *trans;
  3159. int ret;
  3160. int err;
  3161. if (!capable(CAP_SYS_ADMIN))
  3162. return -EPERM;
  3163. ret = mnt_want_write_file(file);
  3164. if (ret)
  3165. return ret;
  3166. sa = memdup_user(arg, sizeof(*sa));
  3167. if (IS_ERR(sa)) {
  3168. ret = PTR_ERR(sa);
  3169. goto drop_write;
  3170. }
  3171. trans = btrfs_join_transaction(root);
  3172. if (IS_ERR(trans)) {
  3173. ret = PTR_ERR(trans);
  3174. goto out;
  3175. }
  3176. /* FIXME: check if the IDs really exist */
  3177. if (sa->assign) {
  3178. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3179. sa->src, sa->dst);
  3180. } else {
  3181. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3182. sa->src, sa->dst);
  3183. }
  3184. err = btrfs_end_transaction(trans, root);
  3185. if (err && !ret)
  3186. ret = err;
  3187. out:
  3188. kfree(sa);
  3189. drop_write:
  3190. mnt_drop_write_file(file);
  3191. return ret;
  3192. }
  3193. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3194. {
  3195. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3196. struct btrfs_ioctl_qgroup_create_args *sa;
  3197. struct btrfs_trans_handle *trans;
  3198. int ret;
  3199. int err;
  3200. if (!capable(CAP_SYS_ADMIN))
  3201. return -EPERM;
  3202. ret = mnt_want_write_file(file);
  3203. if (ret)
  3204. return ret;
  3205. sa = memdup_user(arg, sizeof(*sa));
  3206. if (IS_ERR(sa)) {
  3207. ret = PTR_ERR(sa);
  3208. goto drop_write;
  3209. }
  3210. if (!sa->qgroupid) {
  3211. ret = -EINVAL;
  3212. goto out;
  3213. }
  3214. trans = btrfs_join_transaction(root);
  3215. if (IS_ERR(trans)) {
  3216. ret = PTR_ERR(trans);
  3217. goto out;
  3218. }
  3219. /* FIXME: check if the IDs really exist */
  3220. if (sa->create) {
  3221. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3222. NULL);
  3223. } else {
  3224. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3225. }
  3226. err = btrfs_end_transaction(trans, root);
  3227. if (err && !ret)
  3228. ret = err;
  3229. out:
  3230. kfree(sa);
  3231. drop_write:
  3232. mnt_drop_write_file(file);
  3233. return ret;
  3234. }
  3235. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3236. {
  3237. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3238. struct btrfs_ioctl_qgroup_limit_args *sa;
  3239. struct btrfs_trans_handle *trans;
  3240. int ret;
  3241. int err;
  3242. u64 qgroupid;
  3243. if (!capable(CAP_SYS_ADMIN))
  3244. return -EPERM;
  3245. ret = mnt_want_write_file(file);
  3246. if (ret)
  3247. return ret;
  3248. sa = memdup_user(arg, sizeof(*sa));
  3249. if (IS_ERR(sa)) {
  3250. ret = PTR_ERR(sa);
  3251. goto drop_write;
  3252. }
  3253. trans = btrfs_join_transaction(root);
  3254. if (IS_ERR(trans)) {
  3255. ret = PTR_ERR(trans);
  3256. goto out;
  3257. }
  3258. qgroupid = sa->qgroupid;
  3259. if (!qgroupid) {
  3260. /* take the current subvol as qgroup */
  3261. qgroupid = root->root_key.objectid;
  3262. }
  3263. /* FIXME: check if the IDs really exist */
  3264. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3265. err = btrfs_end_transaction(trans, root);
  3266. if (err && !ret)
  3267. ret = err;
  3268. out:
  3269. kfree(sa);
  3270. drop_write:
  3271. mnt_drop_write_file(file);
  3272. return ret;
  3273. }
  3274. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3275. void __user *arg)
  3276. {
  3277. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3278. struct inode *inode = fdentry(file)->d_inode;
  3279. struct btrfs_root *root = BTRFS_I(inode)->root;
  3280. struct btrfs_root_item *root_item = &root->root_item;
  3281. struct btrfs_trans_handle *trans;
  3282. struct timespec ct = CURRENT_TIME;
  3283. int ret = 0;
  3284. ret = mnt_want_write_file(file);
  3285. if (ret < 0)
  3286. return ret;
  3287. down_write(&root->fs_info->subvol_sem);
  3288. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3289. ret = -EINVAL;
  3290. goto out;
  3291. }
  3292. if (btrfs_root_readonly(root)) {
  3293. ret = -EROFS;
  3294. goto out;
  3295. }
  3296. if (!inode_owner_or_capable(inode)) {
  3297. ret = -EACCES;
  3298. goto out;
  3299. }
  3300. sa = memdup_user(arg, sizeof(*sa));
  3301. if (IS_ERR(sa)) {
  3302. ret = PTR_ERR(sa);
  3303. sa = NULL;
  3304. goto out;
  3305. }
  3306. trans = btrfs_start_transaction(root, 1);
  3307. if (IS_ERR(trans)) {
  3308. ret = PTR_ERR(trans);
  3309. trans = NULL;
  3310. goto out;
  3311. }
  3312. sa->rtransid = trans->transid;
  3313. sa->rtime.sec = ct.tv_sec;
  3314. sa->rtime.nsec = ct.tv_nsec;
  3315. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3316. btrfs_set_root_stransid(root_item, sa->stransid);
  3317. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3318. root_item->stime.sec = cpu_to_le64(sa->stime.sec);
  3319. root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
  3320. root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
  3321. root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
  3322. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3323. &root->root_key, &root->root_item);
  3324. if (ret < 0) {
  3325. btrfs_end_transaction(trans, root);
  3326. trans = NULL;
  3327. goto out;
  3328. } else {
  3329. ret = btrfs_commit_transaction(trans, root);
  3330. if (ret < 0)
  3331. goto out;
  3332. }
  3333. ret = copy_to_user(arg, sa, sizeof(*sa));
  3334. if (ret)
  3335. ret = -EFAULT;
  3336. out:
  3337. kfree(sa);
  3338. up_write(&root->fs_info->subvol_sem);
  3339. mnt_drop_write_file(file);
  3340. return ret;
  3341. }
  3342. long btrfs_ioctl(struct file *file, unsigned int
  3343. cmd, unsigned long arg)
  3344. {
  3345. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3346. void __user *argp = (void __user *)arg;
  3347. switch (cmd) {
  3348. case FS_IOC_GETFLAGS:
  3349. return btrfs_ioctl_getflags(file, argp);
  3350. case FS_IOC_SETFLAGS:
  3351. return btrfs_ioctl_setflags(file, argp);
  3352. case FS_IOC_GETVERSION:
  3353. return btrfs_ioctl_getversion(file, argp);
  3354. case FITRIM:
  3355. return btrfs_ioctl_fitrim(file, argp);
  3356. case BTRFS_IOC_SNAP_CREATE:
  3357. return btrfs_ioctl_snap_create(file, argp, 0);
  3358. case BTRFS_IOC_SNAP_CREATE_V2:
  3359. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  3360. case BTRFS_IOC_SUBVOL_CREATE:
  3361. return btrfs_ioctl_snap_create(file, argp, 1);
  3362. case BTRFS_IOC_SUBVOL_CREATE_V2:
  3363. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  3364. case BTRFS_IOC_SNAP_DESTROY:
  3365. return btrfs_ioctl_snap_destroy(file, argp);
  3366. case BTRFS_IOC_SUBVOL_GETFLAGS:
  3367. return btrfs_ioctl_subvol_getflags(file, argp);
  3368. case BTRFS_IOC_SUBVOL_SETFLAGS:
  3369. return btrfs_ioctl_subvol_setflags(file, argp);
  3370. case BTRFS_IOC_DEFAULT_SUBVOL:
  3371. return btrfs_ioctl_default_subvol(file, argp);
  3372. case BTRFS_IOC_DEFRAG:
  3373. return btrfs_ioctl_defrag(file, NULL);
  3374. case BTRFS_IOC_DEFRAG_RANGE:
  3375. return btrfs_ioctl_defrag(file, argp);
  3376. case BTRFS_IOC_RESIZE:
  3377. return btrfs_ioctl_resize(file, argp);
  3378. case BTRFS_IOC_ADD_DEV:
  3379. return btrfs_ioctl_add_dev(root, argp);
  3380. case BTRFS_IOC_RM_DEV:
  3381. return btrfs_ioctl_rm_dev(file, argp);
  3382. case BTRFS_IOC_FS_INFO:
  3383. return btrfs_ioctl_fs_info(root, argp);
  3384. case BTRFS_IOC_DEV_INFO:
  3385. return btrfs_ioctl_dev_info(root, argp);
  3386. case BTRFS_IOC_BALANCE:
  3387. return btrfs_ioctl_balance(file, NULL);
  3388. case BTRFS_IOC_CLONE:
  3389. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  3390. case BTRFS_IOC_CLONE_RANGE:
  3391. return btrfs_ioctl_clone_range(file, argp);
  3392. case BTRFS_IOC_TRANS_START:
  3393. return btrfs_ioctl_trans_start(file);
  3394. case BTRFS_IOC_TRANS_END:
  3395. return btrfs_ioctl_trans_end(file);
  3396. case BTRFS_IOC_TREE_SEARCH:
  3397. return btrfs_ioctl_tree_search(file, argp);
  3398. case BTRFS_IOC_INO_LOOKUP:
  3399. return btrfs_ioctl_ino_lookup(file, argp);
  3400. case BTRFS_IOC_INO_PATHS:
  3401. return btrfs_ioctl_ino_to_path(root, argp);
  3402. case BTRFS_IOC_LOGICAL_INO:
  3403. return btrfs_ioctl_logical_to_ino(root, argp);
  3404. case BTRFS_IOC_SPACE_INFO:
  3405. return btrfs_ioctl_space_info(root, argp);
  3406. case BTRFS_IOC_SYNC:
  3407. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  3408. return 0;
  3409. case BTRFS_IOC_START_SYNC:
  3410. return btrfs_ioctl_start_sync(root, argp);
  3411. case BTRFS_IOC_WAIT_SYNC:
  3412. return btrfs_ioctl_wait_sync(root, argp);
  3413. case BTRFS_IOC_SCRUB:
  3414. return btrfs_ioctl_scrub(file, argp);
  3415. case BTRFS_IOC_SCRUB_CANCEL:
  3416. return btrfs_ioctl_scrub_cancel(root, argp);
  3417. case BTRFS_IOC_SCRUB_PROGRESS:
  3418. return btrfs_ioctl_scrub_progress(root, argp);
  3419. case BTRFS_IOC_BALANCE_V2:
  3420. return btrfs_ioctl_balance(file, argp);
  3421. case BTRFS_IOC_BALANCE_CTL:
  3422. return btrfs_ioctl_balance_ctl(root, arg);
  3423. case BTRFS_IOC_BALANCE_PROGRESS:
  3424. return btrfs_ioctl_balance_progress(root, argp);
  3425. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  3426. return btrfs_ioctl_set_received_subvol(file, argp);
  3427. case BTRFS_IOC_SEND:
  3428. return btrfs_ioctl_send(file, argp);
  3429. case BTRFS_IOC_GET_DEV_STATS:
  3430. return btrfs_ioctl_get_dev_stats(root, argp);
  3431. case BTRFS_IOC_QUOTA_CTL:
  3432. return btrfs_ioctl_quota_ctl(file, argp);
  3433. case BTRFS_IOC_QGROUP_ASSIGN:
  3434. return btrfs_ioctl_qgroup_assign(file, argp);
  3435. case BTRFS_IOC_QGROUP_CREATE:
  3436. return btrfs_ioctl_qgroup_create(file, argp);
  3437. case BTRFS_IOC_QGROUP_LIMIT:
  3438. return btrfs_ioctl_qgroup_limit(file, argp);
  3439. case BTRFS_IOC_DEV_REPLACE:
  3440. return btrfs_ioctl_dev_replace(root, argp);
  3441. }
  3442. return -ENOTTY;
  3443. }