ioctl.c 93 KB

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