ioctl.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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 "compat.h"
  43. #include "ctree.h"
  44. #include "disk-io.h"
  45. #include "transaction.h"
  46. #include "btrfs_inode.h"
  47. #include "ioctl.h"
  48. #include "print-tree.h"
  49. #include "volumes.h"
  50. #include "locking.h"
  51. /* Mask out flags that are inappropriate for the given type of inode. */
  52. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  53. {
  54. if (S_ISDIR(mode))
  55. return flags;
  56. else if (S_ISREG(mode))
  57. return flags & ~FS_DIRSYNC_FL;
  58. else
  59. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  60. }
  61. /*
  62. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  63. */
  64. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  65. {
  66. unsigned int iflags = 0;
  67. if (flags & BTRFS_INODE_SYNC)
  68. iflags |= FS_SYNC_FL;
  69. if (flags & BTRFS_INODE_IMMUTABLE)
  70. iflags |= FS_IMMUTABLE_FL;
  71. if (flags & BTRFS_INODE_APPEND)
  72. iflags |= FS_APPEND_FL;
  73. if (flags & BTRFS_INODE_NODUMP)
  74. iflags |= FS_NODUMP_FL;
  75. if (flags & BTRFS_INODE_NOATIME)
  76. iflags |= FS_NOATIME_FL;
  77. if (flags & BTRFS_INODE_DIRSYNC)
  78. iflags |= FS_DIRSYNC_FL;
  79. return iflags;
  80. }
  81. /*
  82. * Update inode->i_flags based on the btrfs internal flags.
  83. */
  84. void btrfs_update_iflags(struct inode *inode)
  85. {
  86. struct btrfs_inode *ip = BTRFS_I(inode);
  87. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  88. if (ip->flags & BTRFS_INODE_SYNC)
  89. inode->i_flags |= S_SYNC;
  90. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  91. inode->i_flags |= S_IMMUTABLE;
  92. if (ip->flags & BTRFS_INODE_APPEND)
  93. inode->i_flags |= S_APPEND;
  94. if (ip->flags & BTRFS_INODE_NOATIME)
  95. inode->i_flags |= S_NOATIME;
  96. if (ip->flags & BTRFS_INODE_DIRSYNC)
  97. inode->i_flags |= S_DIRSYNC;
  98. }
  99. /*
  100. * Inherit flags from the parent inode.
  101. *
  102. * Unlike extN we don't have any flags we don't want to inherit currently.
  103. */
  104. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  105. {
  106. unsigned int flags;
  107. if (!dir)
  108. return;
  109. flags = BTRFS_I(dir)->flags;
  110. if (S_ISREG(inode->i_mode))
  111. flags &= ~BTRFS_INODE_DIRSYNC;
  112. else if (!S_ISDIR(inode->i_mode))
  113. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  114. BTRFS_I(inode)->flags = flags;
  115. btrfs_update_iflags(inode);
  116. }
  117. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  118. {
  119. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  120. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  121. if (copy_to_user(arg, &flags, sizeof(flags)))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  126. {
  127. struct inode *inode = file->f_path.dentry->d_inode;
  128. struct btrfs_inode *ip = BTRFS_I(inode);
  129. struct btrfs_root *root = ip->root;
  130. struct btrfs_trans_handle *trans;
  131. unsigned int flags, oldflags;
  132. int ret;
  133. if (copy_from_user(&flags, arg, sizeof(flags)))
  134. return -EFAULT;
  135. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  136. FS_NOATIME_FL | FS_NODUMP_FL | \
  137. FS_SYNC_FL | FS_DIRSYNC_FL))
  138. return -EOPNOTSUPP;
  139. if (!is_owner_or_cap(inode))
  140. return -EACCES;
  141. mutex_lock(&inode->i_mutex);
  142. flags = btrfs_mask_flags(inode->i_mode, flags);
  143. oldflags = btrfs_flags_to_ioctl(ip->flags);
  144. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  145. if (!capable(CAP_LINUX_IMMUTABLE)) {
  146. ret = -EPERM;
  147. goto out_unlock;
  148. }
  149. }
  150. ret = mnt_want_write(file->f_path.mnt);
  151. if (ret)
  152. goto out_unlock;
  153. if (flags & FS_SYNC_FL)
  154. ip->flags |= BTRFS_INODE_SYNC;
  155. else
  156. ip->flags &= ~BTRFS_INODE_SYNC;
  157. if (flags & FS_IMMUTABLE_FL)
  158. ip->flags |= BTRFS_INODE_IMMUTABLE;
  159. else
  160. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  161. if (flags & FS_APPEND_FL)
  162. ip->flags |= BTRFS_INODE_APPEND;
  163. else
  164. ip->flags &= ~BTRFS_INODE_APPEND;
  165. if (flags & FS_NODUMP_FL)
  166. ip->flags |= BTRFS_INODE_NODUMP;
  167. else
  168. ip->flags &= ~BTRFS_INODE_NODUMP;
  169. if (flags & FS_NOATIME_FL)
  170. ip->flags |= BTRFS_INODE_NOATIME;
  171. else
  172. ip->flags &= ~BTRFS_INODE_NOATIME;
  173. if (flags & FS_DIRSYNC_FL)
  174. ip->flags |= BTRFS_INODE_DIRSYNC;
  175. else
  176. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  177. trans = btrfs_join_transaction(root, 1);
  178. BUG_ON(!trans);
  179. ret = btrfs_update_inode(trans, root, inode);
  180. BUG_ON(ret);
  181. btrfs_update_iflags(inode);
  182. inode->i_ctime = CURRENT_TIME;
  183. btrfs_end_transaction(trans, root);
  184. mnt_drop_write(file->f_path.mnt);
  185. out_unlock:
  186. mutex_unlock(&inode->i_mutex);
  187. return 0;
  188. }
  189. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  190. {
  191. struct inode *inode = file->f_path.dentry->d_inode;
  192. return put_user(inode->i_generation, arg);
  193. }
  194. static noinline int create_subvol(struct btrfs_root *root,
  195. struct dentry *dentry,
  196. char *name, int namelen)
  197. {
  198. struct btrfs_trans_handle *trans;
  199. struct btrfs_key key;
  200. struct btrfs_root_item root_item;
  201. struct btrfs_inode_item *inode_item;
  202. struct extent_buffer *leaf;
  203. struct btrfs_root *new_root;
  204. struct inode *dir = dentry->d_parent->d_inode;
  205. int ret;
  206. int err;
  207. u64 objectid;
  208. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  209. u64 index = 0;
  210. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  211. 0, &objectid);
  212. if (ret)
  213. return ret;
  214. /*
  215. * 1 - inode item
  216. * 2 - refs
  217. * 1 - root item
  218. * 2 - dir items
  219. */
  220. trans = btrfs_start_transaction(root, 6);
  221. if (IS_ERR(trans))
  222. return PTR_ERR(trans);
  223. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  224. 0, objectid, NULL, 0, 0, 0);
  225. if (IS_ERR(leaf)) {
  226. ret = PTR_ERR(leaf);
  227. goto fail;
  228. }
  229. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  230. btrfs_set_header_bytenr(leaf, leaf->start);
  231. btrfs_set_header_generation(leaf, trans->transid);
  232. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  233. btrfs_set_header_owner(leaf, objectid);
  234. write_extent_buffer(leaf, root->fs_info->fsid,
  235. (unsigned long)btrfs_header_fsid(leaf),
  236. BTRFS_FSID_SIZE);
  237. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  238. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  239. BTRFS_UUID_SIZE);
  240. btrfs_mark_buffer_dirty(leaf);
  241. inode_item = &root_item.inode;
  242. memset(inode_item, 0, sizeof(*inode_item));
  243. inode_item->generation = cpu_to_le64(1);
  244. inode_item->size = cpu_to_le64(3);
  245. inode_item->nlink = cpu_to_le32(1);
  246. inode_item->nbytes = cpu_to_le64(root->leafsize);
  247. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  248. btrfs_set_root_bytenr(&root_item, leaf->start);
  249. btrfs_set_root_generation(&root_item, trans->transid);
  250. btrfs_set_root_level(&root_item, 0);
  251. btrfs_set_root_refs(&root_item, 1);
  252. btrfs_set_root_used(&root_item, leaf->len);
  253. btrfs_set_root_last_snapshot(&root_item, 0);
  254. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  255. root_item.drop_level = 0;
  256. btrfs_tree_unlock(leaf);
  257. free_extent_buffer(leaf);
  258. leaf = NULL;
  259. btrfs_set_root_dirid(&root_item, new_dirid);
  260. key.objectid = objectid;
  261. key.offset = 0;
  262. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  263. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  264. &root_item);
  265. if (ret)
  266. goto fail;
  267. key.offset = (u64)-1;
  268. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  269. BUG_ON(IS_ERR(new_root));
  270. btrfs_record_root_in_trans(trans, new_root);
  271. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  272. BTRFS_I(dir)->block_group);
  273. /*
  274. * insert the directory item
  275. */
  276. ret = btrfs_set_inode_index(dir, &index);
  277. BUG_ON(ret);
  278. ret = btrfs_insert_dir_item(trans, root,
  279. name, namelen, dir->i_ino, &key,
  280. BTRFS_FT_DIR, index);
  281. if (ret)
  282. goto fail;
  283. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  284. ret = btrfs_update_inode(trans, root, dir);
  285. BUG_ON(ret);
  286. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  287. objectid, root->root_key.objectid,
  288. dir->i_ino, index, name, namelen);
  289. BUG_ON(ret);
  290. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  291. fail:
  292. err = btrfs_commit_transaction(trans, root);
  293. if (err && !ret)
  294. ret = err;
  295. return ret;
  296. }
  297. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry)
  298. {
  299. struct inode *inode;
  300. struct btrfs_pending_snapshot *pending_snapshot;
  301. struct btrfs_trans_handle *trans;
  302. int ret;
  303. if (!root->ref_cows)
  304. return -EINVAL;
  305. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  306. if (!pending_snapshot)
  307. return -ENOMEM;
  308. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  309. pending_snapshot->dentry = dentry;
  310. pending_snapshot->root = root;
  311. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  312. if (IS_ERR(trans)) {
  313. ret = PTR_ERR(trans);
  314. goto fail;
  315. }
  316. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  317. BUG_ON(ret);
  318. list_add(&pending_snapshot->list,
  319. &trans->transaction->pending_snapshots);
  320. ret = btrfs_commit_transaction(trans, root->fs_info->extent_root);
  321. BUG_ON(ret);
  322. ret = pending_snapshot->error;
  323. if (ret)
  324. goto fail;
  325. btrfs_orphan_cleanup(pending_snapshot->snap);
  326. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  327. if (IS_ERR(inode)) {
  328. ret = PTR_ERR(inode);
  329. goto fail;
  330. }
  331. BUG_ON(!inode);
  332. d_instantiate(dentry, inode);
  333. ret = 0;
  334. fail:
  335. kfree(pending_snapshot);
  336. return ret;
  337. }
  338. /* copy of may_create in fs/namei.c() */
  339. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  340. {
  341. if (child->d_inode)
  342. return -EEXIST;
  343. if (IS_DEADDIR(dir))
  344. return -ENOENT;
  345. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  346. }
  347. /*
  348. * Create a new subvolume below @parent. This is largely modeled after
  349. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  350. * inside this filesystem so it's quite a bit simpler.
  351. */
  352. static noinline int btrfs_mksubvol(struct path *parent,
  353. char *name, int namelen,
  354. struct btrfs_root *snap_src)
  355. {
  356. struct inode *dir = parent->dentry->d_inode;
  357. struct dentry *dentry;
  358. int error;
  359. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  360. dentry = lookup_one_len(name, parent->dentry, namelen);
  361. error = PTR_ERR(dentry);
  362. if (IS_ERR(dentry))
  363. goto out_unlock;
  364. error = -EEXIST;
  365. if (dentry->d_inode)
  366. goto out_dput;
  367. error = mnt_want_write(parent->mnt);
  368. if (error)
  369. goto out_dput;
  370. error = btrfs_may_create(dir, dentry);
  371. if (error)
  372. goto out_drop_write;
  373. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  374. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  375. goto out_up_read;
  376. if (snap_src) {
  377. error = create_snapshot(snap_src, dentry);
  378. } else {
  379. error = create_subvol(BTRFS_I(dir)->root, dentry,
  380. name, namelen);
  381. }
  382. if (!error)
  383. fsnotify_mkdir(dir, dentry);
  384. out_up_read:
  385. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  386. out_drop_write:
  387. mnt_drop_write(parent->mnt);
  388. out_dput:
  389. dput(dentry);
  390. out_unlock:
  391. mutex_unlock(&dir->i_mutex);
  392. return error;
  393. }
  394. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  395. int thresh, u64 *last_len, u64 *skip,
  396. u64 *defrag_end)
  397. {
  398. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  399. struct extent_map *em = NULL;
  400. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  401. int ret = 1;
  402. if (thresh == 0)
  403. thresh = 256 * 1024;
  404. /*
  405. * make sure that once we start defragging and extent, we keep on
  406. * defragging it
  407. */
  408. if (start < *defrag_end)
  409. return 1;
  410. *skip = 0;
  411. /*
  412. * hopefully we have this extent in the tree already, try without
  413. * the full extent lock
  414. */
  415. read_lock(&em_tree->lock);
  416. em = lookup_extent_mapping(em_tree, start, len);
  417. read_unlock(&em_tree->lock);
  418. if (!em) {
  419. /* get the big lock and read metadata off disk */
  420. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  421. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  422. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  423. if (IS_ERR(em))
  424. return 0;
  425. }
  426. /* this will cover holes, and inline extents */
  427. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  428. ret = 0;
  429. /*
  430. * we hit a real extent, if it is big don't bother defragging it again
  431. */
  432. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  433. ret = 0;
  434. /*
  435. * last_len ends up being a counter of how many bytes we've defragged.
  436. * every time we choose not to defrag an extent, we reset *last_len
  437. * so that the next tiny extent will force a defrag.
  438. *
  439. * The end result of this is that tiny extents before a single big
  440. * extent will force at least part of that big extent to be defragged.
  441. */
  442. if (ret) {
  443. *last_len += len;
  444. *defrag_end = extent_map_end(em);
  445. } else {
  446. *last_len = 0;
  447. *skip = extent_map_end(em);
  448. *defrag_end = 0;
  449. }
  450. free_extent_map(em);
  451. return ret;
  452. }
  453. static int btrfs_defrag_file(struct file *file,
  454. struct btrfs_ioctl_defrag_range_args *range)
  455. {
  456. struct inode *inode = fdentry(file)->d_inode;
  457. struct btrfs_root *root = BTRFS_I(inode)->root;
  458. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  459. struct btrfs_ordered_extent *ordered;
  460. struct page *page;
  461. unsigned long last_index;
  462. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  463. unsigned long total_read = 0;
  464. u64 page_start;
  465. u64 page_end;
  466. u64 last_len = 0;
  467. u64 skip = 0;
  468. u64 defrag_end = 0;
  469. unsigned long i;
  470. int ret;
  471. if (inode->i_size == 0)
  472. return 0;
  473. if (range->start + range->len > range->start) {
  474. last_index = min_t(u64, inode->i_size - 1,
  475. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  476. } else {
  477. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  478. }
  479. i = range->start >> PAGE_CACHE_SHIFT;
  480. while (i <= last_index) {
  481. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  482. PAGE_CACHE_SIZE,
  483. range->extent_thresh,
  484. &last_len, &skip,
  485. &defrag_end)) {
  486. unsigned long next;
  487. /*
  488. * the should_defrag function tells us how much to skip
  489. * bump our counter by the suggested amount
  490. */
  491. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  492. i = max(i + 1, next);
  493. continue;
  494. }
  495. if (total_read % ra_pages == 0) {
  496. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  497. min(last_index, i + ra_pages - 1));
  498. }
  499. total_read++;
  500. mutex_lock(&inode->i_mutex);
  501. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  502. BTRFS_I(inode)->force_compress = 1;
  503. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  504. if (ret)
  505. goto err_unlock;
  506. again:
  507. if (inode->i_size == 0 ||
  508. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  509. ret = 0;
  510. goto err_reservations;
  511. }
  512. page = grab_cache_page(inode->i_mapping, i);
  513. if (!page) {
  514. ret = -ENOMEM;
  515. goto err_reservations;
  516. }
  517. if (!PageUptodate(page)) {
  518. btrfs_readpage(NULL, page);
  519. lock_page(page);
  520. if (!PageUptodate(page)) {
  521. unlock_page(page);
  522. page_cache_release(page);
  523. ret = -EIO;
  524. goto err_reservations;
  525. }
  526. }
  527. if (page->mapping != inode->i_mapping) {
  528. unlock_page(page);
  529. page_cache_release(page);
  530. goto again;
  531. }
  532. wait_on_page_writeback(page);
  533. if (PageDirty(page)) {
  534. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  535. goto loop_unlock;
  536. }
  537. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  538. page_end = page_start + PAGE_CACHE_SIZE - 1;
  539. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  540. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  541. if (ordered) {
  542. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  543. unlock_page(page);
  544. page_cache_release(page);
  545. btrfs_start_ordered_extent(inode, ordered, 1);
  546. btrfs_put_ordered_extent(ordered);
  547. goto again;
  548. }
  549. set_page_extent_mapped(page);
  550. /*
  551. * this makes sure page_mkwrite is called on the
  552. * page if it is dirtied again later
  553. */
  554. clear_page_dirty_for_io(page);
  555. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  556. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  557. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  558. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  559. ClearPageChecked(page);
  560. set_page_dirty(page);
  561. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  562. loop_unlock:
  563. unlock_page(page);
  564. page_cache_release(page);
  565. mutex_unlock(&inode->i_mutex);
  566. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  567. i++;
  568. }
  569. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  570. filemap_flush(inode->i_mapping);
  571. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  572. /* the filemap_flush will queue IO into the worker threads, but
  573. * we have to make sure the IO is actually started and that
  574. * ordered extents get created before we return
  575. */
  576. atomic_inc(&root->fs_info->async_submit_draining);
  577. while (atomic_read(&root->fs_info->nr_async_submits) ||
  578. atomic_read(&root->fs_info->async_delalloc_pages)) {
  579. wait_event(root->fs_info->async_submit_wait,
  580. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  581. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  582. }
  583. atomic_dec(&root->fs_info->async_submit_draining);
  584. mutex_lock(&inode->i_mutex);
  585. BTRFS_I(inode)->force_compress = 0;
  586. mutex_unlock(&inode->i_mutex);
  587. }
  588. return 0;
  589. err_reservations:
  590. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  591. err_unlock:
  592. mutex_unlock(&inode->i_mutex);
  593. return ret;
  594. }
  595. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  596. void __user *arg)
  597. {
  598. u64 new_size;
  599. u64 old_size;
  600. u64 devid = 1;
  601. struct btrfs_ioctl_vol_args *vol_args;
  602. struct btrfs_trans_handle *trans;
  603. struct btrfs_device *device = NULL;
  604. char *sizestr;
  605. char *devstr = NULL;
  606. int ret = 0;
  607. int mod = 0;
  608. if (root->fs_info->sb->s_flags & MS_RDONLY)
  609. return -EROFS;
  610. if (!capable(CAP_SYS_ADMIN))
  611. return -EPERM;
  612. vol_args = memdup_user(arg, sizeof(*vol_args));
  613. if (IS_ERR(vol_args))
  614. return PTR_ERR(vol_args);
  615. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  616. mutex_lock(&root->fs_info->volume_mutex);
  617. sizestr = vol_args->name;
  618. devstr = strchr(sizestr, ':');
  619. if (devstr) {
  620. char *end;
  621. sizestr = devstr + 1;
  622. *devstr = '\0';
  623. devstr = vol_args->name;
  624. devid = simple_strtoull(devstr, &end, 10);
  625. printk(KERN_INFO "resizing devid %llu\n",
  626. (unsigned long long)devid);
  627. }
  628. device = btrfs_find_device(root, devid, NULL, NULL);
  629. if (!device) {
  630. printk(KERN_INFO "resizer unable to find device %llu\n",
  631. (unsigned long long)devid);
  632. ret = -EINVAL;
  633. goto out_unlock;
  634. }
  635. if (!strcmp(sizestr, "max"))
  636. new_size = device->bdev->bd_inode->i_size;
  637. else {
  638. if (sizestr[0] == '-') {
  639. mod = -1;
  640. sizestr++;
  641. } else if (sizestr[0] == '+') {
  642. mod = 1;
  643. sizestr++;
  644. }
  645. new_size = memparse(sizestr, NULL);
  646. if (new_size == 0) {
  647. ret = -EINVAL;
  648. goto out_unlock;
  649. }
  650. }
  651. old_size = device->total_bytes;
  652. if (mod < 0) {
  653. if (new_size > old_size) {
  654. ret = -EINVAL;
  655. goto out_unlock;
  656. }
  657. new_size = old_size - new_size;
  658. } else if (mod > 0) {
  659. new_size = old_size + new_size;
  660. }
  661. if (new_size < 256 * 1024 * 1024) {
  662. ret = -EINVAL;
  663. goto out_unlock;
  664. }
  665. if (new_size > device->bdev->bd_inode->i_size) {
  666. ret = -EFBIG;
  667. goto out_unlock;
  668. }
  669. do_div(new_size, root->sectorsize);
  670. new_size *= root->sectorsize;
  671. printk(KERN_INFO "new size for %s is %llu\n",
  672. device->name, (unsigned long long)new_size);
  673. if (new_size > old_size) {
  674. trans = btrfs_start_transaction(root, 0);
  675. ret = btrfs_grow_device(trans, device, new_size);
  676. btrfs_commit_transaction(trans, root);
  677. } else {
  678. ret = btrfs_shrink_device(device, new_size);
  679. }
  680. out_unlock:
  681. mutex_unlock(&root->fs_info->volume_mutex);
  682. kfree(vol_args);
  683. return ret;
  684. }
  685. static noinline int btrfs_ioctl_snap_create(struct file *file,
  686. void __user *arg, int subvol)
  687. {
  688. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  689. struct btrfs_ioctl_vol_args *vol_args;
  690. struct file *src_file;
  691. int namelen;
  692. int ret = 0;
  693. if (root->fs_info->sb->s_flags & MS_RDONLY)
  694. return -EROFS;
  695. vol_args = memdup_user(arg, sizeof(*vol_args));
  696. if (IS_ERR(vol_args))
  697. return PTR_ERR(vol_args);
  698. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  699. namelen = strlen(vol_args->name);
  700. if (strchr(vol_args->name, '/')) {
  701. ret = -EINVAL;
  702. goto out;
  703. }
  704. if (subvol) {
  705. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  706. NULL);
  707. } else {
  708. struct inode *src_inode;
  709. src_file = fget(vol_args->fd);
  710. if (!src_file) {
  711. ret = -EINVAL;
  712. goto out;
  713. }
  714. src_inode = src_file->f_path.dentry->d_inode;
  715. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  716. printk(KERN_INFO "btrfs: Snapshot src from "
  717. "another FS\n");
  718. ret = -EINVAL;
  719. fput(src_file);
  720. goto out;
  721. }
  722. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  723. BTRFS_I(src_inode)->root);
  724. fput(src_file);
  725. }
  726. out:
  727. kfree(vol_args);
  728. return ret;
  729. }
  730. /*
  731. * helper to check if the subvolume references other subvolumes
  732. */
  733. static noinline int may_destroy_subvol(struct btrfs_root *root)
  734. {
  735. struct btrfs_path *path;
  736. struct btrfs_key key;
  737. int ret;
  738. path = btrfs_alloc_path();
  739. if (!path)
  740. return -ENOMEM;
  741. key.objectid = root->root_key.objectid;
  742. key.type = BTRFS_ROOT_REF_KEY;
  743. key.offset = (u64)-1;
  744. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  745. &key, path, 0, 0);
  746. if (ret < 0)
  747. goto out;
  748. BUG_ON(ret == 0);
  749. ret = 0;
  750. if (path->slots[0] > 0) {
  751. path->slots[0]--;
  752. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  753. if (key.objectid == root->root_key.objectid &&
  754. key.type == BTRFS_ROOT_REF_KEY)
  755. ret = -ENOTEMPTY;
  756. }
  757. out:
  758. btrfs_free_path(path);
  759. return ret;
  760. }
  761. static noinline int key_in_sk(struct btrfs_key *key,
  762. struct btrfs_ioctl_search_key *sk)
  763. {
  764. struct btrfs_key test;
  765. int ret;
  766. test.objectid = sk->min_objectid;
  767. test.type = sk->min_type;
  768. test.offset = sk->min_offset;
  769. ret = btrfs_comp_cpu_keys(key, &test);
  770. if (ret < 0)
  771. return 0;
  772. test.objectid = sk->max_objectid;
  773. test.type = sk->max_type;
  774. test.offset = sk->max_offset;
  775. ret = btrfs_comp_cpu_keys(key, &test);
  776. if (ret > 0)
  777. return 0;
  778. return 1;
  779. }
  780. static noinline int copy_to_sk(struct btrfs_root *root,
  781. struct btrfs_path *path,
  782. struct btrfs_key *key,
  783. struct btrfs_ioctl_search_key *sk,
  784. char *buf,
  785. unsigned long *sk_offset,
  786. int *num_found)
  787. {
  788. u64 found_transid;
  789. struct extent_buffer *leaf;
  790. struct btrfs_ioctl_search_header sh;
  791. unsigned long item_off;
  792. unsigned long item_len;
  793. int nritems;
  794. int i;
  795. int slot;
  796. int found = 0;
  797. int ret = 0;
  798. leaf = path->nodes[0];
  799. slot = path->slots[0];
  800. nritems = btrfs_header_nritems(leaf);
  801. if (btrfs_header_generation(leaf) > sk->max_transid) {
  802. i = nritems;
  803. goto advance_key;
  804. }
  805. found_transid = btrfs_header_generation(leaf);
  806. for (i = slot; i < nritems; i++) {
  807. item_off = btrfs_item_ptr_offset(leaf, i);
  808. item_len = btrfs_item_size_nr(leaf, i);
  809. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  810. item_len = 0;
  811. if (sizeof(sh) + item_len + *sk_offset >
  812. BTRFS_SEARCH_ARGS_BUFSIZE) {
  813. ret = 1;
  814. goto overflow;
  815. }
  816. btrfs_item_key_to_cpu(leaf, key, i);
  817. if (!key_in_sk(key, sk))
  818. continue;
  819. sh.objectid = key->objectid;
  820. sh.offset = key->offset;
  821. sh.type = key->type;
  822. sh.len = item_len;
  823. sh.transid = found_transid;
  824. /* copy search result header */
  825. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  826. *sk_offset += sizeof(sh);
  827. if (item_len) {
  828. char *p = buf + *sk_offset;
  829. /* copy the item */
  830. read_extent_buffer(leaf, p,
  831. item_off, item_len);
  832. *sk_offset += item_len;
  833. }
  834. found++;
  835. if (*num_found >= sk->nr_items)
  836. break;
  837. }
  838. advance_key:
  839. ret = 0;
  840. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  841. key->offset++;
  842. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  843. key->offset = 0;
  844. key->type++;
  845. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  846. key->offset = 0;
  847. key->type = 0;
  848. key->objectid++;
  849. } else
  850. ret = 1;
  851. overflow:
  852. *num_found += found;
  853. return ret;
  854. }
  855. static noinline int search_ioctl(struct inode *inode,
  856. struct btrfs_ioctl_search_args *args)
  857. {
  858. struct btrfs_root *root;
  859. struct btrfs_key key;
  860. struct btrfs_key max_key;
  861. struct btrfs_path *path;
  862. struct btrfs_ioctl_search_key *sk = &args->key;
  863. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  864. int ret;
  865. int num_found = 0;
  866. unsigned long sk_offset = 0;
  867. path = btrfs_alloc_path();
  868. if (!path)
  869. return -ENOMEM;
  870. if (sk->tree_id == 0) {
  871. /* search the root of the inode that was passed */
  872. root = BTRFS_I(inode)->root;
  873. } else {
  874. key.objectid = sk->tree_id;
  875. key.type = BTRFS_ROOT_ITEM_KEY;
  876. key.offset = (u64)-1;
  877. root = btrfs_read_fs_root_no_name(info, &key);
  878. if (IS_ERR(root)) {
  879. printk(KERN_ERR "could not find root %llu\n",
  880. sk->tree_id);
  881. btrfs_free_path(path);
  882. return -ENOENT;
  883. }
  884. }
  885. key.objectid = sk->min_objectid;
  886. key.type = sk->min_type;
  887. key.offset = sk->min_offset;
  888. max_key.objectid = sk->max_objectid;
  889. max_key.type = sk->max_type;
  890. max_key.offset = sk->max_offset;
  891. path->keep_locks = 1;
  892. while(1) {
  893. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  894. sk->min_transid);
  895. if (ret != 0) {
  896. if (ret > 0)
  897. ret = 0;
  898. goto err;
  899. }
  900. ret = copy_to_sk(root, path, &key, sk, args->buf,
  901. &sk_offset, &num_found);
  902. btrfs_release_path(root, path);
  903. if (ret || num_found >= sk->nr_items)
  904. break;
  905. }
  906. ret = 0;
  907. err:
  908. sk->nr_items = num_found;
  909. btrfs_free_path(path);
  910. return ret;
  911. }
  912. static noinline int btrfs_ioctl_tree_search(struct file *file,
  913. void __user *argp)
  914. {
  915. struct btrfs_ioctl_search_args *args;
  916. struct inode *inode;
  917. int ret;
  918. if (!capable(CAP_SYS_ADMIN))
  919. return -EPERM;
  920. args = memdup_user(argp, sizeof(*args));
  921. if (IS_ERR(args))
  922. return PTR_ERR(args);
  923. inode = fdentry(file)->d_inode;
  924. ret = search_ioctl(inode, args);
  925. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  926. ret = -EFAULT;
  927. kfree(args);
  928. return ret;
  929. }
  930. /*
  931. * Search INODE_REFs to identify path name of 'dirid' directory
  932. * in a 'tree_id' tree. and sets path name to 'name'.
  933. */
  934. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  935. u64 tree_id, u64 dirid, char *name)
  936. {
  937. struct btrfs_root *root;
  938. struct btrfs_key key;
  939. char *ptr;
  940. int ret = -1;
  941. int slot;
  942. int len;
  943. int total_len = 0;
  944. struct btrfs_inode_ref *iref;
  945. struct extent_buffer *l;
  946. struct btrfs_path *path;
  947. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  948. name[0]='\0';
  949. return 0;
  950. }
  951. path = btrfs_alloc_path();
  952. if (!path)
  953. return -ENOMEM;
  954. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  955. key.objectid = tree_id;
  956. key.type = BTRFS_ROOT_ITEM_KEY;
  957. key.offset = (u64)-1;
  958. root = btrfs_read_fs_root_no_name(info, &key);
  959. if (IS_ERR(root)) {
  960. printk(KERN_ERR "could not find root %llu\n", tree_id);
  961. ret = -ENOENT;
  962. goto out;
  963. }
  964. key.objectid = dirid;
  965. key.type = BTRFS_INODE_REF_KEY;
  966. key.offset = (u64)-1;
  967. while(1) {
  968. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  969. if (ret < 0)
  970. goto out;
  971. l = path->nodes[0];
  972. slot = path->slots[0];
  973. if (ret > 0 && slot > 0)
  974. slot--;
  975. btrfs_item_key_to_cpu(l, &key, slot);
  976. if (ret > 0 && (key.objectid != dirid ||
  977. key.type != BTRFS_INODE_REF_KEY)) {
  978. ret = -ENOENT;
  979. goto out;
  980. }
  981. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  982. len = btrfs_inode_ref_name_len(l, iref);
  983. ptr -= len + 1;
  984. total_len += len + 1;
  985. if (ptr < name)
  986. goto out;
  987. *(ptr + len) = '/';
  988. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  989. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  990. break;
  991. btrfs_release_path(root, path);
  992. key.objectid = key.offset;
  993. key.offset = (u64)-1;
  994. dirid = key.objectid;
  995. }
  996. if (ptr < name)
  997. goto out;
  998. memcpy(name, ptr, total_len);
  999. name[total_len]='\0';
  1000. ret = 0;
  1001. out:
  1002. btrfs_free_path(path);
  1003. return ret;
  1004. }
  1005. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1006. void __user *argp)
  1007. {
  1008. struct btrfs_ioctl_ino_lookup_args *args;
  1009. struct inode *inode;
  1010. int ret;
  1011. if (!capable(CAP_SYS_ADMIN))
  1012. return -EPERM;
  1013. args = memdup_user(argp, sizeof(*args));
  1014. if (IS_ERR(args))
  1015. return PTR_ERR(args);
  1016. inode = fdentry(file)->d_inode;
  1017. if (args->treeid == 0)
  1018. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1019. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1020. args->treeid, args->objectid,
  1021. args->name);
  1022. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1023. ret = -EFAULT;
  1024. kfree(args);
  1025. return ret;
  1026. }
  1027. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1028. void __user *arg)
  1029. {
  1030. struct dentry *parent = fdentry(file);
  1031. struct dentry *dentry;
  1032. struct inode *dir = parent->d_inode;
  1033. struct inode *inode;
  1034. struct btrfs_root *root = BTRFS_I(dir)->root;
  1035. struct btrfs_root *dest = NULL;
  1036. struct btrfs_ioctl_vol_args *vol_args;
  1037. struct btrfs_trans_handle *trans;
  1038. int namelen;
  1039. int ret;
  1040. int err = 0;
  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. namelen = strlen(vol_args->name);
  1048. if (strchr(vol_args->name, '/') ||
  1049. strncmp(vol_args->name, "..", namelen) == 0) {
  1050. err = -EINVAL;
  1051. goto out;
  1052. }
  1053. err = mnt_want_write(file->f_path.mnt);
  1054. if (err)
  1055. goto out;
  1056. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1057. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1058. if (IS_ERR(dentry)) {
  1059. err = PTR_ERR(dentry);
  1060. goto out_unlock_dir;
  1061. }
  1062. if (!dentry->d_inode) {
  1063. err = -ENOENT;
  1064. goto out_dput;
  1065. }
  1066. inode = dentry->d_inode;
  1067. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1068. err = -EINVAL;
  1069. goto out_dput;
  1070. }
  1071. dest = BTRFS_I(inode)->root;
  1072. mutex_lock(&inode->i_mutex);
  1073. err = d_invalidate(dentry);
  1074. if (err)
  1075. goto out_unlock;
  1076. down_write(&root->fs_info->subvol_sem);
  1077. err = may_destroy_subvol(dest);
  1078. if (err)
  1079. goto out_up_write;
  1080. trans = btrfs_start_transaction(root, 0);
  1081. if (IS_ERR(trans)) {
  1082. err = PTR_ERR(trans);
  1083. goto out_up_write;
  1084. }
  1085. trans->block_rsv = &root->fs_info->global_block_rsv;
  1086. ret = btrfs_unlink_subvol(trans, root, dir,
  1087. dest->root_key.objectid,
  1088. dentry->d_name.name,
  1089. dentry->d_name.len);
  1090. BUG_ON(ret);
  1091. btrfs_record_root_in_trans(trans, dest);
  1092. memset(&dest->root_item.drop_progress, 0,
  1093. sizeof(dest->root_item.drop_progress));
  1094. dest->root_item.drop_level = 0;
  1095. btrfs_set_root_refs(&dest->root_item, 0);
  1096. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1097. ret = btrfs_insert_orphan_item(trans,
  1098. root->fs_info->tree_root,
  1099. dest->root_key.objectid);
  1100. BUG_ON(ret);
  1101. }
  1102. ret = btrfs_commit_transaction(trans, root);
  1103. BUG_ON(ret);
  1104. inode->i_flags |= S_DEAD;
  1105. out_up_write:
  1106. up_write(&root->fs_info->subvol_sem);
  1107. out_unlock:
  1108. mutex_unlock(&inode->i_mutex);
  1109. if (!err) {
  1110. shrink_dcache_sb(root->fs_info->sb);
  1111. btrfs_invalidate_inodes(dest);
  1112. d_delete(dentry);
  1113. }
  1114. out_dput:
  1115. dput(dentry);
  1116. out_unlock_dir:
  1117. mutex_unlock(&dir->i_mutex);
  1118. mnt_drop_write(file->f_path.mnt);
  1119. out:
  1120. kfree(vol_args);
  1121. return err;
  1122. }
  1123. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1124. {
  1125. struct inode *inode = fdentry(file)->d_inode;
  1126. struct btrfs_root *root = BTRFS_I(inode)->root;
  1127. struct btrfs_ioctl_defrag_range_args *range;
  1128. int ret;
  1129. ret = mnt_want_write(file->f_path.mnt);
  1130. if (ret)
  1131. return ret;
  1132. switch (inode->i_mode & S_IFMT) {
  1133. case S_IFDIR:
  1134. if (!capable(CAP_SYS_ADMIN)) {
  1135. ret = -EPERM;
  1136. goto out;
  1137. }
  1138. ret = btrfs_defrag_root(root, 0);
  1139. if (ret)
  1140. goto out;
  1141. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1142. break;
  1143. case S_IFREG:
  1144. if (!(file->f_mode & FMODE_WRITE)) {
  1145. ret = -EINVAL;
  1146. goto out;
  1147. }
  1148. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1149. if (!range) {
  1150. ret = -ENOMEM;
  1151. goto out;
  1152. }
  1153. if (argp) {
  1154. if (copy_from_user(range, argp,
  1155. sizeof(*range))) {
  1156. ret = -EFAULT;
  1157. kfree(range);
  1158. goto out;
  1159. }
  1160. /* compression requires us to start the IO */
  1161. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1162. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1163. range->extent_thresh = (u32)-1;
  1164. }
  1165. } else {
  1166. /* the rest are all set to zero by kzalloc */
  1167. range->len = (u64)-1;
  1168. }
  1169. ret = btrfs_defrag_file(file, range);
  1170. kfree(range);
  1171. break;
  1172. default:
  1173. ret = -EINVAL;
  1174. }
  1175. out:
  1176. mnt_drop_write(file->f_path.mnt);
  1177. return ret;
  1178. }
  1179. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1180. {
  1181. struct btrfs_ioctl_vol_args *vol_args;
  1182. int ret;
  1183. if (!capable(CAP_SYS_ADMIN))
  1184. return -EPERM;
  1185. vol_args = memdup_user(arg, sizeof(*vol_args));
  1186. if (IS_ERR(vol_args))
  1187. return PTR_ERR(vol_args);
  1188. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1189. ret = btrfs_init_new_device(root, vol_args->name);
  1190. kfree(vol_args);
  1191. return ret;
  1192. }
  1193. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1194. {
  1195. struct btrfs_ioctl_vol_args *vol_args;
  1196. int ret;
  1197. if (!capable(CAP_SYS_ADMIN))
  1198. return -EPERM;
  1199. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1200. return -EROFS;
  1201. vol_args = memdup_user(arg, sizeof(*vol_args));
  1202. if (IS_ERR(vol_args))
  1203. return PTR_ERR(vol_args);
  1204. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1205. ret = btrfs_rm_device(root, vol_args->name);
  1206. kfree(vol_args);
  1207. return ret;
  1208. }
  1209. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1210. u64 off, u64 olen, u64 destoff)
  1211. {
  1212. struct inode *inode = fdentry(file)->d_inode;
  1213. struct btrfs_root *root = BTRFS_I(inode)->root;
  1214. struct file *src_file;
  1215. struct inode *src;
  1216. struct btrfs_trans_handle *trans;
  1217. struct btrfs_path *path;
  1218. struct extent_buffer *leaf;
  1219. char *buf;
  1220. struct btrfs_key key;
  1221. u32 nritems;
  1222. int slot;
  1223. int ret;
  1224. u64 len = olen;
  1225. u64 bs = root->fs_info->sb->s_blocksize;
  1226. u64 hint_byte;
  1227. /*
  1228. * TODO:
  1229. * - split compressed inline extents. annoying: we need to
  1230. * decompress into destination's address_space (the file offset
  1231. * may change, so source mapping won't do), then recompress (or
  1232. * otherwise reinsert) a subrange.
  1233. * - allow ranges within the same file to be cloned (provided
  1234. * they don't overlap)?
  1235. */
  1236. /* the destination must be opened for writing */
  1237. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1238. return -EINVAL;
  1239. ret = mnt_want_write(file->f_path.mnt);
  1240. if (ret)
  1241. return ret;
  1242. src_file = fget(srcfd);
  1243. if (!src_file) {
  1244. ret = -EBADF;
  1245. goto out_drop_write;
  1246. }
  1247. src = src_file->f_dentry->d_inode;
  1248. ret = -EINVAL;
  1249. if (src == inode)
  1250. goto out_fput;
  1251. /* the src must be open for reading */
  1252. if (!(src_file->f_mode & FMODE_READ))
  1253. goto out_fput;
  1254. ret = -EISDIR;
  1255. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1256. goto out_fput;
  1257. ret = -EXDEV;
  1258. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1259. goto out_fput;
  1260. ret = -ENOMEM;
  1261. buf = vmalloc(btrfs_level_size(root, 0));
  1262. if (!buf)
  1263. goto out_fput;
  1264. path = btrfs_alloc_path();
  1265. if (!path) {
  1266. vfree(buf);
  1267. goto out_fput;
  1268. }
  1269. path->reada = 2;
  1270. if (inode < src) {
  1271. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1272. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1273. } else {
  1274. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1275. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1276. }
  1277. /* determine range to clone */
  1278. ret = -EINVAL;
  1279. if (off + len > src->i_size || off + len < off)
  1280. goto out_unlock;
  1281. if (len == 0)
  1282. olen = len = src->i_size - off;
  1283. /* if we extend to eof, continue to block boundary */
  1284. if (off + len == src->i_size)
  1285. len = ((src->i_size + bs-1) & ~(bs-1))
  1286. - off;
  1287. /* verify the end result is block aligned */
  1288. if ((off & (bs-1)) ||
  1289. ((off + len) & (bs-1)))
  1290. goto out_unlock;
  1291. /* do any pending delalloc/csum calc on src, one way or
  1292. another, and lock file content */
  1293. while (1) {
  1294. struct btrfs_ordered_extent *ordered;
  1295. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1296. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1297. if (!ordered &&
  1298. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1299. EXTENT_DELALLOC, 0, NULL))
  1300. break;
  1301. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1302. if (ordered)
  1303. btrfs_put_ordered_extent(ordered);
  1304. btrfs_wait_ordered_range(src, off, len);
  1305. }
  1306. /* clone data */
  1307. key.objectid = src->i_ino;
  1308. key.type = BTRFS_EXTENT_DATA_KEY;
  1309. key.offset = 0;
  1310. while (1) {
  1311. /*
  1312. * note the key will change type as we walk through the
  1313. * tree.
  1314. */
  1315. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1316. if (ret < 0)
  1317. goto out;
  1318. nritems = btrfs_header_nritems(path->nodes[0]);
  1319. if (path->slots[0] >= nritems) {
  1320. ret = btrfs_next_leaf(root, path);
  1321. if (ret < 0)
  1322. goto out;
  1323. if (ret > 0)
  1324. break;
  1325. nritems = btrfs_header_nritems(path->nodes[0]);
  1326. }
  1327. leaf = path->nodes[0];
  1328. slot = path->slots[0];
  1329. btrfs_item_key_to_cpu(leaf, &key, slot);
  1330. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1331. key.objectid != src->i_ino)
  1332. break;
  1333. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1334. struct btrfs_file_extent_item *extent;
  1335. int type;
  1336. u32 size;
  1337. struct btrfs_key new_key;
  1338. u64 disko = 0, diskl = 0;
  1339. u64 datao = 0, datal = 0;
  1340. u8 comp;
  1341. u64 endoff;
  1342. size = btrfs_item_size_nr(leaf, slot);
  1343. read_extent_buffer(leaf, buf,
  1344. btrfs_item_ptr_offset(leaf, slot),
  1345. size);
  1346. extent = btrfs_item_ptr(leaf, slot,
  1347. struct btrfs_file_extent_item);
  1348. comp = btrfs_file_extent_compression(leaf, extent);
  1349. type = btrfs_file_extent_type(leaf, extent);
  1350. if (type == BTRFS_FILE_EXTENT_REG ||
  1351. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1352. disko = btrfs_file_extent_disk_bytenr(leaf,
  1353. extent);
  1354. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1355. extent);
  1356. datao = btrfs_file_extent_offset(leaf, extent);
  1357. datal = btrfs_file_extent_num_bytes(leaf,
  1358. extent);
  1359. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1360. /* take upper bound, may be compressed */
  1361. datal = btrfs_file_extent_ram_bytes(leaf,
  1362. extent);
  1363. }
  1364. btrfs_release_path(root, path);
  1365. if (key.offset + datal <= off ||
  1366. key.offset >= off+len)
  1367. goto next;
  1368. memcpy(&new_key, &key, sizeof(new_key));
  1369. new_key.objectid = inode->i_ino;
  1370. new_key.offset = key.offset + destoff - off;
  1371. trans = btrfs_start_transaction(root, 1);
  1372. if (IS_ERR(trans)) {
  1373. ret = PTR_ERR(trans);
  1374. goto out;
  1375. }
  1376. if (type == BTRFS_FILE_EXTENT_REG ||
  1377. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1378. if (off > key.offset) {
  1379. datao += off - key.offset;
  1380. datal -= off - key.offset;
  1381. }
  1382. if (key.offset + datal > off + len)
  1383. datal = off + len - key.offset;
  1384. ret = btrfs_drop_extents(trans, inode,
  1385. new_key.offset,
  1386. new_key.offset + datal,
  1387. &hint_byte, 1);
  1388. BUG_ON(ret);
  1389. ret = btrfs_insert_empty_item(trans, root, path,
  1390. &new_key, size);
  1391. BUG_ON(ret);
  1392. leaf = path->nodes[0];
  1393. slot = path->slots[0];
  1394. write_extent_buffer(leaf, buf,
  1395. btrfs_item_ptr_offset(leaf, slot),
  1396. size);
  1397. extent = btrfs_item_ptr(leaf, slot,
  1398. struct btrfs_file_extent_item);
  1399. /* disko == 0 means it's a hole */
  1400. if (!disko)
  1401. datao = 0;
  1402. btrfs_set_file_extent_offset(leaf, extent,
  1403. datao);
  1404. btrfs_set_file_extent_num_bytes(leaf, extent,
  1405. datal);
  1406. if (disko) {
  1407. inode_add_bytes(inode, datal);
  1408. ret = btrfs_inc_extent_ref(trans, root,
  1409. disko, diskl, 0,
  1410. root->root_key.objectid,
  1411. inode->i_ino,
  1412. new_key.offset - datao);
  1413. BUG_ON(ret);
  1414. }
  1415. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1416. u64 skip = 0;
  1417. u64 trim = 0;
  1418. if (off > key.offset) {
  1419. skip = off - key.offset;
  1420. new_key.offset += skip;
  1421. }
  1422. if (key.offset + datal > off+len)
  1423. trim = key.offset + datal - (off+len);
  1424. if (comp && (skip || trim)) {
  1425. ret = -EINVAL;
  1426. btrfs_end_transaction(trans, root);
  1427. goto out;
  1428. }
  1429. size -= skip + trim;
  1430. datal -= skip + trim;
  1431. ret = btrfs_drop_extents(trans, inode,
  1432. new_key.offset,
  1433. new_key.offset + datal,
  1434. &hint_byte, 1);
  1435. BUG_ON(ret);
  1436. ret = btrfs_insert_empty_item(trans, root, path,
  1437. &new_key, size);
  1438. BUG_ON(ret);
  1439. if (skip) {
  1440. u32 start =
  1441. btrfs_file_extent_calc_inline_size(0);
  1442. memmove(buf+start, buf+start+skip,
  1443. datal);
  1444. }
  1445. leaf = path->nodes[0];
  1446. slot = path->slots[0];
  1447. write_extent_buffer(leaf, buf,
  1448. btrfs_item_ptr_offset(leaf, slot),
  1449. size);
  1450. inode_add_bytes(inode, datal);
  1451. }
  1452. btrfs_mark_buffer_dirty(leaf);
  1453. btrfs_release_path(root, path);
  1454. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1455. /*
  1456. * we round up to the block size at eof when
  1457. * determining which extents to clone above,
  1458. * but shouldn't round up the file size
  1459. */
  1460. endoff = new_key.offset + datal;
  1461. if (endoff > off+olen)
  1462. endoff = off+olen;
  1463. if (endoff > inode->i_size)
  1464. btrfs_i_size_write(inode, endoff);
  1465. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1466. ret = btrfs_update_inode(trans, root, inode);
  1467. BUG_ON(ret);
  1468. btrfs_end_transaction(trans, root);
  1469. }
  1470. next:
  1471. btrfs_release_path(root, path);
  1472. key.offset++;
  1473. }
  1474. ret = 0;
  1475. out:
  1476. btrfs_release_path(root, path);
  1477. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1478. out_unlock:
  1479. mutex_unlock(&src->i_mutex);
  1480. mutex_unlock(&inode->i_mutex);
  1481. vfree(buf);
  1482. btrfs_free_path(path);
  1483. out_fput:
  1484. fput(src_file);
  1485. out_drop_write:
  1486. mnt_drop_write(file->f_path.mnt);
  1487. return ret;
  1488. }
  1489. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1490. {
  1491. struct btrfs_ioctl_clone_range_args args;
  1492. if (copy_from_user(&args, argp, sizeof(args)))
  1493. return -EFAULT;
  1494. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1495. args.src_length, args.dest_offset);
  1496. }
  1497. /*
  1498. * there are many ways the trans_start and trans_end ioctls can lead
  1499. * to deadlocks. They should only be used by applications that
  1500. * basically own the machine, and have a very in depth understanding
  1501. * of all the possible deadlocks and enospc problems.
  1502. */
  1503. static long btrfs_ioctl_trans_start(struct file *file)
  1504. {
  1505. struct inode *inode = fdentry(file)->d_inode;
  1506. struct btrfs_root *root = BTRFS_I(inode)->root;
  1507. struct btrfs_trans_handle *trans;
  1508. int ret;
  1509. ret = -EPERM;
  1510. if (!capable(CAP_SYS_ADMIN))
  1511. goto out;
  1512. ret = -EINPROGRESS;
  1513. if (file->private_data)
  1514. goto out;
  1515. ret = mnt_want_write(file->f_path.mnt);
  1516. if (ret)
  1517. goto out;
  1518. mutex_lock(&root->fs_info->trans_mutex);
  1519. root->fs_info->open_ioctl_trans++;
  1520. mutex_unlock(&root->fs_info->trans_mutex);
  1521. ret = -ENOMEM;
  1522. trans = btrfs_start_ioctl_transaction(root, 0);
  1523. if (!trans)
  1524. goto out_drop;
  1525. file->private_data = trans;
  1526. return 0;
  1527. out_drop:
  1528. mutex_lock(&root->fs_info->trans_mutex);
  1529. root->fs_info->open_ioctl_trans--;
  1530. mutex_unlock(&root->fs_info->trans_mutex);
  1531. mnt_drop_write(file->f_path.mnt);
  1532. out:
  1533. return ret;
  1534. }
  1535. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1536. {
  1537. struct inode *inode = fdentry(file)->d_inode;
  1538. struct btrfs_root *root = BTRFS_I(inode)->root;
  1539. struct btrfs_root *new_root;
  1540. struct btrfs_dir_item *di;
  1541. struct btrfs_trans_handle *trans;
  1542. struct btrfs_path *path;
  1543. struct btrfs_key location;
  1544. struct btrfs_disk_key disk_key;
  1545. struct btrfs_super_block *disk_super;
  1546. u64 features;
  1547. u64 objectid = 0;
  1548. u64 dir_id;
  1549. if (!capable(CAP_SYS_ADMIN))
  1550. return -EPERM;
  1551. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1552. return -EFAULT;
  1553. if (!objectid)
  1554. objectid = root->root_key.objectid;
  1555. location.objectid = objectid;
  1556. location.type = BTRFS_ROOT_ITEM_KEY;
  1557. location.offset = (u64)-1;
  1558. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1559. if (IS_ERR(new_root))
  1560. return PTR_ERR(new_root);
  1561. if (btrfs_root_refs(&new_root->root_item) == 0)
  1562. return -ENOENT;
  1563. path = btrfs_alloc_path();
  1564. if (!path)
  1565. return -ENOMEM;
  1566. path->leave_spinning = 1;
  1567. trans = btrfs_start_transaction(root, 1);
  1568. if (!trans) {
  1569. btrfs_free_path(path);
  1570. return -ENOMEM;
  1571. }
  1572. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1573. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1574. dir_id, "default", 7, 1);
  1575. if (IS_ERR_OR_NULL(di)) {
  1576. btrfs_free_path(path);
  1577. btrfs_end_transaction(trans, root);
  1578. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1579. "this isn't going to work\n");
  1580. return -ENOENT;
  1581. }
  1582. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1583. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1584. btrfs_mark_buffer_dirty(path->nodes[0]);
  1585. btrfs_free_path(path);
  1586. disk_super = &root->fs_info->super_copy;
  1587. features = btrfs_super_incompat_flags(disk_super);
  1588. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1589. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1590. btrfs_set_super_incompat_flags(disk_super, features);
  1591. }
  1592. btrfs_end_transaction(trans, root);
  1593. return 0;
  1594. }
  1595. static void get_block_group_info(struct list_head *groups_list,
  1596. struct btrfs_ioctl_space_info *space)
  1597. {
  1598. struct btrfs_block_group_cache *block_group;
  1599. space->total_bytes = 0;
  1600. space->used_bytes = 0;
  1601. space->flags = 0;
  1602. list_for_each_entry(block_group, groups_list, list) {
  1603. space->flags = block_group->flags;
  1604. space->total_bytes += block_group->key.offset;
  1605. space->used_bytes +=
  1606. btrfs_block_group_used(&block_group->item);
  1607. }
  1608. }
  1609. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1610. {
  1611. struct btrfs_ioctl_space_args space_args;
  1612. struct btrfs_ioctl_space_info space;
  1613. struct btrfs_ioctl_space_info *dest;
  1614. struct btrfs_ioctl_space_info *dest_orig;
  1615. struct btrfs_ioctl_space_info *user_dest;
  1616. struct btrfs_space_info *info;
  1617. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1618. BTRFS_BLOCK_GROUP_SYSTEM,
  1619. BTRFS_BLOCK_GROUP_METADATA,
  1620. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1621. int num_types = 4;
  1622. int alloc_size;
  1623. int ret = 0;
  1624. int slot_count = 0;
  1625. int i, c;
  1626. if (copy_from_user(&space_args,
  1627. (struct btrfs_ioctl_space_args __user *)arg,
  1628. sizeof(space_args)))
  1629. return -EFAULT;
  1630. for (i = 0; i < num_types; i++) {
  1631. struct btrfs_space_info *tmp;
  1632. info = NULL;
  1633. rcu_read_lock();
  1634. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1635. list) {
  1636. if (tmp->flags == types[i]) {
  1637. info = tmp;
  1638. break;
  1639. }
  1640. }
  1641. rcu_read_unlock();
  1642. if (!info)
  1643. continue;
  1644. down_read(&info->groups_sem);
  1645. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1646. if (!list_empty(&info->block_groups[c]))
  1647. slot_count++;
  1648. }
  1649. up_read(&info->groups_sem);
  1650. }
  1651. /* space_slots == 0 means they are asking for a count */
  1652. if (space_args.space_slots == 0) {
  1653. space_args.total_spaces = slot_count;
  1654. goto out;
  1655. }
  1656. slot_count = min_t(int, space_args.space_slots, slot_count);
  1657. alloc_size = sizeof(*dest) * slot_count;
  1658. /* we generally have at most 6 or so space infos, one for each raid
  1659. * level. So, a whole page should be more than enough for everyone
  1660. */
  1661. if (alloc_size > PAGE_CACHE_SIZE)
  1662. return -ENOMEM;
  1663. space_args.total_spaces = 0;
  1664. dest = kmalloc(alloc_size, GFP_NOFS);
  1665. if (!dest)
  1666. return -ENOMEM;
  1667. dest_orig = dest;
  1668. /* now we have a buffer to copy into */
  1669. for (i = 0; i < num_types; i++) {
  1670. struct btrfs_space_info *tmp;
  1671. info = NULL;
  1672. rcu_read_lock();
  1673. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1674. list) {
  1675. if (tmp->flags == types[i]) {
  1676. info = tmp;
  1677. break;
  1678. }
  1679. }
  1680. rcu_read_unlock();
  1681. if (!info)
  1682. continue;
  1683. down_read(&info->groups_sem);
  1684. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1685. if (!list_empty(&info->block_groups[c])) {
  1686. get_block_group_info(&info->block_groups[c],
  1687. &space);
  1688. memcpy(dest, &space, sizeof(space));
  1689. dest++;
  1690. space_args.total_spaces++;
  1691. }
  1692. }
  1693. up_read(&info->groups_sem);
  1694. }
  1695. user_dest = (struct btrfs_ioctl_space_info *)
  1696. (arg + sizeof(struct btrfs_ioctl_space_args));
  1697. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1698. ret = -EFAULT;
  1699. kfree(dest_orig);
  1700. out:
  1701. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1702. ret = -EFAULT;
  1703. return ret;
  1704. }
  1705. /*
  1706. * there are many ways the trans_start and trans_end ioctls can lead
  1707. * to deadlocks. They should only be used by applications that
  1708. * basically own the machine, and have a very in depth understanding
  1709. * of all the possible deadlocks and enospc problems.
  1710. */
  1711. long btrfs_ioctl_trans_end(struct file *file)
  1712. {
  1713. struct inode *inode = fdentry(file)->d_inode;
  1714. struct btrfs_root *root = BTRFS_I(inode)->root;
  1715. struct btrfs_trans_handle *trans;
  1716. trans = file->private_data;
  1717. if (!trans)
  1718. return -EINVAL;
  1719. file->private_data = NULL;
  1720. btrfs_end_transaction(trans, root);
  1721. mutex_lock(&root->fs_info->trans_mutex);
  1722. root->fs_info->open_ioctl_trans--;
  1723. mutex_unlock(&root->fs_info->trans_mutex);
  1724. mnt_drop_write(file->f_path.mnt);
  1725. return 0;
  1726. }
  1727. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1728. {
  1729. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1730. struct btrfs_trans_handle *trans;
  1731. u64 transid;
  1732. trans = btrfs_start_transaction(root, 0);
  1733. transid = trans->transid;
  1734. btrfs_commit_transaction_async(trans, root, 0);
  1735. if (argp)
  1736. if (copy_to_user(argp, &transid, sizeof(transid)))
  1737. return -EFAULT;
  1738. return 0;
  1739. }
  1740. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  1741. {
  1742. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1743. u64 transid;
  1744. if (argp) {
  1745. if (copy_from_user(&transid, argp, sizeof(transid)))
  1746. return -EFAULT;
  1747. } else {
  1748. transid = 0; /* current trans */
  1749. }
  1750. return btrfs_wait_for_commit(root, transid);
  1751. }
  1752. long btrfs_ioctl(struct file *file, unsigned int
  1753. cmd, unsigned long arg)
  1754. {
  1755. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1756. void __user *argp = (void __user *)arg;
  1757. switch (cmd) {
  1758. case FS_IOC_GETFLAGS:
  1759. return btrfs_ioctl_getflags(file, argp);
  1760. case FS_IOC_SETFLAGS:
  1761. return btrfs_ioctl_setflags(file, argp);
  1762. case FS_IOC_GETVERSION:
  1763. return btrfs_ioctl_getversion(file, argp);
  1764. case BTRFS_IOC_SNAP_CREATE:
  1765. return btrfs_ioctl_snap_create(file, argp, 0);
  1766. case BTRFS_IOC_SUBVOL_CREATE:
  1767. return btrfs_ioctl_snap_create(file, argp, 1);
  1768. case BTRFS_IOC_SNAP_DESTROY:
  1769. return btrfs_ioctl_snap_destroy(file, argp);
  1770. case BTRFS_IOC_DEFAULT_SUBVOL:
  1771. return btrfs_ioctl_default_subvol(file, argp);
  1772. case BTRFS_IOC_DEFRAG:
  1773. return btrfs_ioctl_defrag(file, NULL);
  1774. case BTRFS_IOC_DEFRAG_RANGE:
  1775. return btrfs_ioctl_defrag(file, argp);
  1776. case BTRFS_IOC_RESIZE:
  1777. return btrfs_ioctl_resize(root, argp);
  1778. case BTRFS_IOC_ADD_DEV:
  1779. return btrfs_ioctl_add_dev(root, argp);
  1780. case BTRFS_IOC_RM_DEV:
  1781. return btrfs_ioctl_rm_dev(root, argp);
  1782. case BTRFS_IOC_BALANCE:
  1783. return btrfs_balance(root->fs_info->dev_root);
  1784. case BTRFS_IOC_CLONE:
  1785. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1786. case BTRFS_IOC_CLONE_RANGE:
  1787. return btrfs_ioctl_clone_range(file, argp);
  1788. case BTRFS_IOC_TRANS_START:
  1789. return btrfs_ioctl_trans_start(file);
  1790. case BTRFS_IOC_TRANS_END:
  1791. return btrfs_ioctl_trans_end(file);
  1792. case BTRFS_IOC_TREE_SEARCH:
  1793. return btrfs_ioctl_tree_search(file, argp);
  1794. case BTRFS_IOC_INO_LOOKUP:
  1795. return btrfs_ioctl_ino_lookup(file, argp);
  1796. case BTRFS_IOC_SPACE_INFO:
  1797. return btrfs_ioctl_space_info(root, argp);
  1798. case BTRFS_IOC_SYNC:
  1799. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1800. return 0;
  1801. case BTRFS_IOC_START_SYNC:
  1802. return btrfs_ioctl_start_sync(file, argp);
  1803. case BTRFS_IOC_WAIT_SYNC:
  1804. return btrfs_ioctl_wait_sync(file, argp);
  1805. }
  1806. return -ENOTTY;
  1807. }