ioctl.c 71 KB

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