ioctl.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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 "compat.h"
  42. #include "ctree.h"
  43. #include "disk-io.h"
  44. #include "transaction.h"
  45. #include "btrfs_inode.h"
  46. #include "ioctl.h"
  47. #include "print-tree.h"
  48. #include "volumes.h"
  49. #include "locking.h"
  50. #include "ctree.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. /*
  211. * 1 - inode item
  212. * 2 - refs
  213. * 1 - root item
  214. * 2 - dir items
  215. */
  216. ret = btrfs_reserve_metadata_space(root, 6);
  217. if (ret)
  218. return ret;
  219. trans = btrfs_start_transaction(root, 1);
  220. BUG_ON(!trans);
  221. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  222. 0, &objectid);
  223. if (ret)
  224. goto fail;
  225. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  226. 0, objectid, NULL, 0, 0, 0);
  227. if (IS_ERR(leaf)) {
  228. ret = PTR_ERR(leaf);
  229. goto fail;
  230. }
  231. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  232. btrfs_set_header_bytenr(leaf, leaf->start);
  233. btrfs_set_header_generation(leaf, trans->transid);
  234. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  235. btrfs_set_header_owner(leaf, objectid);
  236. write_extent_buffer(leaf, root->fs_info->fsid,
  237. (unsigned long)btrfs_header_fsid(leaf),
  238. BTRFS_FSID_SIZE);
  239. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  240. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  241. BTRFS_UUID_SIZE);
  242. btrfs_mark_buffer_dirty(leaf);
  243. inode_item = &root_item.inode;
  244. memset(inode_item, 0, sizeof(*inode_item));
  245. inode_item->generation = cpu_to_le64(1);
  246. inode_item->size = cpu_to_le64(3);
  247. inode_item->nlink = cpu_to_le32(1);
  248. inode_item->nbytes = cpu_to_le64(root->leafsize);
  249. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  250. btrfs_set_root_bytenr(&root_item, leaf->start);
  251. btrfs_set_root_generation(&root_item, trans->transid);
  252. btrfs_set_root_level(&root_item, 0);
  253. btrfs_set_root_refs(&root_item, 1);
  254. btrfs_set_root_used(&root_item, leaf->len);
  255. btrfs_set_root_last_snapshot(&root_item, 0);
  256. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  257. root_item.drop_level = 0;
  258. btrfs_tree_unlock(leaf);
  259. free_extent_buffer(leaf);
  260. leaf = NULL;
  261. btrfs_set_root_dirid(&root_item, new_dirid);
  262. key.objectid = objectid;
  263. key.offset = 0;
  264. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  265. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  266. &root_item);
  267. if (ret)
  268. goto fail;
  269. key.offset = (u64)-1;
  270. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  271. BUG_ON(IS_ERR(new_root));
  272. btrfs_record_root_in_trans(trans, new_root);
  273. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  274. BTRFS_I(dir)->block_group);
  275. /*
  276. * insert the directory item
  277. */
  278. ret = btrfs_set_inode_index(dir, &index);
  279. BUG_ON(ret);
  280. ret = btrfs_insert_dir_item(trans, root,
  281. name, namelen, dir->i_ino, &key,
  282. BTRFS_FT_DIR, index);
  283. if (ret)
  284. goto fail;
  285. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  286. ret = btrfs_update_inode(trans, root, dir);
  287. BUG_ON(ret);
  288. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  289. objectid, root->root_key.objectid,
  290. dir->i_ino, index, name, namelen);
  291. BUG_ON(ret);
  292. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  293. fail:
  294. err = btrfs_commit_transaction(trans, root);
  295. if (err && !ret)
  296. ret = err;
  297. btrfs_unreserve_metadata_space(root, 6);
  298. return ret;
  299. }
  300. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  301. char *name, int namelen)
  302. {
  303. struct inode *inode;
  304. struct btrfs_pending_snapshot *pending_snapshot;
  305. struct btrfs_trans_handle *trans;
  306. int ret;
  307. if (!root->ref_cows)
  308. return -EINVAL;
  309. /*
  310. * 1 - inode item
  311. * 2 - refs
  312. * 1 - root item
  313. * 2 - dir items
  314. */
  315. ret = btrfs_reserve_metadata_space(root, 6);
  316. if (ret)
  317. goto fail;
  318. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  319. if (!pending_snapshot) {
  320. ret = -ENOMEM;
  321. btrfs_unreserve_metadata_space(root, 6);
  322. goto fail;
  323. }
  324. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  325. if (!pending_snapshot->name) {
  326. ret = -ENOMEM;
  327. kfree(pending_snapshot);
  328. btrfs_unreserve_metadata_space(root, 6);
  329. goto fail;
  330. }
  331. memcpy(pending_snapshot->name, name, namelen);
  332. pending_snapshot->name[namelen] = '\0';
  333. pending_snapshot->dentry = dentry;
  334. trans = btrfs_start_transaction(root, 1);
  335. BUG_ON(!trans);
  336. pending_snapshot->root = root;
  337. list_add(&pending_snapshot->list,
  338. &trans->transaction->pending_snapshots);
  339. ret = btrfs_commit_transaction(trans, root);
  340. BUG_ON(ret);
  341. btrfs_unreserve_metadata_space(root, 6);
  342. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  343. if (IS_ERR(inode)) {
  344. ret = PTR_ERR(inode);
  345. goto fail;
  346. }
  347. BUG_ON(!inode);
  348. d_instantiate(dentry, inode);
  349. ret = 0;
  350. fail:
  351. return ret;
  352. }
  353. /* copy of may_create in fs/namei.c() */
  354. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  355. {
  356. if (child->d_inode)
  357. return -EEXIST;
  358. if (IS_DEADDIR(dir))
  359. return -ENOENT;
  360. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  361. }
  362. /*
  363. * Create a new subvolume below @parent. This is largely modeled after
  364. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  365. * inside this filesystem so it's quite a bit simpler.
  366. */
  367. static noinline int btrfs_mksubvol(struct path *parent,
  368. char *name, int namelen,
  369. struct btrfs_root *snap_src)
  370. {
  371. struct inode *dir = parent->dentry->d_inode;
  372. struct dentry *dentry;
  373. int error;
  374. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  375. dentry = lookup_one_len(name, parent->dentry, namelen);
  376. error = PTR_ERR(dentry);
  377. if (IS_ERR(dentry))
  378. goto out_unlock;
  379. error = -EEXIST;
  380. if (dentry->d_inode)
  381. goto out_dput;
  382. error = mnt_want_write(parent->mnt);
  383. if (error)
  384. goto out_dput;
  385. error = btrfs_may_create(dir, dentry);
  386. if (error)
  387. goto out_drop_write;
  388. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  389. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  390. goto out_up_read;
  391. if (snap_src) {
  392. error = create_snapshot(snap_src, dentry,
  393. name, namelen);
  394. } else {
  395. error = create_subvol(BTRFS_I(dir)->root, dentry,
  396. name, namelen);
  397. }
  398. if (!error)
  399. fsnotify_mkdir(dir, dentry);
  400. out_up_read:
  401. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  402. out_drop_write:
  403. mnt_drop_write(parent->mnt);
  404. out_dput:
  405. dput(dentry);
  406. out_unlock:
  407. mutex_unlock(&dir->i_mutex);
  408. return error;
  409. }
  410. static int btrfs_defrag_file(struct file *file)
  411. {
  412. struct inode *inode = fdentry(file)->d_inode;
  413. struct btrfs_root *root = BTRFS_I(inode)->root;
  414. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  415. struct btrfs_ordered_extent *ordered;
  416. struct page *page;
  417. unsigned long last_index;
  418. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  419. unsigned long total_read = 0;
  420. u64 page_start;
  421. u64 page_end;
  422. unsigned long i;
  423. int ret;
  424. ret = btrfs_check_data_free_space(root, inode, inode->i_size);
  425. if (ret)
  426. return -ENOSPC;
  427. mutex_lock(&inode->i_mutex);
  428. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  429. for (i = 0; i <= last_index; i++) {
  430. if (total_read % ra_pages == 0) {
  431. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  432. min(last_index, i + ra_pages - 1));
  433. }
  434. total_read++;
  435. again:
  436. page = grab_cache_page(inode->i_mapping, i);
  437. if (!page)
  438. goto out_unlock;
  439. if (!PageUptodate(page)) {
  440. btrfs_readpage(NULL, page);
  441. lock_page(page);
  442. if (!PageUptodate(page)) {
  443. unlock_page(page);
  444. page_cache_release(page);
  445. goto out_unlock;
  446. }
  447. }
  448. wait_on_page_writeback(page);
  449. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  450. page_end = page_start + PAGE_CACHE_SIZE - 1;
  451. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  452. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  453. if (ordered) {
  454. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  455. unlock_page(page);
  456. page_cache_release(page);
  457. btrfs_start_ordered_extent(inode, ordered, 1);
  458. btrfs_put_ordered_extent(ordered);
  459. goto again;
  460. }
  461. set_page_extent_mapped(page);
  462. /*
  463. * this makes sure page_mkwrite is called on the
  464. * page if it is dirtied again later
  465. */
  466. clear_page_dirty_for_io(page);
  467. btrfs_set_extent_delalloc(inode, page_start, page_end);
  468. set_page_dirty(page);
  469. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  470. unlock_page(page);
  471. page_cache_release(page);
  472. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  473. }
  474. out_unlock:
  475. mutex_unlock(&inode->i_mutex);
  476. return 0;
  477. }
  478. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  479. void __user *arg)
  480. {
  481. u64 new_size;
  482. u64 old_size;
  483. u64 devid = 1;
  484. struct btrfs_ioctl_vol_args *vol_args;
  485. struct btrfs_trans_handle *trans;
  486. struct btrfs_device *device = NULL;
  487. char *sizestr;
  488. char *devstr = NULL;
  489. int ret = 0;
  490. int namelen;
  491. int mod = 0;
  492. if (root->fs_info->sb->s_flags & MS_RDONLY)
  493. return -EROFS;
  494. if (!capable(CAP_SYS_ADMIN))
  495. return -EPERM;
  496. vol_args = memdup_user(arg, sizeof(*vol_args));
  497. if (IS_ERR(vol_args))
  498. return PTR_ERR(vol_args);
  499. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  500. namelen = strlen(vol_args->name);
  501. mutex_lock(&root->fs_info->volume_mutex);
  502. sizestr = vol_args->name;
  503. devstr = strchr(sizestr, ':');
  504. if (devstr) {
  505. char *end;
  506. sizestr = devstr + 1;
  507. *devstr = '\0';
  508. devstr = vol_args->name;
  509. devid = simple_strtoull(devstr, &end, 10);
  510. printk(KERN_INFO "resizing devid %llu\n",
  511. (unsigned long long)devid);
  512. }
  513. device = btrfs_find_device(root, devid, NULL, NULL);
  514. if (!device) {
  515. printk(KERN_INFO "resizer unable to find device %llu\n",
  516. (unsigned long long)devid);
  517. ret = -EINVAL;
  518. goto out_unlock;
  519. }
  520. if (!strcmp(sizestr, "max"))
  521. new_size = device->bdev->bd_inode->i_size;
  522. else {
  523. if (sizestr[0] == '-') {
  524. mod = -1;
  525. sizestr++;
  526. } else if (sizestr[0] == '+') {
  527. mod = 1;
  528. sizestr++;
  529. }
  530. new_size = btrfs_parse_size(sizestr);
  531. if (new_size == 0) {
  532. ret = -EINVAL;
  533. goto out_unlock;
  534. }
  535. }
  536. old_size = device->total_bytes;
  537. if (mod < 0) {
  538. if (new_size > old_size) {
  539. ret = -EINVAL;
  540. goto out_unlock;
  541. }
  542. new_size = old_size - new_size;
  543. } else if (mod > 0) {
  544. new_size = old_size + new_size;
  545. }
  546. if (new_size < 256 * 1024 * 1024) {
  547. ret = -EINVAL;
  548. goto out_unlock;
  549. }
  550. if (new_size > device->bdev->bd_inode->i_size) {
  551. ret = -EFBIG;
  552. goto out_unlock;
  553. }
  554. do_div(new_size, root->sectorsize);
  555. new_size *= root->sectorsize;
  556. printk(KERN_INFO "new size for %s is %llu\n",
  557. device->name, (unsigned long long)new_size);
  558. if (new_size > old_size) {
  559. trans = btrfs_start_transaction(root, 1);
  560. ret = btrfs_grow_device(trans, device, new_size);
  561. btrfs_commit_transaction(trans, root);
  562. } else {
  563. ret = btrfs_shrink_device(device, new_size);
  564. }
  565. out_unlock:
  566. mutex_unlock(&root->fs_info->volume_mutex);
  567. kfree(vol_args);
  568. return ret;
  569. }
  570. static noinline int btrfs_ioctl_snap_create(struct file *file,
  571. void __user *arg, int subvol)
  572. {
  573. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  574. struct btrfs_ioctl_vol_args *vol_args;
  575. struct file *src_file;
  576. int namelen;
  577. int ret = 0;
  578. if (root->fs_info->sb->s_flags & MS_RDONLY)
  579. return -EROFS;
  580. vol_args = memdup_user(arg, sizeof(*vol_args));
  581. if (IS_ERR(vol_args))
  582. return PTR_ERR(vol_args);
  583. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  584. namelen = strlen(vol_args->name);
  585. if (strchr(vol_args->name, '/')) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. if (subvol) {
  590. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  591. NULL);
  592. } else {
  593. struct inode *src_inode;
  594. src_file = fget(vol_args->fd);
  595. if (!src_file) {
  596. ret = -EINVAL;
  597. goto out;
  598. }
  599. src_inode = src_file->f_path.dentry->d_inode;
  600. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  601. printk(KERN_INFO "btrfs: Snapshot src from "
  602. "another FS\n");
  603. ret = -EINVAL;
  604. fput(src_file);
  605. goto out;
  606. }
  607. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  608. BTRFS_I(src_inode)->root);
  609. fput(src_file);
  610. }
  611. out:
  612. kfree(vol_args);
  613. return ret;
  614. }
  615. /*
  616. * helper to check if the subvolume references other subvolumes
  617. */
  618. static noinline int may_destroy_subvol(struct btrfs_root *root)
  619. {
  620. struct btrfs_path *path;
  621. struct btrfs_key key;
  622. int ret;
  623. path = btrfs_alloc_path();
  624. if (!path)
  625. return -ENOMEM;
  626. key.objectid = root->root_key.objectid;
  627. key.type = BTRFS_ROOT_REF_KEY;
  628. key.offset = (u64)-1;
  629. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  630. &key, path, 0, 0);
  631. if (ret < 0)
  632. goto out;
  633. BUG_ON(ret == 0);
  634. ret = 0;
  635. if (path->slots[0] > 0) {
  636. path->slots[0]--;
  637. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  638. if (key.objectid == root->root_key.objectid &&
  639. key.type == BTRFS_ROOT_REF_KEY)
  640. ret = -ENOTEMPTY;
  641. }
  642. out:
  643. btrfs_free_path(path);
  644. return ret;
  645. }
  646. static noinline int key_in_sk(struct btrfs_key *key,
  647. struct btrfs_ioctl_search_key *sk)
  648. {
  649. if (key->objectid < sk->min_objectid)
  650. return 0;
  651. if (key->offset < sk->min_offset)
  652. return 0;
  653. if (key->type < sk->min_type)
  654. return 0;
  655. if (key->objectid > sk->max_objectid)
  656. return 0;
  657. if (key->type > sk->max_type)
  658. return 0;
  659. if (key->offset > sk->max_offset)
  660. return 0;
  661. return 1;
  662. }
  663. static noinline int copy_to_sk(struct btrfs_root *root,
  664. struct btrfs_path *path,
  665. struct btrfs_key *key,
  666. struct btrfs_ioctl_search_key *sk,
  667. char *buf,
  668. unsigned long *sk_offset,
  669. int *num_found)
  670. {
  671. u64 found_transid;
  672. struct extent_buffer *leaf;
  673. struct btrfs_ioctl_search_header sh;
  674. unsigned long item_off;
  675. unsigned long item_len;
  676. int nritems;
  677. int i;
  678. int slot;
  679. int found = 0;
  680. int ret = 0;
  681. leaf = path->nodes[0];
  682. slot = path->slots[0];
  683. nritems = btrfs_header_nritems(leaf);
  684. if (btrfs_header_generation(leaf) > sk->max_transid) {
  685. i = nritems;
  686. goto advance_key;
  687. }
  688. found_transid = btrfs_header_generation(leaf);
  689. for (i = slot; i < nritems; i++) {
  690. item_off = btrfs_item_ptr_offset(leaf, i);
  691. item_len = btrfs_item_size_nr(leaf, i);
  692. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  693. item_len = 0;
  694. if (sizeof(sh) + item_len + *sk_offset >
  695. BTRFS_SEARCH_ARGS_BUFSIZE) {
  696. ret = 1;
  697. goto overflow;
  698. }
  699. btrfs_item_key_to_cpu(leaf, key, i);
  700. if (!key_in_sk(key, sk))
  701. continue;
  702. sh.objectid = key->objectid;
  703. sh.offset = key->offset;
  704. sh.type = key->type;
  705. sh.len = item_len;
  706. sh.transid = found_transid;
  707. /* copy search result header */
  708. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  709. *sk_offset += sizeof(sh);
  710. if (item_len) {
  711. char *p = buf + *sk_offset;
  712. /* copy the item */
  713. read_extent_buffer(leaf, p,
  714. item_off, item_len);
  715. *sk_offset += item_len;
  716. found++;
  717. }
  718. if (*num_found >= sk->nr_items)
  719. break;
  720. }
  721. advance_key:
  722. if (key->offset < (u64)-1)
  723. key->offset++;
  724. else if (key->type < (u64)-1)
  725. key->type++;
  726. else if (key->objectid < (u64)-1)
  727. key->objectid++;
  728. ret = 0;
  729. overflow:
  730. *num_found += found;
  731. return ret;
  732. }
  733. static noinline int search_ioctl(struct inode *inode,
  734. struct btrfs_ioctl_search_args *args)
  735. {
  736. struct btrfs_root *root;
  737. struct btrfs_key key;
  738. struct btrfs_key max_key;
  739. struct btrfs_path *path;
  740. struct btrfs_ioctl_search_key *sk = &args->key;
  741. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  742. int ret;
  743. int num_found = 0;
  744. unsigned long sk_offset = 0;
  745. path = btrfs_alloc_path();
  746. if (!path)
  747. return -ENOMEM;
  748. if (sk->tree_id == 0) {
  749. /* search the root of the inode that was passed */
  750. root = BTRFS_I(inode)->root;
  751. } else {
  752. key.objectid = sk->tree_id;
  753. key.type = BTRFS_ROOT_ITEM_KEY;
  754. key.offset = (u64)-1;
  755. root = btrfs_read_fs_root_no_name(info, &key);
  756. if (IS_ERR(root)) {
  757. printk(KERN_ERR "could not find root %llu\n",
  758. sk->tree_id);
  759. btrfs_free_path(path);
  760. return -ENOENT;
  761. }
  762. }
  763. key.objectid = sk->min_objectid;
  764. key.type = sk->min_type;
  765. key.offset = sk->min_offset;
  766. max_key.objectid = sk->max_objectid;
  767. max_key.type = sk->max_type;
  768. max_key.offset = sk->max_offset;
  769. path->keep_locks = 1;
  770. while(1) {
  771. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  772. sk->min_transid);
  773. if (ret != 0) {
  774. if (ret > 0)
  775. ret = 0;
  776. goto err;
  777. }
  778. ret = copy_to_sk(root, path, &key, sk, args->buf,
  779. &sk_offset, &num_found);
  780. btrfs_release_path(root, path);
  781. if (ret || num_found >= sk->nr_items)
  782. break;
  783. }
  784. ret = 0;
  785. err:
  786. sk->nr_items = num_found;
  787. btrfs_free_path(path);
  788. return ret;
  789. }
  790. static noinline int btrfs_ioctl_tree_search(struct file *file,
  791. void __user *argp)
  792. {
  793. struct btrfs_ioctl_search_args *args;
  794. struct inode *inode;
  795. int ret;
  796. if (!capable(CAP_SYS_ADMIN))
  797. return -EPERM;
  798. args = kmalloc(sizeof(*args), GFP_KERNEL);
  799. if (!args)
  800. return -ENOMEM;
  801. if (copy_from_user(args, argp, sizeof(*args))) {
  802. kfree(args);
  803. return -EFAULT;
  804. }
  805. inode = fdentry(file)->d_inode;
  806. ret = search_ioctl(inode, args);
  807. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  808. ret = -EFAULT;
  809. kfree(args);
  810. return ret;
  811. }
  812. /*
  813. * Search INODE_REFs to identify path name of 'dirid' directory
  814. * in a 'tree_id' tree. and sets path name to 'name'.
  815. */
  816. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  817. u64 tree_id, u64 dirid, char *name)
  818. {
  819. struct btrfs_root *root;
  820. struct btrfs_key key;
  821. char *ptr;
  822. int ret = -1;
  823. int slot;
  824. int len;
  825. int total_len = 0;
  826. struct btrfs_inode_ref *iref;
  827. struct extent_buffer *l;
  828. struct btrfs_path *path;
  829. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  830. name[0]='\0';
  831. return 0;
  832. }
  833. path = btrfs_alloc_path();
  834. if (!path)
  835. return -ENOMEM;
  836. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  837. key.objectid = tree_id;
  838. key.type = BTRFS_ROOT_ITEM_KEY;
  839. key.offset = (u64)-1;
  840. root = btrfs_read_fs_root_no_name(info, &key);
  841. if (IS_ERR(root)) {
  842. printk(KERN_ERR "could not find root %llu\n", tree_id);
  843. return -ENOENT;
  844. }
  845. key.objectid = dirid;
  846. key.type = BTRFS_INODE_REF_KEY;
  847. key.offset = 0;
  848. while(1) {
  849. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  850. if (ret < 0)
  851. goto out;
  852. l = path->nodes[0];
  853. slot = path->slots[0];
  854. btrfs_item_key_to_cpu(l, &key, slot);
  855. if (ret > 0 && (key.objectid != dirid ||
  856. key.type != BTRFS_INODE_REF_KEY)) {
  857. ret = -ENOENT;
  858. goto out;
  859. }
  860. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  861. len = btrfs_inode_ref_name_len(l, iref);
  862. ptr -= len + 1;
  863. total_len += len + 1;
  864. if (ptr < name)
  865. goto out;
  866. *(ptr + len) = '/';
  867. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  868. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  869. break;
  870. btrfs_release_path(root, path);
  871. key.objectid = key.offset;
  872. key.offset = 0;
  873. dirid = key.objectid;
  874. }
  875. if (ptr < name)
  876. goto out;
  877. memcpy(name, ptr, total_len);
  878. name[total_len]='\0';
  879. ret = 0;
  880. out:
  881. btrfs_free_path(path);
  882. return ret;
  883. }
  884. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  885. void __user *argp)
  886. {
  887. struct btrfs_ioctl_ino_lookup_args *args;
  888. struct inode *inode;
  889. int ret;
  890. if (!capable(CAP_SYS_ADMIN))
  891. return -EPERM;
  892. args = kmalloc(sizeof(*args), GFP_KERNEL);
  893. if (copy_from_user(args, argp, sizeof(*args))) {
  894. kfree(args);
  895. return -EFAULT;
  896. }
  897. inode = fdentry(file)->d_inode;
  898. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  899. args->treeid, args->objectid,
  900. args->name);
  901. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  902. ret = -EFAULT;
  903. kfree(args);
  904. return ret;
  905. }
  906. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  907. void __user *arg)
  908. {
  909. struct dentry *parent = fdentry(file);
  910. struct dentry *dentry;
  911. struct inode *dir = parent->d_inode;
  912. struct inode *inode;
  913. struct btrfs_root *root = BTRFS_I(dir)->root;
  914. struct btrfs_root *dest = NULL;
  915. struct btrfs_ioctl_vol_args *vol_args;
  916. struct btrfs_trans_handle *trans;
  917. int namelen;
  918. int ret;
  919. int err = 0;
  920. if (!capable(CAP_SYS_ADMIN))
  921. return -EPERM;
  922. vol_args = memdup_user(arg, sizeof(*vol_args));
  923. if (IS_ERR(vol_args))
  924. return PTR_ERR(vol_args);
  925. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  926. namelen = strlen(vol_args->name);
  927. if (strchr(vol_args->name, '/') ||
  928. strncmp(vol_args->name, "..", namelen) == 0) {
  929. err = -EINVAL;
  930. goto out;
  931. }
  932. err = mnt_want_write(file->f_path.mnt);
  933. if (err)
  934. goto out;
  935. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  936. dentry = lookup_one_len(vol_args->name, parent, namelen);
  937. if (IS_ERR(dentry)) {
  938. err = PTR_ERR(dentry);
  939. goto out_unlock_dir;
  940. }
  941. if (!dentry->d_inode) {
  942. err = -ENOENT;
  943. goto out_dput;
  944. }
  945. inode = dentry->d_inode;
  946. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  947. err = -EINVAL;
  948. goto out_dput;
  949. }
  950. dest = BTRFS_I(inode)->root;
  951. mutex_lock(&inode->i_mutex);
  952. err = d_invalidate(dentry);
  953. if (err)
  954. goto out_unlock;
  955. down_write(&root->fs_info->subvol_sem);
  956. err = may_destroy_subvol(dest);
  957. if (err)
  958. goto out_up_write;
  959. trans = btrfs_start_transaction(root, 1);
  960. ret = btrfs_unlink_subvol(trans, root, dir,
  961. dest->root_key.objectid,
  962. dentry->d_name.name,
  963. dentry->d_name.len);
  964. BUG_ON(ret);
  965. btrfs_record_root_in_trans(trans, dest);
  966. memset(&dest->root_item.drop_progress, 0,
  967. sizeof(dest->root_item.drop_progress));
  968. dest->root_item.drop_level = 0;
  969. btrfs_set_root_refs(&dest->root_item, 0);
  970. ret = btrfs_insert_orphan_item(trans,
  971. root->fs_info->tree_root,
  972. dest->root_key.objectid);
  973. BUG_ON(ret);
  974. ret = btrfs_commit_transaction(trans, root);
  975. BUG_ON(ret);
  976. inode->i_flags |= S_DEAD;
  977. out_up_write:
  978. up_write(&root->fs_info->subvol_sem);
  979. out_unlock:
  980. mutex_unlock(&inode->i_mutex);
  981. if (!err) {
  982. shrink_dcache_sb(root->fs_info->sb);
  983. btrfs_invalidate_inodes(dest);
  984. d_delete(dentry);
  985. }
  986. out_dput:
  987. dput(dentry);
  988. out_unlock_dir:
  989. mutex_unlock(&dir->i_mutex);
  990. mnt_drop_write(file->f_path.mnt);
  991. out:
  992. kfree(vol_args);
  993. return err;
  994. }
  995. static int btrfs_ioctl_defrag(struct file *file)
  996. {
  997. struct inode *inode = fdentry(file)->d_inode;
  998. struct btrfs_root *root = BTRFS_I(inode)->root;
  999. int ret;
  1000. ret = mnt_want_write(file->f_path.mnt);
  1001. if (ret)
  1002. return ret;
  1003. switch (inode->i_mode & S_IFMT) {
  1004. case S_IFDIR:
  1005. if (!capable(CAP_SYS_ADMIN)) {
  1006. ret = -EPERM;
  1007. goto out;
  1008. }
  1009. btrfs_defrag_root(root, 0);
  1010. btrfs_defrag_root(root->fs_info->extent_root, 0);
  1011. break;
  1012. case S_IFREG:
  1013. if (!(file->f_mode & FMODE_WRITE)) {
  1014. ret = -EINVAL;
  1015. goto out;
  1016. }
  1017. btrfs_defrag_file(file);
  1018. break;
  1019. }
  1020. out:
  1021. mnt_drop_write(file->f_path.mnt);
  1022. return ret;
  1023. }
  1024. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1025. {
  1026. struct btrfs_ioctl_vol_args *vol_args;
  1027. int ret;
  1028. if (!capable(CAP_SYS_ADMIN))
  1029. return -EPERM;
  1030. vol_args = memdup_user(arg, sizeof(*vol_args));
  1031. if (IS_ERR(vol_args))
  1032. return PTR_ERR(vol_args);
  1033. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1034. ret = btrfs_init_new_device(root, vol_args->name);
  1035. kfree(vol_args);
  1036. return ret;
  1037. }
  1038. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1039. {
  1040. struct btrfs_ioctl_vol_args *vol_args;
  1041. int ret;
  1042. if (!capable(CAP_SYS_ADMIN))
  1043. return -EPERM;
  1044. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1045. return -EROFS;
  1046. vol_args = memdup_user(arg, sizeof(*vol_args));
  1047. if (IS_ERR(vol_args))
  1048. return PTR_ERR(vol_args);
  1049. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1050. ret = btrfs_rm_device(root, vol_args->name);
  1051. kfree(vol_args);
  1052. return ret;
  1053. }
  1054. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1055. u64 off, u64 olen, u64 destoff)
  1056. {
  1057. struct inode *inode = fdentry(file)->d_inode;
  1058. struct btrfs_root *root = BTRFS_I(inode)->root;
  1059. struct file *src_file;
  1060. struct inode *src;
  1061. struct btrfs_trans_handle *trans;
  1062. struct btrfs_path *path;
  1063. struct extent_buffer *leaf;
  1064. char *buf;
  1065. struct btrfs_key key;
  1066. u32 nritems;
  1067. int slot;
  1068. int ret;
  1069. u64 len = olen;
  1070. u64 bs = root->fs_info->sb->s_blocksize;
  1071. u64 hint_byte;
  1072. /*
  1073. * TODO:
  1074. * - split compressed inline extents. annoying: we need to
  1075. * decompress into destination's address_space (the file offset
  1076. * may change, so source mapping won't do), then recompress (or
  1077. * otherwise reinsert) a subrange.
  1078. * - allow ranges within the same file to be cloned (provided
  1079. * they don't overlap)?
  1080. */
  1081. /* the destination must be opened for writing */
  1082. if (!(file->f_mode & FMODE_WRITE))
  1083. return -EINVAL;
  1084. ret = mnt_want_write(file->f_path.mnt);
  1085. if (ret)
  1086. return ret;
  1087. src_file = fget(srcfd);
  1088. if (!src_file) {
  1089. ret = -EBADF;
  1090. goto out_drop_write;
  1091. }
  1092. src = src_file->f_dentry->d_inode;
  1093. ret = -EINVAL;
  1094. if (src == inode)
  1095. goto out_fput;
  1096. ret = -EISDIR;
  1097. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1098. goto out_fput;
  1099. ret = -EXDEV;
  1100. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1101. goto out_fput;
  1102. ret = -ENOMEM;
  1103. buf = vmalloc(btrfs_level_size(root, 0));
  1104. if (!buf)
  1105. goto out_fput;
  1106. path = btrfs_alloc_path();
  1107. if (!path) {
  1108. vfree(buf);
  1109. goto out_fput;
  1110. }
  1111. path->reada = 2;
  1112. if (inode < src) {
  1113. mutex_lock(&inode->i_mutex);
  1114. mutex_lock(&src->i_mutex);
  1115. } else {
  1116. mutex_lock(&src->i_mutex);
  1117. mutex_lock(&inode->i_mutex);
  1118. }
  1119. /* determine range to clone */
  1120. ret = -EINVAL;
  1121. if (off >= src->i_size || off + len > src->i_size)
  1122. goto out_unlock;
  1123. if (len == 0)
  1124. olen = len = src->i_size - off;
  1125. /* if we extend to eof, continue to block boundary */
  1126. if (off + len == src->i_size)
  1127. len = ((src->i_size + bs-1) & ~(bs-1))
  1128. - off;
  1129. /* verify the end result is block aligned */
  1130. if ((off & (bs-1)) ||
  1131. ((off + len) & (bs-1)))
  1132. goto out_unlock;
  1133. /* do any pending delalloc/csum calc on src, one way or
  1134. another, and lock file content */
  1135. while (1) {
  1136. struct btrfs_ordered_extent *ordered;
  1137. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1138. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  1139. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  1140. break;
  1141. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1142. if (ordered)
  1143. btrfs_put_ordered_extent(ordered);
  1144. btrfs_wait_ordered_range(src, off, off+len);
  1145. }
  1146. trans = btrfs_start_transaction(root, 1);
  1147. BUG_ON(!trans);
  1148. /* punch hole in destination first */
  1149. btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
  1150. /* clone data */
  1151. key.objectid = src->i_ino;
  1152. key.type = BTRFS_EXTENT_DATA_KEY;
  1153. key.offset = 0;
  1154. while (1) {
  1155. /*
  1156. * note the key will change type as we walk through the
  1157. * tree.
  1158. */
  1159. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  1160. if (ret < 0)
  1161. goto out;
  1162. nritems = btrfs_header_nritems(path->nodes[0]);
  1163. if (path->slots[0] >= nritems) {
  1164. ret = btrfs_next_leaf(root, path);
  1165. if (ret < 0)
  1166. goto out;
  1167. if (ret > 0)
  1168. break;
  1169. nritems = btrfs_header_nritems(path->nodes[0]);
  1170. }
  1171. leaf = path->nodes[0];
  1172. slot = path->slots[0];
  1173. btrfs_item_key_to_cpu(leaf, &key, slot);
  1174. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1175. key.objectid != src->i_ino)
  1176. break;
  1177. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1178. struct btrfs_file_extent_item *extent;
  1179. int type;
  1180. u32 size;
  1181. struct btrfs_key new_key;
  1182. u64 disko = 0, diskl = 0;
  1183. u64 datao = 0, datal = 0;
  1184. u8 comp;
  1185. size = btrfs_item_size_nr(leaf, slot);
  1186. read_extent_buffer(leaf, buf,
  1187. btrfs_item_ptr_offset(leaf, slot),
  1188. size);
  1189. extent = btrfs_item_ptr(leaf, slot,
  1190. struct btrfs_file_extent_item);
  1191. comp = btrfs_file_extent_compression(leaf, extent);
  1192. type = btrfs_file_extent_type(leaf, extent);
  1193. if (type == BTRFS_FILE_EXTENT_REG ||
  1194. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1195. disko = btrfs_file_extent_disk_bytenr(leaf,
  1196. extent);
  1197. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1198. extent);
  1199. datao = btrfs_file_extent_offset(leaf, extent);
  1200. datal = btrfs_file_extent_num_bytes(leaf,
  1201. extent);
  1202. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1203. /* take upper bound, may be compressed */
  1204. datal = btrfs_file_extent_ram_bytes(leaf,
  1205. extent);
  1206. }
  1207. btrfs_release_path(root, path);
  1208. if (key.offset + datal < off ||
  1209. key.offset >= off+len)
  1210. goto next;
  1211. memcpy(&new_key, &key, sizeof(new_key));
  1212. new_key.objectid = inode->i_ino;
  1213. new_key.offset = key.offset + destoff - off;
  1214. if (type == BTRFS_FILE_EXTENT_REG ||
  1215. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1216. ret = btrfs_insert_empty_item(trans, root, path,
  1217. &new_key, size);
  1218. if (ret)
  1219. goto out;
  1220. leaf = path->nodes[0];
  1221. slot = path->slots[0];
  1222. write_extent_buffer(leaf, buf,
  1223. btrfs_item_ptr_offset(leaf, slot),
  1224. size);
  1225. extent = btrfs_item_ptr(leaf, slot,
  1226. struct btrfs_file_extent_item);
  1227. if (off > key.offset) {
  1228. datao += off - key.offset;
  1229. datal -= off - key.offset;
  1230. }
  1231. if (key.offset + datal > off + len)
  1232. datal = off + len - key.offset;
  1233. /* disko == 0 means it's a hole */
  1234. if (!disko)
  1235. datao = 0;
  1236. btrfs_set_file_extent_offset(leaf, extent,
  1237. datao);
  1238. btrfs_set_file_extent_num_bytes(leaf, extent,
  1239. datal);
  1240. if (disko) {
  1241. inode_add_bytes(inode, datal);
  1242. ret = btrfs_inc_extent_ref(trans, root,
  1243. disko, diskl, 0,
  1244. root->root_key.objectid,
  1245. inode->i_ino,
  1246. new_key.offset - datao);
  1247. BUG_ON(ret);
  1248. }
  1249. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1250. u64 skip = 0;
  1251. u64 trim = 0;
  1252. if (off > key.offset) {
  1253. skip = off - key.offset;
  1254. new_key.offset += skip;
  1255. }
  1256. if (key.offset + datal > off+len)
  1257. trim = key.offset + datal - (off+len);
  1258. if (comp && (skip || trim)) {
  1259. ret = -EINVAL;
  1260. goto out;
  1261. }
  1262. size -= skip + trim;
  1263. datal -= skip + trim;
  1264. ret = btrfs_insert_empty_item(trans, root, path,
  1265. &new_key, size);
  1266. if (ret)
  1267. goto out;
  1268. if (skip) {
  1269. u32 start =
  1270. btrfs_file_extent_calc_inline_size(0);
  1271. memmove(buf+start, buf+start+skip,
  1272. datal);
  1273. }
  1274. leaf = path->nodes[0];
  1275. slot = path->slots[0];
  1276. write_extent_buffer(leaf, buf,
  1277. btrfs_item_ptr_offset(leaf, slot),
  1278. size);
  1279. inode_add_bytes(inode, datal);
  1280. }
  1281. btrfs_mark_buffer_dirty(leaf);
  1282. }
  1283. next:
  1284. btrfs_release_path(root, path);
  1285. key.offset++;
  1286. }
  1287. ret = 0;
  1288. out:
  1289. btrfs_release_path(root, path);
  1290. if (ret == 0) {
  1291. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1292. if (destoff + olen > inode->i_size)
  1293. btrfs_i_size_write(inode, destoff + olen);
  1294. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1295. ret = btrfs_update_inode(trans, root, inode);
  1296. }
  1297. btrfs_end_transaction(trans, root);
  1298. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1299. if (ret)
  1300. vmtruncate(inode, 0);
  1301. out_unlock:
  1302. mutex_unlock(&src->i_mutex);
  1303. mutex_unlock(&inode->i_mutex);
  1304. vfree(buf);
  1305. btrfs_free_path(path);
  1306. out_fput:
  1307. fput(src_file);
  1308. out_drop_write:
  1309. mnt_drop_write(file->f_path.mnt);
  1310. return ret;
  1311. }
  1312. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1313. {
  1314. struct btrfs_ioctl_clone_range_args args;
  1315. if (copy_from_user(&args, argp, sizeof(args)))
  1316. return -EFAULT;
  1317. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1318. args.src_length, args.dest_offset);
  1319. }
  1320. /*
  1321. * there are many ways the trans_start and trans_end ioctls can lead
  1322. * to deadlocks. They should only be used by applications that
  1323. * basically own the machine, and have a very in depth understanding
  1324. * of all the possible deadlocks and enospc problems.
  1325. */
  1326. static long btrfs_ioctl_trans_start(struct file *file)
  1327. {
  1328. struct inode *inode = fdentry(file)->d_inode;
  1329. struct btrfs_root *root = BTRFS_I(inode)->root;
  1330. struct btrfs_trans_handle *trans;
  1331. int ret;
  1332. ret = -EPERM;
  1333. if (!capable(CAP_SYS_ADMIN))
  1334. goto out;
  1335. ret = -EINPROGRESS;
  1336. if (file->private_data)
  1337. goto out;
  1338. ret = mnt_want_write(file->f_path.mnt);
  1339. if (ret)
  1340. goto out;
  1341. mutex_lock(&root->fs_info->trans_mutex);
  1342. root->fs_info->open_ioctl_trans++;
  1343. mutex_unlock(&root->fs_info->trans_mutex);
  1344. ret = -ENOMEM;
  1345. trans = btrfs_start_ioctl_transaction(root, 0);
  1346. if (!trans)
  1347. goto out_drop;
  1348. file->private_data = trans;
  1349. return 0;
  1350. out_drop:
  1351. mutex_lock(&root->fs_info->trans_mutex);
  1352. root->fs_info->open_ioctl_trans--;
  1353. mutex_unlock(&root->fs_info->trans_mutex);
  1354. mnt_drop_write(file->f_path.mnt);
  1355. out:
  1356. return ret;
  1357. }
  1358. /*
  1359. * there are many ways the trans_start and trans_end ioctls can lead
  1360. * to deadlocks. They should only be used by applications that
  1361. * basically own the machine, and have a very in depth understanding
  1362. * of all the possible deadlocks and enospc problems.
  1363. */
  1364. long btrfs_ioctl_trans_end(struct file *file)
  1365. {
  1366. struct inode *inode = fdentry(file)->d_inode;
  1367. struct btrfs_root *root = BTRFS_I(inode)->root;
  1368. struct btrfs_trans_handle *trans;
  1369. trans = file->private_data;
  1370. if (!trans)
  1371. return -EINVAL;
  1372. file->private_data = NULL;
  1373. btrfs_end_transaction(trans, root);
  1374. mutex_lock(&root->fs_info->trans_mutex);
  1375. root->fs_info->open_ioctl_trans--;
  1376. mutex_unlock(&root->fs_info->trans_mutex);
  1377. mnt_drop_write(file->f_path.mnt);
  1378. return 0;
  1379. }
  1380. long btrfs_ioctl(struct file *file, unsigned int
  1381. cmd, unsigned long arg)
  1382. {
  1383. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1384. void __user *argp = (void __user *)arg;
  1385. switch (cmd) {
  1386. case FS_IOC_GETFLAGS:
  1387. return btrfs_ioctl_getflags(file, argp);
  1388. case FS_IOC_SETFLAGS:
  1389. return btrfs_ioctl_setflags(file, argp);
  1390. case FS_IOC_GETVERSION:
  1391. return btrfs_ioctl_getversion(file, argp);
  1392. case BTRFS_IOC_SNAP_CREATE:
  1393. return btrfs_ioctl_snap_create(file, argp, 0);
  1394. case BTRFS_IOC_SUBVOL_CREATE:
  1395. return btrfs_ioctl_snap_create(file, argp, 1);
  1396. case BTRFS_IOC_SNAP_DESTROY:
  1397. return btrfs_ioctl_snap_destroy(file, argp);
  1398. case BTRFS_IOC_DEFRAG:
  1399. return btrfs_ioctl_defrag(file);
  1400. case BTRFS_IOC_RESIZE:
  1401. return btrfs_ioctl_resize(root, argp);
  1402. case BTRFS_IOC_ADD_DEV:
  1403. return btrfs_ioctl_add_dev(root, argp);
  1404. case BTRFS_IOC_RM_DEV:
  1405. return btrfs_ioctl_rm_dev(root, argp);
  1406. case BTRFS_IOC_BALANCE:
  1407. return btrfs_balance(root->fs_info->dev_root);
  1408. case BTRFS_IOC_CLONE:
  1409. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1410. case BTRFS_IOC_CLONE_RANGE:
  1411. return btrfs_ioctl_clone_range(file, argp);
  1412. case BTRFS_IOC_TRANS_START:
  1413. return btrfs_ioctl_trans_start(file);
  1414. case BTRFS_IOC_TRANS_END:
  1415. return btrfs_ioctl_trans_end(file);
  1416. case BTRFS_IOC_TREE_SEARCH:
  1417. return btrfs_ioctl_tree_search(file, argp);
  1418. case BTRFS_IOC_INO_LOOKUP:
  1419. return btrfs_ioctl_ino_lookup(file, argp);
  1420. case BTRFS_IOC_SYNC:
  1421. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1422. return 0;
  1423. }
  1424. return -ENOTTY;
  1425. }