ioctl.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  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 "compat.h"
  44. #include "ctree.h"
  45. #include "disk-io.h"
  46. #include "transaction.h"
  47. #include "btrfs_inode.h"
  48. #include "ioctl.h"
  49. #include "print-tree.h"
  50. #include "volumes.h"
  51. #include "locking.h"
  52. #include "inode-map.h"
  53. /* Mask out flags that are inappropriate for the given type of inode. */
  54. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  55. {
  56. if (S_ISDIR(mode))
  57. return flags;
  58. else if (S_ISREG(mode))
  59. return flags & ~FS_DIRSYNC_FL;
  60. else
  61. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  62. }
  63. /*
  64. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  65. */
  66. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  67. {
  68. unsigned int iflags = 0;
  69. if (flags & BTRFS_INODE_SYNC)
  70. iflags |= FS_SYNC_FL;
  71. if (flags & BTRFS_INODE_IMMUTABLE)
  72. iflags |= FS_IMMUTABLE_FL;
  73. if (flags & BTRFS_INODE_APPEND)
  74. iflags |= FS_APPEND_FL;
  75. if (flags & BTRFS_INODE_NODUMP)
  76. iflags |= FS_NODUMP_FL;
  77. if (flags & BTRFS_INODE_NOATIME)
  78. iflags |= FS_NOATIME_FL;
  79. if (flags & BTRFS_INODE_DIRSYNC)
  80. iflags |= FS_DIRSYNC_FL;
  81. if (flags & BTRFS_INODE_NODATACOW)
  82. iflags |= FS_NOCOW_FL;
  83. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  84. iflags |= FS_COMPR_FL;
  85. else if (flags & BTRFS_INODE_NOCOMPRESS)
  86. iflags |= FS_NOCOMP_FL;
  87. return iflags;
  88. }
  89. /*
  90. * Update inode->i_flags based on the btrfs internal flags.
  91. */
  92. void btrfs_update_iflags(struct inode *inode)
  93. {
  94. struct btrfs_inode *ip = BTRFS_I(inode);
  95. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  96. if (ip->flags & BTRFS_INODE_SYNC)
  97. inode->i_flags |= S_SYNC;
  98. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  99. inode->i_flags |= S_IMMUTABLE;
  100. if (ip->flags & BTRFS_INODE_APPEND)
  101. inode->i_flags |= S_APPEND;
  102. if (ip->flags & BTRFS_INODE_NOATIME)
  103. inode->i_flags |= S_NOATIME;
  104. if (ip->flags & BTRFS_INODE_DIRSYNC)
  105. inode->i_flags |= S_DIRSYNC;
  106. }
  107. /*
  108. * Inherit flags from the parent inode.
  109. *
  110. * Unlike extN we don't have any flags we don't want to inherit currently.
  111. */
  112. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  113. {
  114. unsigned int flags;
  115. if (!dir)
  116. return;
  117. flags = BTRFS_I(dir)->flags;
  118. if (S_ISREG(inode->i_mode))
  119. flags &= ~BTRFS_INODE_DIRSYNC;
  120. else if (!S_ISDIR(inode->i_mode))
  121. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  122. BTRFS_I(inode)->flags = flags;
  123. btrfs_update_iflags(inode);
  124. }
  125. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  126. {
  127. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  128. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  129. if (copy_to_user(arg, &flags, sizeof(flags)))
  130. return -EFAULT;
  131. return 0;
  132. }
  133. static int check_flags(unsigned int flags)
  134. {
  135. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  136. FS_NOATIME_FL | FS_NODUMP_FL | \
  137. FS_SYNC_FL | FS_DIRSYNC_FL | \
  138. FS_NOCOMP_FL | FS_COMPR_FL |
  139. FS_NOCOW_FL))
  140. return -EOPNOTSUPP;
  141. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  142. return -EINVAL;
  143. return 0;
  144. }
  145. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  146. {
  147. struct inode *inode = file->f_path.dentry->d_inode;
  148. struct btrfs_inode *ip = BTRFS_I(inode);
  149. struct btrfs_root *root = ip->root;
  150. struct btrfs_trans_handle *trans;
  151. unsigned int flags, oldflags;
  152. int ret;
  153. if (btrfs_root_readonly(root))
  154. return -EROFS;
  155. if (copy_from_user(&flags, arg, sizeof(flags)))
  156. return -EFAULT;
  157. ret = check_flags(flags);
  158. if (ret)
  159. return ret;
  160. if (!inode_owner_or_capable(inode))
  161. return -EACCES;
  162. mutex_lock(&inode->i_mutex);
  163. flags = btrfs_mask_flags(inode->i_mode, flags);
  164. oldflags = btrfs_flags_to_ioctl(ip->flags);
  165. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  166. if (!capable(CAP_LINUX_IMMUTABLE)) {
  167. ret = -EPERM;
  168. goto out_unlock;
  169. }
  170. }
  171. ret = mnt_want_write(file->f_path.mnt);
  172. if (ret)
  173. goto out_unlock;
  174. if (flags & FS_SYNC_FL)
  175. ip->flags |= BTRFS_INODE_SYNC;
  176. else
  177. ip->flags &= ~BTRFS_INODE_SYNC;
  178. if (flags & FS_IMMUTABLE_FL)
  179. ip->flags |= BTRFS_INODE_IMMUTABLE;
  180. else
  181. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  182. if (flags & FS_APPEND_FL)
  183. ip->flags |= BTRFS_INODE_APPEND;
  184. else
  185. ip->flags &= ~BTRFS_INODE_APPEND;
  186. if (flags & FS_NODUMP_FL)
  187. ip->flags |= BTRFS_INODE_NODUMP;
  188. else
  189. ip->flags &= ~BTRFS_INODE_NODUMP;
  190. if (flags & FS_NOATIME_FL)
  191. ip->flags |= BTRFS_INODE_NOATIME;
  192. else
  193. ip->flags &= ~BTRFS_INODE_NOATIME;
  194. if (flags & FS_DIRSYNC_FL)
  195. ip->flags |= BTRFS_INODE_DIRSYNC;
  196. else
  197. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  198. if (flags & FS_NOCOW_FL)
  199. ip->flags |= BTRFS_INODE_NODATACOW;
  200. else
  201. ip->flags &= ~BTRFS_INODE_NODATACOW;
  202. /*
  203. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  204. * flag may be changed automatically if compression code won't make
  205. * things smaller.
  206. */
  207. if (flags & FS_NOCOMP_FL) {
  208. ip->flags &= ~BTRFS_INODE_COMPRESS;
  209. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  210. } else if (flags & FS_COMPR_FL) {
  211. ip->flags |= BTRFS_INODE_COMPRESS;
  212. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  213. } else {
  214. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  215. }
  216. trans = btrfs_join_transaction(root, 1);
  217. BUG_ON(IS_ERR(trans));
  218. ret = btrfs_update_inode(trans, root, inode);
  219. BUG_ON(ret);
  220. btrfs_update_iflags(inode);
  221. inode->i_ctime = CURRENT_TIME;
  222. btrfs_end_transaction(trans, root);
  223. mnt_drop_write(file->f_path.mnt);
  224. ret = 0;
  225. out_unlock:
  226. mutex_unlock(&inode->i_mutex);
  227. return ret;
  228. }
  229. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  230. {
  231. struct inode *inode = file->f_path.dentry->d_inode;
  232. return put_user(inode->i_generation, arg);
  233. }
  234. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  235. {
  236. struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
  237. struct btrfs_fs_info *fs_info = root->fs_info;
  238. struct btrfs_device *device;
  239. struct request_queue *q;
  240. struct fstrim_range range;
  241. u64 minlen = ULLONG_MAX;
  242. u64 num_devices = 0;
  243. int ret;
  244. if (!capable(CAP_SYS_ADMIN))
  245. return -EPERM;
  246. rcu_read_lock();
  247. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  248. dev_list) {
  249. if (!device->bdev)
  250. continue;
  251. q = bdev_get_queue(device->bdev);
  252. if (blk_queue_discard(q)) {
  253. num_devices++;
  254. minlen = min((u64)q->limits.discard_granularity,
  255. minlen);
  256. }
  257. }
  258. rcu_read_unlock();
  259. if (!num_devices)
  260. return -EOPNOTSUPP;
  261. if (copy_from_user(&range, arg, sizeof(range)))
  262. return -EFAULT;
  263. range.minlen = max(range.minlen, minlen);
  264. ret = btrfs_trim_fs(root, &range);
  265. if (ret < 0)
  266. return ret;
  267. if (copy_to_user(arg, &range, sizeof(range)))
  268. return -EFAULT;
  269. return 0;
  270. }
  271. static noinline int create_subvol(struct btrfs_root *root,
  272. struct dentry *dentry,
  273. char *name, int namelen,
  274. u64 *async_transid)
  275. {
  276. struct btrfs_trans_handle *trans;
  277. struct btrfs_key key;
  278. struct btrfs_root_item root_item;
  279. struct btrfs_inode_item *inode_item;
  280. struct extent_buffer *leaf;
  281. struct btrfs_root *new_root;
  282. struct dentry *parent = dget_parent(dentry);
  283. struct inode *dir;
  284. int ret;
  285. int err;
  286. u64 objectid;
  287. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  288. u64 index = 0;
  289. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  290. if (ret) {
  291. dput(parent);
  292. return ret;
  293. }
  294. dir = parent->d_inode;
  295. /*
  296. * 1 - inode item
  297. * 2 - refs
  298. * 1 - root item
  299. * 2 - dir items
  300. */
  301. trans = btrfs_start_transaction(root, 6);
  302. if (IS_ERR(trans)) {
  303. dput(parent);
  304. return PTR_ERR(trans);
  305. }
  306. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  307. 0, objectid, NULL, 0, 0, 0);
  308. if (IS_ERR(leaf)) {
  309. ret = PTR_ERR(leaf);
  310. goto fail;
  311. }
  312. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  313. btrfs_set_header_bytenr(leaf, leaf->start);
  314. btrfs_set_header_generation(leaf, trans->transid);
  315. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  316. btrfs_set_header_owner(leaf, objectid);
  317. write_extent_buffer(leaf, root->fs_info->fsid,
  318. (unsigned long)btrfs_header_fsid(leaf),
  319. BTRFS_FSID_SIZE);
  320. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  321. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  322. BTRFS_UUID_SIZE);
  323. btrfs_mark_buffer_dirty(leaf);
  324. inode_item = &root_item.inode;
  325. memset(inode_item, 0, sizeof(*inode_item));
  326. inode_item->generation = cpu_to_le64(1);
  327. inode_item->size = cpu_to_le64(3);
  328. inode_item->nlink = cpu_to_le32(1);
  329. inode_item->nbytes = cpu_to_le64(root->leafsize);
  330. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  331. root_item.flags = 0;
  332. root_item.byte_limit = 0;
  333. inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
  334. btrfs_set_root_bytenr(&root_item, leaf->start);
  335. btrfs_set_root_generation(&root_item, trans->transid);
  336. btrfs_set_root_level(&root_item, 0);
  337. btrfs_set_root_refs(&root_item, 1);
  338. btrfs_set_root_used(&root_item, leaf->len);
  339. btrfs_set_root_last_snapshot(&root_item, 0);
  340. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  341. root_item.drop_level = 0;
  342. btrfs_tree_unlock(leaf);
  343. free_extent_buffer(leaf);
  344. leaf = NULL;
  345. btrfs_set_root_dirid(&root_item, new_dirid);
  346. key.objectid = objectid;
  347. key.offset = 0;
  348. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  349. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  350. &root_item);
  351. if (ret)
  352. goto fail;
  353. key.offset = (u64)-1;
  354. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  355. BUG_ON(IS_ERR(new_root));
  356. btrfs_record_root_in_trans(trans, new_root);
  357. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  358. BTRFS_I(dir)->block_group);
  359. /*
  360. * insert the directory item
  361. */
  362. ret = btrfs_set_inode_index(dir, &index);
  363. BUG_ON(ret);
  364. ret = btrfs_insert_dir_item(trans, root,
  365. name, namelen, dir, &key,
  366. BTRFS_FT_DIR, index);
  367. if (ret)
  368. goto fail;
  369. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  370. ret = btrfs_update_inode(trans, root, dir);
  371. BUG_ON(ret);
  372. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  373. objectid, root->root_key.objectid,
  374. btrfs_ino(dir), index, name, namelen);
  375. BUG_ON(ret);
  376. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  377. fail:
  378. dput(parent);
  379. if (async_transid) {
  380. *async_transid = trans->transid;
  381. err = btrfs_commit_transaction_async(trans, root, 1);
  382. } else {
  383. err = btrfs_commit_transaction(trans, root);
  384. }
  385. if (err && !ret)
  386. ret = err;
  387. return ret;
  388. }
  389. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  390. char *name, int namelen, u64 *async_transid,
  391. bool readonly)
  392. {
  393. struct inode *inode;
  394. struct dentry *parent;
  395. struct btrfs_pending_snapshot *pending_snapshot;
  396. struct btrfs_trans_handle *trans;
  397. int ret;
  398. if (!root->ref_cows)
  399. return -EINVAL;
  400. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  401. if (!pending_snapshot)
  402. return -ENOMEM;
  403. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  404. pending_snapshot->dentry = dentry;
  405. pending_snapshot->root = root;
  406. pending_snapshot->readonly = readonly;
  407. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  408. if (IS_ERR(trans)) {
  409. ret = PTR_ERR(trans);
  410. goto fail;
  411. }
  412. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  413. BUG_ON(ret);
  414. list_add(&pending_snapshot->list,
  415. &trans->transaction->pending_snapshots);
  416. if (async_transid) {
  417. *async_transid = trans->transid;
  418. ret = btrfs_commit_transaction_async(trans,
  419. root->fs_info->extent_root, 1);
  420. } else {
  421. ret = btrfs_commit_transaction(trans,
  422. root->fs_info->extent_root);
  423. }
  424. BUG_ON(ret);
  425. ret = pending_snapshot->error;
  426. if (ret)
  427. goto fail;
  428. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  429. if (ret)
  430. goto fail;
  431. parent = dget_parent(dentry);
  432. inode = btrfs_lookup_dentry(parent->d_inode, dentry);
  433. dput(parent);
  434. if (IS_ERR(inode)) {
  435. ret = PTR_ERR(inode);
  436. goto fail;
  437. }
  438. BUG_ON(!inode);
  439. d_instantiate(dentry, inode);
  440. ret = 0;
  441. fail:
  442. kfree(pending_snapshot);
  443. return ret;
  444. }
  445. /* copy of check_sticky in fs/namei.c()
  446. * It's inline, so penalty for filesystems that don't use sticky bit is
  447. * minimal.
  448. */
  449. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  450. {
  451. uid_t fsuid = current_fsuid();
  452. if (!(dir->i_mode & S_ISVTX))
  453. return 0;
  454. if (inode->i_uid == fsuid)
  455. return 0;
  456. if (dir->i_uid == fsuid)
  457. return 0;
  458. return !capable(CAP_FOWNER);
  459. }
  460. /* copy of may_delete in fs/namei.c()
  461. * Check whether we can remove a link victim from directory dir, check
  462. * whether the type of victim is right.
  463. * 1. We can't do it if dir is read-only (done in permission())
  464. * 2. We should have write and exec permissions on dir
  465. * 3. We can't remove anything from append-only dir
  466. * 4. We can't do anything with immutable dir (done in permission())
  467. * 5. If the sticky bit on dir is set we should either
  468. * a. be owner of dir, or
  469. * b. be owner of victim, or
  470. * c. have CAP_FOWNER capability
  471. * 6. If the victim is append-only or immutable we can't do antyhing with
  472. * links pointing to it.
  473. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  474. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  475. * 9. We can't remove a root or mountpoint.
  476. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  477. * nfs_async_unlink().
  478. */
  479. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  480. {
  481. int error;
  482. if (!victim->d_inode)
  483. return -ENOENT;
  484. BUG_ON(victim->d_parent->d_inode != dir);
  485. audit_inode_child(victim, dir);
  486. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  487. if (error)
  488. return error;
  489. if (IS_APPEND(dir))
  490. return -EPERM;
  491. if (btrfs_check_sticky(dir, victim->d_inode)||
  492. IS_APPEND(victim->d_inode)||
  493. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  494. return -EPERM;
  495. if (isdir) {
  496. if (!S_ISDIR(victim->d_inode->i_mode))
  497. return -ENOTDIR;
  498. if (IS_ROOT(victim))
  499. return -EBUSY;
  500. } else if (S_ISDIR(victim->d_inode->i_mode))
  501. return -EISDIR;
  502. if (IS_DEADDIR(dir))
  503. return -ENOENT;
  504. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  505. return -EBUSY;
  506. return 0;
  507. }
  508. /* copy of may_create in fs/namei.c() */
  509. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  510. {
  511. if (child->d_inode)
  512. return -EEXIST;
  513. if (IS_DEADDIR(dir))
  514. return -ENOENT;
  515. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  516. }
  517. /*
  518. * Create a new subvolume below @parent. This is largely modeled after
  519. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  520. * inside this filesystem so it's quite a bit simpler.
  521. */
  522. static noinline int btrfs_mksubvol(struct path *parent,
  523. char *name, int namelen,
  524. struct btrfs_root *snap_src,
  525. u64 *async_transid, bool readonly)
  526. {
  527. struct inode *dir = parent->dentry->d_inode;
  528. struct dentry *dentry;
  529. int error;
  530. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  531. dentry = lookup_one_len(name, parent->dentry, namelen);
  532. error = PTR_ERR(dentry);
  533. if (IS_ERR(dentry))
  534. goto out_unlock;
  535. error = -EEXIST;
  536. if (dentry->d_inode)
  537. goto out_dput;
  538. error = mnt_want_write(parent->mnt);
  539. if (error)
  540. goto out_dput;
  541. error = btrfs_may_create(dir, dentry);
  542. if (error)
  543. goto out_drop_write;
  544. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  545. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  546. goto out_up_read;
  547. if (snap_src) {
  548. error = create_snapshot(snap_src, dentry,
  549. name, namelen, async_transid, readonly);
  550. } else {
  551. error = create_subvol(BTRFS_I(dir)->root, dentry,
  552. name, namelen, async_transid);
  553. }
  554. if (!error)
  555. fsnotify_mkdir(dir, dentry);
  556. out_up_read:
  557. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  558. out_drop_write:
  559. mnt_drop_write(parent->mnt);
  560. out_dput:
  561. dput(dentry);
  562. out_unlock:
  563. mutex_unlock(&dir->i_mutex);
  564. return error;
  565. }
  566. /*
  567. * When we're defragging a range, we don't want to kick it off again
  568. * if it is really just waiting for delalloc to send it down.
  569. * If we find a nice big extent or delalloc range for the bytes in the
  570. * file you want to defrag, we return 0 to let you know to skip this
  571. * part of the file
  572. */
  573. static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
  574. {
  575. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  576. struct extent_map *em = NULL;
  577. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  578. u64 end;
  579. read_lock(&em_tree->lock);
  580. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  581. read_unlock(&em_tree->lock);
  582. if (em) {
  583. end = extent_map_end(em);
  584. free_extent_map(em);
  585. if (end - offset > thresh)
  586. return 0;
  587. }
  588. /* if we already have a nice delalloc here, just stop */
  589. thresh /= 2;
  590. end = count_range_bits(io_tree, &offset, offset + thresh,
  591. thresh, EXTENT_DELALLOC, 1);
  592. if (end >= thresh)
  593. return 0;
  594. return 1;
  595. }
  596. /*
  597. * helper function to walk through a file and find extents
  598. * newer than a specific transid, and smaller than thresh.
  599. *
  600. * This is used by the defragging code to find new and small
  601. * extents
  602. */
  603. static int find_new_extents(struct btrfs_root *root,
  604. struct inode *inode, u64 newer_than,
  605. u64 *off, int thresh)
  606. {
  607. struct btrfs_path *path;
  608. struct btrfs_key min_key;
  609. struct btrfs_key max_key;
  610. struct extent_buffer *leaf;
  611. struct btrfs_file_extent_item *extent;
  612. int type;
  613. int ret;
  614. path = btrfs_alloc_path();
  615. if (!path)
  616. return -ENOMEM;
  617. min_key.objectid = inode->i_ino;
  618. min_key.type = BTRFS_EXTENT_DATA_KEY;
  619. min_key.offset = *off;
  620. max_key.objectid = inode->i_ino;
  621. max_key.type = (u8)-1;
  622. max_key.offset = (u64)-1;
  623. path->keep_locks = 1;
  624. while(1) {
  625. ret = btrfs_search_forward(root, &min_key, &max_key,
  626. path, 0, newer_than);
  627. if (ret != 0)
  628. goto none;
  629. if (min_key.objectid != inode->i_ino)
  630. goto none;
  631. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  632. goto none;
  633. leaf = path->nodes[0];
  634. extent = btrfs_item_ptr(leaf, path->slots[0],
  635. struct btrfs_file_extent_item);
  636. type = btrfs_file_extent_type(leaf, extent);
  637. if (type == BTRFS_FILE_EXTENT_REG &&
  638. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  639. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  640. *off = min_key.offset;
  641. btrfs_free_path(path);
  642. return 0;
  643. }
  644. if (min_key.offset == (u64)-1)
  645. goto none;
  646. min_key.offset++;
  647. btrfs_release_path(path);
  648. }
  649. none:
  650. btrfs_free_path(path);
  651. return -ENOENT;
  652. }
  653. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  654. int thresh, u64 *last_len, u64 *skip,
  655. u64 *defrag_end)
  656. {
  657. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  658. struct extent_map *em = NULL;
  659. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  660. int ret = 1;
  661. /*
  662. * make sure that once we start defragging and extent, we keep on
  663. * defragging it
  664. */
  665. if (start < *defrag_end)
  666. return 1;
  667. *skip = 0;
  668. /*
  669. * hopefully we have this extent in the tree already, try without
  670. * the full extent lock
  671. */
  672. read_lock(&em_tree->lock);
  673. em = lookup_extent_mapping(em_tree, start, len);
  674. read_unlock(&em_tree->lock);
  675. if (!em) {
  676. /* get the big lock and read metadata off disk */
  677. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  678. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  679. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  680. if (IS_ERR(em))
  681. return 0;
  682. }
  683. /* this will cover holes, and inline extents */
  684. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  685. ret = 0;
  686. /*
  687. * we hit a real extent, if it is big don't bother defragging it again
  688. */
  689. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  690. ret = 0;
  691. /*
  692. * last_len ends up being a counter of how many bytes we've defragged.
  693. * every time we choose not to defrag an extent, we reset *last_len
  694. * so that the next tiny extent will force a defrag.
  695. *
  696. * The end result of this is that tiny extents before a single big
  697. * extent will force at least part of that big extent to be defragged.
  698. */
  699. if (ret) {
  700. *last_len += len;
  701. *defrag_end = extent_map_end(em);
  702. } else {
  703. *last_len = 0;
  704. *skip = extent_map_end(em);
  705. *defrag_end = 0;
  706. }
  707. free_extent_map(em);
  708. return ret;
  709. }
  710. /*
  711. * it doesn't do much good to defrag one or two pages
  712. * at a time. This pulls in a nice chunk of pages
  713. * to COW and defrag.
  714. *
  715. * It also makes sure the delalloc code has enough
  716. * dirty data to avoid making new small extents as part
  717. * of the defrag
  718. *
  719. * It's a good idea to start RA on this range
  720. * before calling this.
  721. */
  722. static int cluster_pages_for_defrag(struct inode *inode,
  723. struct page **pages,
  724. unsigned long start_index,
  725. int num_pages)
  726. {
  727. unsigned long file_end;
  728. u64 isize = i_size_read(inode);
  729. u64 page_start;
  730. u64 page_end;
  731. int ret;
  732. int i;
  733. int i_done;
  734. struct btrfs_ordered_extent *ordered;
  735. struct extent_state *cached_state = NULL;
  736. if (isize == 0)
  737. return 0;
  738. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  739. ret = btrfs_delalloc_reserve_space(inode,
  740. num_pages << PAGE_CACHE_SHIFT);
  741. if (ret)
  742. return ret;
  743. again:
  744. ret = 0;
  745. i_done = 0;
  746. /* step one, lock all the pages */
  747. for (i = 0; i < num_pages; i++) {
  748. struct page *page;
  749. page = grab_cache_page(inode->i_mapping,
  750. start_index + i);
  751. if (!page)
  752. break;
  753. if (!PageUptodate(page)) {
  754. btrfs_readpage(NULL, page);
  755. lock_page(page);
  756. if (!PageUptodate(page)) {
  757. unlock_page(page);
  758. page_cache_release(page);
  759. ret = -EIO;
  760. break;
  761. }
  762. }
  763. isize = i_size_read(inode);
  764. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  765. if (!isize || page->index > file_end ||
  766. page->mapping != inode->i_mapping) {
  767. /* whoops, we blew past eof, skip this page */
  768. unlock_page(page);
  769. page_cache_release(page);
  770. break;
  771. }
  772. pages[i] = page;
  773. i_done++;
  774. }
  775. if (!i_done || ret)
  776. goto out;
  777. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  778. goto out;
  779. /*
  780. * so now we have a nice long stream of locked
  781. * and up to date pages, lets wait on them
  782. */
  783. for (i = 0; i < i_done; i++)
  784. wait_on_page_writeback(pages[i]);
  785. page_start = page_offset(pages[0]);
  786. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  787. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  788. page_start, page_end - 1, 0, &cached_state,
  789. GFP_NOFS);
  790. ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
  791. if (ordered &&
  792. ordered->file_offset + ordered->len > page_start &&
  793. ordered->file_offset < page_end) {
  794. btrfs_put_ordered_extent(ordered);
  795. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  796. page_start, page_end - 1,
  797. &cached_state, GFP_NOFS);
  798. for (i = 0; i < i_done; i++) {
  799. unlock_page(pages[i]);
  800. page_cache_release(pages[i]);
  801. }
  802. btrfs_wait_ordered_range(inode, page_start,
  803. page_end - page_start);
  804. goto again;
  805. }
  806. if (ordered)
  807. btrfs_put_ordered_extent(ordered);
  808. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  809. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  810. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  811. GFP_NOFS);
  812. if (i_done != num_pages) {
  813. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  814. btrfs_delalloc_release_space(inode,
  815. (num_pages - i_done) << PAGE_CACHE_SHIFT);
  816. }
  817. btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
  818. &cached_state);
  819. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  820. page_start, page_end - 1, &cached_state,
  821. GFP_NOFS);
  822. for (i = 0; i < i_done; i++) {
  823. clear_page_dirty_for_io(pages[i]);
  824. ClearPageChecked(pages[i]);
  825. set_page_extent_mapped(pages[i]);
  826. set_page_dirty(pages[i]);
  827. unlock_page(pages[i]);
  828. page_cache_release(pages[i]);
  829. }
  830. return i_done;
  831. out:
  832. for (i = 0; i < i_done; i++) {
  833. unlock_page(pages[i]);
  834. page_cache_release(pages[i]);
  835. }
  836. btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
  837. return ret;
  838. }
  839. int btrfs_defrag_file(struct inode *inode, struct file *file,
  840. struct btrfs_ioctl_defrag_range_args *range,
  841. u64 newer_than, unsigned long max_to_defrag)
  842. {
  843. struct btrfs_root *root = BTRFS_I(inode)->root;
  844. struct btrfs_super_block *disk_super;
  845. struct file_ra_state *ra = NULL;
  846. unsigned long last_index;
  847. u64 features;
  848. u64 last_len = 0;
  849. u64 skip = 0;
  850. u64 defrag_end = 0;
  851. u64 newer_off = range->start;
  852. int newer_left = 0;
  853. unsigned long i;
  854. int ret;
  855. int defrag_count = 0;
  856. int compress_type = BTRFS_COMPRESS_ZLIB;
  857. int extent_thresh = range->extent_thresh;
  858. int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  859. u64 new_align = ~((u64)128 * 1024 - 1);
  860. struct page **pages = NULL;
  861. if (extent_thresh == 0)
  862. extent_thresh = 256 * 1024;
  863. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  864. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  865. return -EINVAL;
  866. if (range->compress_type)
  867. compress_type = range->compress_type;
  868. }
  869. if (inode->i_size == 0)
  870. return 0;
  871. /*
  872. * if we were not given a file, allocate a readahead
  873. * context
  874. */
  875. if (!file) {
  876. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  877. if (!ra)
  878. return -ENOMEM;
  879. file_ra_state_init(ra, inode->i_mapping);
  880. } else {
  881. ra = &file->f_ra;
  882. }
  883. pages = kmalloc(sizeof(struct page *) * newer_cluster,
  884. GFP_NOFS);
  885. if (!pages) {
  886. ret = -ENOMEM;
  887. goto out_ra;
  888. }
  889. /* find the last page to defrag */
  890. if (range->start + range->len > range->start) {
  891. last_index = min_t(u64, inode->i_size - 1,
  892. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  893. } else {
  894. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  895. }
  896. if (newer_than) {
  897. ret = find_new_extents(root, inode, newer_than,
  898. &newer_off, 64 * 1024);
  899. if (!ret) {
  900. range->start = newer_off;
  901. /*
  902. * we always align our defrag to help keep
  903. * the extents in the file evenly spaced
  904. */
  905. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  906. newer_left = newer_cluster;
  907. } else
  908. goto out_ra;
  909. } else {
  910. i = range->start >> PAGE_CACHE_SHIFT;
  911. }
  912. if (!max_to_defrag)
  913. max_to_defrag = last_index - 1;
  914. while (i <= last_index && defrag_count < max_to_defrag) {
  915. /*
  916. * make sure we stop running if someone unmounts
  917. * the FS
  918. */
  919. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  920. break;
  921. if (!newer_than &&
  922. !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  923. PAGE_CACHE_SIZE,
  924. extent_thresh,
  925. &last_len, &skip,
  926. &defrag_end)) {
  927. unsigned long next;
  928. /*
  929. * the should_defrag function tells us how much to skip
  930. * bump our counter by the suggested amount
  931. */
  932. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  933. i = max(i + 1, next);
  934. continue;
  935. }
  936. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  937. BTRFS_I(inode)->force_compress = compress_type;
  938. btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
  939. ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
  940. if (ret < 0)
  941. goto out_ra;
  942. defrag_count += ret;
  943. balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
  944. i += ret;
  945. if (newer_than) {
  946. if (newer_off == (u64)-1)
  947. break;
  948. newer_off = max(newer_off + 1,
  949. (u64)i << PAGE_CACHE_SHIFT);
  950. ret = find_new_extents(root, inode,
  951. newer_than, &newer_off,
  952. 64 * 1024);
  953. if (!ret) {
  954. range->start = newer_off;
  955. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  956. newer_left = newer_cluster;
  957. } else {
  958. break;
  959. }
  960. } else {
  961. i++;
  962. }
  963. }
  964. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  965. filemap_flush(inode->i_mapping);
  966. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  967. /* the filemap_flush will queue IO into the worker threads, but
  968. * we have to make sure the IO is actually started and that
  969. * ordered extents get created before we return
  970. */
  971. atomic_inc(&root->fs_info->async_submit_draining);
  972. while (atomic_read(&root->fs_info->nr_async_submits) ||
  973. atomic_read(&root->fs_info->async_delalloc_pages)) {
  974. wait_event(root->fs_info->async_submit_wait,
  975. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  976. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  977. }
  978. atomic_dec(&root->fs_info->async_submit_draining);
  979. mutex_lock(&inode->i_mutex);
  980. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  981. mutex_unlock(&inode->i_mutex);
  982. }
  983. disk_super = &root->fs_info->super_copy;
  984. features = btrfs_super_incompat_flags(disk_super);
  985. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  986. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  987. btrfs_set_super_incompat_flags(disk_super, features);
  988. }
  989. if (!file)
  990. kfree(ra);
  991. return defrag_count;
  992. out_ra:
  993. if (!file)
  994. kfree(ra);
  995. kfree(pages);
  996. return ret;
  997. }
  998. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  999. void __user *arg)
  1000. {
  1001. u64 new_size;
  1002. u64 old_size;
  1003. u64 devid = 1;
  1004. struct btrfs_ioctl_vol_args *vol_args;
  1005. struct btrfs_trans_handle *trans;
  1006. struct btrfs_device *device = NULL;
  1007. char *sizestr;
  1008. char *devstr = NULL;
  1009. int ret = 0;
  1010. int mod = 0;
  1011. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1012. return -EROFS;
  1013. if (!capable(CAP_SYS_ADMIN))
  1014. return -EPERM;
  1015. vol_args = memdup_user(arg, sizeof(*vol_args));
  1016. if (IS_ERR(vol_args))
  1017. return PTR_ERR(vol_args);
  1018. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1019. mutex_lock(&root->fs_info->volume_mutex);
  1020. sizestr = vol_args->name;
  1021. devstr = strchr(sizestr, ':');
  1022. if (devstr) {
  1023. char *end;
  1024. sizestr = devstr + 1;
  1025. *devstr = '\0';
  1026. devstr = vol_args->name;
  1027. devid = simple_strtoull(devstr, &end, 10);
  1028. printk(KERN_INFO "resizing devid %llu\n",
  1029. (unsigned long long)devid);
  1030. }
  1031. device = btrfs_find_device(root, devid, NULL, NULL);
  1032. if (!device) {
  1033. printk(KERN_INFO "resizer unable to find device %llu\n",
  1034. (unsigned long long)devid);
  1035. ret = -EINVAL;
  1036. goto out_unlock;
  1037. }
  1038. if (!strcmp(sizestr, "max"))
  1039. new_size = device->bdev->bd_inode->i_size;
  1040. else {
  1041. if (sizestr[0] == '-') {
  1042. mod = -1;
  1043. sizestr++;
  1044. } else if (sizestr[0] == '+') {
  1045. mod = 1;
  1046. sizestr++;
  1047. }
  1048. new_size = memparse(sizestr, NULL);
  1049. if (new_size == 0) {
  1050. ret = -EINVAL;
  1051. goto out_unlock;
  1052. }
  1053. }
  1054. old_size = device->total_bytes;
  1055. if (mod < 0) {
  1056. if (new_size > old_size) {
  1057. ret = -EINVAL;
  1058. goto out_unlock;
  1059. }
  1060. new_size = old_size - new_size;
  1061. } else if (mod > 0) {
  1062. new_size = old_size + new_size;
  1063. }
  1064. if (new_size < 256 * 1024 * 1024) {
  1065. ret = -EINVAL;
  1066. goto out_unlock;
  1067. }
  1068. if (new_size > device->bdev->bd_inode->i_size) {
  1069. ret = -EFBIG;
  1070. goto out_unlock;
  1071. }
  1072. do_div(new_size, root->sectorsize);
  1073. new_size *= root->sectorsize;
  1074. printk(KERN_INFO "new size for %s is %llu\n",
  1075. device->name, (unsigned long long)new_size);
  1076. if (new_size > old_size) {
  1077. trans = btrfs_start_transaction(root, 0);
  1078. if (IS_ERR(trans)) {
  1079. ret = PTR_ERR(trans);
  1080. goto out_unlock;
  1081. }
  1082. ret = btrfs_grow_device(trans, device, new_size);
  1083. btrfs_commit_transaction(trans, root);
  1084. } else {
  1085. ret = btrfs_shrink_device(device, new_size);
  1086. }
  1087. out_unlock:
  1088. mutex_unlock(&root->fs_info->volume_mutex);
  1089. kfree(vol_args);
  1090. return ret;
  1091. }
  1092. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1093. char *name,
  1094. unsigned long fd,
  1095. int subvol,
  1096. u64 *transid,
  1097. bool readonly)
  1098. {
  1099. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1100. struct file *src_file;
  1101. int namelen;
  1102. int ret = 0;
  1103. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1104. return -EROFS;
  1105. namelen = strlen(name);
  1106. if (strchr(name, '/')) {
  1107. ret = -EINVAL;
  1108. goto out;
  1109. }
  1110. if (subvol) {
  1111. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1112. NULL, transid, readonly);
  1113. } else {
  1114. struct inode *src_inode;
  1115. src_file = fget(fd);
  1116. if (!src_file) {
  1117. ret = -EINVAL;
  1118. goto out;
  1119. }
  1120. src_inode = src_file->f_path.dentry->d_inode;
  1121. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1122. printk(KERN_INFO "btrfs: Snapshot src from "
  1123. "another FS\n");
  1124. ret = -EINVAL;
  1125. fput(src_file);
  1126. goto out;
  1127. }
  1128. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1129. BTRFS_I(src_inode)->root,
  1130. transid, readonly);
  1131. fput(src_file);
  1132. }
  1133. out:
  1134. return ret;
  1135. }
  1136. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1137. void __user *arg, int subvol)
  1138. {
  1139. struct btrfs_ioctl_vol_args *vol_args;
  1140. int ret;
  1141. vol_args = memdup_user(arg, sizeof(*vol_args));
  1142. if (IS_ERR(vol_args))
  1143. return PTR_ERR(vol_args);
  1144. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1145. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1146. vol_args->fd, subvol,
  1147. NULL, false);
  1148. kfree(vol_args);
  1149. return ret;
  1150. }
  1151. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1152. void __user *arg, int subvol)
  1153. {
  1154. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1155. int ret;
  1156. u64 transid = 0;
  1157. u64 *ptr = NULL;
  1158. bool readonly = false;
  1159. vol_args = memdup_user(arg, sizeof(*vol_args));
  1160. if (IS_ERR(vol_args))
  1161. return PTR_ERR(vol_args);
  1162. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1163. if (vol_args->flags &
  1164. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  1165. ret = -EOPNOTSUPP;
  1166. goto out;
  1167. }
  1168. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1169. ptr = &transid;
  1170. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1171. readonly = true;
  1172. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1173. vol_args->fd, subvol,
  1174. ptr, readonly);
  1175. if (ret == 0 && ptr &&
  1176. copy_to_user(arg +
  1177. offsetof(struct btrfs_ioctl_vol_args_v2,
  1178. transid), ptr, sizeof(*ptr)))
  1179. ret = -EFAULT;
  1180. out:
  1181. kfree(vol_args);
  1182. return ret;
  1183. }
  1184. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1185. void __user *arg)
  1186. {
  1187. struct inode *inode = fdentry(file)->d_inode;
  1188. struct btrfs_root *root = BTRFS_I(inode)->root;
  1189. int ret = 0;
  1190. u64 flags = 0;
  1191. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1192. return -EINVAL;
  1193. down_read(&root->fs_info->subvol_sem);
  1194. if (btrfs_root_readonly(root))
  1195. flags |= BTRFS_SUBVOL_RDONLY;
  1196. up_read(&root->fs_info->subvol_sem);
  1197. if (copy_to_user(arg, &flags, sizeof(flags)))
  1198. ret = -EFAULT;
  1199. return ret;
  1200. }
  1201. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1202. void __user *arg)
  1203. {
  1204. struct inode *inode = fdentry(file)->d_inode;
  1205. struct btrfs_root *root = BTRFS_I(inode)->root;
  1206. struct btrfs_trans_handle *trans;
  1207. u64 root_flags;
  1208. u64 flags;
  1209. int ret = 0;
  1210. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1211. return -EROFS;
  1212. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1213. return -EINVAL;
  1214. if (copy_from_user(&flags, arg, sizeof(flags)))
  1215. return -EFAULT;
  1216. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1217. return -EINVAL;
  1218. if (flags & ~BTRFS_SUBVOL_RDONLY)
  1219. return -EOPNOTSUPP;
  1220. if (!inode_owner_or_capable(inode))
  1221. return -EACCES;
  1222. down_write(&root->fs_info->subvol_sem);
  1223. /* nothing to do */
  1224. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1225. goto out;
  1226. root_flags = btrfs_root_flags(&root->root_item);
  1227. if (flags & BTRFS_SUBVOL_RDONLY)
  1228. btrfs_set_root_flags(&root->root_item,
  1229. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1230. else
  1231. btrfs_set_root_flags(&root->root_item,
  1232. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1233. trans = btrfs_start_transaction(root, 1);
  1234. if (IS_ERR(trans)) {
  1235. ret = PTR_ERR(trans);
  1236. goto out_reset;
  1237. }
  1238. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1239. &root->root_key, &root->root_item);
  1240. btrfs_commit_transaction(trans, root);
  1241. out_reset:
  1242. if (ret)
  1243. btrfs_set_root_flags(&root->root_item, root_flags);
  1244. out:
  1245. up_write(&root->fs_info->subvol_sem);
  1246. return ret;
  1247. }
  1248. /*
  1249. * helper to check if the subvolume references other subvolumes
  1250. */
  1251. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1252. {
  1253. struct btrfs_path *path;
  1254. struct btrfs_key key;
  1255. int ret;
  1256. path = btrfs_alloc_path();
  1257. if (!path)
  1258. return -ENOMEM;
  1259. key.objectid = root->root_key.objectid;
  1260. key.type = BTRFS_ROOT_REF_KEY;
  1261. key.offset = (u64)-1;
  1262. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1263. &key, path, 0, 0);
  1264. if (ret < 0)
  1265. goto out;
  1266. BUG_ON(ret == 0);
  1267. ret = 0;
  1268. if (path->slots[0] > 0) {
  1269. path->slots[0]--;
  1270. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1271. if (key.objectid == root->root_key.objectid &&
  1272. key.type == BTRFS_ROOT_REF_KEY)
  1273. ret = -ENOTEMPTY;
  1274. }
  1275. out:
  1276. btrfs_free_path(path);
  1277. return ret;
  1278. }
  1279. static noinline int key_in_sk(struct btrfs_key *key,
  1280. struct btrfs_ioctl_search_key *sk)
  1281. {
  1282. struct btrfs_key test;
  1283. int ret;
  1284. test.objectid = sk->min_objectid;
  1285. test.type = sk->min_type;
  1286. test.offset = sk->min_offset;
  1287. ret = btrfs_comp_cpu_keys(key, &test);
  1288. if (ret < 0)
  1289. return 0;
  1290. test.objectid = sk->max_objectid;
  1291. test.type = sk->max_type;
  1292. test.offset = sk->max_offset;
  1293. ret = btrfs_comp_cpu_keys(key, &test);
  1294. if (ret > 0)
  1295. return 0;
  1296. return 1;
  1297. }
  1298. static noinline int copy_to_sk(struct btrfs_root *root,
  1299. struct btrfs_path *path,
  1300. struct btrfs_key *key,
  1301. struct btrfs_ioctl_search_key *sk,
  1302. char *buf,
  1303. unsigned long *sk_offset,
  1304. int *num_found)
  1305. {
  1306. u64 found_transid;
  1307. struct extent_buffer *leaf;
  1308. struct btrfs_ioctl_search_header sh;
  1309. unsigned long item_off;
  1310. unsigned long item_len;
  1311. int nritems;
  1312. int i;
  1313. int slot;
  1314. int ret = 0;
  1315. leaf = path->nodes[0];
  1316. slot = path->slots[0];
  1317. nritems = btrfs_header_nritems(leaf);
  1318. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1319. i = nritems;
  1320. goto advance_key;
  1321. }
  1322. found_transid = btrfs_header_generation(leaf);
  1323. for (i = slot; i < nritems; i++) {
  1324. item_off = btrfs_item_ptr_offset(leaf, i);
  1325. item_len = btrfs_item_size_nr(leaf, i);
  1326. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1327. item_len = 0;
  1328. if (sizeof(sh) + item_len + *sk_offset >
  1329. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1330. ret = 1;
  1331. goto overflow;
  1332. }
  1333. btrfs_item_key_to_cpu(leaf, key, i);
  1334. if (!key_in_sk(key, sk))
  1335. continue;
  1336. sh.objectid = key->objectid;
  1337. sh.offset = key->offset;
  1338. sh.type = key->type;
  1339. sh.len = item_len;
  1340. sh.transid = found_transid;
  1341. /* copy search result header */
  1342. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1343. *sk_offset += sizeof(sh);
  1344. if (item_len) {
  1345. char *p = buf + *sk_offset;
  1346. /* copy the item */
  1347. read_extent_buffer(leaf, p,
  1348. item_off, item_len);
  1349. *sk_offset += item_len;
  1350. }
  1351. (*num_found)++;
  1352. if (*num_found >= sk->nr_items)
  1353. break;
  1354. }
  1355. advance_key:
  1356. ret = 0;
  1357. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1358. key->offset++;
  1359. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1360. key->offset = 0;
  1361. key->type++;
  1362. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1363. key->offset = 0;
  1364. key->type = 0;
  1365. key->objectid++;
  1366. } else
  1367. ret = 1;
  1368. overflow:
  1369. return ret;
  1370. }
  1371. static noinline int search_ioctl(struct inode *inode,
  1372. struct btrfs_ioctl_search_args *args)
  1373. {
  1374. struct btrfs_root *root;
  1375. struct btrfs_key key;
  1376. struct btrfs_key max_key;
  1377. struct btrfs_path *path;
  1378. struct btrfs_ioctl_search_key *sk = &args->key;
  1379. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1380. int ret;
  1381. int num_found = 0;
  1382. unsigned long sk_offset = 0;
  1383. path = btrfs_alloc_path();
  1384. if (!path)
  1385. return -ENOMEM;
  1386. if (sk->tree_id == 0) {
  1387. /* search the root of the inode that was passed */
  1388. root = BTRFS_I(inode)->root;
  1389. } else {
  1390. key.objectid = sk->tree_id;
  1391. key.type = BTRFS_ROOT_ITEM_KEY;
  1392. key.offset = (u64)-1;
  1393. root = btrfs_read_fs_root_no_name(info, &key);
  1394. if (IS_ERR(root)) {
  1395. printk(KERN_ERR "could not find root %llu\n",
  1396. sk->tree_id);
  1397. btrfs_free_path(path);
  1398. return -ENOENT;
  1399. }
  1400. }
  1401. key.objectid = sk->min_objectid;
  1402. key.type = sk->min_type;
  1403. key.offset = sk->min_offset;
  1404. max_key.objectid = sk->max_objectid;
  1405. max_key.type = sk->max_type;
  1406. max_key.offset = sk->max_offset;
  1407. path->keep_locks = 1;
  1408. while(1) {
  1409. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1410. sk->min_transid);
  1411. if (ret != 0) {
  1412. if (ret > 0)
  1413. ret = 0;
  1414. goto err;
  1415. }
  1416. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1417. &sk_offset, &num_found);
  1418. btrfs_release_path(path);
  1419. if (ret || num_found >= sk->nr_items)
  1420. break;
  1421. }
  1422. ret = 0;
  1423. err:
  1424. sk->nr_items = num_found;
  1425. btrfs_free_path(path);
  1426. return ret;
  1427. }
  1428. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1429. void __user *argp)
  1430. {
  1431. struct btrfs_ioctl_search_args *args;
  1432. struct inode *inode;
  1433. int ret;
  1434. if (!capable(CAP_SYS_ADMIN))
  1435. return -EPERM;
  1436. args = memdup_user(argp, sizeof(*args));
  1437. if (IS_ERR(args))
  1438. return PTR_ERR(args);
  1439. inode = fdentry(file)->d_inode;
  1440. ret = search_ioctl(inode, args);
  1441. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1442. ret = -EFAULT;
  1443. kfree(args);
  1444. return ret;
  1445. }
  1446. /*
  1447. * Search INODE_REFs to identify path name of 'dirid' directory
  1448. * in a 'tree_id' tree. and sets path name to 'name'.
  1449. */
  1450. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1451. u64 tree_id, u64 dirid, char *name)
  1452. {
  1453. struct btrfs_root *root;
  1454. struct btrfs_key key;
  1455. char *ptr;
  1456. int ret = -1;
  1457. int slot;
  1458. int len;
  1459. int total_len = 0;
  1460. struct btrfs_inode_ref *iref;
  1461. struct extent_buffer *l;
  1462. struct btrfs_path *path;
  1463. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1464. name[0]='\0';
  1465. return 0;
  1466. }
  1467. path = btrfs_alloc_path();
  1468. if (!path)
  1469. return -ENOMEM;
  1470. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1471. key.objectid = tree_id;
  1472. key.type = BTRFS_ROOT_ITEM_KEY;
  1473. key.offset = (u64)-1;
  1474. root = btrfs_read_fs_root_no_name(info, &key);
  1475. if (IS_ERR(root)) {
  1476. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1477. ret = -ENOENT;
  1478. goto out;
  1479. }
  1480. key.objectid = dirid;
  1481. key.type = BTRFS_INODE_REF_KEY;
  1482. key.offset = (u64)-1;
  1483. while(1) {
  1484. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1485. if (ret < 0)
  1486. goto out;
  1487. l = path->nodes[0];
  1488. slot = path->slots[0];
  1489. if (ret > 0 && slot > 0)
  1490. slot--;
  1491. btrfs_item_key_to_cpu(l, &key, slot);
  1492. if (ret > 0 && (key.objectid != dirid ||
  1493. key.type != BTRFS_INODE_REF_KEY)) {
  1494. ret = -ENOENT;
  1495. goto out;
  1496. }
  1497. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1498. len = btrfs_inode_ref_name_len(l, iref);
  1499. ptr -= len + 1;
  1500. total_len += len + 1;
  1501. if (ptr < name)
  1502. goto out;
  1503. *(ptr + len) = '/';
  1504. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1505. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1506. break;
  1507. btrfs_release_path(path);
  1508. key.objectid = key.offset;
  1509. key.offset = (u64)-1;
  1510. dirid = key.objectid;
  1511. }
  1512. if (ptr < name)
  1513. goto out;
  1514. memcpy(name, ptr, total_len);
  1515. name[total_len]='\0';
  1516. ret = 0;
  1517. out:
  1518. btrfs_free_path(path);
  1519. return ret;
  1520. }
  1521. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1522. void __user *argp)
  1523. {
  1524. struct btrfs_ioctl_ino_lookup_args *args;
  1525. struct inode *inode;
  1526. int ret;
  1527. if (!capable(CAP_SYS_ADMIN))
  1528. return -EPERM;
  1529. args = memdup_user(argp, sizeof(*args));
  1530. if (IS_ERR(args))
  1531. return PTR_ERR(args);
  1532. inode = fdentry(file)->d_inode;
  1533. if (args->treeid == 0)
  1534. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1535. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1536. args->treeid, args->objectid,
  1537. args->name);
  1538. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1539. ret = -EFAULT;
  1540. kfree(args);
  1541. return ret;
  1542. }
  1543. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1544. void __user *arg)
  1545. {
  1546. struct dentry *parent = fdentry(file);
  1547. struct dentry *dentry;
  1548. struct inode *dir = parent->d_inode;
  1549. struct inode *inode;
  1550. struct btrfs_root *root = BTRFS_I(dir)->root;
  1551. struct btrfs_root *dest = NULL;
  1552. struct btrfs_ioctl_vol_args *vol_args;
  1553. struct btrfs_trans_handle *trans;
  1554. int namelen;
  1555. int ret;
  1556. int err = 0;
  1557. vol_args = memdup_user(arg, sizeof(*vol_args));
  1558. if (IS_ERR(vol_args))
  1559. return PTR_ERR(vol_args);
  1560. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1561. namelen = strlen(vol_args->name);
  1562. if (strchr(vol_args->name, '/') ||
  1563. strncmp(vol_args->name, "..", namelen) == 0) {
  1564. err = -EINVAL;
  1565. goto out;
  1566. }
  1567. err = mnt_want_write(file->f_path.mnt);
  1568. if (err)
  1569. goto out;
  1570. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1571. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1572. if (IS_ERR(dentry)) {
  1573. err = PTR_ERR(dentry);
  1574. goto out_unlock_dir;
  1575. }
  1576. if (!dentry->d_inode) {
  1577. err = -ENOENT;
  1578. goto out_dput;
  1579. }
  1580. inode = dentry->d_inode;
  1581. dest = BTRFS_I(inode)->root;
  1582. if (!capable(CAP_SYS_ADMIN)){
  1583. /*
  1584. * Regular user. Only allow this with a special mount
  1585. * option, when the user has write+exec access to the
  1586. * subvol root, and when rmdir(2) would have been
  1587. * allowed.
  1588. *
  1589. * Note that this is _not_ check that the subvol is
  1590. * empty or doesn't contain data that we wouldn't
  1591. * otherwise be able to delete.
  1592. *
  1593. * Users who want to delete empty subvols should try
  1594. * rmdir(2).
  1595. */
  1596. err = -EPERM;
  1597. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1598. goto out_dput;
  1599. /*
  1600. * Do not allow deletion if the parent dir is the same
  1601. * as the dir to be deleted. That means the ioctl
  1602. * must be called on the dentry referencing the root
  1603. * of the subvol, not a random directory contained
  1604. * within it.
  1605. */
  1606. err = -EINVAL;
  1607. if (root == dest)
  1608. goto out_dput;
  1609. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1610. if (err)
  1611. goto out_dput;
  1612. /* check if subvolume may be deleted by a non-root user */
  1613. err = btrfs_may_delete(dir, dentry, 1);
  1614. if (err)
  1615. goto out_dput;
  1616. }
  1617. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1618. err = -EINVAL;
  1619. goto out_dput;
  1620. }
  1621. mutex_lock(&inode->i_mutex);
  1622. err = d_invalidate(dentry);
  1623. if (err)
  1624. goto out_unlock;
  1625. down_write(&root->fs_info->subvol_sem);
  1626. err = may_destroy_subvol(dest);
  1627. if (err)
  1628. goto out_up_write;
  1629. trans = btrfs_start_transaction(root, 0);
  1630. if (IS_ERR(trans)) {
  1631. err = PTR_ERR(trans);
  1632. goto out_up_write;
  1633. }
  1634. trans->block_rsv = &root->fs_info->global_block_rsv;
  1635. ret = btrfs_unlink_subvol(trans, root, dir,
  1636. dest->root_key.objectid,
  1637. dentry->d_name.name,
  1638. dentry->d_name.len);
  1639. BUG_ON(ret);
  1640. btrfs_record_root_in_trans(trans, dest);
  1641. memset(&dest->root_item.drop_progress, 0,
  1642. sizeof(dest->root_item.drop_progress));
  1643. dest->root_item.drop_level = 0;
  1644. btrfs_set_root_refs(&dest->root_item, 0);
  1645. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1646. ret = btrfs_insert_orphan_item(trans,
  1647. root->fs_info->tree_root,
  1648. dest->root_key.objectid);
  1649. BUG_ON(ret);
  1650. }
  1651. ret = btrfs_end_transaction(trans, root);
  1652. BUG_ON(ret);
  1653. inode->i_flags |= S_DEAD;
  1654. out_up_write:
  1655. up_write(&root->fs_info->subvol_sem);
  1656. out_unlock:
  1657. mutex_unlock(&inode->i_mutex);
  1658. if (!err) {
  1659. shrink_dcache_sb(root->fs_info->sb);
  1660. btrfs_invalidate_inodes(dest);
  1661. d_delete(dentry);
  1662. }
  1663. out_dput:
  1664. dput(dentry);
  1665. out_unlock_dir:
  1666. mutex_unlock(&dir->i_mutex);
  1667. mnt_drop_write(file->f_path.mnt);
  1668. out:
  1669. kfree(vol_args);
  1670. return err;
  1671. }
  1672. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1673. {
  1674. struct inode *inode = fdentry(file)->d_inode;
  1675. struct btrfs_root *root = BTRFS_I(inode)->root;
  1676. struct btrfs_ioctl_defrag_range_args *range;
  1677. int ret;
  1678. if (btrfs_root_readonly(root))
  1679. return -EROFS;
  1680. ret = mnt_want_write(file->f_path.mnt);
  1681. if (ret)
  1682. return ret;
  1683. switch (inode->i_mode & S_IFMT) {
  1684. case S_IFDIR:
  1685. if (!capable(CAP_SYS_ADMIN)) {
  1686. ret = -EPERM;
  1687. goto out;
  1688. }
  1689. ret = btrfs_defrag_root(root, 0);
  1690. if (ret)
  1691. goto out;
  1692. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1693. break;
  1694. case S_IFREG:
  1695. if (!(file->f_mode & FMODE_WRITE)) {
  1696. ret = -EINVAL;
  1697. goto out;
  1698. }
  1699. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1700. if (!range) {
  1701. ret = -ENOMEM;
  1702. goto out;
  1703. }
  1704. if (argp) {
  1705. if (copy_from_user(range, argp,
  1706. sizeof(*range))) {
  1707. ret = -EFAULT;
  1708. kfree(range);
  1709. goto out;
  1710. }
  1711. /* compression requires us to start the IO */
  1712. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1713. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1714. range->extent_thresh = (u32)-1;
  1715. }
  1716. } else {
  1717. /* the rest are all set to zero by kzalloc */
  1718. range->len = (u64)-1;
  1719. }
  1720. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1721. range, 0, 0);
  1722. if (ret > 0)
  1723. ret = 0;
  1724. kfree(range);
  1725. break;
  1726. default:
  1727. ret = -EINVAL;
  1728. }
  1729. out:
  1730. mnt_drop_write(file->f_path.mnt);
  1731. return ret;
  1732. }
  1733. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1734. {
  1735. struct btrfs_ioctl_vol_args *vol_args;
  1736. int ret;
  1737. if (!capable(CAP_SYS_ADMIN))
  1738. return -EPERM;
  1739. vol_args = memdup_user(arg, sizeof(*vol_args));
  1740. if (IS_ERR(vol_args))
  1741. return PTR_ERR(vol_args);
  1742. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1743. ret = btrfs_init_new_device(root, vol_args->name);
  1744. kfree(vol_args);
  1745. return ret;
  1746. }
  1747. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1748. {
  1749. struct btrfs_ioctl_vol_args *vol_args;
  1750. int ret;
  1751. if (!capable(CAP_SYS_ADMIN))
  1752. return -EPERM;
  1753. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1754. return -EROFS;
  1755. vol_args = memdup_user(arg, sizeof(*vol_args));
  1756. if (IS_ERR(vol_args))
  1757. return PTR_ERR(vol_args);
  1758. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1759. ret = btrfs_rm_device(root, vol_args->name);
  1760. kfree(vol_args);
  1761. return ret;
  1762. }
  1763. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  1764. {
  1765. struct btrfs_ioctl_fs_info_args fi_args;
  1766. struct btrfs_device *device;
  1767. struct btrfs_device *next;
  1768. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1769. if (!capable(CAP_SYS_ADMIN))
  1770. return -EPERM;
  1771. fi_args.num_devices = fs_devices->num_devices;
  1772. fi_args.max_id = 0;
  1773. memcpy(&fi_args.fsid, root->fs_info->fsid, sizeof(fi_args.fsid));
  1774. mutex_lock(&fs_devices->device_list_mutex);
  1775. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  1776. if (device->devid > fi_args.max_id)
  1777. fi_args.max_id = device->devid;
  1778. }
  1779. mutex_unlock(&fs_devices->device_list_mutex);
  1780. if (copy_to_user(arg, &fi_args, sizeof(fi_args)))
  1781. return -EFAULT;
  1782. return 0;
  1783. }
  1784. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  1785. {
  1786. struct btrfs_ioctl_dev_info_args *di_args;
  1787. struct btrfs_device *dev;
  1788. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1789. int ret = 0;
  1790. char *s_uuid = NULL;
  1791. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  1792. if (!capable(CAP_SYS_ADMIN))
  1793. return -EPERM;
  1794. di_args = memdup_user(arg, sizeof(*di_args));
  1795. if (IS_ERR(di_args))
  1796. return PTR_ERR(di_args);
  1797. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  1798. s_uuid = di_args->uuid;
  1799. mutex_lock(&fs_devices->device_list_mutex);
  1800. dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
  1801. mutex_unlock(&fs_devices->device_list_mutex);
  1802. if (!dev) {
  1803. ret = -ENODEV;
  1804. goto out;
  1805. }
  1806. di_args->devid = dev->devid;
  1807. di_args->bytes_used = dev->bytes_used;
  1808. di_args->total_bytes = dev->total_bytes;
  1809. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  1810. strncpy(di_args->path, dev->name, sizeof(di_args->path));
  1811. out:
  1812. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  1813. ret = -EFAULT;
  1814. kfree(di_args);
  1815. return ret;
  1816. }
  1817. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1818. u64 off, u64 olen, u64 destoff)
  1819. {
  1820. struct inode *inode = fdentry(file)->d_inode;
  1821. struct btrfs_root *root = BTRFS_I(inode)->root;
  1822. struct file *src_file;
  1823. struct inode *src;
  1824. struct btrfs_trans_handle *trans;
  1825. struct btrfs_path *path;
  1826. struct extent_buffer *leaf;
  1827. char *buf;
  1828. struct btrfs_key key;
  1829. u32 nritems;
  1830. int slot;
  1831. int ret;
  1832. u64 len = olen;
  1833. u64 bs = root->fs_info->sb->s_blocksize;
  1834. u64 hint_byte;
  1835. /*
  1836. * TODO:
  1837. * - split compressed inline extents. annoying: we need to
  1838. * decompress into destination's address_space (the file offset
  1839. * may change, so source mapping won't do), then recompress (or
  1840. * otherwise reinsert) a subrange.
  1841. * - allow ranges within the same file to be cloned (provided
  1842. * they don't overlap)?
  1843. */
  1844. /* the destination must be opened for writing */
  1845. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1846. return -EINVAL;
  1847. if (btrfs_root_readonly(root))
  1848. return -EROFS;
  1849. ret = mnt_want_write(file->f_path.mnt);
  1850. if (ret)
  1851. return ret;
  1852. src_file = fget(srcfd);
  1853. if (!src_file) {
  1854. ret = -EBADF;
  1855. goto out_drop_write;
  1856. }
  1857. src = src_file->f_dentry->d_inode;
  1858. ret = -EINVAL;
  1859. if (src == inode)
  1860. goto out_fput;
  1861. /* the src must be open for reading */
  1862. if (!(src_file->f_mode & FMODE_READ))
  1863. goto out_fput;
  1864. ret = -EISDIR;
  1865. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1866. goto out_fput;
  1867. ret = -EXDEV;
  1868. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1869. goto out_fput;
  1870. ret = -ENOMEM;
  1871. buf = vmalloc(btrfs_level_size(root, 0));
  1872. if (!buf)
  1873. goto out_fput;
  1874. path = btrfs_alloc_path();
  1875. if (!path) {
  1876. vfree(buf);
  1877. goto out_fput;
  1878. }
  1879. path->reada = 2;
  1880. if (inode < src) {
  1881. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1882. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1883. } else {
  1884. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1885. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1886. }
  1887. /* determine range to clone */
  1888. ret = -EINVAL;
  1889. if (off + len > src->i_size || off + len < off)
  1890. goto out_unlock;
  1891. if (len == 0)
  1892. olen = len = src->i_size - off;
  1893. /* if we extend to eof, continue to block boundary */
  1894. if (off + len == src->i_size)
  1895. len = ALIGN(src->i_size, bs) - off;
  1896. /* verify the end result is block aligned */
  1897. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1898. !IS_ALIGNED(destoff, bs))
  1899. goto out_unlock;
  1900. /* do any pending delalloc/csum calc on src, one way or
  1901. another, and lock file content */
  1902. while (1) {
  1903. struct btrfs_ordered_extent *ordered;
  1904. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1905. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1906. if (!ordered &&
  1907. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1908. EXTENT_DELALLOC, 0, NULL))
  1909. break;
  1910. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1911. if (ordered)
  1912. btrfs_put_ordered_extent(ordered);
  1913. btrfs_wait_ordered_range(src, off, len);
  1914. }
  1915. /* clone data */
  1916. key.objectid = btrfs_ino(src);
  1917. key.type = BTRFS_EXTENT_DATA_KEY;
  1918. key.offset = 0;
  1919. while (1) {
  1920. /*
  1921. * note the key will change type as we walk through the
  1922. * tree.
  1923. */
  1924. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1925. if (ret < 0)
  1926. goto out;
  1927. nritems = btrfs_header_nritems(path->nodes[0]);
  1928. if (path->slots[0] >= nritems) {
  1929. ret = btrfs_next_leaf(root, path);
  1930. if (ret < 0)
  1931. goto out;
  1932. if (ret > 0)
  1933. break;
  1934. nritems = btrfs_header_nritems(path->nodes[0]);
  1935. }
  1936. leaf = path->nodes[0];
  1937. slot = path->slots[0];
  1938. btrfs_item_key_to_cpu(leaf, &key, slot);
  1939. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1940. key.objectid != btrfs_ino(src))
  1941. break;
  1942. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1943. struct btrfs_file_extent_item *extent;
  1944. int type;
  1945. u32 size;
  1946. struct btrfs_key new_key;
  1947. u64 disko = 0, diskl = 0;
  1948. u64 datao = 0, datal = 0;
  1949. u8 comp;
  1950. u64 endoff;
  1951. size = btrfs_item_size_nr(leaf, slot);
  1952. read_extent_buffer(leaf, buf,
  1953. btrfs_item_ptr_offset(leaf, slot),
  1954. size);
  1955. extent = btrfs_item_ptr(leaf, slot,
  1956. struct btrfs_file_extent_item);
  1957. comp = btrfs_file_extent_compression(leaf, extent);
  1958. type = btrfs_file_extent_type(leaf, extent);
  1959. if (type == BTRFS_FILE_EXTENT_REG ||
  1960. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1961. disko = btrfs_file_extent_disk_bytenr(leaf,
  1962. extent);
  1963. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1964. extent);
  1965. datao = btrfs_file_extent_offset(leaf, extent);
  1966. datal = btrfs_file_extent_num_bytes(leaf,
  1967. extent);
  1968. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1969. /* take upper bound, may be compressed */
  1970. datal = btrfs_file_extent_ram_bytes(leaf,
  1971. extent);
  1972. }
  1973. btrfs_release_path(path);
  1974. if (key.offset + datal <= off ||
  1975. key.offset >= off+len)
  1976. goto next;
  1977. memcpy(&new_key, &key, sizeof(new_key));
  1978. new_key.objectid = btrfs_ino(inode);
  1979. if (off <= key.offset)
  1980. new_key.offset = key.offset + destoff - off;
  1981. else
  1982. new_key.offset = destoff;
  1983. trans = btrfs_start_transaction(root, 1);
  1984. if (IS_ERR(trans)) {
  1985. ret = PTR_ERR(trans);
  1986. goto out;
  1987. }
  1988. if (type == BTRFS_FILE_EXTENT_REG ||
  1989. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1990. if (off > key.offset) {
  1991. datao += off - key.offset;
  1992. datal -= off - key.offset;
  1993. }
  1994. if (key.offset + datal > off + len)
  1995. datal = off + len - key.offset;
  1996. ret = btrfs_drop_extents(trans, inode,
  1997. new_key.offset,
  1998. new_key.offset + datal,
  1999. &hint_byte, 1);
  2000. BUG_ON(ret);
  2001. ret = btrfs_insert_empty_item(trans, root, path,
  2002. &new_key, size);
  2003. BUG_ON(ret);
  2004. leaf = path->nodes[0];
  2005. slot = path->slots[0];
  2006. write_extent_buffer(leaf, buf,
  2007. btrfs_item_ptr_offset(leaf, slot),
  2008. size);
  2009. extent = btrfs_item_ptr(leaf, slot,
  2010. struct btrfs_file_extent_item);
  2011. /* disko == 0 means it's a hole */
  2012. if (!disko)
  2013. datao = 0;
  2014. btrfs_set_file_extent_offset(leaf, extent,
  2015. datao);
  2016. btrfs_set_file_extent_num_bytes(leaf, extent,
  2017. datal);
  2018. if (disko) {
  2019. inode_add_bytes(inode, datal);
  2020. ret = btrfs_inc_extent_ref(trans, root,
  2021. disko, diskl, 0,
  2022. root->root_key.objectid,
  2023. btrfs_ino(inode),
  2024. new_key.offset - datao);
  2025. BUG_ON(ret);
  2026. }
  2027. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2028. u64 skip = 0;
  2029. u64 trim = 0;
  2030. if (off > key.offset) {
  2031. skip = off - key.offset;
  2032. new_key.offset += skip;
  2033. }
  2034. if (key.offset + datal > off+len)
  2035. trim = key.offset + datal - (off+len);
  2036. if (comp && (skip || trim)) {
  2037. ret = -EINVAL;
  2038. btrfs_end_transaction(trans, root);
  2039. goto out;
  2040. }
  2041. size -= skip + trim;
  2042. datal -= skip + trim;
  2043. ret = btrfs_drop_extents(trans, inode,
  2044. new_key.offset,
  2045. new_key.offset + datal,
  2046. &hint_byte, 1);
  2047. BUG_ON(ret);
  2048. ret = btrfs_insert_empty_item(trans, root, path,
  2049. &new_key, size);
  2050. BUG_ON(ret);
  2051. if (skip) {
  2052. u32 start =
  2053. btrfs_file_extent_calc_inline_size(0);
  2054. memmove(buf+start, buf+start+skip,
  2055. datal);
  2056. }
  2057. leaf = path->nodes[0];
  2058. slot = path->slots[0];
  2059. write_extent_buffer(leaf, buf,
  2060. btrfs_item_ptr_offset(leaf, slot),
  2061. size);
  2062. inode_add_bytes(inode, datal);
  2063. }
  2064. btrfs_mark_buffer_dirty(leaf);
  2065. btrfs_release_path(path);
  2066. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2067. /*
  2068. * we round up to the block size at eof when
  2069. * determining which extents to clone above,
  2070. * but shouldn't round up the file size
  2071. */
  2072. endoff = new_key.offset + datal;
  2073. if (endoff > destoff+olen)
  2074. endoff = destoff+olen;
  2075. if (endoff > inode->i_size)
  2076. btrfs_i_size_write(inode, endoff);
  2077. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  2078. ret = btrfs_update_inode(trans, root, inode);
  2079. BUG_ON(ret);
  2080. btrfs_end_transaction(trans, root);
  2081. }
  2082. next:
  2083. btrfs_release_path(path);
  2084. key.offset++;
  2085. }
  2086. ret = 0;
  2087. out:
  2088. btrfs_release_path(path);
  2089. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  2090. out_unlock:
  2091. mutex_unlock(&src->i_mutex);
  2092. mutex_unlock(&inode->i_mutex);
  2093. vfree(buf);
  2094. btrfs_free_path(path);
  2095. out_fput:
  2096. fput(src_file);
  2097. out_drop_write:
  2098. mnt_drop_write(file->f_path.mnt);
  2099. return ret;
  2100. }
  2101. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2102. {
  2103. struct btrfs_ioctl_clone_range_args args;
  2104. if (copy_from_user(&args, argp, sizeof(args)))
  2105. return -EFAULT;
  2106. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2107. args.src_length, args.dest_offset);
  2108. }
  2109. /*
  2110. * there are many ways the trans_start and trans_end ioctls can lead
  2111. * to deadlocks. They should only be used by applications that
  2112. * basically own the machine, and have a very in depth understanding
  2113. * of all the possible deadlocks and enospc problems.
  2114. */
  2115. static long btrfs_ioctl_trans_start(struct file *file)
  2116. {
  2117. struct inode *inode = fdentry(file)->d_inode;
  2118. struct btrfs_root *root = BTRFS_I(inode)->root;
  2119. struct btrfs_trans_handle *trans;
  2120. int ret;
  2121. ret = -EPERM;
  2122. if (!capable(CAP_SYS_ADMIN))
  2123. goto out;
  2124. ret = -EINPROGRESS;
  2125. if (file->private_data)
  2126. goto out;
  2127. ret = -EROFS;
  2128. if (btrfs_root_readonly(root))
  2129. goto out;
  2130. ret = mnt_want_write(file->f_path.mnt);
  2131. if (ret)
  2132. goto out;
  2133. mutex_lock(&root->fs_info->trans_mutex);
  2134. root->fs_info->open_ioctl_trans++;
  2135. mutex_unlock(&root->fs_info->trans_mutex);
  2136. ret = -ENOMEM;
  2137. trans = btrfs_start_ioctl_transaction(root, 0);
  2138. if (IS_ERR(trans))
  2139. goto out_drop;
  2140. file->private_data = trans;
  2141. return 0;
  2142. out_drop:
  2143. mutex_lock(&root->fs_info->trans_mutex);
  2144. root->fs_info->open_ioctl_trans--;
  2145. mutex_unlock(&root->fs_info->trans_mutex);
  2146. mnt_drop_write(file->f_path.mnt);
  2147. out:
  2148. return ret;
  2149. }
  2150. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2151. {
  2152. struct inode *inode = fdentry(file)->d_inode;
  2153. struct btrfs_root *root = BTRFS_I(inode)->root;
  2154. struct btrfs_root *new_root;
  2155. struct btrfs_dir_item *di;
  2156. struct btrfs_trans_handle *trans;
  2157. struct btrfs_path *path;
  2158. struct btrfs_key location;
  2159. struct btrfs_disk_key disk_key;
  2160. struct btrfs_super_block *disk_super;
  2161. u64 features;
  2162. u64 objectid = 0;
  2163. u64 dir_id;
  2164. if (!capable(CAP_SYS_ADMIN))
  2165. return -EPERM;
  2166. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  2167. return -EFAULT;
  2168. if (!objectid)
  2169. objectid = root->root_key.objectid;
  2170. location.objectid = objectid;
  2171. location.type = BTRFS_ROOT_ITEM_KEY;
  2172. location.offset = (u64)-1;
  2173. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2174. if (IS_ERR(new_root))
  2175. return PTR_ERR(new_root);
  2176. if (btrfs_root_refs(&new_root->root_item) == 0)
  2177. return -ENOENT;
  2178. path = btrfs_alloc_path();
  2179. if (!path)
  2180. return -ENOMEM;
  2181. path->leave_spinning = 1;
  2182. trans = btrfs_start_transaction(root, 1);
  2183. if (IS_ERR(trans)) {
  2184. btrfs_free_path(path);
  2185. return PTR_ERR(trans);
  2186. }
  2187. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  2188. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2189. dir_id, "default", 7, 1);
  2190. if (IS_ERR_OR_NULL(di)) {
  2191. btrfs_free_path(path);
  2192. btrfs_end_transaction(trans, root);
  2193. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2194. "this isn't going to work\n");
  2195. return -ENOENT;
  2196. }
  2197. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2198. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2199. btrfs_mark_buffer_dirty(path->nodes[0]);
  2200. btrfs_free_path(path);
  2201. disk_super = &root->fs_info->super_copy;
  2202. features = btrfs_super_incompat_flags(disk_super);
  2203. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  2204. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  2205. btrfs_set_super_incompat_flags(disk_super, features);
  2206. }
  2207. btrfs_end_transaction(trans, root);
  2208. return 0;
  2209. }
  2210. static void get_block_group_info(struct list_head *groups_list,
  2211. struct btrfs_ioctl_space_info *space)
  2212. {
  2213. struct btrfs_block_group_cache *block_group;
  2214. space->total_bytes = 0;
  2215. space->used_bytes = 0;
  2216. space->flags = 0;
  2217. list_for_each_entry(block_group, groups_list, list) {
  2218. space->flags = block_group->flags;
  2219. space->total_bytes += block_group->key.offset;
  2220. space->used_bytes +=
  2221. btrfs_block_group_used(&block_group->item);
  2222. }
  2223. }
  2224. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2225. {
  2226. struct btrfs_ioctl_space_args space_args;
  2227. struct btrfs_ioctl_space_info space;
  2228. struct btrfs_ioctl_space_info *dest;
  2229. struct btrfs_ioctl_space_info *dest_orig;
  2230. struct btrfs_ioctl_space_info __user *user_dest;
  2231. struct btrfs_space_info *info;
  2232. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2233. BTRFS_BLOCK_GROUP_SYSTEM,
  2234. BTRFS_BLOCK_GROUP_METADATA,
  2235. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2236. int num_types = 4;
  2237. int alloc_size;
  2238. int ret = 0;
  2239. u64 slot_count = 0;
  2240. int i, c;
  2241. if (copy_from_user(&space_args,
  2242. (struct btrfs_ioctl_space_args __user *)arg,
  2243. sizeof(space_args)))
  2244. return -EFAULT;
  2245. for (i = 0; i < num_types; i++) {
  2246. struct btrfs_space_info *tmp;
  2247. info = NULL;
  2248. rcu_read_lock();
  2249. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2250. list) {
  2251. if (tmp->flags == types[i]) {
  2252. info = tmp;
  2253. break;
  2254. }
  2255. }
  2256. rcu_read_unlock();
  2257. if (!info)
  2258. continue;
  2259. down_read(&info->groups_sem);
  2260. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2261. if (!list_empty(&info->block_groups[c]))
  2262. slot_count++;
  2263. }
  2264. up_read(&info->groups_sem);
  2265. }
  2266. /* space_slots == 0 means they are asking for a count */
  2267. if (space_args.space_slots == 0) {
  2268. space_args.total_spaces = slot_count;
  2269. goto out;
  2270. }
  2271. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2272. alloc_size = sizeof(*dest) * slot_count;
  2273. /* we generally have at most 6 or so space infos, one for each raid
  2274. * level. So, a whole page should be more than enough for everyone
  2275. */
  2276. if (alloc_size > PAGE_CACHE_SIZE)
  2277. return -ENOMEM;
  2278. space_args.total_spaces = 0;
  2279. dest = kmalloc(alloc_size, GFP_NOFS);
  2280. if (!dest)
  2281. return -ENOMEM;
  2282. dest_orig = dest;
  2283. /* now we have a buffer to copy into */
  2284. for (i = 0; i < num_types; i++) {
  2285. struct btrfs_space_info *tmp;
  2286. if (!slot_count)
  2287. break;
  2288. info = NULL;
  2289. rcu_read_lock();
  2290. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2291. list) {
  2292. if (tmp->flags == types[i]) {
  2293. info = tmp;
  2294. break;
  2295. }
  2296. }
  2297. rcu_read_unlock();
  2298. if (!info)
  2299. continue;
  2300. down_read(&info->groups_sem);
  2301. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2302. if (!list_empty(&info->block_groups[c])) {
  2303. get_block_group_info(&info->block_groups[c],
  2304. &space);
  2305. memcpy(dest, &space, sizeof(space));
  2306. dest++;
  2307. space_args.total_spaces++;
  2308. slot_count--;
  2309. }
  2310. if (!slot_count)
  2311. break;
  2312. }
  2313. up_read(&info->groups_sem);
  2314. }
  2315. user_dest = (struct btrfs_ioctl_space_info *)
  2316. (arg + sizeof(struct btrfs_ioctl_space_args));
  2317. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2318. ret = -EFAULT;
  2319. kfree(dest_orig);
  2320. out:
  2321. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2322. ret = -EFAULT;
  2323. return ret;
  2324. }
  2325. /*
  2326. * there are many ways the trans_start and trans_end ioctls can lead
  2327. * to deadlocks. They should only be used by applications that
  2328. * basically own the machine, and have a very in depth understanding
  2329. * of all the possible deadlocks and enospc problems.
  2330. */
  2331. long btrfs_ioctl_trans_end(struct file *file)
  2332. {
  2333. struct inode *inode = fdentry(file)->d_inode;
  2334. struct btrfs_root *root = BTRFS_I(inode)->root;
  2335. struct btrfs_trans_handle *trans;
  2336. trans = file->private_data;
  2337. if (!trans)
  2338. return -EINVAL;
  2339. file->private_data = NULL;
  2340. btrfs_end_transaction(trans, root);
  2341. mutex_lock(&root->fs_info->trans_mutex);
  2342. root->fs_info->open_ioctl_trans--;
  2343. mutex_unlock(&root->fs_info->trans_mutex);
  2344. mnt_drop_write(file->f_path.mnt);
  2345. return 0;
  2346. }
  2347. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  2348. {
  2349. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2350. struct btrfs_trans_handle *trans;
  2351. u64 transid;
  2352. int ret;
  2353. trans = btrfs_start_transaction(root, 0);
  2354. if (IS_ERR(trans))
  2355. return PTR_ERR(trans);
  2356. transid = trans->transid;
  2357. ret = btrfs_commit_transaction_async(trans, root, 0);
  2358. if (ret) {
  2359. btrfs_end_transaction(trans, root);
  2360. return ret;
  2361. }
  2362. if (argp)
  2363. if (copy_to_user(argp, &transid, sizeof(transid)))
  2364. return -EFAULT;
  2365. return 0;
  2366. }
  2367. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2368. {
  2369. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2370. u64 transid;
  2371. if (argp) {
  2372. if (copy_from_user(&transid, argp, sizeof(transid)))
  2373. return -EFAULT;
  2374. } else {
  2375. transid = 0; /* current trans */
  2376. }
  2377. return btrfs_wait_for_commit(root, transid);
  2378. }
  2379. static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
  2380. {
  2381. int ret;
  2382. struct btrfs_ioctl_scrub_args *sa;
  2383. if (!capable(CAP_SYS_ADMIN))
  2384. return -EPERM;
  2385. sa = memdup_user(arg, sizeof(*sa));
  2386. if (IS_ERR(sa))
  2387. return PTR_ERR(sa);
  2388. ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
  2389. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
  2390. if (copy_to_user(arg, sa, sizeof(*sa)))
  2391. ret = -EFAULT;
  2392. kfree(sa);
  2393. return ret;
  2394. }
  2395. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2396. {
  2397. if (!capable(CAP_SYS_ADMIN))
  2398. return -EPERM;
  2399. return btrfs_scrub_cancel(root);
  2400. }
  2401. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2402. void __user *arg)
  2403. {
  2404. struct btrfs_ioctl_scrub_args *sa;
  2405. int ret;
  2406. if (!capable(CAP_SYS_ADMIN))
  2407. return -EPERM;
  2408. sa = memdup_user(arg, sizeof(*sa));
  2409. if (IS_ERR(sa))
  2410. return PTR_ERR(sa);
  2411. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2412. if (copy_to_user(arg, sa, sizeof(*sa)))
  2413. ret = -EFAULT;
  2414. kfree(sa);
  2415. return ret;
  2416. }
  2417. long btrfs_ioctl(struct file *file, unsigned int
  2418. cmd, unsigned long arg)
  2419. {
  2420. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2421. void __user *argp = (void __user *)arg;
  2422. switch (cmd) {
  2423. case FS_IOC_GETFLAGS:
  2424. return btrfs_ioctl_getflags(file, argp);
  2425. case FS_IOC_SETFLAGS:
  2426. return btrfs_ioctl_setflags(file, argp);
  2427. case FS_IOC_GETVERSION:
  2428. return btrfs_ioctl_getversion(file, argp);
  2429. case FITRIM:
  2430. return btrfs_ioctl_fitrim(file, argp);
  2431. case BTRFS_IOC_SNAP_CREATE:
  2432. return btrfs_ioctl_snap_create(file, argp, 0);
  2433. case BTRFS_IOC_SNAP_CREATE_V2:
  2434. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2435. case BTRFS_IOC_SUBVOL_CREATE:
  2436. return btrfs_ioctl_snap_create(file, argp, 1);
  2437. case BTRFS_IOC_SNAP_DESTROY:
  2438. return btrfs_ioctl_snap_destroy(file, argp);
  2439. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2440. return btrfs_ioctl_subvol_getflags(file, argp);
  2441. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2442. return btrfs_ioctl_subvol_setflags(file, argp);
  2443. case BTRFS_IOC_DEFAULT_SUBVOL:
  2444. return btrfs_ioctl_default_subvol(file, argp);
  2445. case BTRFS_IOC_DEFRAG:
  2446. return btrfs_ioctl_defrag(file, NULL);
  2447. case BTRFS_IOC_DEFRAG_RANGE:
  2448. return btrfs_ioctl_defrag(file, argp);
  2449. case BTRFS_IOC_RESIZE:
  2450. return btrfs_ioctl_resize(root, argp);
  2451. case BTRFS_IOC_ADD_DEV:
  2452. return btrfs_ioctl_add_dev(root, argp);
  2453. case BTRFS_IOC_RM_DEV:
  2454. return btrfs_ioctl_rm_dev(root, argp);
  2455. case BTRFS_IOC_FS_INFO:
  2456. return btrfs_ioctl_fs_info(root, argp);
  2457. case BTRFS_IOC_DEV_INFO:
  2458. return btrfs_ioctl_dev_info(root, argp);
  2459. case BTRFS_IOC_BALANCE:
  2460. return btrfs_balance(root->fs_info->dev_root);
  2461. case BTRFS_IOC_CLONE:
  2462. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2463. case BTRFS_IOC_CLONE_RANGE:
  2464. return btrfs_ioctl_clone_range(file, argp);
  2465. case BTRFS_IOC_TRANS_START:
  2466. return btrfs_ioctl_trans_start(file);
  2467. case BTRFS_IOC_TRANS_END:
  2468. return btrfs_ioctl_trans_end(file);
  2469. case BTRFS_IOC_TREE_SEARCH:
  2470. return btrfs_ioctl_tree_search(file, argp);
  2471. case BTRFS_IOC_INO_LOOKUP:
  2472. return btrfs_ioctl_ino_lookup(file, argp);
  2473. case BTRFS_IOC_SPACE_INFO:
  2474. return btrfs_ioctl_space_info(root, argp);
  2475. case BTRFS_IOC_SYNC:
  2476. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2477. return 0;
  2478. case BTRFS_IOC_START_SYNC:
  2479. return btrfs_ioctl_start_sync(file, argp);
  2480. case BTRFS_IOC_WAIT_SYNC:
  2481. return btrfs_ioctl_wait_sync(file, argp);
  2482. case BTRFS_IOC_SCRUB:
  2483. return btrfs_ioctl_scrub(root, argp);
  2484. case BTRFS_IOC_SCRUB_CANCEL:
  2485. return btrfs_ioctl_scrub_cancel(root, argp);
  2486. case BTRFS_IOC_SCRUB_PROGRESS:
  2487. return btrfs_ioctl_scrub_progress(root, argp);
  2488. }
  2489. return -ENOTTY;
  2490. }