ioctl.c 71 KB

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