ioctl.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  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);
  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 = dentry->d_parent;
  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. return ret;
  292. dir = parent->d_inode;
  293. /*
  294. * 1 - inode item
  295. * 2 - refs
  296. * 1 - root item
  297. * 2 - dir items
  298. */
  299. trans = btrfs_start_transaction(root, 6);
  300. if (IS_ERR(trans))
  301. return PTR_ERR(trans);
  302. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  303. 0, objectid, NULL, 0, 0, 0);
  304. if (IS_ERR(leaf)) {
  305. ret = PTR_ERR(leaf);
  306. goto fail;
  307. }
  308. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  309. btrfs_set_header_bytenr(leaf, leaf->start);
  310. btrfs_set_header_generation(leaf, trans->transid);
  311. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  312. btrfs_set_header_owner(leaf, objectid);
  313. write_extent_buffer(leaf, root->fs_info->fsid,
  314. (unsigned long)btrfs_header_fsid(leaf),
  315. BTRFS_FSID_SIZE);
  316. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  317. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  318. BTRFS_UUID_SIZE);
  319. btrfs_mark_buffer_dirty(leaf);
  320. inode_item = &root_item.inode;
  321. memset(inode_item, 0, sizeof(*inode_item));
  322. inode_item->generation = cpu_to_le64(1);
  323. inode_item->size = cpu_to_le64(3);
  324. inode_item->nlink = cpu_to_le32(1);
  325. inode_item->nbytes = cpu_to_le64(root->leafsize);
  326. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  327. root_item.flags = 0;
  328. root_item.byte_limit = 0;
  329. inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
  330. btrfs_set_root_bytenr(&root_item, leaf->start);
  331. btrfs_set_root_generation(&root_item, trans->transid);
  332. btrfs_set_root_level(&root_item, 0);
  333. btrfs_set_root_refs(&root_item, 1);
  334. btrfs_set_root_used(&root_item, leaf->len);
  335. btrfs_set_root_last_snapshot(&root_item, 0);
  336. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  337. root_item.drop_level = 0;
  338. btrfs_tree_unlock(leaf);
  339. free_extent_buffer(leaf);
  340. leaf = NULL;
  341. btrfs_set_root_dirid(&root_item, new_dirid);
  342. key.objectid = objectid;
  343. key.offset = 0;
  344. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  345. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  346. &root_item);
  347. if (ret)
  348. goto fail;
  349. key.offset = (u64)-1;
  350. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  351. BUG_ON(IS_ERR(new_root));
  352. btrfs_record_root_in_trans(trans, new_root);
  353. ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
  354. /*
  355. * insert the directory item
  356. */
  357. ret = btrfs_set_inode_index(dir, &index);
  358. BUG_ON(ret);
  359. ret = btrfs_insert_dir_item(trans, root,
  360. name, namelen, dir, &key,
  361. BTRFS_FT_DIR, index);
  362. if (ret)
  363. goto fail;
  364. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  365. ret = btrfs_update_inode(trans, root, dir);
  366. BUG_ON(ret);
  367. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  368. objectid, root->root_key.objectid,
  369. btrfs_ino(dir), index, name, namelen);
  370. BUG_ON(ret);
  371. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  372. fail:
  373. if (async_transid) {
  374. *async_transid = trans->transid;
  375. err = btrfs_commit_transaction_async(trans, root, 1);
  376. } else {
  377. err = btrfs_commit_transaction(trans, root);
  378. }
  379. if (err && !ret)
  380. ret = err;
  381. return ret;
  382. }
  383. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  384. char *name, int namelen, u64 *async_transid,
  385. bool readonly)
  386. {
  387. struct inode *inode;
  388. struct btrfs_pending_snapshot *pending_snapshot;
  389. struct btrfs_trans_handle *trans;
  390. int ret;
  391. if (!root->ref_cows)
  392. return -EINVAL;
  393. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  394. if (!pending_snapshot)
  395. return -ENOMEM;
  396. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  397. pending_snapshot->dentry = dentry;
  398. pending_snapshot->root = root;
  399. pending_snapshot->readonly = readonly;
  400. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  401. if (IS_ERR(trans)) {
  402. ret = PTR_ERR(trans);
  403. goto fail;
  404. }
  405. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  406. BUG_ON(ret);
  407. spin_lock(&root->fs_info->trans_lock);
  408. list_add(&pending_snapshot->list,
  409. &trans->transaction->pending_snapshots);
  410. spin_unlock(&root->fs_info->trans_lock);
  411. if (async_transid) {
  412. *async_transid = trans->transid;
  413. ret = btrfs_commit_transaction_async(trans,
  414. root->fs_info->extent_root, 1);
  415. } else {
  416. ret = btrfs_commit_transaction(trans,
  417. root->fs_info->extent_root);
  418. }
  419. BUG_ON(ret);
  420. ret = pending_snapshot->error;
  421. if (ret)
  422. goto fail;
  423. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  424. if (ret)
  425. goto fail;
  426. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  427. if (IS_ERR(inode)) {
  428. ret = PTR_ERR(inode);
  429. goto fail;
  430. }
  431. BUG_ON(!inode);
  432. d_instantiate(dentry, inode);
  433. ret = 0;
  434. fail:
  435. kfree(pending_snapshot);
  436. return ret;
  437. }
  438. /* copy of check_sticky in fs/namei.c()
  439. * It's inline, so penalty for filesystems that don't use sticky bit is
  440. * minimal.
  441. */
  442. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  443. {
  444. uid_t fsuid = current_fsuid();
  445. if (!(dir->i_mode & S_ISVTX))
  446. return 0;
  447. if (inode->i_uid == fsuid)
  448. return 0;
  449. if (dir->i_uid == fsuid)
  450. return 0;
  451. return !capable(CAP_FOWNER);
  452. }
  453. /* copy of may_delete in fs/namei.c()
  454. * Check whether we can remove a link victim from directory dir, check
  455. * whether the type of victim is right.
  456. * 1. We can't do it if dir is read-only (done in permission())
  457. * 2. We should have write and exec permissions on dir
  458. * 3. We can't remove anything from append-only dir
  459. * 4. We can't do anything with immutable dir (done in permission())
  460. * 5. If the sticky bit on dir is set we should either
  461. * a. be owner of dir, or
  462. * b. be owner of victim, or
  463. * c. have CAP_FOWNER capability
  464. * 6. If the victim is append-only or immutable we can't do antyhing with
  465. * links pointing to it.
  466. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  467. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  468. * 9. We can't remove a root or mountpoint.
  469. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  470. * nfs_async_unlink().
  471. */
  472. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  473. {
  474. int error;
  475. if (!victim->d_inode)
  476. return -ENOENT;
  477. BUG_ON(victim->d_parent->d_inode != dir);
  478. audit_inode_child(victim, dir);
  479. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  480. if (error)
  481. return error;
  482. if (IS_APPEND(dir))
  483. return -EPERM;
  484. if (btrfs_check_sticky(dir, victim->d_inode)||
  485. IS_APPEND(victim->d_inode)||
  486. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  487. return -EPERM;
  488. if (isdir) {
  489. if (!S_ISDIR(victim->d_inode->i_mode))
  490. return -ENOTDIR;
  491. if (IS_ROOT(victim))
  492. return -EBUSY;
  493. } else if (S_ISDIR(victim->d_inode->i_mode))
  494. return -EISDIR;
  495. if (IS_DEADDIR(dir))
  496. return -ENOENT;
  497. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  498. return -EBUSY;
  499. return 0;
  500. }
  501. /* copy of may_create in fs/namei.c() */
  502. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  503. {
  504. if (child->d_inode)
  505. return -EEXIST;
  506. if (IS_DEADDIR(dir))
  507. return -ENOENT;
  508. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  509. }
  510. /*
  511. * Create a new subvolume below @parent. This is largely modeled after
  512. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  513. * inside this filesystem so it's quite a bit simpler.
  514. */
  515. static noinline int btrfs_mksubvol(struct path *parent,
  516. char *name, int namelen,
  517. struct btrfs_root *snap_src,
  518. u64 *async_transid, bool readonly)
  519. {
  520. struct inode *dir = parent->dentry->d_inode;
  521. struct dentry *dentry;
  522. int error;
  523. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  524. dentry = lookup_one_len(name, parent->dentry, namelen);
  525. error = PTR_ERR(dentry);
  526. if (IS_ERR(dentry))
  527. goto out_unlock;
  528. error = -EEXIST;
  529. if (dentry->d_inode)
  530. goto out_dput;
  531. error = mnt_want_write(parent->mnt);
  532. if (error)
  533. goto out_dput;
  534. error = btrfs_may_create(dir, dentry);
  535. if (error)
  536. goto out_drop_write;
  537. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  538. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  539. goto out_up_read;
  540. if (snap_src) {
  541. error = create_snapshot(snap_src, dentry,
  542. name, namelen, async_transid, readonly);
  543. } else {
  544. error = create_subvol(BTRFS_I(dir)->root, dentry,
  545. name, namelen, async_transid);
  546. }
  547. if (!error)
  548. fsnotify_mkdir(dir, dentry);
  549. out_up_read:
  550. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  551. out_drop_write:
  552. mnt_drop_write(parent->mnt);
  553. out_dput:
  554. dput(dentry);
  555. out_unlock:
  556. mutex_unlock(&dir->i_mutex);
  557. return error;
  558. }
  559. /*
  560. * When we're defragging a range, we don't want to kick it off again
  561. * if it is really just waiting for delalloc to send it down.
  562. * If we find a nice big extent or delalloc range for the bytes in the
  563. * file you want to defrag, we return 0 to let you know to skip this
  564. * part of the file
  565. */
  566. static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
  567. {
  568. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  569. struct extent_map *em = NULL;
  570. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  571. u64 end;
  572. read_lock(&em_tree->lock);
  573. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  574. read_unlock(&em_tree->lock);
  575. if (em) {
  576. end = extent_map_end(em);
  577. free_extent_map(em);
  578. if (end - offset > thresh)
  579. return 0;
  580. }
  581. /* if we already have a nice delalloc here, just stop */
  582. thresh /= 2;
  583. end = count_range_bits(io_tree, &offset, offset + thresh,
  584. thresh, EXTENT_DELALLOC, 1);
  585. if (end >= thresh)
  586. return 0;
  587. return 1;
  588. }
  589. /*
  590. * helper function to walk through a file and find extents
  591. * newer than a specific transid, and smaller than thresh.
  592. *
  593. * This is used by the defragging code to find new and small
  594. * extents
  595. */
  596. static int find_new_extents(struct btrfs_root *root,
  597. struct inode *inode, u64 newer_than,
  598. u64 *off, int thresh)
  599. {
  600. struct btrfs_path *path;
  601. struct btrfs_key min_key;
  602. struct btrfs_key max_key;
  603. struct extent_buffer *leaf;
  604. struct btrfs_file_extent_item *extent;
  605. int type;
  606. int ret;
  607. u64 ino = btrfs_ino(inode);
  608. path = btrfs_alloc_path();
  609. if (!path)
  610. return -ENOMEM;
  611. min_key.objectid = ino;
  612. min_key.type = BTRFS_EXTENT_DATA_KEY;
  613. min_key.offset = *off;
  614. max_key.objectid = ino;
  615. max_key.type = (u8)-1;
  616. max_key.offset = (u64)-1;
  617. path->keep_locks = 1;
  618. while(1) {
  619. ret = btrfs_search_forward(root, &min_key, &max_key,
  620. path, 0, newer_than);
  621. if (ret != 0)
  622. goto none;
  623. if (min_key.objectid != ino)
  624. goto none;
  625. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  626. goto none;
  627. leaf = path->nodes[0];
  628. extent = btrfs_item_ptr(leaf, path->slots[0],
  629. struct btrfs_file_extent_item);
  630. type = btrfs_file_extent_type(leaf, extent);
  631. if (type == BTRFS_FILE_EXTENT_REG &&
  632. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  633. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  634. *off = min_key.offset;
  635. btrfs_free_path(path);
  636. return 0;
  637. }
  638. if (min_key.offset == (u64)-1)
  639. goto none;
  640. min_key.offset++;
  641. btrfs_release_path(path);
  642. }
  643. none:
  644. btrfs_free_path(path);
  645. return -ENOENT;
  646. }
  647. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  648. int thresh, u64 *last_len, u64 *skip,
  649. u64 *defrag_end)
  650. {
  651. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  652. struct extent_map *em = NULL;
  653. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  654. int ret = 1;
  655. /*
  656. * make sure that once we start defragging and extent, we keep on
  657. * defragging it
  658. */
  659. if (start < *defrag_end)
  660. return 1;
  661. *skip = 0;
  662. /*
  663. * hopefully we have this extent in the tree already, try without
  664. * the full extent lock
  665. */
  666. read_lock(&em_tree->lock);
  667. em = lookup_extent_mapping(em_tree, start, len);
  668. read_unlock(&em_tree->lock);
  669. if (!em) {
  670. /* get the big lock and read metadata off disk */
  671. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  672. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  673. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  674. if (IS_ERR(em))
  675. return 0;
  676. }
  677. /* this will cover holes, and inline extents */
  678. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  679. ret = 0;
  680. /*
  681. * we hit a real extent, if it is big don't bother defragging it again
  682. */
  683. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  684. ret = 0;
  685. /*
  686. * last_len ends up being a counter of how many bytes we've defragged.
  687. * every time we choose not to defrag an extent, we reset *last_len
  688. * so that the next tiny extent will force a defrag.
  689. *
  690. * The end result of this is that tiny extents before a single big
  691. * extent will force at least part of that big extent to be defragged.
  692. */
  693. if (ret) {
  694. *last_len += len;
  695. *defrag_end = extent_map_end(em);
  696. } else {
  697. *last_len = 0;
  698. *skip = extent_map_end(em);
  699. *defrag_end = 0;
  700. }
  701. free_extent_map(em);
  702. return ret;
  703. }
  704. /*
  705. * it doesn't do much good to defrag one or two pages
  706. * at a time. This pulls in a nice chunk of pages
  707. * to COW and defrag.
  708. *
  709. * It also makes sure the delalloc code has enough
  710. * dirty data to avoid making new small extents as part
  711. * of the defrag
  712. *
  713. * It's a good idea to start RA on this range
  714. * before calling this.
  715. */
  716. static int cluster_pages_for_defrag(struct inode *inode,
  717. struct page **pages,
  718. unsigned long start_index,
  719. int num_pages)
  720. {
  721. unsigned long file_end;
  722. u64 isize = i_size_read(inode);
  723. u64 page_start;
  724. u64 page_end;
  725. int ret;
  726. int i;
  727. int i_done;
  728. struct btrfs_ordered_extent *ordered;
  729. struct extent_state *cached_state = NULL;
  730. if (isize == 0)
  731. return 0;
  732. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  733. ret = btrfs_delalloc_reserve_space(inode,
  734. num_pages << PAGE_CACHE_SHIFT);
  735. if (ret)
  736. return ret;
  737. again:
  738. ret = 0;
  739. i_done = 0;
  740. /* step one, lock all the pages */
  741. for (i = 0; i < num_pages; i++) {
  742. struct page *page;
  743. page = find_or_create_page(inode->i_mapping,
  744. start_index + i, GFP_NOFS);
  745. if (!page)
  746. break;
  747. if (!PageUptodate(page)) {
  748. btrfs_readpage(NULL, page);
  749. lock_page(page);
  750. if (!PageUptodate(page)) {
  751. unlock_page(page);
  752. page_cache_release(page);
  753. ret = -EIO;
  754. break;
  755. }
  756. }
  757. isize = i_size_read(inode);
  758. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  759. if (!isize || page->index > file_end ||
  760. page->mapping != inode->i_mapping) {
  761. /* whoops, we blew past eof, skip this page */
  762. unlock_page(page);
  763. page_cache_release(page);
  764. break;
  765. }
  766. pages[i] = page;
  767. i_done++;
  768. }
  769. if (!i_done || ret)
  770. goto out;
  771. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  772. goto out;
  773. /*
  774. * so now we have a nice long stream of locked
  775. * and up to date pages, lets wait on them
  776. */
  777. for (i = 0; i < i_done; i++)
  778. wait_on_page_writeback(pages[i]);
  779. page_start = page_offset(pages[0]);
  780. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  781. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  782. page_start, page_end - 1, 0, &cached_state,
  783. GFP_NOFS);
  784. ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
  785. if (ordered &&
  786. ordered->file_offset + ordered->len > page_start &&
  787. ordered->file_offset < page_end) {
  788. btrfs_put_ordered_extent(ordered);
  789. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  790. page_start, page_end - 1,
  791. &cached_state, GFP_NOFS);
  792. for (i = 0; i < i_done; i++) {
  793. unlock_page(pages[i]);
  794. page_cache_release(pages[i]);
  795. }
  796. btrfs_wait_ordered_range(inode, page_start,
  797. page_end - page_start);
  798. goto again;
  799. }
  800. if (ordered)
  801. btrfs_put_ordered_extent(ordered);
  802. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  803. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  804. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  805. GFP_NOFS);
  806. if (i_done != num_pages) {
  807. spin_lock(&BTRFS_I(inode)->lock);
  808. BTRFS_I(inode)->outstanding_extents++;
  809. spin_unlock(&BTRFS_I(inode)->lock);
  810. btrfs_delalloc_release_space(inode,
  811. (num_pages - i_done) << PAGE_CACHE_SHIFT);
  812. }
  813. btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
  814. &cached_state);
  815. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  816. page_start, page_end - 1, &cached_state,
  817. GFP_NOFS);
  818. for (i = 0; i < i_done; i++) {
  819. clear_page_dirty_for_io(pages[i]);
  820. ClearPageChecked(pages[i]);
  821. set_page_extent_mapped(pages[i]);
  822. set_page_dirty(pages[i]);
  823. unlock_page(pages[i]);
  824. page_cache_release(pages[i]);
  825. }
  826. return i_done;
  827. out:
  828. for (i = 0; i < i_done; i++) {
  829. unlock_page(pages[i]);
  830. page_cache_release(pages[i]);
  831. }
  832. btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
  833. return ret;
  834. }
  835. int btrfs_defrag_file(struct inode *inode, struct file *file,
  836. struct btrfs_ioctl_defrag_range_args *range,
  837. u64 newer_than, unsigned long max_to_defrag)
  838. {
  839. struct btrfs_root *root = BTRFS_I(inode)->root;
  840. struct btrfs_super_block *disk_super;
  841. struct file_ra_state *ra = NULL;
  842. unsigned long last_index;
  843. u64 features;
  844. u64 last_len = 0;
  845. u64 skip = 0;
  846. u64 defrag_end = 0;
  847. u64 newer_off = range->start;
  848. int newer_left = 0;
  849. unsigned long i;
  850. int ret;
  851. int defrag_count = 0;
  852. int compress_type = BTRFS_COMPRESS_ZLIB;
  853. int extent_thresh = range->extent_thresh;
  854. int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  855. u64 new_align = ~((u64)128 * 1024 - 1);
  856. struct page **pages = NULL;
  857. if (extent_thresh == 0)
  858. extent_thresh = 256 * 1024;
  859. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  860. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  861. return -EINVAL;
  862. if (range->compress_type)
  863. compress_type = range->compress_type;
  864. }
  865. if (inode->i_size == 0)
  866. return 0;
  867. /*
  868. * if we were not given a file, allocate a readahead
  869. * context
  870. */
  871. if (!file) {
  872. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  873. if (!ra)
  874. return -ENOMEM;
  875. file_ra_state_init(ra, inode->i_mapping);
  876. } else {
  877. ra = &file->f_ra;
  878. }
  879. pages = kmalloc(sizeof(struct page *) * newer_cluster,
  880. GFP_NOFS);
  881. if (!pages) {
  882. ret = -ENOMEM;
  883. goto out_ra;
  884. }
  885. /* find the last page to defrag */
  886. if (range->start + range->len > range->start) {
  887. last_index = min_t(u64, inode->i_size - 1,
  888. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  889. } else {
  890. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  891. }
  892. if (newer_than) {
  893. ret = find_new_extents(root, inode, newer_than,
  894. &newer_off, 64 * 1024);
  895. if (!ret) {
  896. range->start = newer_off;
  897. /*
  898. * we always align our defrag to help keep
  899. * the extents in the file evenly spaced
  900. */
  901. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  902. newer_left = newer_cluster;
  903. } else
  904. goto out_ra;
  905. } else {
  906. i = range->start >> PAGE_CACHE_SHIFT;
  907. }
  908. if (!max_to_defrag)
  909. max_to_defrag = last_index - 1;
  910. while (i <= last_index && defrag_count < max_to_defrag) {
  911. /*
  912. * make sure we stop running if someone unmounts
  913. * the FS
  914. */
  915. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  916. break;
  917. if (!newer_than &&
  918. !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  919. PAGE_CACHE_SIZE,
  920. extent_thresh,
  921. &last_len, &skip,
  922. &defrag_end)) {
  923. unsigned long next;
  924. /*
  925. * the should_defrag function tells us how much to skip
  926. * bump our counter by the suggested amount
  927. */
  928. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  929. i = max(i + 1, next);
  930. continue;
  931. }
  932. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  933. BTRFS_I(inode)->force_compress = compress_type;
  934. btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
  935. ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
  936. if (ret < 0)
  937. goto out_ra;
  938. defrag_count += ret;
  939. balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
  940. i += ret;
  941. if (newer_than) {
  942. if (newer_off == (u64)-1)
  943. break;
  944. newer_off = max(newer_off + 1,
  945. (u64)i << PAGE_CACHE_SHIFT);
  946. ret = find_new_extents(root, inode,
  947. newer_than, &newer_off,
  948. 64 * 1024);
  949. if (!ret) {
  950. range->start = newer_off;
  951. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  952. newer_left = newer_cluster;
  953. } else {
  954. break;
  955. }
  956. } else {
  957. i++;
  958. }
  959. }
  960. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  961. filemap_flush(inode->i_mapping);
  962. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  963. /* the filemap_flush will queue IO into the worker threads, but
  964. * we have to make sure the IO is actually started and that
  965. * ordered extents get created before we return
  966. */
  967. atomic_inc(&root->fs_info->async_submit_draining);
  968. while (atomic_read(&root->fs_info->nr_async_submits) ||
  969. atomic_read(&root->fs_info->async_delalloc_pages)) {
  970. wait_event(root->fs_info->async_submit_wait,
  971. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  972. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  973. }
  974. atomic_dec(&root->fs_info->async_submit_draining);
  975. mutex_lock(&inode->i_mutex);
  976. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  977. mutex_unlock(&inode->i_mutex);
  978. }
  979. disk_super = &root->fs_info->super_copy;
  980. features = btrfs_super_incompat_flags(disk_super);
  981. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  982. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  983. btrfs_set_super_incompat_flags(disk_super, features);
  984. }
  985. if (!file)
  986. kfree(ra);
  987. return defrag_count;
  988. out_ra:
  989. if (!file)
  990. kfree(ra);
  991. kfree(pages);
  992. return ret;
  993. }
  994. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  995. void __user *arg)
  996. {
  997. u64 new_size;
  998. u64 old_size;
  999. u64 devid = 1;
  1000. struct btrfs_ioctl_vol_args *vol_args;
  1001. struct btrfs_trans_handle *trans;
  1002. struct btrfs_device *device = NULL;
  1003. char *sizestr;
  1004. char *devstr = NULL;
  1005. int ret = 0;
  1006. int mod = 0;
  1007. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1008. return -EROFS;
  1009. if (!capable(CAP_SYS_ADMIN))
  1010. return -EPERM;
  1011. vol_args = memdup_user(arg, sizeof(*vol_args));
  1012. if (IS_ERR(vol_args))
  1013. return PTR_ERR(vol_args);
  1014. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1015. mutex_lock(&root->fs_info->volume_mutex);
  1016. sizestr = vol_args->name;
  1017. devstr = strchr(sizestr, ':');
  1018. if (devstr) {
  1019. char *end;
  1020. sizestr = devstr + 1;
  1021. *devstr = '\0';
  1022. devstr = vol_args->name;
  1023. devid = simple_strtoull(devstr, &end, 10);
  1024. printk(KERN_INFO "resizing devid %llu\n",
  1025. (unsigned long long)devid);
  1026. }
  1027. device = btrfs_find_device(root, devid, NULL, NULL);
  1028. if (!device) {
  1029. printk(KERN_INFO "resizer unable to find device %llu\n",
  1030. (unsigned long long)devid);
  1031. ret = -EINVAL;
  1032. goto out_unlock;
  1033. }
  1034. if (!strcmp(sizestr, "max"))
  1035. new_size = device->bdev->bd_inode->i_size;
  1036. else {
  1037. if (sizestr[0] == '-') {
  1038. mod = -1;
  1039. sizestr++;
  1040. } else if (sizestr[0] == '+') {
  1041. mod = 1;
  1042. sizestr++;
  1043. }
  1044. new_size = memparse(sizestr, NULL);
  1045. if (new_size == 0) {
  1046. ret = -EINVAL;
  1047. goto out_unlock;
  1048. }
  1049. }
  1050. old_size = device->total_bytes;
  1051. if (mod < 0) {
  1052. if (new_size > old_size) {
  1053. ret = -EINVAL;
  1054. goto out_unlock;
  1055. }
  1056. new_size = old_size - new_size;
  1057. } else if (mod > 0) {
  1058. new_size = old_size + new_size;
  1059. }
  1060. if (new_size < 256 * 1024 * 1024) {
  1061. ret = -EINVAL;
  1062. goto out_unlock;
  1063. }
  1064. if (new_size > device->bdev->bd_inode->i_size) {
  1065. ret = -EFBIG;
  1066. goto out_unlock;
  1067. }
  1068. do_div(new_size, root->sectorsize);
  1069. new_size *= root->sectorsize;
  1070. printk(KERN_INFO "new size for %s is %llu\n",
  1071. device->name, (unsigned long long)new_size);
  1072. if (new_size > old_size) {
  1073. trans = btrfs_start_transaction(root, 0);
  1074. if (IS_ERR(trans)) {
  1075. ret = PTR_ERR(trans);
  1076. goto out_unlock;
  1077. }
  1078. ret = btrfs_grow_device(trans, device, new_size);
  1079. btrfs_commit_transaction(trans, root);
  1080. } else {
  1081. ret = btrfs_shrink_device(device, new_size);
  1082. }
  1083. out_unlock:
  1084. mutex_unlock(&root->fs_info->volume_mutex);
  1085. kfree(vol_args);
  1086. return ret;
  1087. }
  1088. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1089. char *name,
  1090. unsigned long fd,
  1091. int subvol,
  1092. u64 *transid,
  1093. bool readonly)
  1094. {
  1095. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1096. struct file *src_file;
  1097. int namelen;
  1098. int ret = 0;
  1099. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1100. return -EROFS;
  1101. namelen = strlen(name);
  1102. if (strchr(name, '/')) {
  1103. ret = -EINVAL;
  1104. goto out;
  1105. }
  1106. if (subvol) {
  1107. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1108. NULL, transid, readonly);
  1109. } else {
  1110. struct inode *src_inode;
  1111. src_file = fget(fd);
  1112. if (!src_file) {
  1113. ret = -EINVAL;
  1114. goto out;
  1115. }
  1116. src_inode = src_file->f_path.dentry->d_inode;
  1117. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1118. printk(KERN_INFO "btrfs: Snapshot src from "
  1119. "another FS\n");
  1120. ret = -EINVAL;
  1121. fput(src_file);
  1122. goto out;
  1123. }
  1124. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1125. BTRFS_I(src_inode)->root,
  1126. transid, readonly);
  1127. fput(src_file);
  1128. }
  1129. out:
  1130. return ret;
  1131. }
  1132. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1133. void __user *arg, int subvol)
  1134. {
  1135. struct btrfs_ioctl_vol_args *vol_args;
  1136. int ret;
  1137. vol_args = memdup_user(arg, sizeof(*vol_args));
  1138. if (IS_ERR(vol_args))
  1139. return PTR_ERR(vol_args);
  1140. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1141. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1142. vol_args->fd, subvol,
  1143. NULL, false);
  1144. kfree(vol_args);
  1145. return ret;
  1146. }
  1147. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1148. void __user *arg, int subvol)
  1149. {
  1150. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1151. int ret;
  1152. u64 transid = 0;
  1153. u64 *ptr = NULL;
  1154. bool readonly = false;
  1155. vol_args = memdup_user(arg, sizeof(*vol_args));
  1156. if (IS_ERR(vol_args))
  1157. return PTR_ERR(vol_args);
  1158. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1159. if (vol_args->flags &
  1160. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  1161. ret = -EOPNOTSUPP;
  1162. goto out;
  1163. }
  1164. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1165. ptr = &transid;
  1166. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1167. readonly = true;
  1168. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1169. vol_args->fd, subvol,
  1170. ptr, readonly);
  1171. if (ret == 0 && ptr &&
  1172. copy_to_user(arg +
  1173. offsetof(struct btrfs_ioctl_vol_args_v2,
  1174. transid), ptr, sizeof(*ptr)))
  1175. ret = -EFAULT;
  1176. out:
  1177. kfree(vol_args);
  1178. return ret;
  1179. }
  1180. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1181. void __user *arg)
  1182. {
  1183. struct inode *inode = fdentry(file)->d_inode;
  1184. struct btrfs_root *root = BTRFS_I(inode)->root;
  1185. int ret = 0;
  1186. u64 flags = 0;
  1187. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1188. return -EINVAL;
  1189. down_read(&root->fs_info->subvol_sem);
  1190. if (btrfs_root_readonly(root))
  1191. flags |= BTRFS_SUBVOL_RDONLY;
  1192. up_read(&root->fs_info->subvol_sem);
  1193. if (copy_to_user(arg, &flags, sizeof(flags)))
  1194. ret = -EFAULT;
  1195. return ret;
  1196. }
  1197. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1198. void __user *arg)
  1199. {
  1200. struct inode *inode = fdentry(file)->d_inode;
  1201. struct btrfs_root *root = BTRFS_I(inode)->root;
  1202. struct btrfs_trans_handle *trans;
  1203. u64 root_flags;
  1204. u64 flags;
  1205. int ret = 0;
  1206. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1207. return -EROFS;
  1208. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1209. return -EINVAL;
  1210. if (copy_from_user(&flags, arg, sizeof(flags)))
  1211. return -EFAULT;
  1212. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1213. return -EINVAL;
  1214. if (flags & ~BTRFS_SUBVOL_RDONLY)
  1215. return -EOPNOTSUPP;
  1216. if (!inode_owner_or_capable(inode))
  1217. return -EACCES;
  1218. down_write(&root->fs_info->subvol_sem);
  1219. /* nothing to do */
  1220. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1221. goto out;
  1222. root_flags = btrfs_root_flags(&root->root_item);
  1223. if (flags & BTRFS_SUBVOL_RDONLY)
  1224. btrfs_set_root_flags(&root->root_item,
  1225. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1226. else
  1227. btrfs_set_root_flags(&root->root_item,
  1228. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1229. trans = btrfs_start_transaction(root, 1);
  1230. if (IS_ERR(trans)) {
  1231. ret = PTR_ERR(trans);
  1232. goto out_reset;
  1233. }
  1234. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1235. &root->root_key, &root->root_item);
  1236. btrfs_commit_transaction(trans, root);
  1237. out_reset:
  1238. if (ret)
  1239. btrfs_set_root_flags(&root->root_item, root_flags);
  1240. out:
  1241. up_write(&root->fs_info->subvol_sem);
  1242. return ret;
  1243. }
  1244. /*
  1245. * helper to check if the subvolume references other subvolumes
  1246. */
  1247. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1248. {
  1249. struct btrfs_path *path;
  1250. struct btrfs_key key;
  1251. int ret;
  1252. path = btrfs_alloc_path();
  1253. if (!path)
  1254. return -ENOMEM;
  1255. key.objectid = root->root_key.objectid;
  1256. key.type = BTRFS_ROOT_REF_KEY;
  1257. key.offset = (u64)-1;
  1258. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1259. &key, path, 0, 0);
  1260. if (ret < 0)
  1261. goto out;
  1262. BUG_ON(ret == 0);
  1263. ret = 0;
  1264. if (path->slots[0] > 0) {
  1265. path->slots[0]--;
  1266. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1267. if (key.objectid == root->root_key.objectid &&
  1268. key.type == BTRFS_ROOT_REF_KEY)
  1269. ret = -ENOTEMPTY;
  1270. }
  1271. out:
  1272. btrfs_free_path(path);
  1273. return ret;
  1274. }
  1275. static noinline int key_in_sk(struct btrfs_key *key,
  1276. struct btrfs_ioctl_search_key *sk)
  1277. {
  1278. struct btrfs_key test;
  1279. int ret;
  1280. test.objectid = sk->min_objectid;
  1281. test.type = sk->min_type;
  1282. test.offset = sk->min_offset;
  1283. ret = btrfs_comp_cpu_keys(key, &test);
  1284. if (ret < 0)
  1285. return 0;
  1286. test.objectid = sk->max_objectid;
  1287. test.type = sk->max_type;
  1288. test.offset = sk->max_offset;
  1289. ret = btrfs_comp_cpu_keys(key, &test);
  1290. if (ret > 0)
  1291. return 0;
  1292. return 1;
  1293. }
  1294. static noinline int copy_to_sk(struct btrfs_root *root,
  1295. struct btrfs_path *path,
  1296. struct btrfs_key *key,
  1297. struct btrfs_ioctl_search_key *sk,
  1298. char *buf,
  1299. unsigned long *sk_offset,
  1300. int *num_found)
  1301. {
  1302. u64 found_transid;
  1303. struct extent_buffer *leaf;
  1304. struct btrfs_ioctl_search_header sh;
  1305. unsigned long item_off;
  1306. unsigned long item_len;
  1307. int nritems;
  1308. int i;
  1309. int slot;
  1310. int ret = 0;
  1311. leaf = path->nodes[0];
  1312. slot = path->slots[0];
  1313. nritems = btrfs_header_nritems(leaf);
  1314. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1315. i = nritems;
  1316. goto advance_key;
  1317. }
  1318. found_transid = btrfs_header_generation(leaf);
  1319. for (i = slot; i < nritems; i++) {
  1320. item_off = btrfs_item_ptr_offset(leaf, i);
  1321. item_len = btrfs_item_size_nr(leaf, i);
  1322. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1323. item_len = 0;
  1324. if (sizeof(sh) + item_len + *sk_offset >
  1325. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1326. ret = 1;
  1327. goto overflow;
  1328. }
  1329. btrfs_item_key_to_cpu(leaf, key, i);
  1330. if (!key_in_sk(key, sk))
  1331. continue;
  1332. sh.objectid = key->objectid;
  1333. sh.offset = key->offset;
  1334. sh.type = key->type;
  1335. sh.len = item_len;
  1336. sh.transid = found_transid;
  1337. /* copy search result header */
  1338. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1339. *sk_offset += sizeof(sh);
  1340. if (item_len) {
  1341. char *p = buf + *sk_offset;
  1342. /* copy the item */
  1343. read_extent_buffer(leaf, p,
  1344. item_off, item_len);
  1345. *sk_offset += item_len;
  1346. }
  1347. (*num_found)++;
  1348. if (*num_found >= sk->nr_items)
  1349. break;
  1350. }
  1351. advance_key:
  1352. ret = 0;
  1353. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1354. key->offset++;
  1355. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1356. key->offset = 0;
  1357. key->type++;
  1358. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1359. key->offset = 0;
  1360. key->type = 0;
  1361. key->objectid++;
  1362. } else
  1363. ret = 1;
  1364. overflow:
  1365. return ret;
  1366. }
  1367. static noinline int search_ioctl(struct inode *inode,
  1368. struct btrfs_ioctl_search_args *args)
  1369. {
  1370. struct btrfs_root *root;
  1371. struct btrfs_key key;
  1372. struct btrfs_key max_key;
  1373. struct btrfs_path *path;
  1374. struct btrfs_ioctl_search_key *sk = &args->key;
  1375. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1376. int ret;
  1377. int num_found = 0;
  1378. unsigned long sk_offset = 0;
  1379. path = btrfs_alloc_path();
  1380. if (!path)
  1381. return -ENOMEM;
  1382. if (sk->tree_id == 0) {
  1383. /* search the root of the inode that was passed */
  1384. root = BTRFS_I(inode)->root;
  1385. } else {
  1386. key.objectid = sk->tree_id;
  1387. key.type = BTRFS_ROOT_ITEM_KEY;
  1388. key.offset = (u64)-1;
  1389. root = btrfs_read_fs_root_no_name(info, &key);
  1390. if (IS_ERR(root)) {
  1391. printk(KERN_ERR "could not find root %llu\n",
  1392. sk->tree_id);
  1393. btrfs_free_path(path);
  1394. return -ENOENT;
  1395. }
  1396. }
  1397. key.objectid = sk->min_objectid;
  1398. key.type = sk->min_type;
  1399. key.offset = sk->min_offset;
  1400. max_key.objectid = sk->max_objectid;
  1401. max_key.type = sk->max_type;
  1402. max_key.offset = sk->max_offset;
  1403. path->keep_locks = 1;
  1404. while(1) {
  1405. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1406. sk->min_transid);
  1407. if (ret != 0) {
  1408. if (ret > 0)
  1409. ret = 0;
  1410. goto err;
  1411. }
  1412. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1413. &sk_offset, &num_found);
  1414. btrfs_release_path(path);
  1415. if (ret || num_found >= sk->nr_items)
  1416. break;
  1417. }
  1418. ret = 0;
  1419. err:
  1420. sk->nr_items = num_found;
  1421. btrfs_free_path(path);
  1422. return ret;
  1423. }
  1424. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1425. void __user *argp)
  1426. {
  1427. struct btrfs_ioctl_search_args *args;
  1428. struct inode *inode;
  1429. int ret;
  1430. if (!capable(CAP_SYS_ADMIN))
  1431. return -EPERM;
  1432. args = memdup_user(argp, sizeof(*args));
  1433. if (IS_ERR(args))
  1434. return PTR_ERR(args);
  1435. inode = fdentry(file)->d_inode;
  1436. ret = search_ioctl(inode, args);
  1437. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1438. ret = -EFAULT;
  1439. kfree(args);
  1440. return ret;
  1441. }
  1442. /*
  1443. * Search INODE_REFs to identify path name of 'dirid' directory
  1444. * in a 'tree_id' tree. and sets path name to 'name'.
  1445. */
  1446. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1447. u64 tree_id, u64 dirid, char *name)
  1448. {
  1449. struct btrfs_root *root;
  1450. struct btrfs_key key;
  1451. char *ptr;
  1452. int ret = -1;
  1453. int slot;
  1454. int len;
  1455. int total_len = 0;
  1456. struct btrfs_inode_ref *iref;
  1457. struct extent_buffer *l;
  1458. struct btrfs_path *path;
  1459. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1460. name[0]='\0';
  1461. return 0;
  1462. }
  1463. path = btrfs_alloc_path();
  1464. if (!path)
  1465. return -ENOMEM;
  1466. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1467. key.objectid = tree_id;
  1468. key.type = BTRFS_ROOT_ITEM_KEY;
  1469. key.offset = (u64)-1;
  1470. root = btrfs_read_fs_root_no_name(info, &key);
  1471. if (IS_ERR(root)) {
  1472. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1473. ret = -ENOENT;
  1474. goto out;
  1475. }
  1476. key.objectid = dirid;
  1477. key.type = BTRFS_INODE_REF_KEY;
  1478. key.offset = (u64)-1;
  1479. while(1) {
  1480. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1481. if (ret < 0)
  1482. goto out;
  1483. l = path->nodes[0];
  1484. slot = path->slots[0];
  1485. if (ret > 0 && slot > 0)
  1486. slot--;
  1487. btrfs_item_key_to_cpu(l, &key, slot);
  1488. if (ret > 0 && (key.objectid != dirid ||
  1489. key.type != BTRFS_INODE_REF_KEY)) {
  1490. ret = -ENOENT;
  1491. goto out;
  1492. }
  1493. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1494. len = btrfs_inode_ref_name_len(l, iref);
  1495. ptr -= len + 1;
  1496. total_len += len + 1;
  1497. if (ptr < name)
  1498. goto out;
  1499. *(ptr + len) = '/';
  1500. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1501. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1502. break;
  1503. btrfs_release_path(path);
  1504. key.objectid = key.offset;
  1505. key.offset = (u64)-1;
  1506. dirid = key.objectid;
  1507. }
  1508. if (ptr < name)
  1509. goto out;
  1510. memmove(name, ptr, total_len);
  1511. name[total_len]='\0';
  1512. ret = 0;
  1513. out:
  1514. btrfs_free_path(path);
  1515. return ret;
  1516. }
  1517. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1518. void __user *argp)
  1519. {
  1520. struct btrfs_ioctl_ino_lookup_args *args;
  1521. struct inode *inode;
  1522. int ret;
  1523. if (!capable(CAP_SYS_ADMIN))
  1524. return -EPERM;
  1525. args = memdup_user(argp, sizeof(*args));
  1526. if (IS_ERR(args))
  1527. return PTR_ERR(args);
  1528. inode = fdentry(file)->d_inode;
  1529. if (args->treeid == 0)
  1530. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1531. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1532. args->treeid, args->objectid,
  1533. args->name);
  1534. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1535. ret = -EFAULT;
  1536. kfree(args);
  1537. return ret;
  1538. }
  1539. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1540. void __user *arg)
  1541. {
  1542. struct dentry *parent = fdentry(file);
  1543. struct dentry *dentry;
  1544. struct inode *dir = parent->d_inode;
  1545. struct inode *inode;
  1546. struct btrfs_root *root = BTRFS_I(dir)->root;
  1547. struct btrfs_root *dest = NULL;
  1548. struct btrfs_ioctl_vol_args *vol_args;
  1549. struct btrfs_trans_handle *trans;
  1550. int namelen;
  1551. int ret;
  1552. int err = 0;
  1553. vol_args = memdup_user(arg, sizeof(*vol_args));
  1554. if (IS_ERR(vol_args))
  1555. return PTR_ERR(vol_args);
  1556. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1557. namelen = strlen(vol_args->name);
  1558. if (strchr(vol_args->name, '/') ||
  1559. strncmp(vol_args->name, "..", namelen) == 0) {
  1560. err = -EINVAL;
  1561. goto out;
  1562. }
  1563. err = mnt_want_write(file->f_path.mnt);
  1564. if (err)
  1565. goto out;
  1566. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1567. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1568. if (IS_ERR(dentry)) {
  1569. err = PTR_ERR(dentry);
  1570. goto out_unlock_dir;
  1571. }
  1572. if (!dentry->d_inode) {
  1573. err = -ENOENT;
  1574. goto out_dput;
  1575. }
  1576. inode = dentry->d_inode;
  1577. dest = BTRFS_I(inode)->root;
  1578. if (!capable(CAP_SYS_ADMIN)){
  1579. /*
  1580. * Regular user. Only allow this with a special mount
  1581. * option, when the user has write+exec access to the
  1582. * subvol root, and when rmdir(2) would have been
  1583. * allowed.
  1584. *
  1585. * Note that this is _not_ check that the subvol is
  1586. * empty or doesn't contain data that we wouldn't
  1587. * otherwise be able to delete.
  1588. *
  1589. * Users who want to delete empty subvols should try
  1590. * rmdir(2).
  1591. */
  1592. err = -EPERM;
  1593. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1594. goto out_dput;
  1595. /*
  1596. * Do not allow deletion if the parent dir is the same
  1597. * as the dir to be deleted. That means the ioctl
  1598. * must be called on the dentry referencing the root
  1599. * of the subvol, not a random directory contained
  1600. * within it.
  1601. */
  1602. err = -EINVAL;
  1603. if (root == dest)
  1604. goto out_dput;
  1605. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1606. if (err)
  1607. goto out_dput;
  1608. /* check if subvolume may be deleted by a non-root user */
  1609. err = btrfs_may_delete(dir, dentry, 1);
  1610. if (err)
  1611. goto out_dput;
  1612. }
  1613. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1614. err = -EINVAL;
  1615. goto out_dput;
  1616. }
  1617. mutex_lock(&inode->i_mutex);
  1618. err = d_invalidate(dentry);
  1619. if (err)
  1620. goto out_unlock;
  1621. down_write(&root->fs_info->subvol_sem);
  1622. err = may_destroy_subvol(dest);
  1623. if (err)
  1624. goto out_up_write;
  1625. trans = btrfs_start_transaction(root, 0);
  1626. if (IS_ERR(trans)) {
  1627. err = PTR_ERR(trans);
  1628. goto out_up_write;
  1629. }
  1630. trans->block_rsv = &root->fs_info->global_block_rsv;
  1631. ret = btrfs_unlink_subvol(trans, root, dir,
  1632. dest->root_key.objectid,
  1633. dentry->d_name.name,
  1634. dentry->d_name.len);
  1635. BUG_ON(ret);
  1636. btrfs_record_root_in_trans(trans, dest);
  1637. memset(&dest->root_item.drop_progress, 0,
  1638. sizeof(dest->root_item.drop_progress));
  1639. dest->root_item.drop_level = 0;
  1640. btrfs_set_root_refs(&dest->root_item, 0);
  1641. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1642. ret = btrfs_insert_orphan_item(trans,
  1643. root->fs_info->tree_root,
  1644. dest->root_key.objectid);
  1645. BUG_ON(ret);
  1646. }
  1647. ret = btrfs_end_transaction(trans, root);
  1648. BUG_ON(ret);
  1649. inode->i_flags |= S_DEAD;
  1650. out_up_write:
  1651. up_write(&root->fs_info->subvol_sem);
  1652. out_unlock:
  1653. mutex_unlock(&inode->i_mutex);
  1654. if (!err) {
  1655. shrink_dcache_sb(root->fs_info->sb);
  1656. btrfs_invalidate_inodes(dest);
  1657. d_delete(dentry);
  1658. }
  1659. out_dput:
  1660. dput(dentry);
  1661. out_unlock_dir:
  1662. mutex_unlock(&dir->i_mutex);
  1663. mnt_drop_write(file->f_path.mnt);
  1664. out:
  1665. kfree(vol_args);
  1666. return err;
  1667. }
  1668. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1669. {
  1670. struct inode *inode = fdentry(file)->d_inode;
  1671. struct btrfs_root *root = BTRFS_I(inode)->root;
  1672. struct btrfs_ioctl_defrag_range_args *range;
  1673. int ret;
  1674. if (btrfs_root_readonly(root))
  1675. return -EROFS;
  1676. ret = mnt_want_write(file->f_path.mnt);
  1677. if (ret)
  1678. return ret;
  1679. switch (inode->i_mode & S_IFMT) {
  1680. case S_IFDIR:
  1681. if (!capable(CAP_SYS_ADMIN)) {
  1682. ret = -EPERM;
  1683. goto out;
  1684. }
  1685. ret = btrfs_defrag_root(root, 0);
  1686. if (ret)
  1687. goto out;
  1688. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1689. break;
  1690. case S_IFREG:
  1691. if (!(file->f_mode & FMODE_WRITE)) {
  1692. ret = -EINVAL;
  1693. goto out;
  1694. }
  1695. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1696. if (!range) {
  1697. ret = -ENOMEM;
  1698. goto out;
  1699. }
  1700. if (argp) {
  1701. if (copy_from_user(range, argp,
  1702. sizeof(*range))) {
  1703. ret = -EFAULT;
  1704. kfree(range);
  1705. goto out;
  1706. }
  1707. /* compression requires us to start the IO */
  1708. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1709. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1710. range->extent_thresh = (u32)-1;
  1711. }
  1712. } else {
  1713. /* the rest are all set to zero by kzalloc */
  1714. range->len = (u64)-1;
  1715. }
  1716. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1717. range, 0, 0);
  1718. if (ret > 0)
  1719. ret = 0;
  1720. kfree(range);
  1721. break;
  1722. default:
  1723. ret = -EINVAL;
  1724. }
  1725. out:
  1726. mnt_drop_write(file->f_path.mnt);
  1727. return ret;
  1728. }
  1729. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1730. {
  1731. struct btrfs_ioctl_vol_args *vol_args;
  1732. int ret;
  1733. if (!capable(CAP_SYS_ADMIN))
  1734. return -EPERM;
  1735. vol_args = memdup_user(arg, sizeof(*vol_args));
  1736. if (IS_ERR(vol_args))
  1737. return PTR_ERR(vol_args);
  1738. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1739. ret = btrfs_init_new_device(root, vol_args->name);
  1740. kfree(vol_args);
  1741. return ret;
  1742. }
  1743. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1744. {
  1745. struct btrfs_ioctl_vol_args *vol_args;
  1746. int ret;
  1747. if (!capable(CAP_SYS_ADMIN))
  1748. return -EPERM;
  1749. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1750. return -EROFS;
  1751. vol_args = memdup_user(arg, sizeof(*vol_args));
  1752. if (IS_ERR(vol_args))
  1753. return PTR_ERR(vol_args);
  1754. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1755. ret = btrfs_rm_device(root, vol_args->name);
  1756. kfree(vol_args);
  1757. return ret;
  1758. }
  1759. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  1760. {
  1761. struct btrfs_ioctl_fs_info_args *fi_args;
  1762. struct btrfs_device *device;
  1763. struct btrfs_device *next;
  1764. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1765. int ret = 0;
  1766. if (!capable(CAP_SYS_ADMIN))
  1767. return -EPERM;
  1768. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  1769. if (!fi_args)
  1770. return -ENOMEM;
  1771. fi_args->num_devices = fs_devices->num_devices;
  1772. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  1773. mutex_lock(&fs_devices->device_list_mutex);
  1774. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  1775. if (device->devid > fi_args->max_id)
  1776. fi_args->max_id = device->devid;
  1777. }
  1778. mutex_unlock(&fs_devices->device_list_mutex);
  1779. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  1780. ret = -EFAULT;
  1781. kfree(fi_args);
  1782. return ret;
  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. atomic_inc(&root->fs_info->open_ioctl_trans);
  2134. ret = -ENOMEM;
  2135. trans = btrfs_start_ioctl_transaction(root);
  2136. if (IS_ERR(trans))
  2137. goto out_drop;
  2138. file->private_data = trans;
  2139. return 0;
  2140. out_drop:
  2141. atomic_dec(&root->fs_info->open_ioctl_trans);
  2142. mnt_drop_write(file->f_path.mnt);
  2143. out:
  2144. return ret;
  2145. }
  2146. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2147. {
  2148. struct inode *inode = fdentry(file)->d_inode;
  2149. struct btrfs_root *root = BTRFS_I(inode)->root;
  2150. struct btrfs_root *new_root;
  2151. struct btrfs_dir_item *di;
  2152. struct btrfs_trans_handle *trans;
  2153. struct btrfs_path *path;
  2154. struct btrfs_key location;
  2155. struct btrfs_disk_key disk_key;
  2156. struct btrfs_super_block *disk_super;
  2157. u64 features;
  2158. u64 objectid = 0;
  2159. u64 dir_id;
  2160. if (!capable(CAP_SYS_ADMIN))
  2161. return -EPERM;
  2162. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  2163. return -EFAULT;
  2164. if (!objectid)
  2165. objectid = root->root_key.objectid;
  2166. location.objectid = objectid;
  2167. location.type = BTRFS_ROOT_ITEM_KEY;
  2168. location.offset = (u64)-1;
  2169. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2170. if (IS_ERR(new_root))
  2171. return PTR_ERR(new_root);
  2172. if (btrfs_root_refs(&new_root->root_item) == 0)
  2173. return -ENOENT;
  2174. path = btrfs_alloc_path();
  2175. if (!path)
  2176. return -ENOMEM;
  2177. path->leave_spinning = 1;
  2178. trans = btrfs_start_transaction(root, 1);
  2179. if (IS_ERR(trans)) {
  2180. btrfs_free_path(path);
  2181. return PTR_ERR(trans);
  2182. }
  2183. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  2184. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2185. dir_id, "default", 7, 1);
  2186. if (IS_ERR_OR_NULL(di)) {
  2187. btrfs_free_path(path);
  2188. btrfs_end_transaction(trans, root);
  2189. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2190. "this isn't going to work\n");
  2191. return -ENOENT;
  2192. }
  2193. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2194. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2195. btrfs_mark_buffer_dirty(path->nodes[0]);
  2196. btrfs_free_path(path);
  2197. disk_super = &root->fs_info->super_copy;
  2198. features = btrfs_super_incompat_flags(disk_super);
  2199. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  2200. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  2201. btrfs_set_super_incompat_flags(disk_super, features);
  2202. }
  2203. btrfs_end_transaction(trans, root);
  2204. return 0;
  2205. }
  2206. static void get_block_group_info(struct list_head *groups_list,
  2207. struct btrfs_ioctl_space_info *space)
  2208. {
  2209. struct btrfs_block_group_cache *block_group;
  2210. space->total_bytes = 0;
  2211. space->used_bytes = 0;
  2212. space->flags = 0;
  2213. list_for_each_entry(block_group, groups_list, list) {
  2214. space->flags = block_group->flags;
  2215. space->total_bytes += block_group->key.offset;
  2216. space->used_bytes +=
  2217. btrfs_block_group_used(&block_group->item);
  2218. }
  2219. }
  2220. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2221. {
  2222. struct btrfs_ioctl_space_args space_args;
  2223. struct btrfs_ioctl_space_info space;
  2224. struct btrfs_ioctl_space_info *dest;
  2225. struct btrfs_ioctl_space_info *dest_orig;
  2226. struct btrfs_ioctl_space_info __user *user_dest;
  2227. struct btrfs_space_info *info;
  2228. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2229. BTRFS_BLOCK_GROUP_SYSTEM,
  2230. BTRFS_BLOCK_GROUP_METADATA,
  2231. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2232. int num_types = 4;
  2233. int alloc_size;
  2234. int ret = 0;
  2235. u64 slot_count = 0;
  2236. int i, c;
  2237. if (copy_from_user(&space_args,
  2238. (struct btrfs_ioctl_space_args __user *)arg,
  2239. sizeof(space_args)))
  2240. return -EFAULT;
  2241. for (i = 0; i < num_types; i++) {
  2242. struct btrfs_space_info *tmp;
  2243. info = NULL;
  2244. rcu_read_lock();
  2245. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2246. list) {
  2247. if (tmp->flags == types[i]) {
  2248. info = tmp;
  2249. break;
  2250. }
  2251. }
  2252. rcu_read_unlock();
  2253. if (!info)
  2254. continue;
  2255. down_read(&info->groups_sem);
  2256. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2257. if (!list_empty(&info->block_groups[c]))
  2258. slot_count++;
  2259. }
  2260. up_read(&info->groups_sem);
  2261. }
  2262. /* space_slots == 0 means they are asking for a count */
  2263. if (space_args.space_slots == 0) {
  2264. space_args.total_spaces = slot_count;
  2265. goto out;
  2266. }
  2267. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2268. alloc_size = sizeof(*dest) * slot_count;
  2269. /* we generally have at most 6 or so space infos, one for each raid
  2270. * level. So, a whole page should be more than enough for everyone
  2271. */
  2272. if (alloc_size > PAGE_CACHE_SIZE)
  2273. return -ENOMEM;
  2274. space_args.total_spaces = 0;
  2275. dest = kmalloc(alloc_size, GFP_NOFS);
  2276. if (!dest)
  2277. return -ENOMEM;
  2278. dest_orig = dest;
  2279. /* now we have a buffer to copy into */
  2280. for (i = 0; i < num_types; i++) {
  2281. struct btrfs_space_info *tmp;
  2282. if (!slot_count)
  2283. break;
  2284. info = NULL;
  2285. rcu_read_lock();
  2286. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2287. list) {
  2288. if (tmp->flags == types[i]) {
  2289. info = tmp;
  2290. break;
  2291. }
  2292. }
  2293. rcu_read_unlock();
  2294. if (!info)
  2295. continue;
  2296. down_read(&info->groups_sem);
  2297. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2298. if (!list_empty(&info->block_groups[c])) {
  2299. get_block_group_info(&info->block_groups[c],
  2300. &space);
  2301. memcpy(dest, &space, sizeof(space));
  2302. dest++;
  2303. space_args.total_spaces++;
  2304. slot_count--;
  2305. }
  2306. if (!slot_count)
  2307. break;
  2308. }
  2309. up_read(&info->groups_sem);
  2310. }
  2311. user_dest = (struct btrfs_ioctl_space_info *)
  2312. (arg + sizeof(struct btrfs_ioctl_space_args));
  2313. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2314. ret = -EFAULT;
  2315. kfree(dest_orig);
  2316. out:
  2317. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2318. ret = -EFAULT;
  2319. return ret;
  2320. }
  2321. /*
  2322. * there are many ways the trans_start and trans_end ioctls can lead
  2323. * to deadlocks. They should only be used by applications that
  2324. * basically own the machine, and have a very in depth understanding
  2325. * of all the possible deadlocks and enospc problems.
  2326. */
  2327. long btrfs_ioctl_trans_end(struct file *file)
  2328. {
  2329. struct inode *inode = fdentry(file)->d_inode;
  2330. struct btrfs_root *root = BTRFS_I(inode)->root;
  2331. struct btrfs_trans_handle *trans;
  2332. trans = file->private_data;
  2333. if (!trans)
  2334. return -EINVAL;
  2335. file->private_data = NULL;
  2336. btrfs_end_transaction(trans, root);
  2337. atomic_dec(&root->fs_info->open_ioctl_trans);
  2338. mnt_drop_write(file->f_path.mnt);
  2339. return 0;
  2340. }
  2341. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  2342. {
  2343. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2344. struct btrfs_trans_handle *trans;
  2345. u64 transid;
  2346. int ret;
  2347. trans = btrfs_start_transaction(root, 0);
  2348. if (IS_ERR(trans))
  2349. return PTR_ERR(trans);
  2350. transid = trans->transid;
  2351. ret = btrfs_commit_transaction_async(trans, root, 0);
  2352. if (ret) {
  2353. btrfs_end_transaction(trans, root);
  2354. return ret;
  2355. }
  2356. if (argp)
  2357. if (copy_to_user(argp, &transid, sizeof(transid)))
  2358. return -EFAULT;
  2359. return 0;
  2360. }
  2361. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2362. {
  2363. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2364. u64 transid;
  2365. if (argp) {
  2366. if (copy_from_user(&transid, argp, sizeof(transid)))
  2367. return -EFAULT;
  2368. } else {
  2369. transid = 0; /* current trans */
  2370. }
  2371. return btrfs_wait_for_commit(root, transid);
  2372. }
  2373. static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
  2374. {
  2375. int ret;
  2376. struct btrfs_ioctl_scrub_args *sa;
  2377. if (!capable(CAP_SYS_ADMIN))
  2378. return -EPERM;
  2379. sa = memdup_user(arg, sizeof(*sa));
  2380. if (IS_ERR(sa))
  2381. return PTR_ERR(sa);
  2382. ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
  2383. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
  2384. if (copy_to_user(arg, sa, sizeof(*sa)))
  2385. ret = -EFAULT;
  2386. kfree(sa);
  2387. return ret;
  2388. }
  2389. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2390. {
  2391. if (!capable(CAP_SYS_ADMIN))
  2392. return -EPERM;
  2393. return btrfs_scrub_cancel(root);
  2394. }
  2395. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2396. void __user *arg)
  2397. {
  2398. struct btrfs_ioctl_scrub_args *sa;
  2399. int ret;
  2400. if (!capable(CAP_SYS_ADMIN))
  2401. return -EPERM;
  2402. sa = memdup_user(arg, sizeof(*sa));
  2403. if (IS_ERR(sa))
  2404. return PTR_ERR(sa);
  2405. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2406. if (copy_to_user(arg, sa, sizeof(*sa)))
  2407. ret = -EFAULT;
  2408. kfree(sa);
  2409. return ret;
  2410. }
  2411. long btrfs_ioctl(struct file *file, unsigned int
  2412. cmd, unsigned long arg)
  2413. {
  2414. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2415. void __user *argp = (void __user *)arg;
  2416. switch (cmd) {
  2417. case FS_IOC_GETFLAGS:
  2418. return btrfs_ioctl_getflags(file, argp);
  2419. case FS_IOC_SETFLAGS:
  2420. return btrfs_ioctl_setflags(file, argp);
  2421. case FS_IOC_GETVERSION:
  2422. return btrfs_ioctl_getversion(file, argp);
  2423. case FITRIM:
  2424. return btrfs_ioctl_fitrim(file, argp);
  2425. case BTRFS_IOC_SNAP_CREATE:
  2426. return btrfs_ioctl_snap_create(file, argp, 0);
  2427. case BTRFS_IOC_SNAP_CREATE_V2:
  2428. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2429. case BTRFS_IOC_SUBVOL_CREATE:
  2430. return btrfs_ioctl_snap_create(file, argp, 1);
  2431. case BTRFS_IOC_SNAP_DESTROY:
  2432. return btrfs_ioctl_snap_destroy(file, argp);
  2433. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2434. return btrfs_ioctl_subvol_getflags(file, argp);
  2435. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2436. return btrfs_ioctl_subvol_setflags(file, argp);
  2437. case BTRFS_IOC_DEFAULT_SUBVOL:
  2438. return btrfs_ioctl_default_subvol(file, argp);
  2439. case BTRFS_IOC_DEFRAG:
  2440. return btrfs_ioctl_defrag(file, NULL);
  2441. case BTRFS_IOC_DEFRAG_RANGE:
  2442. return btrfs_ioctl_defrag(file, argp);
  2443. case BTRFS_IOC_RESIZE:
  2444. return btrfs_ioctl_resize(root, argp);
  2445. case BTRFS_IOC_ADD_DEV:
  2446. return btrfs_ioctl_add_dev(root, argp);
  2447. case BTRFS_IOC_RM_DEV:
  2448. return btrfs_ioctl_rm_dev(root, argp);
  2449. case BTRFS_IOC_FS_INFO:
  2450. return btrfs_ioctl_fs_info(root, argp);
  2451. case BTRFS_IOC_DEV_INFO:
  2452. return btrfs_ioctl_dev_info(root, argp);
  2453. case BTRFS_IOC_BALANCE:
  2454. return btrfs_balance(root->fs_info->dev_root);
  2455. case BTRFS_IOC_CLONE:
  2456. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2457. case BTRFS_IOC_CLONE_RANGE:
  2458. return btrfs_ioctl_clone_range(file, argp);
  2459. case BTRFS_IOC_TRANS_START:
  2460. return btrfs_ioctl_trans_start(file);
  2461. case BTRFS_IOC_TRANS_END:
  2462. return btrfs_ioctl_trans_end(file);
  2463. case BTRFS_IOC_TREE_SEARCH:
  2464. return btrfs_ioctl_tree_search(file, argp);
  2465. case BTRFS_IOC_INO_LOOKUP:
  2466. return btrfs_ioctl_ino_lookup(file, argp);
  2467. case BTRFS_IOC_SPACE_INFO:
  2468. return btrfs_ioctl_space_info(root, argp);
  2469. case BTRFS_IOC_SYNC:
  2470. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2471. return 0;
  2472. case BTRFS_IOC_START_SYNC:
  2473. return btrfs_ioctl_start_sync(file, argp);
  2474. case BTRFS_IOC_WAIT_SYNC:
  2475. return btrfs_ioctl_wait_sync(file, argp);
  2476. case BTRFS_IOC_SCRUB:
  2477. return btrfs_ioctl_scrub(root, argp);
  2478. case BTRFS_IOC_SCRUB_CANCEL:
  2479. return btrfs_ioctl_scrub_cancel(root, argp);
  2480. case BTRFS_IOC_SCRUB_PROGRESS:
  2481. return btrfs_ioctl_scrub_progress(root, argp);
  2482. }
  2483. return -ENOTTY;
  2484. }