ioctl.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963
  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_nr(inode->i_mapping, ret);
  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. return -EINPROGRESS;
  1166. }
  1167. mutex_lock(&root->fs_info->volume_mutex);
  1168. vol_args = memdup_user(arg, sizeof(*vol_args));
  1169. if (IS_ERR(vol_args)) {
  1170. ret = PTR_ERR(vol_args);
  1171. goto out;
  1172. }
  1173. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1174. sizestr = vol_args->name;
  1175. devstr = strchr(sizestr, ':');
  1176. if (devstr) {
  1177. char *end;
  1178. sizestr = devstr + 1;
  1179. *devstr = '\0';
  1180. devstr = vol_args->name;
  1181. devid = simple_strtoull(devstr, &end, 10);
  1182. printk(KERN_INFO "btrfs: resizing devid %llu\n",
  1183. (unsigned long long)devid);
  1184. }
  1185. device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  1186. if (!device) {
  1187. printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
  1188. (unsigned long long)devid);
  1189. ret = -EINVAL;
  1190. goto out_free;
  1191. }
  1192. if (device->fs_devices && device->fs_devices->seeding) {
  1193. printk(KERN_INFO "btrfs: resizer unable to apply on "
  1194. "seeding device %llu\n",
  1195. (unsigned long long)devid);
  1196. ret = -EINVAL;
  1197. goto out_free;
  1198. }
  1199. if (!strcmp(sizestr, "max"))
  1200. new_size = device->bdev->bd_inode->i_size;
  1201. else {
  1202. if (sizestr[0] == '-') {
  1203. mod = -1;
  1204. sizestr++;
  1205. } else if (sizestr[0] == '+') {
  1206. mod = 1;
  1207. sizestr++;
  1208. }
  1209. new_size = memparse(sizestr, NULL);
  1210. if (new_size == 0) {
  1211. ret = -EINVAL;
  1212. goto out_free;
  1213. }
  1214. }
  1215. if (device->is_tgtdev_for_dev_replace) {
  1216. ret = -EINVAL;
  1217. goto out_free;
  1218. }
  1219. old_size = device->total_bytes;
  1220. if (mod < 0) {
  1221. if (new_size > old_size) {
  1222. ret = -EINVAL;
  1223. goto out_free;
  1224. }
  1225. new_size = old_size - new_size;
  1226. } else if (mod > 0) {
  1227. new_size = old_size + new_size;
  1228. }
  1229. if (new_size < 256 * 1024 * 1024) {
  1230. ret = -EINVAL;
  1231. goto out_free;
  1232. }
  1233. if (new_size > device->bdev->bd_inode->i_size) {
  1234. ret = -EFBIG;
  1235. goto out_free;
  1236. }
  1237. do_div(new_size, root->sectorsize);
  1238. new_size *= root->sectorsize;
  1239. printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
  1240. rcu_str_deref(device->name),
  1241. (unsigned long long)new_size);
  1242. if (new_size > old_size) {
  1243. trans = btrfs_start_transaction(root, 0);
  1244. if (IS_ERR(trans)) {
  1245. ret = PTR_ERR(trans);
  1246. goto out_free;
  1247. }
  1248. ret = btrfs_grow_device(trans, device, new_size);
  1249. btrfs_commit_transaction(trans, root);
  1250. } else if (new_size < old_size) {
  1251. ret = btrfs_shrink_device(device, new_size);
  1252. } /* equal, nothing need to do */
  1253. out_free:
  1254. kfree(vol_args);
  1255. out:
  1256. mutex_unlock(&root->fs_info->volume_mutex);
  1257. mnt_drop_write_file(file);
  1258. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1259. return ret;
  1260. }
  1261. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1262. char *name, unsigned long fd, int subvol,
  1263. u64 *transid, bool readonly,
  1264. struct btrfs_qgroup_inherit **inherit)
  1265. {
  1266. int namelen;
  1267. int ret = 0;
  1268. ret = mnt_want_write_file(file);
  1269. if (ret)
  1270. goto out;
  1271. namelen = strlen(name);
  1272. if (strchr(name, '/')) {
  1273. ret = -EINVAL;
  1274. goto out_drop_write;
  1275. }
  1276. if (name[0] == '.' &&
  1277. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1278. ret = -EEXIST;
  1279. goto out_drop_write;
  1280. }
  1281. if (subvol) {
  1282. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1283. NULL, transid, readonly, inherit);
  1284. } else {
  1285. struct fd src = fdget(fd);
  1286. struct inode *src_inode;
  1287. if (!src.file) {
  1288. ret = -EINVAL;
  1289. goto out_drop_write;
  1290. }
  1291. src_inode = src.file->f_path.dentry->d_inode;
  1292. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1293. printk(KERN_INFO "btrfs: Snapshot src from "
  1294. "another FS\n");
  1295. ret = -EINVAL;
  1296. } else {
  1297. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1298. BTRFS_I(src_inode)->root,
  1299. transid, readonly, inherit);
  1300. }
  1301. fdput(src);
  1302. }
  1303. out_drop_write:
  1304. mnt_drop_write_file(file);
  1305. out:
  1306. return ret;
  1307. }
  1308. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1309. void __user *arg, int subvol)
  1310. {
  1311. struct btrfs_ioctl_vol_args *vol_args;
  1312. int ret;
  1313. vol_args = memdup_user(arg, sizeof(*vol_args));
  1314. if (IS_ERR(vol_args))
  1315. return PTR_ERR(vol_args);
  1316. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1317. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1318. vol_args->fd, subvol,
  1319. NULL, false, NULL);
  1320. kfree(vol_args);
  1321. return ret;
  1322. }
  1323. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1324. void __user *arg, int subvol)
  1325. {
  1326. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1327. int ret;
  1328. u64 transid = 0;
  1329. u64 *ptr = NULL;
  1330. bool readonly = false;
  1331. struct btrfs_qgroup_inherit *inherit = NULL;
  1332. vol_args = memdup_user(arg, sizeof(*vol_args));
  1333. if (IS_ERR(vol_args))
  1334. return PTR_ERR(vol_args);
  1335. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1336. if (vol_args->flags &
  1337. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1338. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1339. ret = -EOPNOTSUPP;
  1340. goto out;
  1341. }
  1342. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1343. ptr = &transid;
  1344. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1345. readonly = true;
  1346. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1347. if (vol_args->size > PAGE_CACHE_SIZE) {
  1348. ret = -EINVAL;
  1349. goto out;
  1350. }
  1351. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1352. if (IS_ERR(inherit)) {
  1353. ret = PTR_ERR(inherit);
  1354. goto out;
  1355. }
  1356. }
  1357. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1358. vol_args->fd, subvol, ptr,
  1359. readonly, &inherit);
  1360. if (ret == 0 && ptr &&
  1361. copy_to_user(arg +
  1362. offsetof(struct btrfs_ioctl_vol_args_v2,
  1363. transid), ptr, sizeof(*ptr)))
  1364. ret = -EFAULT;
  1365. out:
  1366. kfree(vol_args);
  1367. kfree(inherit);
  1368. return ret;
  1369. }
  1370. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1371. void __user *arg)
  1372. {
  1373. struct inode *inode = fdentry(file)->d_inode;
  1374. struct btrfs_root *root = BTRFS_I(inode)->root;
  1375. int ret = 0;
  1376. u64 flags = 0;
  1377. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1378. return -EINVAL;
  1379. down_read(&root->fs_info->subvol_sem);
  1380. if (btrfs_root_readonly(root))
  1381. flags |= BTRFS_SUBVOL_RDONLY;
  1382. up_read(&root->fs_info->subvol_sem);
  1383. if (copy_to_user(arg, &flags, sizeof(flags)))
  1384. ret = -EFAULT;
  1385. return ret;
  1386. }
  1387. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1388. void __user *arg)
  1389. {
  1390. struct inode *inode = fdentry(file)->d_inode;
  1391. struct btrfs_root *root = BTRFS_I(inode)->root;
  1392. struct btrfs_trans_handle *trans;
  1393. u64 root_flags;
  1394. u64 flags;
  1395. int ret = 0;
  1396. ret = mnt_want_write_file(file);
  1397. if (ret)
  1398. goto out;
  1399. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1400. ret = -EINVAL;
  1401. goto out_drop_write;
  1402. }
  1403. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1404. ret = -EFAULT;
  1405. goto out_drop_write;
  1406. }
  1407. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1408. ret = -EINVAL;
  1409. goto out_drop_write;
  1410. }
  1411. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1412. ret = -EOPNOTSUPP;
  1413. goto out_drop_write;
  1414. }
  1415. if (!inode_owner_or_capable(inode)) {
  1416. ret = -EACCES;
  1417. goto out_drop_write;
  1418. }
  1419. down_write(&root->fs_info->subvol_sem);
  1420. /* nothing to do */
  1421. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1422. goto out_drop_sem;
  1423. root_flags = btrfs_root_flags(&root->root_item);
  1424. if (flags & BTRFS_SUBVOL_RDONLY)
  1425. btrfs_set_root_flags(&root->root_item,
  1426. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1427. else
  1428. btrfs_set_root_flags(&root->root_item,
  1429. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1430. trans = btrfs_start_transaction(root, 1);
  1431. if (IS_ERR(trans)) {
  1432. ret = PTR_ERR(trans);
  1433. goto out_reset;
  1434. }
  1435. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1436. &root->root_key, &root->root_item);
  1437. btrfs_commit_transaction(trans, root);
  1438. out_reset:
  1439. if (ret)
  1440. btrfs_set_root_flags(&root->root_item, root_flags);
  1441. out_drop_sem:
  1442. up_write(&root->fs_info->subvol_sem);
  1443. out_drop_write:
  1444. mnt_drop_write_file(file);
  1445. out:
  1446. return ret;
  1447. }
  1448. /*
  1449. * helper to check if the subvolume references other subvolumes
  1450. */
  1451. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1452. {
  1453. struct btrfs_path *path;
  1454. struct btrfs_key key;
  1455. int ret;
  1456. path = btrfs_alloc_path();
  1457. if (!path)
  1458. return -ENOMEM;
  1459. key.objectid = root->root_key.objectid;
  1460. key.type = BTRFS_ROOT_REF_KEY;
  1461. key.offset = (u64)-1;
  1462. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1463. &key, path, 0, 0);
  1464. if (ret < 0)
  1465. goto out;
  1466. BUG_ON(ret == 0);
  1467. ret = 0;
  1468. if (path->slots[0] > 0) {
  1469. path->slots[0]--;
  1470. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1471. if (key.objectid == root->root_key.objectid &&
  1472. key.type == BTRFS_ROOT_REF_KEY)
  1473. ret = -ENOTEMPTY;
  1474. }
  1475. out:
  1476. btrfs_free_path(path);
  1477. return ret;
  1478. }
  1479. static noinline int key_in_sk(struct btrfs_key *key,
  1480. struct btrfs_ioctl_search_key *sk)
  1481. {
  1482. struct btrfs_key test;
  1483. int ret;
  1484. test.objectid = sk->min_objectid;
  1485. test.type = sk->min_type;
  1486. test.offset = sk->min_offset;
  1487. ret = btrfs_comp_cpu_keys(key, &test);
  1488. if (ret < 0)
  1489. return 0;
  1490. test.objectid = sk->max_objectid;
  1491. test.type = sk->max_type;
  1492. test.offset = sk->max_offset;
  1493. ret = btrfs_comp_cpu_keys(key, &test);
  1494. if (ret > 0)
  1495. return 0;
  1496. return 1;
  1497. }
  1498. static noinline int copy_to_sk(struct btrfs_root *root,
  1499. struct btrfs_path *path,
  1500. struct btrfs_key *key,
  1501. struct btrfs_ioctl_search_key *sk,
  1502. char *buf,
  1503. unsigned long *sk_offset,
  1504. int *num_found)
  1505. {
  1506. u64 found_transid;
  1507. struct extent_buffer *leaf;
  1508. struct btrfs_ioctl_search_header sh;
  1509. unsigned long item_off;
  1510. unsigned long item_len;
  1511. int nritems;
  1512. int i;
  1513. int slot;
  1514. int ret = 0;
  1515. leaf = path->nodes[0];
  1516. slot = path->slots[0];
  1517. nritems = btrfs_header_nritems(leaf);
  1518. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1519. i = nritems;
  1520. goto advance_key;
  1521. }
  1522. found_transid = btrfs_header_generation(leaf);
  1523. for (i = slot; i < nritems; i++) {
  1524. item_off = btrfs_item_ptr_offset(leaf, i);
  1525. item_len = btrfs_item_size_nr(leaf, i);
  1526. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1527. item_len = 0;
  1528. if (sizeof(sh) + item_len + *sk_offset >
  1529. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1530. ret = 1;
  1531. goto overflow;
  1532. }
  1533. btrfs_item_key_to_cpu(leaf, key, i);
  1534. if (!key_in_sk(key, sk))
  1535. continue;
  1536. sh.objectid = key->objectid;
  1537. sh.offset = key->offset;
  1538. sh.type = key->type;
  1539. sh.len = item_len;
  1540. sh.transid = found_transid;
  1541. /* copy search result header */
  1542. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1543. *sk_offset += sizeof(sh);
  1544. if (item_len) {
  1545. char *p = buf + *sk_offset;
  1546. /* copy the item */
  1547. read_extent_buffer(leaf, p,
  1548. item_off, item_len);
  1549. *sk_offset += item_len;
  1550. }
  1551. (*num_found)++;
  1552. if (*num_found >= sk->nr_items)
  1553. break;
  1554. }
  1555. advance_key:
  1556. ret = 0;
  1557. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1558. key->offset++;
  1559. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1560. key->offset = 0;
  1561. key->type++;
  1562. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1563. key->offset = 0;
  1564. key->type = 0;
  1565. key->objectid++;
  1566. } else
  1567. ret = 1;
  1568. overflow:
  1569. return ret;
  1570. }
  1571. static noinline int search_ioctl(struct inode *inode,
  1572. struct btrfs_ioctl_search_args *args)
  1573. {
  1574. struct btrfs_root *root;
  1575. struct btrfs_key key;
  1576. struct btrfs_key max_key;
  1577. struct btrfs_path *path;
  1578. struct btrfs_ioctl_search_key *sk = &args->key;
  1579. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1580. int ret;
  1581. int num_found = 0;
  1582. unsigned long sk_offset = 0;
  1583. path = btrfs_alloc_path();
  1584. if (!path)
  1585. return -ENOMEM;
  1586. if (sk->tree_id == 0) {
  1587. /* search the root of the inode that was passed */
  1588. root = BTRFS_I(inode)->root;
  1589. } else {
  1590. key.objectid = sk->tree_id;
  1591. key.type = BTRFS_ROOT_ITEM_KEY;
  1592. key.offset = (u64)-1;
  1593. root = btrfs_read_fs_root_no_name(info, &key);
  1594. if (IS_ERR(root)) {
  1595. printk(KERN_ERR "could not find root %llu\n",
  1596. sk->tree_id);
  1597. btrfs_free_path(path);
  1598. return -ENOENT;
  1599. }
  1600. }
  1601. key.objectid = sk->min_objectid;
  1602. key.type = sk->min_type;
  1603. key.offset = sk->min_offset;
  1604. max_key.objectid = sk->max_objectid;
  1605. max_key.type = sk->max_type;
  1606. max_key.offset = sk->max_offset;
  1607. path->keep_locks = 1;
  1608. while(1) {
  1609. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1610. sk->min_transid);
  1611. if (ret != 0) {
  1612. if (ret > 0)
  1613. ret = 0;
  1614. goto err;
  1615. }
  1616. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1617. &sk_offset, &num_found);
  1618. btrfs_release_path(path);
  1619. if (ret || num_found >= sk->nr_items)
  1620. break;
  1621. }
  1622. ret = 0;
  1623. err:
  1624. sk->nr_items = num_found;
  1625. btrfs_free_path(path);
  1626. return ret;
  1627. }
  1628. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1629. void __user *argp)
  1630. {
  1631. struct btrfs_ioctl_search_args *args;
  1632. struct inode *inode;
  1633. int ret;
  1634. if (!capable(CAP_SYS_ADMIN))
  1635. return -EPERM;
  1636. args = memdup_user(argp, sizeof(*args));
  1637. if (IS_ERR(args))
  1638. return PTR_ERR(args);
  1639. inode = fdentry(file)->d_inode;
  1640. ret = search_ioctl(inode, args);
  1641. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1642. ret = -EFAULT;
  1643. kfree(args);
  1644. return ret;
  1645. }
  1646. /*
  1647. * Search INODE_REFs to identify path name of 'dirid' directory
  1648. * in a 'tree_id' tree. and sets path name to 'name'.
  1649. */
  1650. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1651. u64 tree_id, u64 dirid, char *name)
  1652. {
  1653. struct btrfs_root *root;
  1654. struct btrfs_key key;
  1655. char *ptr;
  1656. int ret = -1;
  1657. int slot;
  1658. int len;
  1659. int total_len = 0;
  1660. struct btrfs_inode_ref *iref;
  1661. struct extent_buffer *l;
  1662. struct btrfs_path *path;
  1663. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1664. name[0]='\0';
  1665. return 0;
  1666. }
  1667. path = btrfs_alloc_path();
  1668. if (!path)
  1669. return -ENOMEM;
  1670. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1671. key.objectid = tree_id;
  1672. key.type = BTRFS_ROOT_ITEM_KEY;
  1673. key.offset = (u64)-1;
  1674. root = btrfs_read_fs_root_no_name(info, &key);
  1675. if (IS_ERR(root)) {
  1676. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1677. ret = -ENOENT;
  1678. goto out;
  1679. }
  1680. key.objectid = dirid;
  1681. key.type = BTRFS_INODE_REF_KEY;
  1682. key.offset = (u64)-1;
  1683. while(1) {
  1684. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1685. if (ret < 0)
  1686. goto out;
  1687. l = path->nodes[0];
  1688. slot = path->slots[0];
  1689. if (ret > 0 && slot > 0)
  1690. slot--;
  1691. btrfs_item_key_to_cpu(l, &key, slot);
  1692. if (ret > 0 && (key.objectid != dirid ||
  1693. key.type != BTRFS_INODE_REF_KEY)) {
  1694. ret = -ENOENT;
  1695. goto out;
  1696. }
  1697. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1698. len = btrfs_inode_ref_name_len(l, iref);
  1699. ptr -= len + 1;
  1700. total_len += len + 1;
  1701. if (ptr < name)
  1702. goto out;
  1703. *(ptr + len) = '/';
  1704. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1705. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1706. break;
  1707. btrfs_release_path(path);
  1708. key.objectid = key.offset;
  1709. key.offset = (u64)-1;
  1710. dirid = key.objectid;
  1711. }
  1712. if (ptr < name)
  1713. goto out;
  1714. memmove(name, ptr, total_len);
  1715. name[total_len]='\0';
  1716. ret = 0;
  1717. out:
  1718. btrfs_free_path(path);
  1719. return ret;
  1720. }
  1721. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1722. void __user *argp)
  1723. {
  1724. struct btrfs_ioctl_ino_lookup_args *args;
  1725. struct inode *inode;
  1726. int ret;
  1727. if (!capable(CAP_SYS_ADMIN))
  1728. return -EPERM;
  1729. args = memdup_user(argp, sizeof(*args));
  1730. if (IS_ERR(args))
  1731. return PTR_ERR(args);
  1732. inode = fdentry(file)->d_inode;
  1733. if (args->treeid == 0)
  1734. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1735. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1736. args->treeid, args->objectid,
  1737. args->name);
  1738. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1739. ret = -EFAULT;
  1740. kfree(args);
  1741. return ret;
  1742. }
  1743. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1744. void __user *arg)
  1745. {
  1746. struct dentry *parent = fdentry(file);
  1747. struct dentry *dentry;
  1748. struct inode *dir = parent->d_inode;
  1749. struct inode *inode;
  1750. struct btrfs_root *root = BTRFS_I(dir)->root;
  1751. struct btrfs_root *dest = NULL;
  1752. struct btrfs_ioctl_vol_args *vol_args;
  1753. struct btrfs_trans_handle *trans;
  1754. int namelen;
  1755. int ret;
  1756. int err = 0;
  1757. vol_args = memdup_user(arg, sizeof(*vol_args));
  1758. if (IS_ERR(vol_args))
  1759. return PTR_ERR(vol_args);
  1760. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1761. namelen = strlen(vol_args->name);
  1762. if (strchr(vol_args->name, '/') ||
  1763. strncmp(vol_args->name, "..", namelen) == 0) {
  1764. err = -EINVAL;
  1765. goto out;
  1766. }
  1767. err = mnt_want_write_file(file);
  1768. if (err)
  1769. goto out;
  1770. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1771. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1772. if (IS_ERR(dentry)) {
  1773. err = PTR_ERR(dentry);
  1774. goto out_unlock_dir;
  1775. }
  1776. if (!dentry->d_inode) {
  1777. err = -ENOENT;
  1778. goto out_dput;
  1779. }
  1780. inode = dentry->d_inode;
  1781. dest = BTRFS_I(inode)->root;
  1782. if (!capable(CAP_SYS_ADMIN)){
  1783. /*
  1784. * Regular user. Only allow this with a special mount
  1785. * option, when the user has write+exec access to the
  1786. * subvol root, and when rmdir(2) would have been
  1787. * allowed.
  1788. *
  1789. * Note that this is _not_ check that the subvol is
  1790. * empty or doesn't contain data that we wouldn't
  1791. * otherwise be able to delete.
  1792. *
  1793. * Users who want to delete empty subvols should try
  1794. * rmdir(2).
  1795. */
  1796. err = -EPERM;
  1797. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1798. goto out_dput;
  1799. /*
  1800. * Do not allow deletion if the parent dir is the same
  1801. * as the dir to be deleted. That means the ioctl
  1802. * must be called on the dentry referencing the root
  1803. * of the subvol, not a random directory contained
  1804. * within it.
  1805. */
  1806. err = -EINVAL;
  1807. if (root == dest)
  1808. goto out_dput;
  1809. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1810. if (err)
  1811. goto out_dput;
  1812. /* check if subvolume may be deleted by a non-root user */
  1813. err = btrfs_may_delete(dir, dentry, 1);
  1814. if (err)
  1815. goto out_dput;
  1816. }
  1817. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1818. err = -EINVAL;
  1819. goto out_dput;
  1820. }
  1821. mutex_lock(&inode->i_mutex);
  1822. err = d_invalidate(dentry);
  1823. if (err)
  1824. goto out_unlock;
  1825. down_write(&root->fs_info->subvol_sem);
  1826. err = may_destroy_subvol(dest);
  1827. if (err)
  1828. goto out_up_write;
  1829. trans = btrfs_start_transaction(root, 0);
  1830. if (IS_ERR(trans)) {
  1831. err = PTR_ERR(trans);
  1832. goto out_up_write;
  1833. }
  1834. trans->block_rsv = &root->fs_info->global_block_rsv;
  1835. ret = btrfs_unlink_subvol(trans, root, dir,
  1836. dest->root_key.objectid,
  1837. dentry->d_name.name,
  1838. dentry->d_name.len);
  1839. if (ret) {
  1840. err = ret;
  1841. btrfs_abort_transaction(trans, root, ret);
  1842. goto out_end_trans;
  1843. }
  1844. btrfs_record_root_in_trans(trans, dest);
  1845. memset(&dest->root_item.drop_progress, 0,
  1846. sizeof(dest->root_item.drop_progress));
  1847. dest->root_item.drop_level = 0;
  1848. btrfs_set_root_refs(&dest->root_item, 0);
  1849. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1850. ret = btrfs_insert_orphan_item(trans,
  1851. root->fs_info->tree_root,
  1852. dest->root_key.objectid);
  1853. if (ret) {
  1854. btrfs_abort_transaction(trans, root, ret);
  1855. err = ret;
  1856. goto out_end_trans;
  1857. }
  1858. }
  1859. out_end_trans:
  1860. ret = btrfs_end_transaction(trans, root);
  1861. if (ret && !err)
  1862. err = ret;
  1863. inode->i_flags |= S_DEAD;
  1864. out_up_write:
  1865. up_write(&root->fs_info->subvol_sem);
  1866. out_unlock:
  1867. mutex_unlock(&inode->i_mutex);
  1868. if (!err) {
  1869. shrink_dcache_sb(root->fs_info->sb);
  1870. btrfs_invalidate_inodes(dest);
  1871. d_delete(dentry);
  1872. }
  1873. out_dput:
  1874. dput(dentry);
  1875. out_unlock_dir:
  1876. mutex_unlock(&dir->i_mutex);
  1877. mnt_drop_write_file(file);
  1878. out:
  1879. kfree(vol_args);
  1880. return err;
  1881. }
  1882. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1883. {
  1884. struct inode *inode = fdentry(file)->d_inode;
  1885. struct btrfs_root *root = BTRFS_I(inode)->root;
  1886. struct btrfs_ioctl_defrag_range_args *range;
  1887. int ret;
  1888. if (btrfs_root_readonly(root))
  1889. return -EROFS;
  1890. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1891. 1)) {
  1892. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1893. return -EINPROGRESS;
  1894. }
  1895. ret = mnt_want_write_file(file);
  1896. if (ret) {
  1897. atomic_set(&root->fs_info->mutually_exclusive_operation_running,
  1898. 0);
  1899. return ret;
  1900. }
  1901. switch (inode->i_mode & S_IFMT) {
  1902. case S_IFDIR:
  1903. if (!capable(CAP_SYS_ADMIN)) {
  1904. ret = -EPERM;
  1905. goto out;
  1906. }
  1907. ret = btrfs_defrag_root(root, 0);
  1908. if (ret)
  1909. goto out;
  1910. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1911. break;
  1912. case S_IFREG:
  1913. if (!(file->f_mode & FMODE_WRITE)) {
  1914. ret = -EINVAL;
  1915. goto out;
  1916. }
  1917. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1918. if (!range) {
  1919. ret = -ENOMEM;
  1920. goto out;
  1921. }
  1922. if (argp) {
  1923. if (copy_from_user(range, argp,
  1924. sizeof(*range))) {
  1925. ret = -EFAULT;
  1926. kfree(range);
  1927. goto out;
  1928. }
  1929. /* compression requires us to start the IO */
  1930. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1931. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1932. range->extent_thresh = (u32)-1;
  1933. }
  1934. } else {
  1935. /* the rest are all set to zero by kzalloc */
  1936. range->len = (u64)-1;
  1937. }
  1938. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1939. range, 0, 0);
  1940. if (ret > 0)
  1941. ret = 0;
  1942. kfree(range);
  1943. break;
  1944. default:
  1945. ret = -EINVAL;
  1946. }
  1947. out:
  1948. mnt_drop_write_file(file);
  1949. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1950. return ret;
  1951. }
  1952. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1953. {
  1954. struct btrfs_ioctl_vol_args *vol_args;
  1955. int ret;
  1956. if (!capable(CAP_SYS_ADMIN))
  1957. return -EPERM;
  1958. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1959. 1)) {
  1960. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1961. return -EINPROGRESS;
  1962. }
  1963. mutex_lock(&root->fs_info->volume_mutex);
  1964. vol_args = memdup_user(arg, sizeof(*vol_args));
  1965. if (IS_ERR(vol_args)) {
  1966. ret = PTR_ERR(vol_args);
  1967. goto out;
  1968. }
  1969. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1970. ret = btrfs_init_new_device(root, vol_args->name);
  1971. kfree(vol_args);
  1972. out:
  1973. mutex_unlock(&root->fs_info->volume_mutex);
  1974. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1975. return ret;
  1976. }
  1977. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  1978. {
  1979. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1980. struct btrfs_ioctl_vol_args *vol_args;
  1981. int ret;
  1982. if (!capable(CAP_SYS_ADMIN))
  1983. return -EPERM;
  1984. ret = mnt_want_write_file(file);
  1985. if (ret)
  1986. return ret;
  1987. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1988. 1)) {
  1989. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  1990. mnt_drop_write_file(file);
  1991. return -EINPROGRESS;
  1992. }
  1993. mutex_lock(&root->fs_info->volume_mutex);
  1994. vol_args = memdup_user(arg, sizeof(*vol_args));
  1995. if (IS_ERR(vol_args)) {
  1996. ret = PTR_ERR(vol_args);
  1997. goto out;
  1998. }
  1999. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2000. ret = btrfs_rm_device(root, vol_args->name);
  2001. kfree(vol_args);
  2002. out:
  2003. mutex_unlock(&root->fs_info->volume_mutex);
  2004. mnt_drop_write_file(file);
  2005. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2006. return ret;
  2007. }
  2008. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2009. {
  2010. struct btrfs_ioctl_fs_info_args *fi_args;
  2011. struct btrfs_device *device;
  2012. struct btrfs_device *next;
  2013. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2014. int ret = 0;
  2015. if (!capable(CAP_SYS_ADMIN))
  2016. return -EPERM;
  2017. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2018. if (!fi_args)
  2019. return -ENOMEM;
  2020. fi_args->num_devices = fs_devices->num_devices;
  2021. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2022. mutex_lock(&fs_devices->device_list_mutex);
  2023. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2024. if (device->devid > fi_args->max_id)
  2025. fi_args->max_id = device->devid;
  2026. }
  2027. mutex_unlock(&fs_devices->device_list_mutex);
  2028. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2029. ret = -EFAULT;
  2030. kfree(fi_args);
  2031. return ret;
  2032. }
  2033. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2034. {
  2035. struct btrfs_ioctl_dev_info_args *di_args;
  2036. struct btrfs_device *dev;
  2037. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2038. int ret = 0;
  2039. char *s_uuid = NULL;
  2040. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  2041. if (!capable(CAP_SYS_ADMIN))
  2042. return -EPERM;
  2043. di_args = memdup_user(arg, sizeof(*di_args));
  2044. if (IS_ERR(di_args))
  2045. return PTR_ERR(di_args);
  2046. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  2047. s_uuid = di_args->uuid;
  2048. mutex_lock(&fs_devices->device_list_mutex);
  2049. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2050. mutex_unlock(&fs_devices->device_list_mutex);
  2051. if (!dev) {
  2052. ret = -ENODEV;
  2053. goto out;
  2054. }
  2055. di_args->devid = dev->devid;
  2056. di_args->bytes_used = dev->bytes_used;
  2057. di_args->total_bytes = dev->total_bytes;
  2058. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2059. if (dev->name) {
  2060. struct rcu_string *name;
  2061. rcu_read_lock();
  2062. name = rcu_dereference(dev->name);
  2063. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2064. rcu_read_unlock();
  2065. di_args->path[sizeof(di_args->path) - 1] = 0;
  2066. } else {
  2067. di_args->path[0] = '\0';
  2068. }
  2069. out:
  2070. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2071. ret = -EFAULT;
  2072. kfree(di_args);
  2073. return ret;
  2074. }
  2075. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  2076. u64 off, u64 olen, u64 destoff)
  2077. {
  2078. struct inode *inode = fdentry(file)->d_inode;
  2079. struct btrfs_root *root = BTRFS_I(inode)->root;
  2080. struct fd src_file;
  2081. struct inode *src;
  2082. struct btrfs_trans_handle *trans;
  2083. struct btrfs_path *path;
  2084. struct extent_buffer *leaf;
  2085. char *buf;
  2086. struct btrfs_key key;
  2087. u32 nritems;
  2088. int slot;
  2089. int ret;
  2090. u64 len = olen;
  2091. u64 bs = root->fs_info->sb->s_blocksize;
  2092. /*
  2093. * TODO:
  2094. * - split compressed inline extents. annoying: we need to
  2095. * decompress into destination's address_space (the file offset
  2096. * may change, so source mapping won't do), then recompress (or
  2097. * otherwise reinsert) a subrange.
  2098. * - allow ranges within the same file to be cloned (provided
  2099. * they don't overlap)?
  2100. */
  2101. /* the destination must be opened for writing */
  2102. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  2103. return -EINVAL;
  2104. if (btrfs_root_readonly(root))
  2105. return -EROFS;
  2106. ret = mnt_want_write_file(file);
  2107. if (ret)
  2108. return ret;
  2109. src_file = fdget(srcfd);
  2110. if (!src_file.file) {
  2111. ret = -EBADF;
  2112. goto out_drop_write;
  2113. }
  2114. ret = -EXDEV;
  2115. if (src_file.file->f_path.mnt != file->f_path.mnt)
  2116. goto out_fput;
  2117. src = src_file.file->f_dentry->d_inode;
  2118. ret = -EINVAL;
  2119. if (src == inode)
  2120. goto out_fput;
  2121. /* the src must be open for reading */
  2122. if (!(src_file.file->f_mode & FMODE_READ))
  2123. goto out_fput;
  2124. /* don't make the dst file partly checksummed */
  2125. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2126. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  2127. goto out_fput;
  2128. ret = -EISDIR;
  2129. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  2130. goto out_fput;
  2131. ret = -EXDEV;
  2132. if (src->i_sb != inode->i_sb)
  2133. goto out_fput;
  2134. ret = -ENOMEM;
  2135. buf = vmalloc(btrfs_level_size(root, 0));
  2136. if (!buf)
  2137. goto out_fput;
  2138. path = btrfs_alloc_path();
  2139. if (!path) {
  2140. vfree(buf);
  2141. goto out_fput;
  2142. }
  2143. path->reada = 2;
  2144. if (inode < src) {
  2145. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  2146. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  2147. } else {
  2148. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  2149. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2150. }
  2151. /* determine range to clone */
  2152. ret = -EINVAL;
  2153. if (off + len > src->i_size || off + len < off)
  2154. goto out_unlock;
  2155. if (len == 0)
  2156. olen = len = src->i_size - off;
  2157. /* if we extend to eof, continue to block boundary */
  2158. if (off + len == src->i_size)
  2159. len = ALIGN(src->i_size, bs) - off;
  2160. /* verify the end result is block aligned */
  2161. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  2162. !IS_ALIGNED(destoff, bs))
  2163. goto out_unlock;
  2164. if (destoff > inode->i_size) {
  2165. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  2166. if (ret)
  2167. goto out_unlock;
  2168. }
  2169. /* truncate page cache pages from target inode range */
  2170. truncate_inode_pages_range(&inode->i_data, destoff,
  2171. PAGE_CACHE_ALIGN(destoff + len) - 1);
  2172. /* do any pending delalloc/csum calc on src, one way or
  2173. another, and lock file content */
  2174. while (1) {
  2175. struct btrfs_ordered_extent *ordered;
  2176. lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2177. ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
  2178. if (!ordered &&
  2179. !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
  2180. EXTENT_DELALLOC, 0, NULL))
  2181. break;
  2182. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2183. if (ordered)
  2184. btrfs_put_ordered_extent(ordered);
  2185. btrfs_wait_ordered_range(src, off, len);
  2186. }
  2187. /* clone data */
  2188. key.objectid = btrfs_ino(src);
  2189. key.type = BTRFS_EXTENT_DATA_KEY;
  2190. key.offset = 0;
  2191. while (1) {
  2192. /*
  2193. * note the key will change type as we walk through the
  2194. * tree.
  2195. */
  2196. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2197. 0, 0);
  2198. if (ret < 0)
  2199. goto out;
  2200. nritems = btrfs_header_nritems(path->nodes[0]);
  2201. if (path->slots[0] >= nritems) {
  2202. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2203. if (ret < 0)
  2204. goto out;
  2205. if (ret > 0)
  2206. break;
  2207. nritems = btrfs_header_nritems(path->nodes[0]);
  2208. }
  2209. leaf = path->nodes[0];
  2210. slot = path->slots[0];
  2211. btrfs_item_key_to_cpu(leaf, &key, slot);
  2212. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2213. key.objectid != btrfs_ino(src))
  2214. break;
  2215. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2216. struct btrfs_file_extent_item *extent;
  2217. int type;
  2218. u32 size;
  2219. struct btrfs_key new_key;
  2220. u64 disko = 0, diskl = 0;
  2221. u64 datao = 0, datal = 0;
  2222. u8 comp;
  2223. u64 endoff;
  2224. size = btrfs_item_size_nr(leaf, slot);
  2225. read_extent_buffer(leaf, buf,
  2226. btrfs_item_ptr_offset(leaf, slot),
  2227. size);
  2228. extent = btrfs_item_ptr(leaf, slot,
  2229. struct btrfs_file_extent_item);
  2230. comp = btrfs_file_extent_compression(leaf, extent);
  2231. type = btrfs_file_extent_type(leaf, extent);
  2232. if (type == BTRFS_FILE_EXTENT_REG ||
  2233. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2234. disko = btrfs_file_extent_disk_bytenr(leaf,
  2235. extent);
  2236. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2237. extent);
  2238. datao = btrfs_file_extent_offset(leaf, extent);
  2239. datal = btrfs_file_extent_num_bytes(leaf,
  2240. extent);
  2241. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2242. /* take upper bound, may be compressed */
  2243. datal = btrfs_file_extent_ram_bytes(leaf,
  2244. extent);
  2245. }
  2246. btrfs_release_path(path);
  2247. if (key.offset + datal <= off ||
  2248. key.offset >= off + len - 1)
  2249. goto next;
  2250. memcpy(&new_key, &key, sizeof(new_key));
  2251. new_key.objectid = btrfs_ino(inode);
  2252. if (off <= key.offset)
  2253. new_key.offset = key.offset + destoff - off;
  2254. else
  2255. new_key.offset = destoff;
  2256. /*
  2257. * 1 - adjusting old extent (we may have to split it)
  2258. * 1 - add new extent
  2259. * 1 - inode update
  2260. */
  2261. trans = btrfs_start_transaction(root, 3);
  2262. if (IS_ERR(trans)) {
  2263. ret = PTR_ERR(trans);
  2264. goto out;
  2265. }
  2266. if (type == BTRFS_FILE_EXTENT_REG ||
  2267. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2268. /*
  2269. * a | --- range to clone ---| b
  2270. * | ------------- extent ------------- |
  2271. */
  2272. /* substract range b */
  2273. if (key.offset + datal > off + len)
  2274. datal = off + len - key.offset;
  2275. /* substract range a */
  2276. if (off > key.offset) {
  2277. datao += off - key.offset;
  2278. datal -= off - key.offset;
  2279. }
  2280. ret = btrfs_drop_extents(trans, root, inode,
  2281. new_key.offset,
  2282. new_key.offset + datal,
  2283. 1);
  2284. if (ret) {
  2285. btrfs_abort_transaction(trans, root,
  2286. ret);
  2287. btrfs_end_transaction(trans, root);
  2288. goto out;
  2289. }
  2290. ret = btrfs_insert_empty_item(trans, root, path,
  2291. &new_key, size);
  2292. if (ret) {
  2293. btrfs_abort_transaction(trans, root,
  2294. ret);
  2295. btrfs_end_transaction(trans, root);
  2296. goto out;
  2297. }
  2298. leaf = path->nodes[0];
  2299. slot = path->slots[0];
  2300. write_extent_buffer(leaf, buf,
  2301. btrfs_item_ptr_offset(leaf, slot),
  2302. size);
  2303. extent = btrfs_item_ptr(leaf, slot,
  2304. struct btrfs_file_extent_item);
  2305. /* disko == 0 means it's a hole */
  2306. if (!disko)
  2307. datao = 0;
  2308. btrfs_set_file_extent_offset(leaf, extent,
  2309. datao);
  2310. btrfs_set_file_extent_num_bytes(leaf, extent,
  2311. datal);
  2312. if (disko) {
  2313. inode_add_bytes(inode, datal);
  2314. ret = btrfs_inc_extent_ref(trans, root,
  2315. disko, diskl, 0,
  2316. root->root_key.objectid,
  2317. btrfs_ino(inode),
  2318. new_key.offset - datao,
  2319. 0);
  2320. if (ret) {
  2321. btrfs_abort_transaction(trans,
  2322. root,
  2323. ret);
  2324. btrfs_end_transaction(trans,
  2325. root);
  2326. goto out;
  2327. }
  2328. }
  2329. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2330. u64 skip = 0;
  2331. u64 trim = 0;
  2332. if (off > key.offset) {
  2333. skip = off - key.offset;
  2334. new_key.offset += skip;
  2335. }
  2336. if (key.offset + datal > off + len)
  2337. trim = key.offset + datal - (off + len);
  2338. if (comp && (skip || trim)) {
  2339. ret = -EINVAL;
  2340. btrfs_end_transaction(trans, root);
  2341. goto out;
  2342. }
  2343. size -= skip + trim;
  2344. datal -= skip + trim;
  2345. ret = btrfs_drop_extents(trans, root, inode,
  2346. new_key.offset,
  2347. new_key.offset + datal,
  2348. 1);
  2349. if (ret) {
  2350. btrfs_abort_transaction(trans, root,
  2351. ret);
  2352. btrfs_end_transaction(trans, root);
  2353. goto out;
  2354. }
  2355. ret = btrfs_insert_empty_item(trans, root, path,
  2356. &new_key, size);
  2357. if (ret) {
  2358. btrfs_abort_transaction(trans, root,
  2359. ret);
  2360. btrfs_end_transaction(trans, root);
  2361. goto out;
  2362. }
  2363. if (skip) {
  2364. u32 start =
  2365. btrfs_file_extent_calc_inline_size(0);
  2366. memmove(buf+start, buf+start+skip,
  2367. datal);
  2368. }
  2369. leaf = path->nodes[0];
  2370. slot = path->slots[0];
  2371. write_extent_buffer(leaf, buf,
  2372. btrfs_item_ptr_offset(leaf, slot),
  2373. size);
  2374. inode_add_bytes(inode, datal);
  2375. }
  2376. btrfs_mark_buffer_dirty(leaf);
  2377. btrfs_release_path(path);
  2378. inode_inc_iversion(inode);
  2379. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2380. /*
  2381. * we round up to the block size at eof when
  2382. * determining which extents to clone above,
  2383. * but shouldn't round up the file size
  2384. */
  2385. endoff = new_key.offset + datal;
  2386. if (endoff > destoff+olen)
  2387. endoff = destoff+olen;
  2388. if (endoff > inode->i_size)
  2389. btrfs_i_size_write(inode, endoff);
  2390. ret = btrfs_update_inode(trans, root, inode);
  2391. if (ret) {
  2392. btrfs_abort_transaction(trans, root, ret);
  2393. btrfs_end_transaction(trans, root);
  2394. goto out;
  2395. }
  2396. ret = btrfs_end_transaction(trans, root);
  2397. }
  2398. next:
  2399. btrfs_release_path(path);
  2400. key.offset++;
  2401. }
  2402. ret = 0;
  2403. out:
  2404. btrfs_release_path(path);
  2405. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  2406. out_unlock:
  2407. mutex_unlock(&src->i_mutex);
  2408. mutex_unlock(&inode->i_mutex);
  2409. vfree(buf);
  2410. btrfs_free_path(path);
  2411. out_fput:
  2412. fdput(src_file);
  2413. out_drop_write:
  2414. mnt_drop_write_file(file);
  2415. return ret;
  2416. }
  2417. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2418. {
  2419. struct btrfs_ioctl_clone_range_args args;
  2420. if (copy_from_user(&args, argp, sizeof(args)))
  2421. return -EFAULT;
  2422. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2423. args.src_length, args.dest_offset);
  2424. }
  2425. /*
  2426. * there are many ways the trans_start and trans_end ioctls can lead
  2427. * to deadlocks. They should only be used by applications that
  2428. * basically own the machine, and have a very in depth understanding
  2429. * of all the possible deadlocks and enospc problems.
  2430. */
  2431. static long btrfs_ioctl_trans_start(struct file *file)
  2432. {
  2433. struct inode *inode = fdentry(file)->d_inode;
  2434. struct btrfs_root *root = BTRFS_I(inode)->root;
  2435. struct btrfs_trans_handle *trans;
  2436. int ret;
  2437. ret = -EPERM;
  2438. if (!capable(CAP_SYS_ADMIN))
  2439. goto out;
  2440. ret = -EINPROGRESS;
  2441. if (file->private_data)
  2442. goto out;
  2443. ret = -EROFS;
  2444. if (btrfs_root_readonly(root))
  2445. goto out;
  2446. ret = mnt_want_write_file(file);
  2447. if (ret)
  2448. goto out;
  2449. atomic_inc(&root->fs_info->open_ioctl_trans);
  2450. ret = -ENOMEM;
  2451. trans = btrfs_start_ioctl_transaction(root);
  2452. if (IS_ERR(trans))
  2453. goto out_drop;
  2454. file->private_data = trans;
  2455. return 0;
  2456. out_drop:
  2457. atomic_dec(&root->fs_info->open_ioctl_trans);
  2458. mnt_drop_write_file(file);
  2459. out:
  2460. return ret;
  2461. }
  2462. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2463. {
  2464. struct inode *inode = fdentry(file)->d_inode;
  2465. struct btrfs_root *root = BTRFS_I(inode)->root;
  2466. struct btrfs_root *new_root;
  2467. struct btrfs_dir_item *di;
  2468. struct btrfs_trans_handle *trans;
  2469. struct btrfs_path *path;
  2470. struct btrfs_key location;
  2471. struct btrfs_disk_key disk_key;
  2472. u64 objectid = 0;
  2473. u64 dir_id;
  2474. int ret;
  2475. if (!capable(CAP_SYS_ADMIN))
  2476. return -EPERM;
  2477. ret = mnt_want_write_file(file);
  2478. if (ret)
  2479. return ret;
  2480. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2481. ret = -EFAULT;
  2482. goto out;
  2483. }
  2484. if (!objectid)
  2485. objectid = root->root_key.objectid;
  2486. location.objectid = objectid;
  2487. location.type = BTRFS_ROOT_ITEM_KEY;
  2488. location.offset = (u64)-1;
  2489. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2490. if (IS_ERR(new_root)) {
  2491. ret = PTR_ERR(new_root);
  2492. goto out;
  2493. }
  2494. if (btrfs_root_refs(&new_root->root_item) == 0) {
  2495. ret = -ENOENT;
  2496. goto out;
  2497. }
  2498. path = btrfs_alloc_path();
  2499. if (!path) {
  2500. ret = -ENOMEM;
  2501. goto out;
  2502. }
  2503. path->leave_spinning = 1;
  2504. trans = btrfs_start_transaction(root, 1);
  2505. if (IS_ERR(trans)) {
  2506. btrfs_free_path(path);
  2507. ret = PTR_ERR(trans);
  2508. goto out;
  2509. }
  2510. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  2511. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2512. dir_id, "default", 7, 1);
  2513. if (IS_ERR_OR_NULL(di)) {
  2514. btrfs_free_path(path);
  2515. btrfs_end_transaction(trans, root);
  2516. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2517. "this isn't going to work\n");
  2518. ret = -ENOENT;
  2519. goto out;
  2520. }
  2521. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2522. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2523. btrfs_mark_buffer_dirty(path->nodes[0]);
  2524. btrfs_free_path(path);
  2525. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  2526. btrfs_end_transaction(trans, root);
  2527. out:
  2528. mnt_drop_write_file(file);
  2529. return ret;
  2530. }
  2531. void btrfs_get_block_group_info(struct list_head *groups_list,
  2532. struct btrfs_ioctl_space_info *space)
  2533. {
  2534. struct btrfs_block_group_cache *block_group;
  2535. space->total_bytes = 0;
  2536. space->used_bytes = 0;
  2537. space->flags = 0;
  2538. list_for_each_entry(block_group, groups_list, list) {
  2539. space->flags = block_group->flags;
  2540. space->total_bytes += block_group->key.offset;
  2541. space->used_bytes +=
  2542. btrfs_block_group_used(&block_group->item);
  2543. }
  2544. }
  2545. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2546. {
  2547. struct btrfs_ioctl_space_args space_args;
  2548. struct btrfs_ioctl_space_info space;
  2549. struct btrfs_ioctl_space_info *dest;
  2550. struct btrfs_ioctl_space_info *dest_orig;
  2551. struct btrfs_ioctl_space_info __user *user_dest;
  2552. struct btrfs_space_info *info;
  2553. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2554. BTRFS_BLOCK_GROUP_SYSTEM,
  2555. BTRFS_BLOCK_GROUP_METADATA,
  2556. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2557. int num_types = 4;
  2558. int alloc_size;
  2559. int ret = 0;
  2560. u64 slot_count = 0;
  2561. int i, c;
  2562. if (copy_from_user(&space_args,
  2563. (struct btrfs_ioctl_space_args __user *)arg,
  2564. sizeof(space_args)))
  2565. return -EFAULT;
  2566. for (i = 0; i < num_types; i++) {
  2567. struct btrfs_space_info *tmp;
  2568. info = NULL;
  2569. rcu_read_lock();
  2570. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2571. list) {
  2572. if (tmp->flags == types[i]) {
  2573. info = tmp;
  2574. break;
  2575. }
  2576. }
  2577. rcu_read_unlock();
  2578. if (!info)
  2579. continue;
  2580. down_read(&info->groups_sem);
  2581. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2582. if (!list_empty(&info->block_groups[c]))
  2583. slot_count++;
  2584. }
  2585. up_read(&info->groups_sem);
  2586. }
  2587. /* space_slots == 0 means they are asking for a count */
  2588. if (space_args.space_slots == 0) {
  2589. space_args.total_spaces = slot_count;
  2590. goto out;
  2591. }
  2592. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2593. alloc_size = sizeof(*dest) * slot_count;
  2594. /* we generally have at most 6 or so space infos, one for each raid
  2595. * level. So, a whole page should be more than enough for everyone
  2596. */
  2597. if (alloc_size > PAGE_CACHE_SIZE)
  2598. return -ENOMEM;
  2599. space_args.total_spaces = 0;
  2600. dest = kmalloc(alloc_size, GFP_NOFS);
  2601. if (!dest)
  2602. return -ENOMEM;
  2603. dest_orig = dest;
  2604. /* now we have a buffer to copy into */
  2605. for (i = 0; i < num_types; i++) {
  2606. struct btrfs_space_info *tmp;
  2607. if (!slot_count)
  2608. break;
  2609. info = NULL;
  2610. rcu_read_lock();
  2611. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2612. list) {
  2613. if (tmp->flags == types[i]) {
  2614. info = tmp;
  2615. break;
  2616. }
  2617. }
  2618. rcu_read_unlock();
  2619. if (!info)
  2620. continue;
  2621. down_read(&info->groups_sem);
  2622. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2623. if (!list_empty(&info->block_groups[c])) {
  2624. btrfs_get_block_group_info(
  2625. &info->block_groups[c], &space);
  2626. memcpy(dest, &space, sizeof(space));
  2627. dest++;
  2628. space_args.total_spaces++;
  2629. slot_count--;
  2630. }
  2631. if (!slot_count)
  2632. break;
  2633. }
  2634. up_read(&info->groups_sem);
  2635. }
  2636. user_dest = (struct btrfs_ioctl_space_info __user *)
  2637. (arg + sizeof(struct btrfs_ioctl_space_args));
  2638. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2639. ret = -EFAULT;
  2640. kfree(dest_orig);
  2641. out:
  2642. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2643. ret = -EFAULT;
  2644. return ret;
  2645. }
  2646. /*
  2647. * there are many ways the trans_start and trans_end ioctls can lead
  2648. * to deadlocks. They should only be used by applications that
  2649. * basically own the machine, and have a very in depth understanding
  2650. * of all the possible deadlocks and enospc problems.
  2651. */
  2652. long btrfs_ioctl_trans_end(struct file *file)
  2653. {
  2654. struct inode *inode = fdentry(file)->d_inode;
  2655. struct btrfs_root *root = BTRFS_I(inode)->root;
  2656. struct btrfs_trans_handle *trans;
  2657. trans = file->private_data;
  2658. if (!trans)
  2659. return -EINVAL;
  2660. file->private_data = NULL;
  2661. btrfs_end_transaction(trans, root);
  2662. atomic_dec(&root->fs_info->open_ioctl_trans);
  2663. mnt_drop_write_file(file);
  2664. return 0;
  2665. }
  2666. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  2667. void __user *argp)
  2668. {
  2669. struct btrfs_trans_handle *trans;
  2670. u64 transid;
  2671. int ret;
  2672. trans = btrfs_attach_transaction(root);
  2673. if (IS_ERR(trans)) {
  2674. if (PTR_ERR(trans) != -ENOENT)
  2675. return PTR_ERR(trans);
  2676. /* No running transaction, don't bother */
  2677. transid = root->fs_info->last_trans_committed;
  2678. goto out;
  2679. }
  2680. transid = trans->transid;
  2681. ret = btrfs_commit_transaction_async(trans, root, 0);
  2682. if (ret) {
  2683. btrfs_end_transaction(trans, root);
  2684. return ret;
  2685. }
  2686. out:
  2687. if (argp)
  2688. if (copy_to_user(argp, &transid, sizeof(transid)))
  2689. return -EFAULT;
  2690. return 0;
  2691. }
  2692. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  2693. void __user *argp)
  2694. {
  2695. u64 transid;
  2696. if (argp) {
  2697. if (copy_from_user(&transid, argp, sizeof(transid)))
  2698. return -EFAULT;
  2699. } else {
  2700. transid = 0; /* current trans */
  2701. }
  2702. return btrfs_wait_for_commit(root, transid);
  2703. }
  2704. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  2705. {
  2706. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2707. struct btrfs_ioctl_scrub_args *sa;
  2708. int ret;
  2709. if (!capable(CAP_SYS_ADMIN))
  2710. return -EPERM;
  2711. sa = memdup_user(arg, sizeof(*sa));
  2712. if (IS_ERR(sa))
  2713. return PTR_ERR(sa);
  2714. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  2715. ret = mnt_want_write_file(file);
  2716. if (ret)
  2717. goto out;
  2718. }
  2719. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  2720. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  2721. 0);
  2722. if (copy_to_user(arg, sa, sizeof(*sa)))
  2723. ret = -EFAULT;
  2724. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  2725. mnt_drop_write_file(file);
  2726. out:
  2727. kfree(sa);
  2728. return ret;
  2729. }
  2730. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2731. {
  2732. if (!capable(CAP_SYS_ADMIN))
  2733. return -EPERM;
  2734. return btrfs_scrub_cancel(root->fs_info);
  2735. }
  2736. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2737. void __user *arg)
  2738. {
  2739. struct btrfs_ioctl_scrub_args *sa;
  2740. int ret;
  2741. if (!capable(CAP_SYS_ADMIN))
  2742. return -EPERM;
  2743. sa = memdup_user(arg, sizeof(*sa));
  2744. if (IS_ERR(sa))
  2745. return PTR_ERR(sa);
  2746. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2747. if (copy_to_user(arg, sa, sizeof(*sa)))
  2748. ret = -EFAULT;
  2749. kfree(sa);
  2750. return ret;
  2751. }
  2752. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  2753. void __user *arg)
  2754. {
  2755. struct btrfs_ioctl_get_dev_stats *sa;
  2756. int ret;
  2757. sa = memdup_user(arg, sizeof(*sa));
  2758. if (IS_ERR(sa))
  2759. return PTR_ERR(sa);
  2760. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  2761. kfree(sa);
  2762. return -EPERM;
  2763. }
  2764. ret = btrfs_get_dev_stats(root, sa);
  2765. if (copy_to_user(arg, sa, sizeof(*sa)))
  2766. ret = -EFAULT;
  2767. kfree(sa);
  2768. return ret;
  2769. }
  2770. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  2771. {
  2772. struct btrfs_ioctl_dev_replace_args *p;
  2773. int ret;
  2774. if (!capable(CAP_SYS_ADMIN))
  2775. return -EPERM;
  2776. p = memdup_user(arg, sizeof(*p));
  2777. if (IS_ERR(p))
  2778. return PTR_ERR(p);
  2779. switch (p->cmd) {
  2780. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  2781. if (atomic_xchg(
  2782. &root->fs_info->mutually_exclusive_operation_running,
  2783. 1)) {
  2784. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2785. ret = -EINPROGRESS;
  2786. } else {
  2787. ret = btrfs_dev_replace_start(root, p);
  2788. atomic_set(
  2789. &root->fs_info->mutually_exclusive_operation_running,
  2790. 0);
  2791. }
  2792. break;
  2793. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  2794. btrfs_dev_replace_status(root->fs_info, p);
  2795. ret = 0;
  2796. break;
  2797. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  2798. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  2799. break;
  2800. default:
  2801. ret = -EINVAL;
  2802. break;
  2803. }
  2804. if (copy_to_user(arg, p, sizeof(*p)))
  2805. ret = -EFAULT;
  2806. kfree(p);
  2807. return ret;
  2808. }
  2809. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  2810. {
  2811. int ret = 0;
  2812. int i;
  2813. u64 rel_ptr;
  2814. int size;
  2815. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  2816. struct inode_fs_paths *ipath = NULL;
  2817. struct btrfs_path *path;
  2818. if (!capable(CAP_SYS_ADMIN))
  2819. return -EPERM;
  2820. path = btrfs_alloc_path();
  2821. if (!path) {
  2822. ret = -ENOMEM;
  2823. goto out;
  2824. }
  2825. ipa = memdup_user(arg, sizeof(*ipa));
  2826. if (IS_ERR(ipa)) {
  2827. ret = PTR_ERR(ipa);
  2828. ipa = NULL;
  2829. goto out;
  2830. }
  2831. size = min_t(u32, ipa->size, 4096);
  2832. ipath = init_ipath(size, root, path);
  2833. if (IS_ERR(ipath)) {
  2834. ret = PTR_ERR(ipath);
  2835. ipath = NULL;
  2836. goto out;
  2837. }
  2838. ret = paths_from_inode(ipa->inum, ipath);
  2839. if (ret < 0)
  2840. goto out;
  2841. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  2842. rel_ptr = ipath->fspath->val[i] -
  2843. (u64)(unsigned long)ipath->fspath->val;
  2844. ipath->fspath->val[i] = rel_ptr;
  2845. }
  2846. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  2847. (void *)(unsigned long)ipath->fspath, size);
  2848. if (ret) {
  2849. ret = -EFAULT;
  2850. goto out;
  2851. }
  2852. out:
  2853. btrfs_free_path(path);
  2854. free_ipath(ipath);
  2855. kfree(ipa);
  2856. return ret;
  2857. }
  2858. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  2859. {
  2860. struct btrfs_data_container *inodes = ctx;
  2861. const size_t c = 3 * sizeof(u64);
  2862. if (inodes->bytes_left >= c) {
  2863. inodes->bytes_left -= c;
  2864. inodes->val[inodes->elem_cnt] = inum;
  2865. inodes->val[inodes->elem_cnt + 1] = offset;
  2866. inodes->val[inodes->elem_cnt + 2] = root;
  2867. inodes->elem_cnt += 3;
  2868. } else {
  2869. inodes->bytes_missing += c - inodes->bytes_left;
  2870. inodes->bytes_left = 0;
  2871. inodes->elem_missed += 3;
  2872. }
  2873. return 0;
  2874. }
  2875. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  2876. void __user *arg)
  2877. {
  2878. int ret = 0;
  2879. int size;
  2880. struct btrfs_ioctl_logical_ino_args *loi;
  2881. struct btrfs_data_container *inodes = NULL;
  2882. struct btrfs_path *path = NULL;
  2883. if (!capable(CAP_SYS_ADMIN))
  2884. return -EPERM;
  2885. loi = memdup_user(arg, sizeof(*loi));
  2886. if (IS_ERR(loi)) {
  2887. ret = PTR_ERR(loi);
  2888. loi = NULL;
  2889. goto out;
  2890. }
  2891. path = btrfs_alloc_path();
  2892. if (!path) {
  2893. ret = -ENOMEM;
  2894. goto out;
  2895. }
  2896. size = min_t(u32, loi->size, 64 * 1024);
  2897. inodes = init_data_container(size);
  2898. if (IS_ERR(inodes)) {
  2899. ret = PTR_ERR(inodes);
  2900. inodes = NULL;
  2901. goto out;
  2902. }
  2903. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  2904. build_ino_list, inodes);
  2905. if (ret == -EINVAL)
  2906. ret = -ENOENT;
  2907. if (ret < 0)
  2908. goto out;
  2909. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  2910. (void *)(unsigned long)inodes, size);
  2911. if (ret)
  2912. ret = -EFAULT;
  2913. out:
  2914. btrfs_free_path(path);
  2915. vfree(inodes);
  2916. kfree(loi);
  2917. return ret;
  2918. }
  2919. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2920. struct btrfs_ioctl_balance_args *bargs)
  2921. {
  2922. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2923. bargs->flags = bctl->flags;
  2924. if (atomic_read(&fs_info->balance_running))
  2925. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  2926. if (atomic_read(&fs_info->balance_pause_req))
  2927. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  2928. if (atomic_read(&fs_info->balance_cancel_req))
  2929. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  2930. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  2931. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  2932. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  2933. if (lock) {
  2934. spin_lock(&fs_info->balance_lock);
  2935. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2936. spin_unlock(&fs_info->balance_lock);
  2937. } else {
  2938. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2939. }
  2940. }
  2941. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  2942. {
  2943. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2944. struct btrfs_fs_info *fs_info = root->fs_info;
  2945. struct btrfs_ioctl_balance_args *bargs;
  2946. struct btrfs_balance_control *bctl;
  2947. int ret;
  2948. int need_to_clear_lock = 0;
  2949. if (!capable(CAP_SYS_ADMIN))
  2950. return -EPERM;
  2951. ret = mnt_want_write_file(file);
  2952. if (ret)
  2953. return ret;
  2954. mutex_lock(&fs_info->volume_mutex);
  2955. mutex_lock(&fs_info->balance_mutex);
  2956. if (arg) {
  2957. bargs = memdup_user(arg, sizeof(*bargs));
  2958. if (IS_ERR(bargs)) {
  2959. ret = PTR_ERR(bargs);
  2960. goto out;
  2961. }
  2962. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  2963. if (!fs_info->balance_ctl) {
  2964. ret = -ENOTCONN;
  2965. goto out_bargs;
  2966. }
  2967. bctl = fs_info->balance_ctl;
  2968. spin_lock(&fs_info->balance_lock);
  2969. bctl->flags |= BTRFS_BALANCE_RESUME;
  2970. spin_unlock(&fs_info->balance_lock);
  2971. goto do_balance;
  2972. }
  2973. } else {
  2974. bargs = NULL;
  2975. }
  2976. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2977. 1)) {
  2978. pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
  2979. ret = -EINPROGRESS;
  2980. goto out_bargs;
  2981. }
  2982. need_to_clear_lock = 1;
  2983. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2984. if (!bctl) {
  2985. ret = -ENOMEM;
  2986. goto out_bargs;
  2987. }
  2988. bctl->fs_info = fs_info;
  2989. if (arg) {
  2990. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  2991. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  2992. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  2993. bctl->flags = bargs->flags;
  2994. } else {
  2995. /* balance everything - no filters */
  2996. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  2997. }
  2998. do_balance:
  2999. ret = btrfs_balance(bctl, bargs);
  3000. /*
  3001. * bctl is freed in __cancel_balance or in free_fs_info if
  3002. * restriper was paused all the way until unmount
  3003. */
  3004. if (arg) {
  3005. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3006. ret = -EFAULT;
  3007. }
  3008. out_bargs:
  3009. kfree(bargs);
  3010. out:
  3011. if (need_to_clear_lock)
  3012. atomic_set(&root->fs_info->mutually_exclusive_operation_running,
  3013. 0);
  3014. mutex_unlock(&fs_info->balance_mutex);
  3015. mutex_unlock(&fs_info->volume_mutex);
  3016. mnt_drop_write_file(file);
  3017. return ret;
  3018. }
  3019. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3020. {
  3021. if (!capable(CAP_SYS_ADMIN))
  3022. return -EPERM;
  3023. switch (cmd) {
  3024. case BTRFS_BALANCE_CTL_PAUSE:
  3025. return btrfs_pause_balance(root->fs_info);
  3026. case BTRFS_BALANCE_CTL_CANCEL:
  3027. return btrfs_cancel_balance(root->fs_info);
  3028. }
  3029. return -EINVAL;
  3030. }
  3031. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3032. void __user *arg)
  3033. {
  3034. struct btrfs_fs_info *fs_info = root->fs_info;
  3035. struct btrfs_ioctl_balance_args *bargs;
  3036. int ret = 0;
  3037. if (!capable(CAP_SYS_ADMIN))
  3038. return -EPERM;
  3039. mutex_lock(&fs_info->balance_mutex);
  3040. if (!fs_info->balance_ctl) {
  3041. ret = -ENOTCONN;
  3042. goto out;
  3043. }
  3044. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3045. if (!bargs) {
  3046. ret = -ENOMEM;
  3047. goto out;
  3048. }
  3049. update_ioctl_balance_args(fs_info, 1, bargs);
  3050. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3051. ret = -EFAULT;
  3052. kfree(bargs);
  3053. out:
  3054. mutex_unlock(&fs_info->balance_mutex);
  3055. return ret;
  3056. }
  3057. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3058. {
  3059. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3060. struct btrfs_ioctl_quota_ctl_args *sa;
  3061. struct btrfs_trans_handle *trans = NULL;
  3062. int ret;
  3063. int err;
  3064. if (!capable(CAP_SYS_ADMIN))
  3065. return -EPERM;
  3066. ret = mnt_want_write_file(file);
  3067. if (ret)
  3068. return ret;
  3069. sa = memdup_user(arg, sizeof(*sa));
  3070. if (IS_ERR(sa)) {
  3071. ret = PTR_ERR(sa);
  3072. goto drop_write;
  3073. }
  3074. if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
  3075. trans = btrfs_start_transaction(root, 2);
  3076. if (IS_ERR(trans)) {
  3077. ret = PTR_ERR(trans);
  3078. goto out;
  3079. }
  3080. }
  3081. switch (sa->cmd) {
  3082. case BTRFS_QUOTA_CTL_ENABLE:
  3083. ret = btrfs_quota_enable(trans, root->fs_info);
  3084. break;
  3085. case BTRFS_QUOTA_CTL_DISABLE:
  3086. ret = btrfs_quota_disable(trans, root->fs_info);
  3087. break;
  3088. case BTRFS_QUOTA_CTL_RESCAN:
  3089. ret = btrfs_quota_rescan(root->fs_info);
  3090. break;
  3091. default:
  3092. ret = -EINVAL;
  3093. break;
  3094. }
  3095. if (copy_to_user(arg, sa, sizeof(*sa)))
  3096. ret = -EFAULT;
  3097. if (trans) {
  3098. err = btrfs_commit_transaction(trans, root);
  3099. if (err && !ret)
  3100. ret = err;
  3101. }
  3102. out:
  3103. kfree(sa);
  3104. drop_write:
  3105. mnt_drop_write_file(file);
  3106. return ret;
  3107. }
  3108. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3109. {
  3110. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3111. struct btrfs_ioctl_qgroup_assign_args *sa;
  3112. struct btrfs_trans_handle *trans;
  3113. int ret;
  3114. int err;
  3115. if (!capable(CAP_SYS_ADMIN))
  3116. return -EPERM;
  3117. ret = mnt_want_write_file(file);
  3118. if (ret)
  3119. return ret;
  3120. sa = memdup_user(arg, sizeof(*sa));
  3121. if (IS_ERR(sa)) {
  3122. ret = PTR_ERR(sa);
  3123. goto drop_write;
  3124. }
  3125. trans = btrfs_join_transaction(root);
  3126. if (IS_ERR(trans)) {
  3127. ret = PTR_ERR(trans);
  3128. goto out;
  3129. }
  3130. /* FIXME: check if the IDs really exist */
  3131. if (sa->assign) {
  3132. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3133. sa->src, sa->dst);
  3134. } else {
  3135. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3136. sa->src, sa->dst);
  3137. }
  3138. err = btrfs_end_transaction(trans, root);
  3139. if (err && !ret)
  3140. ret = err;
  3141. out:
  3142. kfree(sa);
  3143. drop_write:
  3144. mnt_drop_write_file(file);
  3145. return ret;
  3146. }
  3147. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3148. {
  3149. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3150. struct btrfs_ioctl_qgroup_create_args *sa;
  3151. struct btrfs_trans_handle *trans;
  3152. int ret;
  3153. int err;
  3154. if (!capable(CAP_SYS_ADMIN))
  3155. return -EPERM;
  3156. ret = mnt_want_write_file(file);
  3157. if (ret)
  3158. return ret;
  3159. sa = memdup_user(arg, sizeof(*sa));
  3160. if (IS_ERR(sa)) {
  3161. ret = PTR_ERR(sa);
  3162. goto drop_write;
  3163. }
  3164. if (!sa->qgroupid) {
  3165. ret = -EINVAL;
  3166. goto out;
  3167. }
  3168. trans = btrfs_join_transaction(root);
  3169. if (IS_ERR(trans)) {
  3170. ret = PTR_ERR(trans);
  3171. goto out;
  3172. }
  3173. /* FIXME: check if the IDs really exist */
  3174. if (sa->create) {
  3175. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  3176. NULL);
  3177. } else {
  3178. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  3179. }
  3180. err = btrfs_end_transaction(trans, root);
  3181. if (err && !ret)
  3182. ret = err;
  3183. out:
  3184. kfree(sa);
  3185. drop_write:
  3186. mnt_drop_write_file(file);
  3187. return ret;
  3188. }
  3189. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3190. {
  3191. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3192. struct btrfs_ioctl_qgroup_limit_args *sa;
  3193. struct btrfs_trans_handle *trans;
  3194. int ret;
  3195. int err;
  3196. u64 qgroupid;
  3197. if (!capable(CAP_SYS_ADMIN))
  3198. return -EPERM;
  3199. ret = mnt_want_write_file(file);
  3200. if (ret)
  3201. return ret;
  3202. sa = memdup_user(arg, sizeof(*sa));
  3203. if (IS_ERR(sa)) {
  3204. ret = PTR_ERR(sa);
  3205. goto drop_write;
  3206. }
  3207. trans = btrfs_join_transaction(root);
  3208. if (IS_ERR(trans)) {
  3209. ret = PTR_ERR(trans);
  3210. goto out;
  3211. }
  3212. qgroupid = sa->qgroupid;
  3213. if (!qgroupid) {
  3214. /* take the current subvol as qgroup */
  3215. qgroupid = root->root_key.objectid;
  3216. }
  3217. /* FIXME: check if the IDs really exist */
  3218. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  3219. err = btrfs_end_transaction(trans, root);
  3220. if (err && !ret)
  3221. ret = err;
  3222. out:
  3223. kfree(sa);
  3224. drop_write:
  3225. mnt_drop_write_file(file);
  3226. return ret;
  3227. }
  3228. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3229. void __user *arg)
  3230. {
  3231. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3232. struct inode *inode = fdentry(file)->d_inode;
  3233. struct btrfs_root *root = BTRFS_I(inode)->root;
  3234. struct btrfs_root_item *root_item = &root->root_item;
  3235. struct btrfs_trans_handle *trans;
  3236. struct timespec ct = CURRENT_TIME;
  3237. int ret = 0;
  3238. ret = mnt_want_write_file(file);
  3239. if (ret < 0)
  3240. return ret;
  3241. down_write(&root->fs_info->subvol_sem);
  3242. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  3243. ret = -EINVAL;
  3244. goto out;
  3245. }
  3246. if (btrfs_root_readonly(root)) {
  3247. ret = -EROFS;
  3248. goto out;
  3249. }
  3250. if (!inode_owner_or_capable(inode)) {
  3251. ret = -EACCES;
  3252. goto out;
  3253. }
  3254. sa = memdup_user(arg, sizeof(*sa));
  3255. if (IS_ERR(sa)) {
  3256. ret = PTR_ERR(sa);
  3257. sa = NULL;
  3258. goto out;
  3259. }
  3260. trans = btrfs_start_transaction(root, 1);
  3261. if (IS_ERR(trans)) {
  3262. ret = PTR_ERR(trans);
  3263. trans = NULL;
  3264. goto out;
  3265. }
  3266. sa->rtransid = trans->transid;
  3267. sa->rtime.sec = ct.tv_sec;
  3268. sa->rtime.nsec = ct.tv_nsec;
  3269. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3270. btrfs_set_root_stransid(root_item, sa->stransid);
  3271. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3272. root_item->stime.sec = cpu_to_le64(sa->stime.sec);
  3273. root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
  3274. root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
  3275. root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
  3276. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3277. &root->root_key, &root->root_item);
  3278. if (ret < 0) {
  3279. btrfs_end_transaction(trans, root);
  3280. trans = NULL;
  3281. goto out;
  3282. } else {
  3283. ret = btrfs_commit_transaction(trans, root);
  3284. if (ret < 0)
  3285. goto out;
  3286. }
  3287. ret = copy_to_user(arg, sa, sizeof(*sa));
  3288. if (ret)
  3289. ret = -EFAULT;
  3290. out:
  3291. kfree(sa);
  3292. up_write(&root->fs_info->subvol_sem);
  3293. mnt_drop_write_file(file);
  3294. return ret;
  3295. }
  3296. long btrfs_ioctl(struct file *file, unsigned int
  3297. cmd, unsigned long arg)
  3298. {
  3299. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  3300. void __user *argp = (void __user *)arg;
  3301. switch (cmd) {
  3302. case FS_IOC_GETFLAGS:
  3303. return btrfs_ioctl_getflags(file, argp);
  3304. case FS_IOC_SETFLAGS:
  3305. return btrfs_ioctl_setflags(file, argp);
  3306. case FS_IOC_GETVERSION:
  3307. return btrfs_ioctl_getversion(file, argp);
  3308. case FITRIM:
  3309. return btrfs_ioctl_fitrim(file, argp);
  3310. case BTRFS_IOC_SNAP_CREATE:
  3311. return btrfs_ioctl_snap_create(file, argp, 0);
  3312. case BTRFS_IOC_SNAP_CREATE_V2:
  3313. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  3314. case BTRFS_IOC_SUBVOL_CREATE:
  3315. return btrfs_ioctl_snap_create(file, argp, 1);
  3316. case BTRFS_IOC_SUBVOL_CREATE_V2:
  3317. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  3318. case BTRFS_IOC_SNAP_DESTROY:
  3319. return btrfs_ioctl_snap_destroy(file, argp);
  3320. case BTRFS_IOC_SUBVOL_GETFLAGS:
  3321. return btrfs_ioctl_subvol_getflags(file, argp);
  3322. case BTRFS_IOC_SUBVOL_SETFLAGS:
  3323. return btrfs_ioctl_subvol_setflags(file, argp);
  3324. case BTRFS_IOC_DEFAULT_SUBVOL:
  3325. return btrfs_ioctl_default_subvol(file, argp);
  3326. case BTRFS_IOC_DEFRAG:
  3327. return btrfs_ioctl_defrag(file, NULL);
  3328. case BTRFS_IOC_DEFRAG_RANGE:
  3329. return btrfs_ioctl_defrag(file, argp);
  3330. case BTRFS_IOC_RESIZE:
  3331. return btrfs_ioctl_resize(file, argp);
  3332. case BTRFS_IOC_ADD_DEV:
  3333. return btrfs_ioctl_add_dev(root, argp);
  3334. case BTRFS_IOC_RM_DEV:
  3335. return btrfs_ioctl_rm_dev(file, argp);
  3336. case BTRFS_IOC_FS_INFO:
  3337. return btrfs_ioctl_fs_info(root, argp);
  3338. case BTRFS_IOC_DEV_INFO:
  3339. return btrfs_ioctl_dev_info(root, argp);
  3340. case BTRFS_IOC_BALANCE:
  3341. return btrfs_ioctl_balance(file, NULL);
  3342. case BTRFS_IOC_CLONE:
  3343. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  3344. case BTRFS_IOC_CLONE_RANGE:
  3345. return btrfs_ioctl_clone_range(file, argp);
  3346. case BTRFS_IOC_TRANS_START:
  3347. return btrfs_ioctl_trans_start(file);
  3348. case BTRFS_IOC_TRANS_END:
  3349. return btrfs_ioctl_trans_end(file);
  3350. case BTRFS_IOC_TREE_SEARCH:
  3351. return btrfs_ioctl_tree_search(file, argp);
  3352. case BTRFS_IOC_INO_LOOKUP:
  3353. return btrfs_ioctl_ino_lookup(file, argp);
  3354. case BTRFS_IOC_INO_PATHS:
  3355. return btrfs_ioctl_ino_to_path(root, argp);
  3356. case BTRFS_IOC_LOGICAL_INO:
  3357. return btrfs_ioctl_logical_to_ino(root, argp);
  3358. case BTRFS_IOC_SPACE_INFO:
  3359. return btrfs_ioctl_space_info(root, argp);
  3360. case BTRFS_IOC_SYNC:
  3361. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  3362. return 0;
  3363. case BTRFS_IOC_START_SYNC:
  3364. return btrfs_ioctl_start_sync(root, argp);
  3365. case BTRFS_IOC_WAIT_SYNC:
  3366. return btrfs_ioctl_wait_sync(root, argp);
  3367. case BTRFS_IOC_SCRUB:
  3368. return btrfs_ioctl_scrub(file, argp);
  3369. case BTRFS_IOC_SCRUB_CANCEL:
  3370. return btrfs_ioctl_scrub_cancel(root, argp);
  3371. case BTRFS_IOC_SCRUB_PROGRESS:
  3372. return btrfs_ioctl_scrub_progress(root, argp);
  3373. case BTRFS_IOC_BALANCE_V2:
  3374. return btrfs_ioctl_balance(file, argp);
  3375. case BTRFS_IOC_BALANCE_CTL:
  3376. return btrfs_ioctl_balance_ctl(root, arg);
  3377. case BTRFS_IOC_BALANCE_PROGRESS:
  3378. return btrfs_ioctl_balance_progress(root, argp);
  3379. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  3380. return btrfs_ioctl_set_received_subvol(file, argp);
  3381. case BTRFS_IOC_SEND:
  3382. return btrfs_ioctl_send(file, argp);
  3383. case BTRFS_IOC_GET_DEV_STATS:
  3384. return btrfs_ioctl_get_dev_stats(root, argp);
  3385. case BTRFS_IOC_QUOTA_CTL:
  3386. return btrfs_ioctl_quota_ctl(file, argp);
  3387. case BTRFS_IOC_QGROUP_ASSIGN:
  3388. return btrfs_ioctl_qgroup_assign(file, argp);
  3389. case BTRFS_IOC_QGROUP_CREATE:
  3390. return btrfs_ioctl_qgroup_create(file, argp);
  3391. case BTRFS_IOC_QGROUP_LIMIT:
  3392. return btrfs_ioctl_qgroup_limit(file, argp);
  3393. case BTRFS_IOC_DEV_REPLACE:
  3394. return btrfs_ioctl_dev_replace(root, argp);
  3395. }
  3396. return -ENOTTY;
  3397. }