ioctl.c 94 KB

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