ioctl.c 74 KB

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