ioctl.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934
  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. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  731. if (isize == 0)
  732. return 0;
  733. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  734. ret = btrfs_delalloc_reserve_space(inode,
  735. num_pages << PAGE_CACHE_SHIFT);
  736. if (ret)
  737. return ret;
  738. again:
  739. ret = 0;
  740. i_done = 0;
  741. /* step one, lock all the pages */
  742. for (i = 0; i < num_pages; i++) {
  743. struct page *page;
  744. page = find_or_create_page(inode->i_mapping,
  745. start_index + i, mask);
  746. if (!page)
  747. break;
  748. if (!PageUptodate(page)) {
  749. btrfs_readpage(NULL, page);
  750. lock_page(page);
  751. if (!PageUptodate(page)) {
  752. unlock_page(page);
  753. page_cache_release(page);
  754. ret = -EIO;
  755. break;
  756. }
  757. }
  758. isize = i_size_read(inode);
  759. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  760. if (!isize || page->index > file_end ||
  761. page->mapping != inode->i_mapping) {
  762. /* whoops, we blew past eof, skip this page */
  763. unlock_page(page);
  764. page_cache_release(page);
  765. break;
  766. }
  767. pages[i] = page;
  768. i_done++;
  769. }
  770. if (!i_done || ret)
  771. goto out;
  772. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  773. goto out;
  774. /*
  775. * so now we have a nice long stream of locked
  776. * and up to date pages, lets wait on them
  777. */
  778. for (i = 0; i < i_done; i++)
  779. wait_on_page_writeback(pages[i]);
  780. page_start = page_offset(pages[0]);
  781. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  782. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  783. page_start, page_end - 1, 0, &cached_state,
  784. GFP_NOFS);
  785. ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
  786. if (ordered &&
  787. ordered->file_offset + ordered->len > page_start &&
  788. ordered->file_offset < page_end) {
  789. btrfs_put_ordered_extent(ordered);
  790. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  791. page_start, page_end - 1,
  792. &cached_state, GFP_NOFS);
  793. for (i = 0; i < i_done; i++) {
  794. unlock_page(pages[i]);
  795. page_cache_release(pages[i]);
  796. }
  797. btrfs_wait_ordered_range(inode, page_start,
  798. page_end - page_start);
  799. goto again;
  800. }
  801. if (ordered)
  802. btrfs_put_ordered_extent(ordered);
  803. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  804. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  805. EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
  806. GFP_NOFS);
  807. if (i_done != num_pages) {
  808. spin_lock(&BTRFS_I(inode)->lock);
  809. BTRFS_I(inode)->outstanding_extents++;
  810. spin_unlock(&BTRFS_I(inode)->lock);
  811. btrfs_delalloc_release_space(inode,
  812. (num_pages - i_done) << PAGE_CACHE_SHIFT);
  813. }
  814. btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
  815. &cached_state);
  816. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  817. page_start, page_end - 1, &cached_state,
  818. GFP_NOFS);
  819. for (i = 0; i < i_done; i++) {
  820. clear_page_dirty_for_io(pages[i]);
  821. ClearPageChecked(pages[i]);
  822. set_page_extent_mapped(pages[i]);
  823. set_page_dirty(pages[i]);
  824. unlock_page(pages[i]);
  825. page_cache_release(pages[i]);
  826. }
  827. return i_done;
  828. out:
  829. for (i = 0; i < i_done; i++) {
  830. unlock_page(pages[i]);
  831. page_cache_release(pages[i]);
  832. }
  833. btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
  834. return ret;
  835. }
  836. int btrfs_defrag_file(struct inode *inode, struct file *file,
  837. struct btrfs_ioctl_defrag_range_args *range,
  838. u64 newer_than, unsigned long max_to_defrag)
  839. {
  840. struct btrfs_root *root = BTRFS_I(inode)->root;
  841. struct btrfs_super_block *disk_super;
  842. struct file_ra_state *ra = NULL;
  843. unsigned long last_index;
  844. u64 features;
  845. u64 last_len = 0;
  846. u64 skip = 0;
  847. u64 defrag_end = 0;
  848. u64 newer_off = range->start;
  849. int newer_left = 0;
  850. unsigned long i;
  851. int ret;
  852. int defrag_count = 0;
  853. int compress_type = BTRFS_COMPRESS_ZLIB;
  854. int extent_thresh = range->extent_thresh;
  855. int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  856. u64 new_align = ~((u64)128 * 1024 - 1);
  857. struct page **pages = NULL;
  858. if (extent_thresh == 0)
  859. extent_thresh = 256 * 1024;
  860. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  861. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  862. return -EINVAL;
  863. if (range->compress_type)
  864. compress_type = range->compress_type;
  865. }
  866. if (inode->i_size == 0)
  867. return 0;
  868. /*
  869. * if we were not given a file, allocate a readahead
  870. * context
  871. */
  872. if (!file) {
  873. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  874. if (!ra)
  875. return -ENOMEM;
  876. file_ra_state_init(ra, inode->i_mapping);
  877. } else {
  878. ra = &file->f_ra;
  879. }
  880. pages = kmalloc(sizeof(struct page *) * newer_cluster,
  881. GFP_NOFS);
  882. if (!pages) {
  883. ret = -ENOMEM;
  884. goto out_ra;
  885. }
  886. /* find the last page to defrag */
  887. if (range->start + range->len > range->start) {
  888. last_index = min_t(u64, inode->i_size - 1,
  889. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  890. } else {
  891. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  892. }
  893. if (newer_than) {
  894. ret = find_new_extents(root, inode, newer_than,
  895. &newer_off, 64 * 1024);
  896. if (!ret) {
  897. range->start = newer_off;
  898. /*
  899. * we always align our defrag to help keep
  900. * the extents in the file evenly spaced
  901. */
  902. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  903. newer_left = newer_cluster;
  904. } else
  905. goto out_ra;
  906. } else {
  907. i = range->start >> PAGE_CACHE_SHIFT;
  908. }
  909. if (!max_to_defrag)
  910. max_to_defrag = last_index - 1;
  911. while (i <= last_index && defrag_count < max_to_defrag) {
  912. /*
  913. * make sure we stop running if someone unmounts
  914. * the FS
  915. */
  916. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  917. break;
  918. if (!newer_than &&
  919. !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  920. PAGE_CACHE_SIZE,
  921. extent_thresh,
  922. &last_len, &skip,
  923. &defrag_end)) {
  924. unsigned long next;
  925. /*
  926. * the should_defrag function tells us how much to skip
  927. * bump our counter by the suggested amount
  928. */
  929. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  930. i = max(i + 1, next);
  931. continue;
  932. }
  933. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  934. BTRFS_I(inode)->force_compress = compress_type;
  935. btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
  936. ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
  937. if (ret < 0)
  938. goto out_ra;
  939. defrag_count += ret;
  940. balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
  941. i += ret;
  942. if (newer_than) {
  943. if (newer_off == (u64)-1)
  944. break;
  945. newer_off = max(newer_off + 1,
  946. (u64)i << PAGE_CACHE_SHIFT);
  947. ret = find_new_extents(root, inode,
  948. newer_than, &newer_off,
  949. 64 * 1024);
  950. if (!ret) {
  951. range->start = newer_off;
  952. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  953. newer_left = newer_cluster;
  954. } else {
  955. break;
  956. }
  957. } else {
  958. i++;
  959. }
  960. }
  961. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  962. filemap_flush(inode->i_mapping);
  963. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  964. /* the filemap_flush will queue IO into the worker threads, but
  965. * we have to make sure the IO is actually started and that
  966. * ordered extents get created before we return
  967. */
  968. atomic_inc(&root->fs_info->async_submit_draining);
  969. while (atomic_read(&root->fs_info->nr_async_submits) ||
  970. atomic_read(&root->fs_info->async_delalloc_pages)) {
  971. wait_event(root->fs_info->async_submit_wait,
  972. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  973. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  974. }
  975. atomic_dec(&root->fs_info->async_submit_draining);
  976. mutex_lock(&inode->i_mutex);
  977. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  978. mutex_unlock(&inode->i_mutex);
  979. }
  980. disk_super = &root->fs_info->super_copy;
  981. features = btrfs_super_incompat_flags(disk_super);
  982. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  983. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  984. btrfs_set_super_incompat_flags(disk_super, features);
  985. }
  986. if (!file)
  987. kfree(ra);
  988. return defrag_count;
  989. out_ra:
  990. if (!file)
  991. kfree(ra);
  992. kfree(pages);
  993. return ret;
  994. }
  995. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  996. void __user *arg)
  997. {
  998. u64 new_size;
  999. u64 old_size;
  1000. u64 devid = 1;
  1001. struct btrfs_ioctl_vol_args *vol_args;
  1002. struct btrfs_trans_handle *trans;
  1003. struct btrfs_device *device = NULL;
  1004. char *sizestr;
  1005. char *devstr = NULL;
  1006. int ret = 0;
  1007. int mod = 0;
  1008. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1009. return -EROFS;
  1010. if (!capable(CAP_SYS_ADMIN))
  1011. return -EPERM;
  1012. vol_args = memdup_user(arg, sizeof(*vol_args));
  1013. if (IS_ERR(vol_args))
  1014. return PTR_ERR(vol_args);
  1015. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1016. mutex_lock(&root->fs_info->volume_mutex);
  1017. sizestr = vol_args->name;
  1018. devstr = strchr(sizestr, ':');
  1019. if (devstr) {
  1020. char *end;
  1021. sizestr = devstr + 1;
  1022. *devstr = '\0';
  1023. devstr = vol_args->name;
  1024. devid = simple_strtoull(devstr, &end, 10);
  1025. printk(KERN_INFO "resizing devid %llu\n",
  1026. (unsigned long long)devid);
  1027. }
  1028. device = btrfs_find_device(root, devid, NULL, NULL);
  1029. if (!device) {
  1030. printk(KERN_INFO "resizer unable to find device %llu\n",
  1031. (unsigned long long)devid);
  1032. ret = -EINVAL;
  1033. goto out_unlock;
  1034. }
  1035. if (!strcmp(sizestr, "max"))
  1036. new_size = device->bdev->bd_inode->i_size;
  1037. else {
  1038. if (sizestr[0] == '-') {
  1039. mod = -1;
  1040. sizestr++;
  1041. } else if (sizestr[0] == '+') {
  1042. mod = 1;
  1043. sizestr++;
  1044. }
  1045. new_size = memparse(sizestr, NULL);
  1046. if (new_size == 0) {
  1047. ret = -EINVAL;
  1048. goto out_unlock;
  1049. }
  1050. }
  1051. old_size = device->total_bytes;
  1052. if (mod < 0) {
  1053. if (new_size > old_size) {
  1054. ret = -EINVAL;
  1055. goto out_unlock;
  1056. }
  1057. new_size = old_size - new_size;
  1058. } else if (mod > 0) {
  1059. new_size = old_size + new_size;
  1060. }
  1061. if (new_size < 256 * 1024 * 1024) {
  1062. ret = -EINVAL;
  1063. goto out_unlock;
  1064. }
  1065. if (new_size > device->bdev->bd_inode->i_size) {
  1066. ret = -EFBIG;
  1067. goto out_unlock;
  1068. }
  1069. do_div(new_size, root->sectorsize);
  1070. new_size *= root->sectorsize;
  1071. printk(KERN_INFO "new size for %s is %llu\n",
  1072. device->name, (unsigned long long)new_size);
  1073. if (new_size > old_size) {
  1074. trans = btrfs_start_transaction(root, 0);
  1075. if (IS_ERR(trans)) {
  1076. ret = PTR_ERR(trans);
  1077. goto out_unlock;
  1078. }
  1079. ret = btrfs_grow_device(trans, device, new_size);
  1080. btrfs_commit_transaction(trans, root);
  1081. } else {
  1082. ret = btrfs_shrink_device(device, new_size);
  1083. }
  1084. out_unlock:
  1085. mutex_unlock(&root->fs_info->volume_mutex);
  1086. kfree(vol_args);
  1087. return ret;
  1088. }
  1089. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1090. char *name,
  1091. unsigned long fd,
  1092. int subvol,
  1093. u64 *transid,
  1094. bool readonly)
  1095. {
  1096. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1097. struct file *src_file;
  1098. int namelen;
  1099. int ret = 0;
  1100. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1101. return -EROFS;
  1102. namelen = strlen(name);
  1103. if (strchr(name, '/')) {
  1104. ret = -EINVAL;
  1105. goto out;
  1106. }
  1107. if (subvol) {
  1108. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1109. NULL, transid, readonly);
  1110. } else {
  1111. struct inode *src_inode;
  1112. src_file = fget(fd);
  1113. if (!src_file) {
  1114. ret = -EINVAL;
  1115. goto out;
  1116. }
  1117. src_inode = src_file->f_path.dentry->d_inode;
  1118. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  1119. printk(KERN_INFO "btrfs: Snapshot src from "
  1120. "another FS\n");
  1121. ret = -EINVAL;
  1122. fput(src_file);
  1123. goto out;
  1124. }
  1125. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1126. BTRFS_I(src_inode)->root,
  1127. transid, readonly);
  1128. fput(src_file);
  1129. }
  1130. out:
  1131. return ret;
  1132. }
  1133. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1134. void __user *arg, int subvol)
  1135. {
  1136. struct btrfs_ioctl_vol_args *vol_args;
  1137. int ret;
  1138. vol_args = memdup_user(arg, sizeof(*vol_args));
  1139. if (IS_ERR(vol_args))
  1140. return PTR_ERR(vol_args);
  1141. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1142. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1143. vol_args->fd, subvol,
  1144. NULL, false);
  1145. kfree(vol_args);
  1146. return ret;
  1147. }
  1148. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1149. void __user *arg, int subvol)
  1150. {
  1151. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1152. int ret;
  1153. u64 transid = 0;
  1154. u64 *ptr = NULL;
  1155. bool readonly = false;
  1156. vol_args = memdup_user(arg, sizeof(*vol_args));
  1157. if (IS_ERR(vol_args))
  1158. return PTR_ERR(vol_args);
  1159. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1160. if (vol_args->flags &
  1161. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  1162. ret = -EOPNOTSUPP;
  1163. goto out;
  1164. }
  1165. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1166. ptr = &transid;
  1167. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1168. readonly = true;
  1169. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1170. vol_args->fd, subvol,
  1171. ptr, readonly);
  1172. if (ret == 0 && ptr &&
  1173. copy_to_user(arg +
  1174. offsetof(struct btrfs_ioctl_vol_args_v2,
  1175. transid), ptr, sizeof(*ptr)))
  1176. ret = -EFAULT;
  1177. out:
  1178. kfree(vol_args);
  1179. return ret;
  1180. }
  1181. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1182. void __user *arg)
  1183. {
  1184. struct inode *inode = fdentry(file)->d_inode;
  1185. struct btrfs_root *root = BTRFS_I(inode)->root;
  1186. int ret = 0;
  1187. u64 flags = 0;
  1188. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1189. return -EINVAL;
  1190. down_read(&root->fs_info->subvol_sem);
  1191. if (btrfs_root_readonly(root))
  1192. flags |= BTRFS_SUBVOL_RDONLY;
  1193. up_read(&root->fs_info->subvol_sem);
  1194. if (copy_to_user(arg, &flags, sizeof(flags)))
  1195. ret = -EFAULT;
  1196. return ret;
  1197. }
  1198. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1199. void __user *arg)
  1200. {
  1201. struct inode *inode = fdentry(file)->d_inode;
  1202. struct btrfs_root *root = BTRFS_I(inode)->root;
  1203. struct btrfs_trans_handle *trans;
  1204. u64 root_flags;
  1205. u64 flags;
  1206. int ret = 0;
  1207. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1208. return -EROFS;
  1209. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1210. return -EINVAL;
  1211. if (copy_from_user(&flags, arg, sizeof(flags)))
  1212. return -EFAULT;
  1213. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1214. return -EINVAL;
  1215. if (flags & ~BTRFS_SUBVOL_RDONLY)
  1216. return -EOPNOTSUPP;
  1217. if (!inode_owner_or_capable(inode))
  1218. return -EACCES;
  1219. down_write(&root->fs_info->subvol_sem);
  1220. /* nothing to do */
  1221. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1222. goto out;
  1223. root_flags = btrfs_root_flags(&root->root_item);
  1224. if (flags & BTRFS_SUBVOL_RDONLY)
  1225. btrfs_set_root_flags(&root->root_item,
  1226. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1227. else
  1228. btrfs_set_root_flags(&root->root_item,
  1229. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1230. trans = btrfs_start_transaction(root, 1);
  1231. if (IS_ERR(trans)) {
  1232. ret = PTR_ERR(trans);
  1233. goto out_reset;
  1234. }
  1235. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1236. &root->root_key, &root->root_item);
  1237. btrfs_commit_transaction(trans, root);
  1238. out_reset:
  1239. if (ret)
  1240. btrfs_set_root_flags(&root->root_item, root_flags);
  1241. out:
  1242. up_write(&root->fs_info->subvol_sem);
  1243. return ret;
  1244. }
  1245. /*
  1246. * helper to check if the subvolume references other subvolumes
  1247. */
  1248. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1249. {
  1250. struct btrfs_path *path;
  1251. struct btrfs_key key;
  1252. int ret;
  1253. path = btrfs_alloc_path();
  1254. if (!path)
  1255. return -ENOMEM;
  1256. key.objectid = root->root_key.objectid;
  1257. key.type = BTRFS_ROOT_REF_KEY;
  1258. key.offset = (u64)-1;
  1259. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1260. &key, path, 0, 0);
  1261. if (ret < 0)
  1262. goto out;
  1263. BUG_ON(ret == 0);
  1264. ret = 0;
  1265. if (path->slots[0] > 0) {
  1266. path->slots[0]--;
  1267. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1268. if (key.objectid == root->root_key.objectid &&
  1269. key.type == BTRFS_ROOT_REF_KEY)
  1270. ret = -ENOTEMPTY;
  1271. }
  1272. out:
  1273. btrfs_free_path(path);
  1274. return ret;
  1275. }
  1276. static noinline int key_in_sk(struct btrfs_key *key,
  1277. struct btrfs_ioctl_search_key *sk)
  1278. {
  1279. struct btrfs_key test;
  1280. int ret;
  1281. test.objectid = sk->min_objectid;
  1282. test.type = sk->min_type;
  1283. test.offset = sk->min_offset;
  1284. ret = btrfs_comp_cpu_keys(key, &test);
  1285. if (ret < 0)
  1286. return 0;
  1287. test.objectid = sk->max_objectid;
  1288. test.type = sk->max_type;
  1289. test.offset = sk->max_offset;
  1290. ret = btrfs_comp_cpu_keys(key, &test);
  1291. if (ret > 0)
  1292. return 0;
  1293. return 1;
  1294. }
  1295. static noinline int copy_to_sk(struct btrfs_root *root,
  1296. struct btrfs_path *path,
  1297. struct btrfs_key *key,
  1298. struct btrfs_ioctl_search_key *sk,
  1299. char *buf,
  1300. unsigned long *sk_offset,
  1301. int *num_found)
  1302. {
  1303. u64 found_transid;
  1304. struct extent_buffer *leaf;
  1305. struct btrfs_ioctl_search_header sh;
  1306. unsigned long item_off;
  1307. unsigned long item_len;
  1308. int nritems;
  1309. int i;
  1310. int slot;
  1311. int ret = 0;
  1312. leaf = path->nodes[0];
  1313. slot = path->slots[0];
  1314. nritems = btrfs_header_nritems(leaf);
  1315. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1316. i = nritems;
  1317. goto advance_key;
  1318. }
  1319. found_transid = btrfs_header_generation(leaf);
  1320. for (i = slot; i < nritems; i++) {
  1321. item_off = btrfs_item_ptr_offset(leaf, i);
  1322. item_len = btrfs_item_size_nr(leaf, i);
  1323. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1324. item_len = 0;
  1325. if (sizeof(sh) + item_len + *sk_offset >
  1326. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1327. ret = 1;
  1328. goto overflow;
  1329. }
  1330. btrfs_item_key_to_cpu(leaf, key, i);
  1331. if (!key_in_sk(key, sk))
  1332. continue;
  1333. sh.objectid = key->objectid;
  1334. sh.offset = key->offset;
  1335. sh.type = key->type;
  1336. sh.len = item_len;
  1337. sh.transid = found_transid;
  1338. /* copy search result header */
  1339. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1340. *sk_offset += sizeof(sh);
  1341. if (item_len) {
  1342. char *p = buf + *sk_offset;
  1343. /* copy the item */
  1344. read_extent_buffer(leaf, p,
  1345. item_off, item_len);
  1346. *sk_offset += item_len;
  1347. }
  1348. (*num_found)++;
  1349. if (*num_found >= sk->nr_items)
  1350. break;
  1351. }
  1352. advance_key:
  1353. ret = 0;
  1354. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1355. key->offset++;
  1356. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1357. key->offset = 0;
  1358. key->type++;
  1359. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1360. key->offset = 0;
  1361. key->type = 0;
  1362. key->objectid++;
  1363. } else
  1364. ret = 1;
  1365. overflow:
  1366. return ret;
  1367. }
  1368. static noinline int search_ioctl(struct inode *inode,
  1369. struct btrfs_ioctl_search_args *args)
  1370. {
  1371. struct btrfs_root *root;
  1372. struct btrfs_key key;
  1373. struct btrfs_key max_key;
  1374. struct btrfs_path *path;
  1375. struct btrfs_ioctl_search_key *sk = &args->key;
  1376. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1377. int ret;
  1378. int num_found = 0;
  1379. unsigned long sk_offset = 0;
  1380. path = btrfs_alloc_path();
  1381. if (!path)
  1382. return -ENOMEM;
  1383. if (sk->tree_id == 0) {
  1384. /* search the root of the inode that was passed */
  1385. root = BTRFS_I(inode)->root;
  1386. } else {
  1387. key.objectid = sk->tree_id;
  1388. key.type = BTRFS_ROOT_ITEM_KEY;
  1389. key.offset = (u64)-1;
  1390. root = btrfs_read_fs_root_no_name(info, &key);
  1391. if (IS_ERR(root)) {
  1392. printk(KERN_ERR "could not find root %llu\n",
  1393. sk->tree_id);
  1394. btrfs_free_path(path);
  1395. return -ENOENT;
  1396. }
  1397. }
  1398. key.objectid = sk->min_objectid;
  1399. key.type = sk->min_type;
  1400. key.offset = sk->min_offset;
  1401. max_key.objectid = sk->max_objectid;
  1402. max_key.type = sk->max_type;
  1403. max_key.offset = sk->max_offset;
  1404. path->keep_locks = 1;
  1405. while(1) {
  1406. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1407. sk->min_transid);
  1408. if (ret != 0) {
  1409. if (ret > 0)
  1410. ret = 0;
  1411. goto err;
  1412. }
  1413. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1414. &sk_offset, &num_found);
  1415. btrfs_release_path(path);
  1416. if (ret || num_found >= sk->nr_items)
  1417. break;
  1418. }
  1419. ret = 0;
  1420. err:
  1421. sk->nr_items = num_found;
  1422. btrfs_free_path(path);
  1423. return ret;
  1424. }
  1425. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1426. void __user *argp)
  1427. {
  1428. struct btrfs_ioctl_search_args *args;
  1429. struct inode *inode;
  1430. int ret;
  1431. if (!capable(CAP_SYS_ADMIN))
  1432. return -EPERM;
  1433. args = memdup_user(argp, sizeof(*args));
  1434. if (IS_ERR(args))
  1435. return PTR_ERR(args);
  1436. inode = fdentry(file)->d_inode;
  1437. ret = search_ioctl(inode, args);
  1438. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1439. ret = -EFAULT;
  1440. kfree(args);
  1441. return ret;
  1442. }
  1443. /*
  1444. * Search INODE_REFs to identify path name of 'dirid' directory
  1445. * in a 'tree_id' tree. and sets path name to 'name'.
  1446. */
  1447. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1448. u64 tree_id, u64 dirid, char *name)
  1449. {
  1450. struct btrfs_root *root;
  1451. struct btrfs_key key;
  1452. char *ptr;
  1453. int ret = -1;
  1454. int slot;
  1455. int len;
  1456. int total_len = 0;
  1457. struct btrfs_inode_ref *iref;
  1458. struct extent_buffer *l;
  1459. struct btrfs_path *path;
  1460. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1461. name[0]='\0';
  1462. return 0;
  1463. }
  1464. path = btrfs_alloc_path();
  1465. if (!path)
  1466. return -ENOMEM;
  1467. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1468. key.objectid = tree_id;
  1469. key.type = BTRFS_ROOT_ITEM_KEY;
  1470. key.offset = (u64)-1;
  1471. root = btrfs_read_fs_root_no_name(info, &key);
  1472. if (IS_ERR(root)) {
  1473. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1474. ret = -ENOENT;
  1475. goto out;
  1476. }
  1477. key.objectid = dirid;
  1478. key.type = BTRFS_INODE_REF_KEY;
  1479. key.offset = (u64)-1;
  1480. while(1) {
  1481. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1482. if (ret < 0)
  1483. goto out;
  1484. l = path->nodes[0];
  1485. slot = path->slots[0];
  1486. if (ret > 0 && slot > 0)
  1487. slot--;
  1488. btrfs_item_key_to_cpu(l, &key, slot);
  1489. if (ret > 0 && (key.objectid != dirid ||
  1490. key.type != BTRFS_INODE_REF_KEY)) {
  1491. ret = -ENOENT;
  1492. goto out;
  1493. }
  1494. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1495. len = btrfs_inode_ref_name_len(l, iref);
  1496. ptr -= len + 1;
  1497. total_len += len + 1;
  1498. if (ptr < name)
  1499. goto out;
  1500. *(ptr + len) = '/';
  1501. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1502. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1503. break;
  1504. btrfs_release_path(path);
  1505. key.objectid = key.offset;
  1506. key.offset = (u64)-1;
  1507. dirid = key.objectid;
  1508. }
  1509. if (ptr < name)
  1510. goto out;
  1511. memmove(name, ptr, total_len);
  1512. name[total_len]='\0';
  1513. ret = 0;
  1514. out:
  1515. btrfs_free_path(path);
  1516. return ret;
  1517. }
  1518. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1519. void __user *argp)
  1520. {
  1521. struct btrfs_ioctl_ino_lookup_args *args;
  1522. struct inode *inode;
  1523. int ret;
  1524. if (!capable(CAP_SYS_ADMIN))
  1525. return -EPERM;
  1526. args = memdup_user(argp, sizeof(*args));
  1527. if (IS_ERR(args))
  1528. return PTR_ERR(args);
  1529. inode = fdentry(file)->d_inode;
  1530. if (args->treeid == 0)
  1531. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1532. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1533. args->treeid, args->objectid,
  1534. args->name);
  1535. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1536. ret = -EFAULT;
  1537. kfree(args);
  1538. return ret;
  1539. }
  1540. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1541. void __user *arg)
  1542. {
  1543. struct dentry *parent = fdentry(file);
  1544. struct dentry *dentry;
  1545. struct inode *dir = parent->d_inode;
  1546. struct inode *inode;
  1547. struct btrfs_root *root = BTRFS_I(dir)->root;
  1548. struct btrfs_root *dest = NULL;
  1549. struct btrfs_ioctl_vol_args *vol_args;
  1550. struct btrfs_trans_handle *trans;
  1551. int namelen;
  1552. int ret;
  1553. int err = 0;
  1554. vol_args = memdup_user(arg, sizeof(*vol_args));
  1555. if (IS_ERR(vol_args))
  1556. return PTR_ERR(vol_args);
  1557. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1558. namelen = strlen(vol_args->name);
  1559. if (strchr(vol_args->name, '/') ||
  1560. strncmp(vol_args->name, "..", namelen) == 0) {
  1561. err = -EINVAL;
  1562. goto out;
  1563. }
  1564. err = mnt_want_write(file->f_path.mnt);
  1565. if (err)
  1566. goto out;
  1567. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1568. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1569. if (IS_ERR(dentry)) {
  1570. err = PTR_ERR(dentry);
  1571. goto out_unlock_dir;
  1572. }
  1573. if (!dentry->d_inode) {
  1574. err = -ENOENT;
  1575. goto out_dput;
  1576. }
  1577. inode = dentry->d_inode;
  1578. dest = BTRFS_I(inode)->root;
  1579. if (!capable(CAP_SYS_ADMIN)){
  1580. /*
  1581. * Regular user. Only allow this with a special mount
  1582. * option, when the user has write+exec access to the
  1583. * subvol root, and when rmdir(2) would have been
  1584. * allowed.
  1585. *
  1586. * Note that this is _not_ check that the subvol is
  1587. * empty or doesn't contain data that we wouldn't
  1588. * otherwise be able to delete.
  1589. *
  1590. * Users who want to delete empty subvols should try
  1591. * rmdir(2).
  1592. */
  1593. err = -EPERM;
  1594. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1595. goto out_dput;
  1596. /*
  1597. * Do not allow deletion if the parent dir is the same
  1598. * as the dir to be deleted. That means the ioctl
  1599. * must be called on the dentry referencing the root
  1600. * of the subvol, not a random directory contained
  1601. * within it.
  1602. */
  1603. err = -EINVAL;
  1604. if (root == dest)
  1605. goto out_dput;
  1606. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1607. if (err)
  1608. goto out_dput;
  1609. /* check if subvolume may be deleted by a non-root user */
  1610. err = btrfs_may_delete(dir, dentry, 1);
  1611. if (err)
  1612. goto out_dput;
  1613. }
  1614. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1615. err = -EINVAL;
  1616. goto out_dput;
  1617. }
  1618. mutex_lock(&inode->i_mutex);
  1619. err = d_invalidate(dentry);
  1620. if (err)
  1621. goto out_unlock;
  1622. down_write(&root->fs_info->subvol_sem);
  1623. err = may_destroy_subvol(dest);
  1624. if (err)
  1625. goto out_up_write;
  1626. trans = btrfs_start_transaction(root, 0);
  1627. if (IS_ERR(trans)) {
  1628. err = PTR_ERR(trans);
  1629. goto out_up_write;
  1630. }
  1631. trans->block_rsv = &root->fs_info->global_block_rsv;
  1632. ret = btrfs_unlink_subvol(trans, root, dir,
  1633. dest->root_key.objectid,
  1634. dentry->d_name.name,
  1635. dentry->d_name.len);
  1636. BUG_ON(ret);
  1637. btrfs_record_root_in_trans(trans, dest);
  1638. memset(&dest->root_item.drop_progress, 0,
  1639. sizeof(dest->root_item.drop_progress));
  1640. dest->root_item.drop_level = 0;
  1641. btrfs_set_root_refs(&dest->root_item, 0);
  1642. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1643. ret = btrfs_insert_orphan_item(trans,
  1644. root->fs_info->tree_root,
  1645. dest->root_key.objectid);
  1646. BUG_ON(ret);
  1647. }
  1648. ret = btrfs_end_transaction(trans, root);
  1649. BUG_ON(ret);
  1650. inode->i_flags |= S_DEAD;
  1651. out_up_write:
  1652. up_write(&root->fs_info->subvol_sem);
  1653. out_unlock:
  1654. mutex_unlock(&inode->i_mutex);
  1655. if (!err) {
  1656. shrink_dcache_sb(root->fs_info->sb);
  1657. btrfs_invalidate_inodes(dest);
  1658. d_delete(dentry);
  1659. }
  1660. out_dput:
  1661. dput(dentry);
  1662. out_unlock_dir:
  1663. mutex_unlock(&dir->i_mutex);
  1664. mnt_drop_write(file->f_path.mnt);
  1665. out:
  1666. kfree(vol_args);
  1667. return err;
  1668. }
  1669. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1670. {
  1671. struct inode *inode = fdentry(file)->d_inode;
  1672. struct btrfs_root *root = BTRFS_I(inode)->root;
  1673. struct btrfs_ioctl_defrag_range_args *range;
  1674. int ret;
  1675. if (btrfs_root_readonly(root))
  1676. return -EROFS;
  1677. ret = mnt_want_write(file->f_path.mnt);
  1678. if (ret)
  1679. return ret;
  1680. switch (inode->i_mode & S_IFMT) {
  1681. case S_IFDIR:
  1682. if (!capable(CAP_SYS_ADMIN)) {
  1683. ret = -EPERM;
  1684. goto out;
  1685. }
  1686. ret = btrfs_defrag_root(root, 0);
  1687. if (ret)
  1688. goto out;
  1689. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1690. break;
  1691. case S_IFREG:
  1692. if (!(file->f_mode & FMODE_WRITE)) {
  1693. ret = -EINVAL;
  1694. goto out;
  1695. }
  1696. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1697. if (!range) {
  1698. ret = -ENOMEM;
  1699. goto out;
  1700. }
  1701. if (argp) {
  1702. if (copy_from_user(range, argp,
  1703. sizeof(*range))) {
  1704. ret = -EFAULT;
  1705. kfree(range);
  1706. goto out;
  1707. }
  1708. /* compression requires us to start the IO */
  1709. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1710. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1711. range->extent_thresh = (u32)-1;
  1712. }
  1713. } else {
  1714. /* the rest are all set to zero by kzalloc */
  1715. range->len = (u64)-1;
  1716. }
  1717. ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
  1718. range, 0, 0);
  1719. if (ret > 0)
  1720. ret = 0;
  1721. kfree(range);
  1722. break;
  1723. default:
  1724. ret = -EINVAL;
  1725. }
  1726. out:
  1727. mnt_drop_write(file->f_path.mnt);
  1728. return ret;
  1729. }
  1730. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1731. {
  1732. struct btrfs_ioctl_vol_args *vol_args;
  1733. int ret;
  1734. if (!capable(CAP_SYS_ADMIN))
  1735. return -EPERM;
  1736. vol_args = memdup_user(arg, sizeof(*vol_args));
  1737. if (IS_ERR(vol_args))
  1738. return PTR_ERR(vol_args);
  1739. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1740. ret = btrfs_init_new_device(root, vol_args->name);
  1741. kfree(vol_args);
  1742. return ret;
  1743. }
  1744. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1745. {
  1746. struct btrfs_ioctl_vol_args *vol_args;
  1747. int ret;
  1748. if (!capable(CAP_SYS_ADMIN))
  1749. return -EPERM;
  1750. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1751. return -EROFS;
  1752. vol_args = memdup_user(arg, sizeof(*vol_args));
  1753. if (IS_ERR(vol_args))
  1754. return PTR_ERR(vol_args);
  1755. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1756. ret = btrfs_rm_device(root, vol_args->name);
  1757. kfree(vol_args);
  1758. return ret;
  1759. }
  1760. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  1761. {
  1762. struct btrfs_ioctl_fs_info_args *fi_args;
  1763. struct btrfs_device *device;
  1764. struct btrfs_device *next;
  1765. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1766. int ret = 0;
  1767. if (!capable(CAP_SYS_ADMIN))
  1768. return -EPERM;
  1769. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  1770. if (!fi_args)
  1771. return -ENOMEM;
  1772. fi_args->num_devices = fs_devices->num_devices;
  1773. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  1774. mutex_lock(&fs_devices->device_list_mutex);
  1775. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  1776. if (device->devid > fi_args->max_id)
  1777. fi_args->max_id = device->devid;
  1778. }
  1779. mutex_unlock(&fs_devices->device_list_mutex);
  1780. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  1781. ret = -EFAULT;
  1782. kfree(fi_args);
  1783. return ret;
  1784. }
  1785. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  1786. {
  1787. struct btrfs_ioctl_dev_info_args *di_args;
  1788. struct btrfs_device *dev;
  1789. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1790. int ret = 0;
  1791. char *s_uuid = NULL;
  1792. char empty_uuid[BTRFS_UUID_SIZE] = {0};
  1793. if (!capable(CAP_SYS_ADMIN))
  1794. return -EPERM;
  1795. di_args = memdup_user(arg, sizeof(*di_args));
  1796. if (IS_ERR(di_args))
  1797. return PTR_ERR(di_args);
  1798. if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
  1799. s_uuid = di_args->uuid;
  1800. mutex_lock(&fs_devices->device_list_mutex);
  1801. dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
  1802. mutex_unlock(&fs_devices->device_list_mutex);
  1803. if (!dev) {
  1804. ret = -ENODEV;
  1805. goto out;
  1806. }
  1807. di_args->devid = dev->devid;
  1808. di_args->bytes_used = dev->bytes_used;
  1809. di_args->total_bytes = dev->total_bytes;
  1810. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  1811. strncpy(di_args->path, dev->name, sizeof(di_args->path));
  1812. out:
  1813. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  1814. ret = -EFAULT;
  1815. kfree(di_args);
  1816. return ret;
  1817. }
  1818. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1819. u64 off, u64 olen, u64 destoff)
  1820. {
  1821. struct inode *inode = fdentry(file)->d_inode;
  1822. struct btrfs_root *root = BTRFS_I(inode)->root;
  1823. struct file *src_file;
  1824. struct inode *src;
  1825. struct btrfs_trans_handle *trans;
  1826. struct btrfs_path *path;
  1827. struct extent_buffer *leaf;
  1828. char *buf;
  1829. struct btrfs_key key;
  1830. u32 nritems;
  1831. int slot;
  1832. int ret;
  1833. u64 len = olen;
  1834. u64 bs = root->fs_info->sb->s_blocksize;
  1835. u64 hint_byte;
  1836. /*
  1837. * TODO:
  1838. * - split compressed inline extents. annoying: we need to
  1839. * decompress into destination's address_space (the file offset
  1840. * may change, so source mapping won't do), then recompress (or
  1841. * otherwise reinsert) a subrange.
  1842. * - allow ranges within the same file to be cloned (provided
  1843. * they don't overlap)?
  1844. */
  1845. /* the destination must be opened for writing */
  1846. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1847. return -EINVAL;
  1848. if (btrfs_root_readonly(root))
  1849. return -EROFS;
  1850. ret = mnt_want_write(file->f_path.mnt);
  1851. if (ret)
  1852. return ret;
  1853. src_file = fget(srcfd);
  1854. if (!src_file) {
  1855. ret = -EBADF;
  1856. goto out_drop_write;
  1857. }
  1858. src = src_file->f_dentry->d_inode;
  1859. ret = -EINVAL;
  1860. if (src == inode)
  1861. goto out_fput;
  1862. /* the src must be open for reading */
  1863. if (!(src_file->f_mode & FMODE_READ))
  1864. goto out_fput;
  1865. /* don't make the dst file partly checksummed */
  1866. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  1867. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  1868. goto out_fput;
  1869. ret = -EISDIR;
  1870. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1871. goto out_fput;
  1872. ret = -EXDEV;
  1873. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1874. goto out_fput;
  1875. ret = -ENOMEM;
  1876. buf = vmalloc(btrfs_level_size(root, 0));
  1877. if (!buf)
  1878. goto out_fput;
  1879. path = btrfs_alloc_path();
  1880. if (!path) {
  1881. vfree(buf);
  1882. goto out_fput;
  1883. }
  1884. path->reada = 2;
  1885. if (inode < src) {
  1886. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1887. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1888. } else {
  1889. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1890. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1891. }
  1892. /* determine range to clone */
  1893. ret = -EINVAL;
  1894. if (off + len > src->i_size || off + len < off)
  1895. goto out_unlock;
  1896. if (len == 0)
  1897. olen = len = src->i_size - off;
  1898. /* if we extend to eof, continue to block boundary */
  1899. if (off + len == src->i_size)
  1900. len = ALIGN(src->i_size, bs) - off;
  1901. /* verify the end result is block aligned */
  1902. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1903. !IS_ALIGNED(destoff, bs))
  1904. goto out_unlock;
  1905. if (destoff > inode->i_size) {
  1906. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  1907. if (ret)
  1908. goto out_unlock;
  1909. }
  1910. /* truncate page cache pages from target inode range */
  1911. truncate_inode_pages_range(&inode->i_data, destoff,
  1912. PAGE_CACHE_ALIGN(destoff + len) - 1);
  1913. /* do any pending delalloc/csum calc on src, one way or
  1914. another, and lock file content */
  1915. while (1) {
  1916. struct btrfs_ordered_extent *ordered;
  1917. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1918. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1919. if (!ordered &&
  1920. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1921. EXTENT_DELALLOC, 0, NULL))
  1922. break;
  1923. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1924. if (ordered)
  1925. btrfs_put_ordered_extent(ordered);
  1926. btrfs_wait_ordered_range(src, off, len);
  1927. }
  1928. /* clone data */
  1929. key.objectid = btrfs_ino(src);
  1930. key.type = BTRFS_EXTENT_DATA_KEY;
  1931. key.offset = 0;
  1932. while (1) {
  1933. /*
  1934. * note the key will change type as we walk through the
  1935. * tree.
  1936. */
  1937. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1938. if (ret < 0)
  1939. goto out;
  1940. nritems = btrfs_header_nritems(path->nodes[0]);
  1941. if (path->slots[0] >= nritems) {
  1942. ret = btrfs_next_leaf(root, path);
  1943. if (ret < 0)
  1944. goto out;
  1945. if (ret > 0)
  1946. break;
  1947. nritems = btrfs_header_nritems(path->nodes[0]);
  1948. }
  1949. leaf = path->nodes[0];
  1950. slot = path->slots[0];
  1951. btrfs_item_key_to_cpu(leaf, &key, slot);
  1952. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1953. key.objectid != btrfs_ino(src))
  1954. break;
  1955. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1956. struct btrfs_file_extent_item *extent;
  1957. int type;
  1958. u32 size;
  1959. struct btrfs_key new_key;
  1960. u64 disko = 0, diskl = 0;
  1961. u64 datao = 0, datal = 0;
  1962. u8 comp;
  1963. u64 endoff;
  1964. size = btrfs_item_size_nr(leaf, slot);
  1965. read_extent_buffer(leaf, buf,
  1966. btrfs_item_ptr_offset(leaf, slot),
  1967. size);
  1968. extent = btrfs_item_ptr(leaf, slot,
  1969. struct btrfs_file_extent_item);
  1970. comp = btrfs_file_extent_compression(leaf, extent);
  1971. type = btrfs_file_extent_type(leaf, extent);
  1972. if (type == BTRFS_FILE_EXTENT_REG ||
  1973. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1974. disko = btrfs_file_extent_disk_bytenr(leaf,
  1975. extent);
  1976. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1977. extent);
  1978. datao = btrfs_file_extent_offset(leaf, extent);
  1979. datal = btrfs_file_extent_num_bytes(leaf,
  1980. extent);
  1981. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1982. /* take upper bound, may be compressed */
  1983. datal = btrfs_file_extent_ram_bytes(leaf,
  1984. extent);
  1985. }
  1986. btrfs_release_path(path);
  1987. if (key.offset + datal <= off ||
  1988. key.offset >= off+len)
  1989. goto next;
  1990. memcpy(&new_key, &key, sizeof(new_key));
  1991. new_key.objectid = btrfs_ino(inode);
  1992. if (off <= key.offset)
  1993. new_key.offset = key.offset + destoff - off;
  1994. else
  1995. new_key.offset = destoff;
  1996. /*
  1997. * 1 - adjusting old extent (we may have to split it)
  1998. * 1 - add new extent
  1999. * 1 - inode update
  2000. */
  2001. trans = btrfs_start_transaction(root, 3);
  2002. if (IS_ERR(trans)) {
  2003. ret = PTR_ERR(trans);
  2004. goto out;
  2005. }
  2006. if (type == BTRFS_FILE_EXTENT_REG ||
  2007. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2008. /*
  2009. * a | --- range to clone ---| b
  2010. * | ------------- extent ------------- |
  2011. */
  2012. /* substract range b */
  2013. if (key.offset + datal > off + len)
  2014. datal = off + len - key.offset;
  2015. /* substract range a */
  2016. if (off > key.offset) {
  2017. datao += off - key.offset;
  2018. datal -= off - key.offset;
  2019. }
  2020. ret = btrfs_drop_extents(trans, inode,
  2021. new_key.offset,
  2022. new_key.offset + datal,
  2023. &hint_byte, 1);
  2024. BUG_ON(ret);
  2025. ret = btrfs_insert_empty_item(trans, root, path,
  2026. &new_key, size);
  2027. BUG_ON(ret);
  2028. leaf = path->nodes[0];
  2029. slot = path->slots[0];
  2030. write_extent_buffer(leaf, buf,
  2031. btrfs_item_ptr_offset(leaf, slot),
  2032. size);
  2033. extent = btrfs_item_ptr(leaf, slot,
  2034. struct btrfs_file_extent_item);
  2035. /* disko == 0 means it's a hole */
  2036. if (!disko)
  2037. datao = 0;
  2038. btrfs_set_file_extent_offset(leaf, extent,
  2039. datao);
  2040. btrfs_set_file_extent_num_bytes(leaf, extent,
  2041. datal);
  2042. if (disko) {
  2043. inode_add_bytes(inode, datal);
  2044. ret = btrfs_inc_extent_ref(trans, root,
  2045. disko, diskl, 0,
  2046. root->root_key.objectid,
  2047. btrfs_ino(inode),
  2048. new_key.offset - datao);
  2049. BUG_ON(ret);
  2050. }
  2051. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2052. u64 skip = 0;
  2053. u64 trim = 0;
  2054. if (off > key.offset) {
  2055. skip = off - key.offset;
  2056. new_key.offset += skip;
  2057. }
  2058. if (key.offset + datal > off+len)
  2059. trim = key.offset + datal - (off+len);
  2060. if (comp && (skip || trim)) {
  2061. ret = -EINVAL;
  2062. btrfs_end_transaction(trans, root);
  2063. goto out;
  2064. }
  2065. size -= skip + trim;
  2066. datal -= skip + trim;
  2067. ret = btrfs_drop_extents(trans, inode,
  2068. new_key.offset,
  2069. new_key.offset + datal,
  2070. &hint_byte, 1);
  2071. BUG_ON(ret);
  2072. ret = btrfs_insert_empty_item(trans, root, path,
  2073. &new_key, size);
  2074. BUG_ON(ret);
  2075. if (skip) {
  2076. u32 start =
  2077. btrfs_file_extent_calc_inline_size(0);
  2078. memmove(buf+start, buf+start+skip,
  2079. datal);
  2080. }
  2081. leaf = path->nodes[0];
  2082. slot = path->slots[0];
  2083. write_extent_buffer(leaf, buf,
  2084. btrfs_item_ptr_offset(leaf, slot),
  2085. size);
  2086. inode_add_bytes(inode, datal);
  2087. }
  2088. btrfs_mark_buffer_dirty(leaf);
  2089. btrfs_release_path(path);
  2090. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2091. /*
  2092. * we round up to the block size at eof when
  2093. * determining which extents to clone above,
  2094. * but shouldn't round up the file size
  2095. */
  2096. endoff = new_key.offset + datal;
  2097. if (endoff > destoff+olen)
  2098. endoff = destoff+olen;
  2099. if (endoff > inode->i_size)
  2100. btrfs_i_size_write(inode, endoff);
  2101. ret = btrfs_update_inode(trans, root, inode);
  2102. BUG_ON(ret);
  2103. btrfs_end_transaction(trans, root);
  2104. }
  2105. next:
  2106. btrfs_release_path(path);
  2107. key.offset++;
  2108. }
  2109. ret = 0;
  2110. out:
  2111. btrfs_release_path(path);
  2112. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  2113. out_unlock:
  2114. mutex_unlock(&src->i_mutex);
  2115. mutex_unlock(&inode->i_mutex);
  2116. vfree(buf);
  2117. btrfs_free_path(path);
  2118. out_fput:
  2119. fput(src_file);
  2120. out_drop_write:
  2121. mnt_drop_write(file->f_path.mnt);
  2122. return ret;
  2123. }
  2124. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  2125. {
  2126. struct btrfs_ioctl_clone_range_args args;
  2127. if (copy_from_user(&args, argp, sizeof(args)))
  2128. return -EFAULT;
  2129. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  2130. args.src_length, args.dest_offset);
  2131. }
  2132. /*
  2133. * there are many ways the trans_start and trans_end ioctls can lead
  2134. * to deadlocks. They should only be used by applications that
  2135. * basically own the machine, and have a very in depth understanding
  2136. * of all the possible deadlocks and enospc problems.
  2137. */
  2138. static long btrfs_ioctl_trans_start(struct file *file)
  2139. {
  2140. struct inode *inode = fdentry(file)->d_inode;
  2141. struct btrfs_root *root = BTRFS_I(inode)->root;
  2142. struct btrfs_trans_handle *trans;
  2143. int ret;
  2144. ret = -EPERM;
  2145. if (!capable(CAP_SYS_ADMIN))
  2146. goto out;
  2147. ret = -EINPROGRESS;
  2148. if (file->private_data)
  2149. goto out;
  2150. ret = -EROFS;
  2151. if (btrfs_root_readonly(root))
  2152. goto out;
  2153. ret = mnt_want_write(file->f_path.mnt);
  2154. if (ret)
  2155. goto out;
  2156. atomic_inc(&root->fs_info->open_ioctl_trans);
  2157. ret = -ENOMEM;
  2158. trans = btrfs_start_ioctl_transaction(root);
  2159. if (IS_ERR(trans))
  2160. goto out_drop;
  2161. file->private_data = trans;
  2162. return 0;
  2163. out_drop:
  2164. atomic_dec(&root->fs_info->open_ioctl_trans);
  2165. mnt_drop_write(file->f_path.mnt);
  2166. out:
  2167. return ret;
  2168. }
  2169. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2170. {
  2171. struct inode *inode = fdentry(file)->d_inode;
  2172. struct btrfs_root *root = BTRFS_I(inode)->root;
  2173. struct btrfs_root *new_root;
  2174. struct btrfs_dir_item *di;
  2175. struct btrfs_trans_handle *trans;
  2176. struct btrfs_path *path;
  2177. struct btrfs_key location;
  2178. struct btrfs_disk_key disk_key;
  2179. struct btrfs_super_block *disk_super;
  2180. u64 features;
  2181. u64 objectid = 0;
  2182. u64 dir_id;
  2183. if (!capable(CAP_SYS_ADMIN))
  2184. return -EPERM;
  2185. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  2186. return -EFAULT;
  2187. if (!objectid)
  2188. objectid = root->root_key.objectid;
  2189. location.objectid = objectid;
  2190. location.type = BTRFS_ROOT_ITEM_KEY;
  2191. location.offset = (u64)-1;
  2192. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  2193. if (IS_ERR(new_root))
  2194. return PTR_ERR(new_root);
  2195. if (btrfs_root_refs(&new_root->root_item) == 0)
  2196. return -ENOENT;
  2197. path = btrfs_alloc_path();
  2198. if (!path)
  2199. return -ENOMEM;
  2200. path->leave_spinning = 1;
  2201. trans = btrfs_start_transaction(root, 1);
  2202. if (IS_ERR(trans)) {
  2203. btrfs_free_path(path);
  2204. return PTR_ERR(trans);
  2205. }
  2206. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  2207. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  2208. dir_id, "default", 7, 1);
  2209. if (IS_ERR_OR_NULL(di)) {
  2210. btrfs_free_path(path);
  2211. btrfs_end_transaction(trans, root);
  2212. printk(KERN_ERR "Umm, you don't have the default dir item, "
  2213. "this isn't going to work\n");
  2214. return -ENOENT;
  2215. }
  2216. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2217. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2218. btrfs_mark_buffer_dirty(path->nodes[0]);
  2219. btrfs_free_path(path);
  2220. disk_super = &root->fs_info->super_copy;
  2221. features = btrfs_super_incompat_flags(disk_super);
  2222. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  2223. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  2224. btrfs_set_super_incompat_flags(disk_super, features);
  2225. }
  2226. btrfs_end_transaction(trans, root);
  2227. return 0;
  2228. }
  2229. static void get_block_group_info(struct list_head *groups_list,
  2230. struct btrfs_ioctl_space_info *space)
  2231. {
  2232. struct btrfs_block_group_cache *block_group;
  2233. space->total_bytes = 0;
  2234. space->used_bytes = 0;
  2235. space->flags = 0;
  2236. list_for_each_entry(block_group, groups_list, list) {
  2237. space->flags = block_group->flags;
  2238. space->total_bytes += block_group->key.offset;
  2239. space->used_bytes +=
  2240. btrfs_block_group_used(&block_group->item);
  2241. }
  2242. }
  2243. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  2244. {
  2245. struct btrfs_ioctl_space_args space_args;
  2246. struct btrfs_ioctl_space_info space;
  2247. struct btrfs_ioctl_space_info *dest;
  2248. struct btrfs_ioctl_space_info *dest_orig;
  2249. struct btrfs_ioctl_space_info __user *user_dest;
  2250. struct btrfs_space_info *info;
  2251. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  2252. BTRFS_BLOCK_GROUP_SYSTEM,
  2253. BTRFS_BLOCK_GROUP_METADATA,
  2254. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  2255. int num_types = 4;
  2256. int alloc_size;
  2257. int ret = 0;
  2258. u64 slot_count = 0;
  2259. int i, c;
  2260. if (copy_from_user(&space_args,
  2261. (struct btrfs_ioctl_space_args __user *)arg,
  2262. sizeof(space_args)))
  2263. return -EFAULT;
  2264. for (i = 0; i < num_types; i++) {
  2265. struct btrfs_space_info *tmp;
  2266. info = NULL;
  2267. rcu_read_lock();
  2268. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2269. list) {
  2270. if (tmp->flags == types[i]) {
  2271. info = tmp;
  2272. break;
  2273. }
  2274. }
  2275. rcu_read_unlock();
  2276. if (!info)
  2277. continue;
  2278. down_read(&info->groups_sem);
  2279. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2280. if (!list_empty(&info->block_groups[c]))
  2281. slot_count++;
  2282. }
  2283. up_read(&info->groups_sem);
  2284. }
  2285. /* space_slots == 0 means they are asking for a count */
  2286. if (space_args.space_slots == 0) {
  2287. space_args.total_spaces = slot_count;
  2288. goto out;
  2289. }
  2290. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2291. alloc_size = sizeof(*dest) * slot_count;
  2292. /* we generally have at most 6 or so space infos, one for each raid
  2293. * level. So, a whole page should be more than enough for everyone
  2294. */
  2295. if (alloc_size > PAGE_CACHE_SIZE)
  2296. return -ENOMEM;
  2297. space_args.total_spaces = 0;
  2298. dest = kmalloc(alloc_size, GFP_NOFS);
  2299. if (!dest)
  2300. return -ENOMEM;
  2301. dest_orig = dest;
  2302. /* now we have a buffer to copy into */
  2303. for (i = 0; i < num_types; i++) {
  2304. struct btrfs_space_info *tmp;
  2305. if (!slot_count)
  2306. break;
  2307. info = NULL;
  2308. rcu_read_lock();
  2309. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2310. list) {
  2311. if (tmp->flags == types[i]) {
  2312. info = tmp;
  2313. break;
  2314. }
  2315. }
  2316. rcu_read_unlock();
  2317. if (!info)
  2318. continue;
  2319. down_read(&info->groups_sem);
  2320. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2321. if (!list_empty(&info->block_groups[c])) {
  2322. get_block_group_info(&info->block_groups[c],
  2323. &space);
  2324. memcpy(dest, &space, sizeof(space));
  2325. dest++;
  2326. space_args.total_spaces++;
  2327. slot_count--;
  2328. }
  2329. if (!slot_count)
  2330. break;
  2331. }
  2332. up_read(&info->groups_sem);
  2333. }
  2334. user_dest = (struct btrfs_ioctl_space_info *)
  2335. (arg + sizeof(struct btrfs_ioctl_space_args));
  2336. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2337. ret = -EFAULT;
  2338. kfree(dest_orig);
  2339. out:
  2340. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2341. ret = -EFAULT;
  2342. return ret;
  2343. }
  2344. /*
  2345. * there are many ways the trans_start and trans_end ioctls can lead
  2346. * to deadlocks. They should only be used by applications that
  2347. * basically own the machine, and have a very in depth understanding
  2348. * of all the possible deadlocks and enospc problems.
  2349. */
  2350. long btrfs_ioctl_trans_end(struct file *file)
  2351. {
  2352. struct inode *inode = fdentry(file)->d_inode;
  2353. struct btrfs_root *root = BTRFS_I(inode)->root;
  2354. struct btrfs_trans_handle *trans;
  2355. trans = file->private_data;
  2356. if (!trans)
  2357. return -EINVAL;
  2358. file->private_data = NULL;
  2359. btrfs_end_transaction(trans, root);
  2360. atomic_dec(&root->fs_info->open_ioctl_trans);
  2361. mnt_drop_write(file->f_path.mnt);
  2362. return 0;
  2363. }
  2364. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  2365. {
  2366. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2367. struct btrfs_trans_handle *trans;
  2368. u64 transid;
  2369. int ret;
  2370. trans = btrfs_start_transaction(root, 0);
  2371. if (IS_ERR(trans))
  2372. return PTR_ERR(trans);
  2373. transid = trans->transid;
  2374. ret = btrfs_commit_transaction_async(trans, root, 0);
  2375. if (ret) {
  2376. btrfs_end_transaction(trans, root);
  2377. return ret;
  2378. }
  2379. if (argp)
  2380. if (copy_to_user(argp, &transid, sizeof(transid)))
  2381. return -EFAULT;
  2382. return 0;
  2383. }
  2384. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2385. {
  2386. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2387. u64 transid;
  2388. if (argp) {
  2389. if (copy_from_user(&transid, argp, sizeof(transid)))
  2390. return -EFAULT;
  2391. } else {
  2392. transid = 0; /* current trans */
  2393. }
  2394. return btrfs_wait_for_commit(root, transid);
  2395. }
  2396. static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
  2397. {
  2398. int ret;
  2399. struct btrfs_ioctl_scrub_args *sa;
  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_dev(root, sa->devid, sa->start, sa->end,
  2406. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
  2407. if (copy_to_user(arg, sa, sizeof(*sa)))
  2408. ret = -EFAULT;
  2409. kfree(sa);
  2410. return ret;
  2411. }
  2412. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  2413. {
  2414. if (!capable(CAP_SYS_ADMIN))
  2415. return -EPERM;
  2416. return btrfs_scrub_cancel(root);
  2417. }
  2418. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  2419. void __user *arg)
  2420. {
  2421. struct btrfs_ioctl_scrub_args *sa;
  2422. int ret;
  2423. if (!capable(CAP_SYS_ADMIN))
  2424. return -EPERM;
  2425. sa = memdup_user(arg, sizeof(*sa));
  2426. if (IS_ERR(sa))
  2427. return PTR_ERR(sa);
  2428. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  2429. if (copy_to_user(arg, sa, sizeof(*sa)))
  2430. ret = -EFAULT;
  2431. kfree(sa);
  2432. return ret;
  2433. }
  2434. long btrfs_ioctl(struct file *file, unsigned int
  2435. cmd, unsigned long arg)
  2436. {
  2437. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2438. void __user *argp = (void __user *)arg;
  2439. switch (cmd) {
  2440. case FS_IOC_GETFLAGS:
  2441. return btrfs_ioctl_getflags(file, argp);
  2442. case FS_IOC_SETFLAGS:
  2443. return btrfs_ioctl_setflags(file, argp);
  2444. case FS_IOC_GETVERSION:
  2445. return btrfs_ioctl_getversion(file, argp);
  2446. case FITRIM:
  2447. return btrfs_ioctl_fitrim(file, argp);
  2448. case BTRFS_IOC_SNAP_CREATE:
  2449. return btrfs_ioctl_snap_create(file, argp, 0);
  2450. case BTRFS_IOC_SNAP_CREATE_V2:
  2451. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2452. case BTRFS_IOC_SUBVOL_CREATE:
  2453. return btrfs_ioctl_snap_create(file, argp, 1);
  2454. case BTRFS_IOC_SNAP_DESTROY:
  2455. return btrfs_ioctl_snap_destroy(file, argp);
  2456. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2457. return btrfs_ioctl_subvol_getflags(file, argp);
  2458. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2459. return btrfs_ioctl_subvol_setflags(file, argp);
  2460. case BTRFS_IOC_DEFAULT_SUBVOL:
  2461. return btrfs_ioctl_default_subvol(file, argp);
  2462. case BTRFS_IOC_DEFRAG:
  2463. return btrfs_ioctl_defrag(file, NULL);
  2464. case BTRFS_IOC_DEFRAG_RANGE:
  2465. return btrfs_ioctl_defrag(file, argp);
  2466. case BTRFS_IOC_RESIZE:
  2467. return btrfs_ioctl_resize(root, argp);
  2468. case BTRFS_IOC_ADD_DEV:
  2469. return btrfs_ioctl_add_dev(root, argp);
  2470. case BTRFS_IOC_RM_DEV:
  2471. return btrfs_ioctl_rm_dev(root, argp);
  2472. case BTRFS_IOC_FS_INFO:
  2473. return btrfs_ioctl_fs_info(root, argp);
  2474. case BTRFS_IOC_DEV_INFO:
  2475. return btrfs_ioctl_dev_info(root, argp);
  2476. case BTRFS_IOC_BALANCE:
  2477. return btrfs_balance(root->fs_info->dev_root);
  2478. case BTRFS_IOC_CLONE:
  2479. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2480. case BTRFS_IOC_CLONE_RANGE:
  2481. return btrfs_ioctl_clone_range(file, argp);
  2482. case BTRFS_IOC_TRANS_START:
  2483. return btrfs_ioctl_trans_start(file);
  2484. case BTRFS_IOC_TRANS_END:
  2485. return btrfs_ioctl_trans_end(file);
  2486. case BTRFS_IOC_TREE_SEARCH:
  2487. return btrfs_ioctl_tree_search(file, argp);
  2488. case BTRFS_IOC_INO_LOOKUP:
  2489. return btrfs_ioctl_ino_lookup(file, argp);
  2490. case BTRFS_IOC_SPACE_INFO:
  2491. return btrfs_ioctl_space_info(root, argp);
  2492. case BTRFS_IOC_SYNC:
  2493. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2494. return 0;
  2495. case BTRFS_IOC_START_SYNC:
  2496. return btrfs_ioctl_start_sync(file, argp);
  2497. case BTRFS_IOC_WAIT_SYNC:
  2498. return btrfs_ioctl_wait_sync(file, argp);
  2499. case BTRFS_IOC_SCRUB:
  2500. return btrfs_ioctl_scrub(root, argp);
  2501. case BTRFS_IOC_SCRUB_CANCEL:
  2502. return btrfs_ioctl_scrub_cancel(root, argp);
  2503. case BTRFS_IOC_SCRUB_PROGRESS:
  2504. return btrfs_ioctl_scrub_progress(root, argp);
  2505. }
  2506. return -ENOTTY;
  2507. }