ioctl.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  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 namelen;
  608. int mod = 0;
  609. if (root->fs_info->sb->s_flags & MS_RDONLY)
  610. return -EROFS;
  611. if (!capable(CAP_SYS_ADMIN))
  612. return -EPERM;
  613. vol_args = memdup_user(arg, sizeof(*vol_args));
  614. if (IS_ERR(vol_args))
  615. return PTR_ERR(vol_args);
  616. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  617. namelen = strlen(vol_args->name);
  618. mutex_lock(&root->fs_info->volume_mutex);
  619. sizestr = vol_args->name;
  620. devstr = strchr(sizestr, ':');
  621. if (devstr) {
  622. char *end;
  623. sizestr = devstr + 1;
  624. *devstr = '\0';
  625. devstr = vol_args->name;
  626. devid = simple_strtoull(devstr, &end, 10);
  627. printk(KERN_INFO "resizing devid %llu\n",
  628. (unsigned long long)devid);
  629. }
  630. device = btrfs_find_device(root, devid, NULL, NULL);
  631. if (!device) {
  632. printk(KERN_INFO "resizer unable to find device %llu\n",
  633. (unsigned long long)devid);
  634. ret = -EINVAL;
  635. goto out_unlock;
  636. }
  637. if (!strcmp(sizestr, "max"))
  638. new_size = device->bdev->bd_inode->i_size;
  639. else {
  640. if (sizestr[0] == '-') {
  641. mod = -1;
  642. sizestr++;
  643. } else if (sizestr[0] == '+') {
  644. mod = 1;
  645. sizestr++;
  646. }
  647. new_size = memparse(sizestr, NULL);
  648. if (new_size == 0) {
  649. ret = -EINVAL;
  650. goto out_unlock;
  651. }
  652. }
  653. old_size = device->total_bytes;
  654. if (mod < 0) {
  655. if (new_size > old_size) {
  656. ret = -EINVAL;
  657. goto out_unlock;
  658. }
  659. new_size = old_size - new_size;
  660. } else if (mod > 0) {
  661. new_size = old_size + new_size;
  662. }
  663. if (new_size < 256 * 1024 * 1024) {
  664. ret = -EINVAL;
  665. goto out_unlock;
  666. }
  667. if (new_size > device->bdev->bd_inode->i_size) {
  668. ret = -EFBIG;
  669. goto out_unlock;
  670. }
  671. do_div(new_size, root->sectorsize);
  672. new_size *= root->sectorsize;
  673. printk(KERN_INFO "new size for %s is %llu\n",
  674. device->name, (unsigned long long)new_size);
  675. if (new_size > old_size) {
  676. trans = btrfs_start_transaction(root, 0);
  677. ret = btrfs_grow_device(trans, device, new_size);
  678. btrfs_commit_transaction(trans, root);
  679. } else {
  680. ret = btrfs_shrink_device(device, new_size);
  681. }
  682. out_unlock:
  683. mutex_unlock(&root->fs_info->volume_mutex);
  684. kfree(vol_args);
  685. return ret;
  686. }
  687. static noinline int btrfs_ioctl_snap_create(struct file *file,
  688. void __user *arg, int subvol)
  689. {
  690. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  691. struct btrfs_ioctl_vol_args *vol_args;
  692. struct file *src_file;
  693. int namelen;
  694. int ret = 0;
  695. if (root->fs_info->sb->s_flags & MS_RDONLY)
  696. return -EROFS;
  697. vol_args = memdup_user(arg, sizeof(*vol_args));
  698. if (IS_ERR(vol_args))
  699. return PTR_ERR(vol_args);
  700. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  701. namelen = strlen(vol_args->name);
  702. if (strchr(vol_args->name, '/')) {
  703. ret = -EINVAL;
  704. goto out;
  705. }
  706. if (subvol) {
  707. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  708. NULL);
  709. } else {
  710. struct inode *src_inode;
  711. src_file = fget(vol_args->fd);
  712. if (!src_file) {
  713. ret = -EINVAL;
  714. goto out;
  715. }
  716. src_inode = src_file->f_path.dentry->d_inode;
  717. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  718. printk(KERN_INFO "btrfs: Snapshot src from "
  719. "another FS\n");
  720. ret = -EINVAL;
  721. fput(src_file);
  722. goto out;
  723. }
  724. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  725. BTRFS_I(src_inode)->root);
  726. fput(src_file);
  727. }
  728. out:
  729. kfree(vol_args);
  730. return ret;
  731. }
  732. /*
  733. * helper to check if the subvolume references other subvolumes
  734. */
  735. static noinline int may_destroy_subvol(struct btrfs_root *root)
  736. {
  737. struct btrfs_path *path;
  738. struct btrfs_key key;
  739. int ret;
  740. path = btrfs_alloc_path();
  741. if (!path)
  742. return -ENOMEM;
  743. key.objectid = root->root_key.objectid;
  744. key.type = BTRFS_ROOT_REF_KEY;
  745. key.offset = (u64)-1;
  746. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  747. &key, path, 0, 0);
  748. if (ret < 0)
  749. goto out;
  750. BUG_ON(ret == 0);
  751. ret = 0;
  752. if (path->slots[0] > 0) {
  753. path->slots[0]--;
  754. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  755. if (key.objectid == root->root_key.objectid &&
  756. key.type == BTRFS_ROOT_REF_KEY)
  757. ret = -ENOTEMPTY;
  758. }
  759. out:
  760. btrfs_free_path(path);
  761. return ret;
  762. }
  763. static noinline int key_in_sk(struct btrfs_key *key,
  764. struct btrfs_ioctl_search_key *sk)
  765. {
  766. struct btrfs_key test;
  767. int ret;
  768. test.objectid = sk->min_objectid;
  769. test.type = sk->min_type;
  770. test.offset = sk->min_offset;
  771. ret = btrfs_comp_cpu_keys(key, &test);
  772. if (ret < 0)
  773. return 0;
  774. test.objectid = sk->max_objectid;
  775. test.type = sk->max_type;
  776. test.offset = sk->max_offset;
  777. ret = btrfs_comp_cpu_keys(key, &test);
  778. if (ret > 0)
  779. return 0;
  780. return 1;
  781. }
  782. static noinline int copy_to_sk(struct btrfs_root *root,
  783. struct btrfs_path *path,
  784. struct btrfs_key *key,
  785. struct btrfs_ioctl_search_key *sk,
  786. char *buf,
  787. unsigned long *sk_offset,
  788. int *num_found)
  789. {
  790. u64 found_transid;
  791. struct extent_buffer *leaf;
  792. struct btrfs_ioctl_search_header sh;
  793. unsigned long item_off;
  794. unsigned long item_len;
  795. int nritems;
  796. int i;
  797. int slot;
  798. int found = 0;
  799. int ret = 0;
  800. leaf = path->nodes[0];
  801. slot = path->slots[0];
  802. nritems = btrfs_header_nritems(leaf);
  803. if (btrfs_header_generation(leaf) > sk->max_transid) {
  804. i = nritems;
  805. goto advance_key;
  806. }
  807. found_transid = btrfs_header_generation(leaf);
  808. for (i = slot; i < nritems; i++) {
  809. item_off = btrfs_item_ptr_offset(leaf, i);
  810. item_len = btrfs_item_size_nr(leaf, i);
  811. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  812. item_len = 0;
  813. if (sizeof(sh) + item_len + *sk_offset >
  814. BTRFS_SEARCH_ARGS_BUFSIZE) {
  815. ret = 1;
  816. goto overflow;
  817. }
  818. btrfs_item_key_to_cpu(leaf, key, i);
  819. if (!key_in_sk(key, sk))
  820. continue;
  821. sh.objectid = key->objectid;
  822. sh.offset = key->offset;
  823. sh.type = key->type;
  824. sh.len = item_len;
  825. sh.transid = found_transid;
  826. /* copy search result header */
  827. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  828. *sk_offset += sizeof(sh);
  829. if (item_len) {
  830. char *p = buf + *sk_offset;
  831. /* copy the item */
  832. read_extent_buffer(leaf, p,
  833. item_off, item_len);
  834. *sk_offset += item_len;
  835. }
  836. found++;
  837. if (*num_found >= sk->nr_items)
  838. break;
  839. }
  840. advance_key:
  841. ret = 0;
  842. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  843. key->offset++;
  844. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  845. key->offset = 0;
  846. key->type++;
  847. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  848. key->offset = 0;
  849. key->type = 0;
  850. key->objectid++;
  851. } else
  852. ret = 1;
  853. overflow:
  854. *num_found += found;
  855. return ret;
  856. }
  857. static noinline int search_ioctl(struct inode *inode,
  858. struct btrfs_ioctl_search_args *args)
  859. {
  860. struct btrfs_root *root;
  861. struct btrfs_key key;
  862. struct btrfs_key max_key;
  863. struct btrfs_path *path;
  864. struct btrfs_ioctl_search_key *sk = &args->key;
  865. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  866. int ret;
  867. int num_found = 0;
  868. unsigned long sk_offset = 0;
  869. path = btrfs_alloc_path();
  870. if (!path)
  871. return -ENOMEM;
  872. if (sk->tree_id == 0) {
  873. /* search the root of the inode that was passed */
  874. root = BTRFS_I(inode)->root;
  875. } else {
  876. key.objectid = sk->tree_id;
  877. key.type = BTRFS_ROOT_ITEM_KEY;
  878. key.offset = (u64)-1;
  879. root = btrfs_read_fs_root_no_name(info, &key);
  880. if (IS_ERR(root)) {
  881. printk(KERN_ERR "could not find root %llu\n",
  882. sk->tree_id);
  883. btrfs_free_path(path);
  884. return -ENOENT;
  885. }
  886. }
  887. key.objectid = sk->min_objectid;
  888. key.type = sk->min_type;
  889. key.offset = sk->min_offset;
  890. max_key.objectid = sk->max_objectid;
  891. max_key.type = sk->max_type;
  892. max_key.offset = sk->max_offset;
  893. path->keep_locks = 1;
  894. while(1) {
  895. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  896. sk->min_transid);
  897. if (ret != 0) {
  898. if (ret > 0)
  899. ret = 0;
  900. goto err;
  901. }
  902. ret = copy_to_sk(root, path, &key, sk, args->buf,
  903. &sk_offset, &num_found);
  904. btrfs_release_path(root, path);
  905. if (ret || num_found >= sk->nr_items)
  906. break;
  907. }
  908. ret = 0;
  909. err:
  910. sk->nr_items = num_found;
  911. btrfs_free_path(path);
  912. return ret;
  913. }
  914. static noinline int btrfs_ioctl_tree_search(struct file *file,
  915. void __user *argp)
  916. {
  917. struct btrfs_ioctl_search_args *args;
  918. struct inode *inode;
  919. int ret;
  920. if (!capable(CAP_SYS_ADMIN))
  921. return -EPERM;
  922. args = kmalloc(sizeof(*args), GFP_KERNEL);
  923. if (!args)
  924. return -ENOMEM;
  925. if (copy_from_user(args, argp, sizeof(*args))) {
  926. kfree(args);
  927. return -EFAULT;
  928. }
  929. inode = fdentry(file)->d_inode;
  930. ret = search_ioctl(inode, args);
  931. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  932. ret = -EFAULT;
  933. kfree(args);
  934. return ret;
  935. }
  936. /*
  937. * Search INODE_REFs to identify path name of 'dirid' directory
  938. * in a 'tree_id' tree. and sets path name to 'name'.
  939. */
  940. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  941. u64 tree_id, u64 dirid, char *name)
  942. {
  943. struct btrfs_root *root;
  944. struct btrfs_key key;
  945. char *ptr;
  946. int ret = -1;
  947. int slot;
  948. int len;
  949. int total_len = 0;
  950. struct btrfs_inode_ref *iref;
  951. struct extent_buffer *l;
  952. struct btrfs_path *path;
  953. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  954. name[0]='\0';
  955. return 0;
  956. }
  957. path = btrfs_alloc_path();
  958. if (!path)
  959. return -ENOMEM;
  960. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  961. key.objectid = tree_id;
  962. key.type = BTRFS_ROOT_ITEM_KEY;
  963. key.offset = (u64)-1;
  964. root = btrfs_read_fs_root_no_name(info, &key);
  965. if (IS_ERR(root)) {
  966. printk(KERN_ERR "could not find root %llu\n", tree_id);
  967. ret = -ENOENT;
  968. goto out;
  969. }
  970. key.objectid = dirid;
  971. key.type = BTRFS_INODE_REF_KEY;
  972. key.offset = (u64)-1;
  973. while(1) {
  974. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  975. if (ret < 0)
  976. goto out;
  977. l = path->nodes[0];
  978. slot = path->slots[0];
  979. if (ret > 0 && slot > 0)
  980. slot--;
  981. btrfs_item_key_to_cpu(l, &key, slot);
  982. if (ret > 0 && (key.objectid != dirid ||
  983. key.type != BTRFS_INODE_REF_KEY)) {
  984. ret = -ENOENT;
  985. goto out;
  986. }
  987. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  988. len = btrfs_inode_ref_name_len(l, iref);
  989. ptr -= len + 1;
  990. total_len += len + 1;
  991. if (ptr < name)
  992. goto out;
  993. *(ptr + len) = '/';
  994. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  995. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  996. break;
  997. btrfs_release_path(root, path);
  998. key.objectid = key.offset;
  999. key.offset = (u64)-1;
  1000. dirid = key.objectid;
  1001. }
  1002. if (ptr < name)
  1003. goto out;
  1004. memcpy(name, ptr, total_len);
  1005. name[total_len]='\0';
  1006. ret = 0;
  1007. out:
  1008. btrfs_free_path(path);
  1009. return ret;
  1010. }
  1011. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1012. void __user *argp)
  1013. {
  1014. struct btrfs_ioctl_ino_lookup_args *args;
  1015. struct inode *inode;
  1016. int ret;
  1017. if (!capable(CAP_SYS_ADMIN))
  1018. return -EPERM;
  1019. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1020. if (!args)
  1021. return -ENOMEM;
  1022. if (copy_from_user(args, argp, sizeof(*args))) {
  1023. kfree(args);
  1024. return -EFAULT;
  1025. }
  1026. inode = fdentry(file)->d_inode;
  1027. if (args->treeid == 0)
  1028. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1029. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1030. args->treeid, args->objectid,
  1031. args->name);
  1032. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1033. ret = -EFAULT;
  1034. kfree(args);
  1035. return ret;
  1036. }
  1037. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1038. void __user *arg)
  1039. {
  1040. struct dentry *parent = fdentry(file);
  1041. struct dentry *dentry;
  1042. struct inode *dir = parent->d_inode;
  1043. struct inode *inode;
  1044. struct btrfs_root *root = BTRFS_I(dir)->root;
  1045. struct btrfs_root *dest = NULL;
  1046. struct btrfs_ioctl_vol_args *vol_args;
  1047. struct btrfs_trans_handle *trans;
  1048. int namelen;
  1049. int ret;
  1050. int err = 0;
  1051. if (!capable(CAP_SYS_ADMIN))
  1052. return -EPERM;
  1053. vol_args = memdup_user(arg, sizeof(*vol_args));
  1054. if (IS_ERR(vol_args))
  1055. return PTR_ERR(vol_args);
  1056. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1057. namelen = strlen(vol_args->name);
  1058. if (strchr(vol_args->name, '/') ||
  1059. strncmp(vol_args->name, "..", namelen) == 0) {
  1060. err = -EINVAL;
  1061. goto out;
  1062. }
  1063. err = mnt_want_write(file->f_path.mnt);
  1064. if (err)
  1065. goto out;
  1066. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1067. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1068. if (IS_ERR(dentry)) {
  1069. err = PTR_ERR(dentry);
  1070. goto out_unlock_dir;
  1071. }
  1072. if (!dentry->d_inode) {
  1073. err = -ENOENT;
  1074. goto out_dput;
  1075. }
  1076. inode = dentry->d_inode;
  1077. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1078. err = -EINVAL;
  1079. goto out_dput;
  1080. }
  1081. dest = BTRFS_I(inode)->root;
  1082. mutex_lock(&inode->i_mutex);
  1083. err = d_invalidate(dentry);
  1084. if (err)
  1085. goto out_unlock;
  1086. down_write(&root->fs_info->subvol_sem);
  1087. err = may_destroy_subvol(dest);
  1088. if (err)
  1089. goto out_up_write;
  1090. trans = btrfs_start_transaction(root, 0);
  1091. if (IS_ERR(trans)) {
  1092. err = PTR_ERR(trans);
  1093. goto out_up_write;
  1094. }
  1095. trans->block_rsv = &root->fs_info->global_block_rsv;
  1096. ret = btrfs_unlink_subvol(trans, root, dir,
  1097. dest->root_key.objectid,
  1098. dentry->d_name.name,
  1099. dentry->d_name.len);
  1100. BUG_ON(ret);
  1101. btrfs_record_root_in_trans(trans, dest);
  1102. memset(&dest->root_item.drop_progress, 0,
  1103. sizeof(dest->root_item.drop_progress));
  1104. dest->root_item.drop_level = 0;
  1105. btrfs_set_root_refs(&dest->root_item, 0);
  1106. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1107. ret = btrfs_insert_orphan_item(trans,
  1108. root->fs_info->tree_root,
  1109. dest->root_key.objectid);
  1110. BUG_ON(ret);
  1111. }
  1112. ret = btrfs_commit_transaction(trans, root);
  1113. BUG_ON(ret);
  1114. inode->i_flags |= S_DEAD;
  1115. out_up_write:
  1116. up_write(&root->fs_info->subvol_sem);
  1117. out_unlock:
  1118. mutex_unlock(&inode->i_mutex);
  1119. if (!err) {
  1120. shrink_dcache_sb(root->fs_info->sb);
  1121. btrfs_invalidate_inodes(dest);
  1122. d_delete(dentry);
  1123. }
  1124. out_dput:
  1125. dput(dentry);
  1126. out_unlock_dir:
  1127. mutex_unlock(&dir->i_mutex);
  1128. mnt_drop_write(file->f_path.mnt);
  1129. out:
  1130. kfree(vol_args);
  1131. return err;
  1132. }
  1133. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1134. {
  1135. struct inode *inode = fdentry(file)->d_inode;
  1136. struct btrfs_root *root = BTRFS_I(inode)->root;
  1137. struct btrfs_ioctl_defrag_range_args *range;
  1138. int ret;
  1139. ret = mnt_want_write(file->f_path.mnt);
  1140. if (ret)
  1141. return ret;
  1142. switch (inode->i_mode & S_IFMT) {
  1143. case S_IFDIR:
  1144. if (!capable(CAP_SYS_ADMIN)) {
  1145. ret = -EPERM;
  1146. goto out;
  1147. }
  1148. ret = btrfs_defrag_root(root, 0);
  1149. if (ret)
  1150. goto out;
  1151. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1152. break;
  1153. case S_IFREG:
  1154. if (!(file->f_mode & FMODE_WRITE)) {
  1155. ret = -EINVAL;
  1156. goto out;
  1157. }
  1158. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1159. if (!range) {
  1160. ret = -ENOMEM;
  1161. goto out;
  1162. }
  1163. if (argp) {
  1164. if (copy_from_user(range, argp,
  1165. sizeof(*range))) {
  1166. ret = -EFAULT;
  1167. kfree(range);
  1168. goto out;
  1169. }
  1170. /* compression requires us to start the IO */
  1171. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1172. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1173. range->extent_thresh = (u32)-1;
  1174. }
  1175. } else {
  1176. /* the rest are all set to zero by kzalloc */
  1177. range->len = (u64)-1;
  1178. }
  1179. ret = btrfs_defrag_file(file, range);
  1180. kfree(range);
  1181. break;
  1182. default:
  1183. ret = -EINVAL;
  1184. }
  1185. out:
  1186. mnt_drop_write(file->f_path.mnt);
  1187. return ret;
  1188. }
  1189. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1190. {
  1191. struct btrfs_ioctl_vol_args *vol_args;
  1192. int ret;
  1193. if (!capable(CAP_SYS_ADMIN))
  1194. return -EPERM;
  1195. vol_args = memdup_user(arg, sizeof(*vol_args));
  1196. if (IS_ERR(vol_args))
  1197. return PTR_ERR(vol_args);
  1198. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1199. ret = btrfs_init_new_device(root, vol_args->name);
  1200. kfree(vol_args);
  1201. return ret;
  1202. }
  1203. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1204. {
  1205. struct btrfs_ioctl_vol_args *vol_args;
  1206. int ret;
  1207. if (!capable(CAP_SYS_ADMIN))
  1208. return -EPERM;
  1209. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1210. return -EROFS;
  1211. vol_args = memdup_user(arg, sizeof(*vol_args));
  1212. if (IS_ERR(vol_args))
  1213. return PTR_ERR(vol_args);
  1214. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1215. ret = btrfs_rm_device(root, vol_args->name);
  1216. kfree(vol_args);
  1217. return ret;
  1218. }
  1219. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1220. u64 off, u64 olen, u64 destoff)
  1221. {
  1222. struct inode *inode = fdentry(file)->d_inode;
  1223. struct btrfs_root *root = BTRFS_I(inode)->root;
  1224. struct file *src_file;
  1225. struct inode *src;
  1226. struct btrfs_trans_handle *trans;
  1227. struct btrfs_path *path;
  1228. struct extent_buffer *leaf;
  1229. char *buf;
  1230. struct btrfs_key key;
  1231. u32 nritems;
  1232. int slot;
  1233. int ret;
  1234. u64 len = olen;
  1235. u64 bs = root->fs_info->sb->s_blocksize;
  1236. u64 hint_byte;
  1237. /*
  1238. * TODO:
  1239. * - split compressed inline extents. annoying: we need to
  1240. * decompress into destination's address_space (the file offset
  1241. * may change, so source mapping won't do), then recompress (or
  1242. * otherwise reinsert) a subrange.
  1243. * - allow ranges within the same file to be cloned (provided
  1244. * they don't overlap)?
  1245. */
  1246. /* the destination must be opened for writing */
  1247. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1248. return -EINVAL;
  1249. ret = mnt_want_write(file->f_path.mnt);
  1250. if (ret)
  1251. return ret;
  1252. src_file = fget(srcfd);
  1253. if (!src_file) {
  1254. ret = -EBADF;
  1255. goto out_drop_write;
  1256. }
  1257. src = src_file->f_dentry->d_inode;
  1258. ret = -EINVAL;
  1259. if (src == inode)
  1260. goto out_fput;
  1261. /* the src must be open for reading */
  1262. if (!(src_file->f_mode & FMODE_READ))
  1263. goto out_fput;
  1264. ret = -EISDIR;
  1265. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1266. goto out_fput;
  1267. ret = -EXDEV;
  1268. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1269. goto out_fput;
  1270. ret = -ENOMEM;
  1271. buf = vmalloc(btrfs_level_size(root, 0));
  1272. if (!buf)
  1273. goto out_fput;
  1274. path = btrfs_alloc_path();
  1275. if (!path) {
  1276. vfree(buf);
  1277. goto out_fput;
  1278. }
  1279. path->reada = 2;
  1280. if (inode < src) {
  1281. mutex_lock(&inode->i_mutex);
  1282. mutex_lock(&src->i_mutex);
  1283. } else {
  1284. mutex_lock(&src->i_mutex);
  1285. mutex_lock(&inode->i_mutex);
  1286. }
  1287. /* determine range to clone */
  1288. ret = -EINVAL;
  1289. if (off + len > src->i_size || off + len < off)
  1290. goto out_unlock;
  1291. if (len == 0)
  1292. olen = len = src->i_size - off;
  1293. /* if we extend to eof, continue to block boundary */
  1294. if (off + len == src->i_size)
  1295. len = ((src->i_size + bs-1) & ~(bs-1))
  1296. - off;
  1297. /* verify the end result is block aligned */
  1298. if ((off & (bs-1)) ||
  1299. ((off + len) & (bs-1)))
  1300. goto out_unlock;
  1301. /* do any pending delalloc/csum calc on src, one way or
  1302. another, and lock file content */
  1303. while (1) {
  1304. struct btrfs_ordered_extent *ordered;
  1305. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1306. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  1307. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  1308. break;
  1309. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1310. if (ordered)
  1311. btrfs_put_ordered_extent(ordered);
  1312. btrfs_wait_ordered_range(src, off, off+len);
  1313. }
  1314. /* clone data */
  1315. key.objectid = src->i_ino;
  1316. key.type = BTRFS_EXTENT_DATA_KEY;
  1317. key.offset = 0;
  1318. while (1) {
  1319. /*
  1320. * note the key will change type as we walk through the
  1321. * tree.
  1322. */
  1323. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1324. if (ret < 0)
  1325. goto out;
  1326. nritems = btrfs_header_nritems(path->nodes[0]);
  1327. if (path->slots[0] >= nritems) {
  1328. ret = btrfs_next_leaf(root, path);
  1329. if (ret < 0)
  1330. goto out;
  1331. if (ret > 0)
  1332. break;
  1333. nritems = btrfs_header_nritems(path->nodes[0]);
  1334. }
  1335. leaf = path->nodes[0];
  1336. slot = path->slots[0];
  1337. btrfs_item_key_to_cpu(leaf, &key, slot);
  1338. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1339. key.objectid != src->i_ino)
  1340. break;
  1341. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1342. struct btrfs_file_extent_item *extent;
  1343. int type;
  1344. u32 size;
  1345. struct btrfs_key new_key;
  1346. u64 disko = 0, diskl = 0;
  1347. u64 datao = 0, datal = 0;
  1348. u8 comp;
  1349. u64 endoff;
  1350. size = btrfs_item_size_nr(leaf, slot);
  1351. read_extent_buffer(leaf, buf,
  1352. btrfs_item_ptr_offset(leaf, slot),
  1353. size);
  1354. extent = btrfs_item_ptr(leaf, slot,
  1355. struct btrfs_file_extent_item);
  1356. comp = btrfs_file_extent_compression(leaf, extent);
  1357. type = btrfs_file_extent_type(leaf, extent);
  1358. if (type == BTRFS_FILE_EXTENT_REG ||
  1359. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1360. disko = btrfs_file_extent_disk_bytenr(leaf,
  1361. extent);
  1362. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1363. extent);
  1364. datao = btrfs_file_extent_offset(leaf, extent);
  1365. datal = btrfs_file_extent_num_bytes(leaf,
  1366. extent);
  1367. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1368. /* take upper bound, may be compressed */
  1369. datal = btrfs_file_extent_ram_bytes(leaf,
  1370. extent);
  1371. }
  1372. btrfs_release_path(root, path);
  1373. if (key.offset + datal < off ||
  1374. key.offset >= off+len)
  1375. goto next;
  1376. memcpy(&new_key, &key, sizeof(new_key));
  1377. new_key.objectid = inode->i_ino;
  1378. new_key.offset = key.offset + destoff - off;
  1379. trans = btrfs_start_transaction(root, 1);
  1380. if (IS_ERR(trans)) {
  1381. ret = PTR_ERR(trans);
  1382. goto out;
  1383. }
  1384. if (type == BTRFS_FILE_EXTENT_REG ||
  1385. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1386. if (off > key.offset) {
  1387. datao += off - key.offset;
  1388. datal -= off - key.offset;
  1389. }
  1390. if (key.offset + datal > off + len)
  1391. datal = off + len - key.offset;
  1392. ret = btrfs_drop_extents(trans, inode,
  1393. new_key.offset,
  1394. new_key.offset + datal,
  1395. &hint_byte, 1);
  1396. BUG_ON(ret);
  1397. ret = btrfs_insert_empty_item(trans, root, path,
  1398. &new_key, size);
  1399. BUG_ON(ret);
  1400. leaf = path->nodes[0];
  1401. slot = path->slots[0];
  1402. write_extent_buffer(leaf, buf,
  1403. btrfs_item_ptr_offset(leaf, slot),
  1404. size);
  1405. extent = btrfs_item_ptr(leaf, slot,
  1406. struct btrfs_file_extent_item);
  1407. /* disko == 0 means it's a hole */
  1408. if (!disko)
  1409. datao = 0;
  1410. btrfs_set_file_extent_offset(leaf, extent,
  1411. datao);
  1412. btrfs_set_file_extent_num_bytes(leaf, extent,
  1413. datal);
  1414. if (disko) {
  1415. inode_add_bytes(inode, datal);
  1416. ret = btrfs_inc_extent_ref(trans, root,
  1417. disko, diskl, 0,
  1418. root->root_key.objectid,
  1419. inode->i_ino,
  1420. new_key.offset - datao);
  1421. BUG_ON(ret);
  1422. }
  1423. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1424. u64 skip = 0;
  1425. u64 trim = 0;
  1426. if (off > key.offset) {
  1427. skip = off - key.offset;
  1428. new_key.offset += skip;
  1429. }
  1430. if (key.offset + datal > off+len)
  1431. trim = key.offset + datal - (off+len);
  1432. if (comp && (skip || trim)) {
  1433. ret = -EINVAL;
  1434. btrfs_end_transaction(trans, root);
  1435. goto out;
  1436. }
  1437. size -= skip + trim;
  1438. datal -= skip + trim;
  1439. ret = btrfs_drop_extents(trans, inode,
  1440. new_key.offset,
  1441. new_key.offset + datal,
  1442. &hint_byte, 1);
  1443. BUG_ON(ret);
  1444. ret = btrfs_insert_empty_item(trans, root, path,
  1445. &new_key, size);
  1446. BUG_ON(ret);
  1447. if (skip) {
  1448. u32 start =
  1449. btrfs_file_extent_calc_inline_size(0);
  1450. memmove(buf+start, buf+start+skip,
  1451. datal);
  1452. }
  1453. leaf = path->nodes[0];
  1454. slot = path->slots[0];
  1455. write_extent_buffer(leaf, buf,
  1456. btrfs_item_ptr_offset(leaf, slot),
  1457. size);
  1458. inode_add_bytes(inode, datal);
  1459. }
  1460. btrfs_mark_buffer_dirty(leaf);
  1461. btrfs_release_path(root, path);
  1462. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1463. /*
  1464. * we round up to the block size at eof when
  1465. * determining which extents to clone above,
  1466. * but shouldn't round up the file size
  1467. */
  1468. endoff = new_key.offset + datal;
  1469. if (endoff > off+olen)
  1470. endoff = off+olen;
  1471. if (endoff > inode->i_size)
  1472. btrfs_i_size_write(inode, endoff);
  1473. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1474. ret = btrfs_update_inode(trans, root, inode);
  1475. BUG_ON(ret);
  1476. btrfs_end_transaction(trans, root);
  1477. }
  1478. next:
  1479. btrfs_release_path(root, path);
  1480. key.offset++;
  1481. }
  1482. ret = 0;
  1483. out:
  1484. btrfs_release_path(root, path);
  1485. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1486. out_unlock:
  1487. mutex_unlock(&src->i_mutex);
  1488. mutex_unlock(&inode->i_mutex);
  1489. vfree(buf);
  1490. btrfs_free_path(path);
  1491. out_fput:
  1492. fput(src_file);
  1493. out_drop_write:
  1494. mnt_drop_write(file->f_path.mnt);
  1495. return ret;
  1496. }
  1497. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1498. {
  1499. struct btrfs_ioctl_clone_range_args args;
  1500. if (copy_from_user(&args, argp, sizeof(args)))
  1501. return -EFAULT;
  1502. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1503. args.src_length, args.dest_offset);
  1504. }
  1505. /*
  1506. * there are many ways the trans_start and trans_end ioctls can lead
  1507. * to deadlocks. They should only be used by applications that
  1508. * basically own the machine, and have a very in depth understanding
  1509. * of all the possible deadlocks and enospc problems.
  1510. */
  1511. static long btrfs_ioctl_trans_start(struct file *file)
  1512. {
  1513. struct inode *inode = fdentry(file)->d_inode;
  1514. struct btrfs_root *root = BTRFS_I(inode)->root;
  1515. struct btrfs_trans_handle *trans;
  1516. int ret;
  1517. ret = -EPERM;
  1518. if (!capable(CAP_SYS_ADMIN))
  1519. goto out;
  1520. ret = -EINPROGRESS;
  1521. if (file->private_data)
  1522. goto out;
  1523. ret = mnt_want_write(file->f_path.mnt);
  1524. if (ret)
  1525. goto out;
  1526. mutex_lock(&root->fs_info->trans_mutex);
  1527. root->fs_info->open_ioctl_trans++;
  1528. mutex_unlock(&root->fs_info->trans_mutex);
  1529. ret = -ENOMEM;
  1530. trans = btrfs_start_ioctl_transaction(root, 0);
  1531. if (!trans)
  1532. goto out_drop;
  1533. file->private_data = trans;
  1534. return 0;
  1535. out_drop:
  1536. mutex_lock(&root->fs_info->trans_mutex);
  1537. root->fs_info->open_ioctl_trans--;
  1538. mutex_unlock(&root->fs_info->trans_mutex);
  1539. mnt_drop_write(file->f_path.mnt);
  1540. out:
  1541. return ret;
  1542. }
  1543. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1544. {
  1545. struct inode *inode = fdentry(file)->d_inode;
  1546. struct btrfs_root *root = BTRFS_I(inode)->root;
  1547. struct btrfs_root *new_root;
  1548. struct btrfs_dir_item *di;
  1549. struct btrfs_trans_handle *trans;
  1550. struct btrfs_path *path;
  1551. struct btrfs_key location;
  1552. struct btrfs_disk_key disk_key;
  1553. struct btrfs_super_block *disk_super;
  1554. u64 features;
  1555. u64 objectid = 0;
  1556. u64 dir_id;
  1557. if (!capable(CAP_SYS_ADMIN))
  1558. return -EPERM;
  1559. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1560. return -EFAULT;
  1561. if (!objectid)
  1562. objectid = root->root_key.objectid;
  1563. location.objectid = objectid;
  1564. location.type = BTRFS_ROOT_ITEM_KEY;
  1565. location.offset = (u64)-1;
  1566. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1567. if (IS_ERR(new_root))
  1568. return PTR_ERR(new_root);
  1569. if (btrfs_root_refs(&new_root->root_item) == 0)
  1570. return -ENOENT;
  1571. path = btrfs_alloc_path();
  1572. if (!path)
  1573. return -ENOMEM;
  1574. path->leave_spinning = 1;
  1575. trans = btrfs_start_transaction(root, 1);
  1576. if (!trans) {
  1577. btrfs_free_path(path);
  1578. return -ENOMEM;
  1579. }
  1580. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1581. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1582. dir_id, "default", 7, 1);
  1583. if (IS_ERR_OR_NULL(di)) {
  1584. btrfs_free_path(path);
  1585. btrfs_end_transaction(trans, root);
  1586. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1587. "this isn't going to work\n");
  1588. return -ENOENT;
  1589. }
  1590. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1591. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1592. btrfs_mark_buffer_dirty(path->nodes[0]);
  1593. btrfs_free_path(path);
  1594. disk_super = &root->fs_info->super_copy;
  1595. features = btrfs_super_incompat_flags(disk_super);
  1596. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1597. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1598. btrfs_set_super_incompat_flags(disk_super, features);
  1599. }
  1600. btrfs_end_transaction(trans, root);
  1601. return 0;
  1602. }
  1603. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1604. {
  1605. struct btrfs_ioctl_space_args space_args;
  1606. struct btrfs_ioctl_space_info space;
  1607. struct btrfs_ioctl_space_info *dest;
  1608. struct btrfs_ioctl_space_info *dest_orig;
  1609. struct btrfs_ioctl_space_info *user_dest;
  1610. struct btrfs_space_info *info;
  1611. int alloc_size;
  1612. int ret = 0;
  1613. int slot_count = 0;
  1614. if (copy_from_user(&space_args,
  1615. (struct btrfs_ioctl_space_args __user *)arg,
  1616. sizeof(space_args)))
  1617. return -EFAULT;
  1618. /* first we count slots */
  1619. rcu_read_lock();
  1620. list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
  1621. slot_count++;
  1622. rcu_read_unlock();
  1623. /* space_slots == 0 means they are asking for a count */
  1624. if (space_args.space_slots == 0) {
  1625. space_args.total_spaces = slot_count;
  1626. goto out;
  1627. }
  1628. alloc_size = sizeof(*dest) * slot_count;
  1629. /* we generally have at most 6 or so space infos, one for each raid
  1630. * level. So, a whole page should be more than enough for everyone
  1631. */
  1632. if (alloc_size > PAGE_CACHE_SIZE)
  1633. return -ENOMEM;
  1634. space_args.total_spaces = 0;
  1635. dest = kmalloc(alloc_size, GFP_NOFS);
  1636. if (!dest)
  1637. return -ENOMEM;
  1638. dest_orig = dest;
  1639. /* now we have a buffer to copy into */
  1640. rcu_read_lock();
  1641. list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
  1642. /* make sure we don't copy more than we allocated
  1643. * in our buffer
  1644. */
  1645. if (slot_count == 0)
  1646. break;
  1647. slot_count--;
  1648. /* make sure userland has enough room in their buffer */
  1649. if (space_args.total_spaces >= space_args.space_slots)
  1650. break;
  1651. space.flags = info->flags;
  1652. space.total_bytes = info->total_bytes;
  1653. space.used_bytes = info->bytes_used;
  1654. memcpy(dest, &space, sizeof(space));
  1655. dest++;
  1656. space_args.total_spaces++;
  1657. }
  1658. rcu_read_unlock();
  1659. user_dest = (struct btrfs_ioctl_space_info *)
  1660. (arg + sizeof(struct btrfs_ioctl_space_args));
  1661. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1662. ret = -EFAULT;
  1663. kfree(dest_orig);
  1664. out:
  1665. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1666. ret = -EFAULT;
  1667. return ret;
  1668. }
  1669. /*
  1670. * there are many ways the trans_start and trans_end ioctls can lead
  1671. * to deadlocks. They should only be used by applications that
  1672. * basically own the machine, and have a very in depth understanding
  1673. * of all the possible deadlocks and enospc problems.
  1674. */
  1675. long btrfs_ioctl_trans_end(struct file *file)
  1676. {
  1677. struct inode *inode = fdentry(file)->d_inode;
  1678. struct btrfs_root *root = BTRFS_I(inode)->root;
  1679. struct btrfs_trans_handle *trans;
  1680. trans = file->private_data;
  1681. if (!trans)
  1682. return -EINVAL;
  1683. file->private_data = NULL;
  1684. btrfs_end_transaction(trans, root);
  1685. mutex_lock(&root->fs_info->trans_mutex);
  1686. root->fs_info->open_ioctl_trans--;
  1687. mutex_unlock(&root->fs_info->trans_mutex);
  1688. mnt_drop_write(file->f_path.mnt);
  1689. return 0;
  1690. }
  1691. long btrfs_ioctl(struct file *file, unsigned int
  1692. cmd, unsigned long arg)
  1693. {
  1694. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1695. void __user *argp = (void __user *)arg;
  1696. switch (cmd) {
  1697. case FS_IOC_GETFLAGS:
  1698. return btrfs_ioctl_getflags(file, argp);
  1699. case FS_IOC_SETFLAGS:
  1700. return btrfs_ioctl_setflags(file, argp);
  1701. case FS_IOC_GETVERSION:
  1702. return btrfs_ioctl_getversion(file, argp);
  1703. case BTRFS_IOC_SNAP_CREATE:
  1704. return btrfs_ioctl_snap_create(file, argp, 0);
  1705. case BTRFS_IOC_SUBVOL_CREATE:
  1706. return btrfs_ioctl_snap_create(file, argp, 1);
  1707. case BTRFS_IOC_SNAP_DESTROY:
  1708. return btrfs_ioctl_snap_destroy(file, argp);
  1709. case BTRFS_IOC_DEFAULT_SUBVOL:
  1710. return btrfs_ioctl_default_subvol(file, argp);
  1711. case BTRFS_IOC_DEFRAG:
  1712. return btrfs_ioctl_defrag(file, NULL);
  1713. case BTRFS_IOC_DEFRAG_RANGE:
  1714. return btrfs_ioctl_defrag(file, argp);
  1715. case BTRFS_IOC_RESIZE:
  1716. return btrfs_ioctl_resize(root, argp);
  1717. case BTRFS_IOC_ADD_DEV:
  1718. return btrfs_ioctl_add_dev(root, argp);
  1719. case BTRFS_IOC_RM_DEV:
  1720. return btrfs_ioctl_rm_dev(root, argp);
  1721. case BTRFS_IOC_BALANCE:
  1722. return btrfs_balance(root->fs_info->dev_root);
  1723. case BTRFS_IOC_CLONE:
  1724. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1725. case BTRFS_IOC_CLONE_RANGE:
  1726. return btrfs_ioctl_clone_range(file, argp);
  1727. case BTRFS_IOC_TRANS_START:
  1728. return btrfs_ioctl_trans_start(file);
  1729. case BTRFS_IOC_TRANS_END:
  1730. return btrfs_ioctl_trans_end(file);
  1731. case BTRFS_IOC_TREE_SEARCH:
  1732. return btrfs_ioctl_tree_search(file, argp);
  1733. case BTRFS_IOC_INO_LOOKUP:
  1734. return btrfs_ioctl_ino_lookup(file, argp);
  1735. case BTRFS_IOC_SPACE_INFO:
  1736. return btrfs_ioctl_space_info(root, argp);
  1737. case BTRFS_IOC_SYNC:
  1738. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1739. return 0;
  1740. }
  1741. return -ENOTTY;
  1742. }