ioctl.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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 should_defrag_range(struct inode *inode, u64 start, u64 len,
  411. u64 *last_len, u64 *skip, u64 *defrag_end)
  412. {
  413. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  414. struct extent_map *em = NULL;
  415. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  416. int ret = 1;
  417. /*
  418. * make sure that once we start defragging and extent, we keep on
  419. * defragging it
  420. */
  421. if (start < *defrag_end)
  422. return 1;
  423. *skip = 0;
  424. /*
  425. * hopefully we have this extent in the tree already, try without
  426. * the full extent lock
  427. */
  428. read_lock(&em_tree->lock);
  429. em = lookup_extent_mapping(em_tree, start, len);
  430. read_unlock(&em_tree->lock);
  431. if (!em) {
  432. /* get the big lock and read metadata off disk */
  433. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  434. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  435. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  436. if (!em)
  437. return 0;
  438. }
  439. /* this will cover holes, and inline extents */
  440. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  441. ret = 0;
  442. /*
  443. * we hit a real extent, if it is big don't bother defragging it again
  444. */
  445. if ((*last_len == 0 || *last_len >= 256 * 1024) &&
  446. em->len >= 256 * 1024)
  447. ret = 0;
  448. /*
  449. * last_len ends up being a counter of how many bytes we've defragged.
  450. * every time we choose not to defrag an extent, we reset *last_len
  451. * so that the next tiny extent will force a defrag.
  452. *
  453. * The end result of this is that tiny extents before a single big
  454. * extent will force at least part of that big extent to be defragged.
  455. */
  456. if (ret) {
  457. *last_len += len;
  458. *defrag_end = extent_map_end(em);
  459. } else {
  460. *last_len = 0;
  461. *skip = extent_map_end(em);
  462. *defrag_end = 0;
  463. }
  464. free_extent_map(em);
  465. return ret;
  466. }
  467. static int btrfs_defrag_file(struct file *file)
  468. {
  469. struct inode *inode = fdentry(file)->d_inode;
  470. struct btrfs_root *root = BTRFS_I(inode)->root;
  471. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  472. struct btrfs_ordered_extent *ordered;
  473. struct page *page;
  474. unsigned long last_index;
  475. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  476. unsigned long total_read = 0;
  477. u64 page_start;
  478. u64 page_end;
  479. u64 last_len = 0;
  480. u64 skip = 0;
  481. u64 defrag_end = 0;
  482. unsigned long i;
  483. int ret;
  484. if (inode->i_size == 0)
  485. return 0;
  486. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  487. i = 0;
  488. while (i <= last_index) {
  489. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  490. PAGE_CACHE_SIZE, &last_len, &skip,
  491. &defrag_end)) {
  492. unsigned long next;
  493. /*
  494. * the should_defrag function tells us how much to skip
  495. * bump our counter by the suggested amount
  496. */
  497. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  498. i = max(i + 1, next);
  499. continue;
  500. }
  501. if (total_read % ra_pages == 0) {
  502. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  503. min(last_index, i + ra_pages - 1));
  504. }
  505. total_read++;
  506. mutex_lock(&inode->i_mutex);
  507. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  508. if (ret) {
  509. ret = -ENOSPC;
  510. break;
  511. }
  512. ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  513. if (ret) {
  514. btrfs_free_reserved_data_space(root, inode,
  515. PAGE_CACHE_SIZE);
  516. ret = -ENOSPC;
  517. break;
  518. }
  519. again:
  520. if (inode->i_size == 0 ||
  521. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  522. ret = 0;
  523. goto err_reservations;
  524. }
  525. page = grab_cache_page(inode->i_mapping, i);
  526. if (!page)
  527. goto err_reservations;
  528. if (!PageUptodate(page)) {
  529. btrfs_readpage(NULL, page);
  530. lock_page(page);
  531. if (!PageUptodate(page)) {
  532. unlock_page(page);
  533. page_cache_release(page);
  534. goto err_reservations;
  535. }
  536. }
  537. if (page->mapping != inode->i_mapping) {
  538. unlock_page(page);
  539. page_cache_release(page);
  540. goto again;
  541. }
  542. wait_on_page_writeback(page);
  543. if (PageDirty(page)) {
  544. btrfs_free_reserved_data_space(root, inode,
  545. PAGE_CACHE_SIZE);
  546. goto loop_unlock;
  547. }
  548. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  549. page_end = page_start + PAGE_CACHE_SIZE - 1;
  550. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  551. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  552. if (ordered) {
  553. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  554. unlock_page(page);
  555. page_cache_release(page);
  556. btrfs_start_ordered_extent(inode, ordered, 1);
  557. btrfs_put_ordered_extent(ordered);
  558. goto again;
  559. }
  560. set_page_extent_mapped(page);
  561. /*
  562. * this makes sure page_mkwrite is called on the
  563. * page if it is dirtied again later
  564. */
  565. clear_page_dirty_for_io(page);
  566. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  567. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  568. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  569. btrfs_set_extent_delalloc(inode, page_start, page_end);
  570. ClearPageChecked(page);
  571. set_page_dirty(page);
  572. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  573. loop_unlock:
  574. unlock_page(page);
  575. page_cache_release(page);
  576. mutex_unlock(&inode->i_mutex);
  577. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  578. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  579. i++;
  580. }
  581. return 0;
  582. err_reservations:
  583. mutex_unlock(&inode->i_mutex);
  584. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  585. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  586. return ret;
  587. }
  588. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  589. void __user *arg)
  590. {
  591. u64 new_size;
  592. u64 old_size;
  593. u64 devid = 1;
  594. struct btrfs_ioctl_vol_args *vol_args;
  595. struct btrfs_trans_handle *trans;
  596. struct btrfs_device *device = NULL;
  597. char *sizestr;
  598. char *devstr = NULL;
  599. int ret = 0;
  600. int namelen;
  601. int mod = 0;
  602. if (root->fs_info->sb->s_flags & MS_RDONLY)
  603. return -EROFS;
  604. if (!capable(CAP_SYS_ADMIN))
  605. return -EPERM;
  606. vol_args = memdup_user(arg, sizeof(*vol_args));
  607. if (IS_ERR(vol_args))
  608. return PTR_ERR(vol_args);
  609. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  610. namelen = strlen(vol_args->name);
  611. mutex_lock(&root->fs_info->volume_mutex);
  612. sizestr = vol_args->name;
  613. devstr = strchr(sizestr, ':');
  614. if (devstr) {
  615. char *end;
  616. sizestr = devstr + 1;
  617. *devstr = '\0';
  618. devstr = vol_args->name;
  619. devid = simple_strtoull(devstr, &end, 10);
  620. printk(KERN_INFO "resizing devid %llu\n",
  621. (unsigned long long)devid);
  622. }
  623. device = btrfs_find_device(root, devid, NULL, NULL);
  624. if (!device) {
  625. printk(KERN_INFO "resizer unable to find device %llu\n",
  626. (unsigned long long)devid);
  627. ret = -EINVAL;
  628. goto out_unlock;
  629. }
  630. if (!strcmp(sizestr, "max"))
  631. new_size = device->bdev->bd_inode->i_size;
  632. else {
  633. if (sizestr[0] == '-') {
  634. mod = -1;
  635. sizestr++;
  636. } else if (sizestr[0] == '+') {
  637. mod = 1;
  638. sizestr++;
  639. }
  640. new_size = btrfs_parse_size(sizestr);
  641. if (new_size == 0) {
  642. ret = -EINVAL;
  643. goto out_unlock;
  644. }
  645. }
  646. old_size = device->total_bytes;
  647. if (mod < 0) {
  648. if (new_size > old_size) {
  649. ret = -EINVAL;
  650. goto out_unlock;
  651. }
  652. new_size = old_size - new_size;
  653. } else if (mod > 0) {
  654. new_size = old_size + new_size;
  655. }
  656. if (new_size < 256 * 1024 * 1024) {
  657. ret = -EINVAL;
  658. goto out_unlock;
  659. }
  660. if (new_size > device->bdev->bd_inode->i_size) {
  661. ret = -EFBIG;
  662. goto out_unlock;
  663. }
  664. do_div(new_size, root->sectorsize);
  665. new_size *= root->sectorsize;
  666. printk(KERN_INFO "new size for %s is %llu\n",
  667. device->name, (unsigned long long)new_size);
  668. if (new_size > old_size) {
  669. trans = btrfs_start_transaction(root, 1);
  670. ret = btrfs_grow_device(trans, device, new_size);
  671. btrfs_commit_transaction(trans, root);
  672. } else {
  673. ret = btrfs_shrink_device(device, new_size);
  674. }
  675. out_unlock:
  676. mutex_unlock(&root->fs_info->volume_mutex);
  677. kfree(vol_args);
  678. return ret;
  679. }
  680. static noinline int btrfs_ioctl_snap_create(struct file *file,
  681. void __user *arg, int subvol)
  682. {
  683. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  684. struct btrfs_ioctl_vol_args *vol_args;
  685. struct file *src_file;
  686. int namelen;
  687. int ret = 0;
  688. if (root->fs_info->sb->s_flags & MS_RDONLY)
  689. return -EROFS;
  690. vol_args = memdup_user(arg, sizeof(*vol_args));
  691. if (IS_ERR(vol_args))
  692. return PTR_ERR(vol_args);
  693. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  694. namelen = strlen(vol_args->name);
  695. if (strchr(vol_args->name, '/')) {
  696. ret = -EINVAL;
  697. goto out;
  698. }
  699. if (subvol) {
  700. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  701. NULL);
  702. } else {
  703. struct inode *src_inode;
  704. src_file = fget(vol_args->fd);
  705. if (!src_file) {
  706. ret = -EINVAL;
  707. goto out;
  708. }
  709. src_inode = src_file->f_path.dentry->d_inode;
  710. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  711. printk(KERN_INFO "btrfs: Snapshot src from "
  712. "another FS\n");
  713. ret = -EINVAL;
  714. fput(src_file);
  715. goto out;
  716. }
  717. ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
  718. BTRFS_I(src_inode)->root);
  719. fput(src_file);
  720. }
  721. out:
  722. kfree(vol_args);
  723. return ret;
  724. }
  725. /*
  726. * helper to check if the subvolume references other subvolumes
  727. */
  728. static noinline int may_destroy_subvol(struct btrfs_root *root)
  729. {
  730. struct btrfs_path *path;
  731. struct btrfs_key key;
  732. int ret;
  733. path = btrfs_alloc_path();
  734. if (!path)
  735. return -ENOMEM;
  736. key.objectid = root->root_key.objectid;
  737. key.type = BTRFS_ROOT_REF_KEY;
  738. key.offset = (u64)-1;
  739. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  740. &key, path, 0, 0);
  741. if (ret < 0)
  742. goto out;
  743. BUG_ON(ret == 0);
  744. ret = 0;
  745. if (path->slots[0] > 0) {
  746. path->slots[0]--;
  747. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  748. if (key.objectid == root->root_key.objectid &&
  749. key.type == BTRFS_ROOT_REF_KEY)
  750. ret = -ENOTEMPTY;
  751. }
  752. out:
  753. btrfs_free_path(path);
  754. return ret;
  755. }
  756. static noinline int key_in_sk(struct btrfs_key *key,
  757. struct btrfs_ioctl_search_key *sk)
  758. {
  759. if (key->objectid < sk->min_objectid)
  760. return 0;
  761. if (key->offset < sk->min_offset)
  762. return 0;
  763. if (key->type < sk->min_type)
  764. return 0;
  765. if (key->objectid > sk->max_objectid)
  766. return 0;
  767. if (key->type > sk->max_type)
  768. return 0;
  769. if (key->offset > sk->max_offset)
  770. return 0;
  771. return 1;
  772. }
  773. static noinline int copy_to_sk(struct btrfs_root *root,
  774. struct btrfs_path *path,
  775. struct btrfs_key *key,
  776. struct btrfs_ioctl_search_key *sk,
  777. char *buf,
  778. unsigned long *sk_offset,
  779. int *num_found)
  780. {
  781. u64 found_transid;
  782. struct extent_buffer *leaf;
  783. struct btrfs_ioctl_search_header sh;
  784. unsigned long item_off;
  785. unsigned long item_len;
  786. int nritems;
  787. int i;
  788. int slot;
  789. int found = 0;
  790. int ret = 0;
  791. leaf = path->nodes[0];
  792. slot = path->slots[0];
  793. nritems = btrfs_header_nritems(leaf);
  794. if (btrfs_header_generation(leaf) > sk->max_transid) {
  795. i = nritems;
  796. goto advance_key;
  797. }
  798. found_transid = btrfs_header_generation(leaf);
  799. for (i = slot; i < nritems; i++) {
  800. item_off = btrfs_item_ptr_offset(leaf, i);
  801. item_len = btrfs_item_size_nr(leaf, i);
  802. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  803. item_len = 0;
  804. if (sizeof(sh) + item_len + *sk_offset >
  805. BTRFS_SEARCH_ARGS_BUFSIZE) {
  806. ret = 1;
  807. goto overflow;
  808. }
  809. btrfs_item_key_to_cpu(leaf, key, i);
  810. if (!key_in_sk(key, sk))
  811. continue;
  812. sh.objectid = key->objectid;
  813. sh.offset = key->offset;
  814. sh.type = key->type;
  815. sh.len = item_len;
  816. sh.transid = found_transid;
  817. /* copy search result header */
  818. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  819. *sk_offset += sizeof(sh);
  820. if (item_len) {
  821. char *p = buf + *sk_offset;
  822. /* copy the item */
  823. read_extent_buffer(leaf, p,
  824. item_off, item_len);
  825. *sk_offset += item_len;
  826. found++;
  827. }
  828. if (*num_found >= sk->nr_items)
  829. break;
  830. }
  831. advance_key:
  832. if (key->offset < (u64)-1)
  833. key->offset++;
  834. else if (key->type < (u64)-1)
  835. key->type++;
  836. else if (key->objectid < (u64)-1)
  837. key->objectid++;
  838. ret = 0;
  839. overflow:
  840. *num_found += found;
  841. return ret;
  842. }
  843. static noinline int search_ioctl(struct inode *inode,
  844. struct btrfs_ioctl_search_args *args)
  845. {
  846. struct btrfs_root *root;
  847. struct btrfs_key key;
  848. struct btrfs_key max_key;
  849. struct btrfs_path *path;
  850. struct btrfs_ioctl_search_key *sk = &args->key;
  851. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  852. int ret;
  853. int num_found = 0;
  854. unsigned long sk_offset = 0;
  855. path = btrfs_alloc_path();
  856. if (!path)
  857. return -ENOMEM;
  858. if (sk->tree_id == 0) {
  859. /* search the root of the inode that was passed */
  860. root = BTRFS_I(inode)->root;
  861. } else {
  862. key.objectid = sk->tree_id;
  863. key.type = BTRFS_ROOT_ITEM_KEY;
  864. key.offset = (u64)-1;
  865. root = btrfs_read_fs_root_no_name(info, &key);
  866. if (IS_ERR(root)) {
  867. printk(KERN_ERR "could not find root %llu\n",
  868. sk->tree_id);
  869. btrfs_free_path(path);
  870. return -ENOENT;
  871. }
  872. }
  873. key.objectid = sk->min_objectid;
  874. key.type = sk->min_type;
  875. key.offset = sk->min_offset;
  876. max_key.objectid = sk->max_objectid;
  877. max_key.type = sk->max_type;
  878. max_key.offset = sk->max_offset;
  879. path->keep_locks = 1;
  880. while(1) {
  881. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  882. sk->min_transid);
  883. if (ret != 0) {
  884. if (ret > 0)
  885. ret = 0;
  886. goto err;
  887. }
  888. ret = copy_to_sk(root, path, &key, sk, args->buf,
  889. &sk_offset, &num_found);
  890. btrfs_release_path(root, path);
  891. if (ret || num_found >= sk->nr_items)
  892. break;
  893. }
  894. ret = 0;
  895. err:
  896. sk->nr_items = num_found;
  897. btrfs_free_path(path);
  898. return ret;
  899. }
  900. static noinline int btrfs_ioctl_tree_search(struct file *file,
  901. void __user *argp)
  902. {
  903. struct btrfs_ioctl_search_args *args;
  904. struct inode *inode;
  905. int ret;
  906. if (!capable(CAP_SYS_ADMIN))
  907. return -EPERM;
  908. args = kmalloc(sizeof(*args), GFP_KERNEL);
  909. if (!args)
  910. return -ENOMEM;
  911. if (copy_from_user(args, argp, sizeof(*args))) {
  912. kfree(args);
  913. return -EFAULT;
  914. }
  915. inode = fdentry(file)->d_inode;
  916. ret = search_ioctl(inode, args);
  917. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  918. ret = -EFAULT;
  919. kfree(args);
  920. return ret;
  921. }
  922. /*
  923. * Search INODE_REFs to identify path name of 'dirid' directory
  924. * in a 'tree_id' tree. and sets path name to 'name'.
  925. */
  926. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  927. u64 tree_id, u64 dirid, char *name)
  928. {
  929. struct btrfs_root *root;
  930. struct btrfs_key key;
  931. char *ptr;
  932. int ret = -1;
  933. int slot;
  934. int len;
  935. int total_len = 0;
  936. struct btrfs_inode_ref *iref;
  937. struct extent_buffer *l;
  938. struct btrfs_path *path;
  939. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  940. name[0]='\0';
  941. return 0;
  942. }
  943. path = btrfs_alloc_path();
  944. if (!path)
  945. return -ENOMEM;
  946. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  947. key.objectid = tree_id;
  948. key.type = BTRFS_ROOT_ITEM_KEY;
  949. key.offset = (u64)-1;
  950. root = btrfs_read_fs_root_no_name(info, &key);
  951. if (IS_ERR(root)) {
  952. printk(KERN_ERR "could not find root %llu\n", tree_id);
  953. return -ENOENT;
  954. }
  955. key.objectid = dirid;
  956. key.type = BTRFS_INODE_REF_KEY;
  957. key.offset = 0;
  958. while(1) {
  959. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  960. if (ret < 0)
  961. goto out;
  962. l = path->nodes[0];
  963. slot = path->slots[0];
  964. btrfs_item_key_to_cpu(l, &key, slot);
  965. if (ret > 0 && (key.objectid != dirid ||
  966. key.type != BTRFS_INODE_REF_KEY)) {
  967. ret = -ENOENT;
  968. goto out;
  969. }
  970. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  971. len = btrfs_inode_ref_name_len(l, iref);
  972. ptr -= len + 1;
  973. total_len += len + 1;
  974. if (ptr < name)
  975. goto out;
  976. *(ptr + len) = '/';
  977. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  978. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  979. break;
  980. btrfs_release_path(root, path);
  981. key.objectid = key.offset;
  982. key.offset = 0;
  983. dirid = key.objectid;
  984. }
  985. if (ptr < name)
  986. goto out;
  987. memcpy(name, ptr, total_len);
  988. name[total_len]='\0';
  989. ret = 0;
  990. out:
  991. btrfs_free_path(path);
  992. return ret;
  993. }
  994. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  995. void __user *argp)
  996. {
  997. struct btrfs_ioctl_ino_lookup_args *args;
  998. struct inode *inode;
  999. int ret;
  1000. if (!capable(CAP_SYS_ADMIN))
  1001. return -EPERM;
  1002. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1003. if (copy_from_user(args, argp, sizeof(*args))) {
  1004. kfree(args);
  1005. return -EFAULT;
  1006. }
  1007. inode = fdentry(file)->d_inode;
  1008. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1009. args->treeid, args->objectid,
  1010. args->name);
  1011. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1012. ret = -EFAULT;
  1013. kfree(args);
  1014. return ret;
  1015. }
  1016. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1017. void __user *arg)
  1018. {
  1019. struct dentry *parent = fdentry(file);
  1020. struct dentry *dentry;
  1021. struct inode *dir = parent->d_inode;
  1022. struct inode *inode;
  1023. struct btrfs_root *root = BTRFS_I(dir)->root;
  1024. struct btrfs_root *dest = NULL;
  1025. struct btrfs_ioctl_vol_args *vol_args;
  1026. struct btrfs_trans_handle *trans;
  1027. int namelen;
  1028. int ret;
  1029. int err = 0;
  1030. if (!capable(CAP_SYS_ADMIN))
  1031. return -EPERM;
  1032. vol_args = memdup_user(arg, sizeof(*vol_args));
  1033. if (IS_ERR(vol_args))
  1034. return PTR_ERR(vol_args);
  1035. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1036. namelen = strlen(vol_args->name);
  1037. if (strchr(vol_args->name, '/') ||
  1038. strncmp(vol_args->name, "..", namelen) == 0) {
  1039. err = -EINVAL;
  1040. goto out;
  1041. }
  1042. err = mnt_want_write(file->f_path.mnt);
  1043. if (err)
  1044. goto out;
  1045. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1046. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1047. if (IS_ERR(dentry)) {
  1048. err = PTR_ERR(dentry);
  1049. goto out_unlock_dir;
  1050. }
  1051. if (!dentry->d_inode) {
  1052. err = -ENOENT;
  1053. goto out_dput;
  1054. }
  1055. inode = dentry->d_inode;
  1056. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1057. err = -EINVAL;
  1058. goto out_dput;
  1059. }
  1060. dest = BTRFS_I(inode)->root;
  1061. mutex_lock(&inode->i_mutex);
  1062. err = d_invalidate(dentry);
  1063. if (err)
  1064. goto out_unlock;
  1065. down_write(&root->fs_info->subvol_sem);
  1066. err = may_destroy_subvol(dest);
  1067. if (err)
  1068. goto out_up_write;
  1069. trans = btrfs_start_transaction(root, 1);
  1070. ret = btrfs_unlink_subvol(trans, root, dir,
  1071. dest->root_key.objectid,
  1072. dentry->d_name.name,
  1073. dentry->d_name.len);
  1074. BUG_ON(ret);
  1075. btrfs_record_root_in_trans(trans, dest);
  1076. memset(&dest->root_item.drop_progress, 0,
  1077. sizeof(dest->root_item.drop_progress));
  1078. dest->root_item.drop_level = 0;
  1079. btrfs_set_root_refs(&dest->root_item, 0);
  1080. ret = btrfs_insert_orphan_item(trans,
  1081. root->fs_info->tree_root,
  1082. dest->root_key.objectid);
  1083. BUG_ON(ret);
  1084. ret = btrfs_commit_transaction(trans, root);
  1085. BUG_ON(ret);
  1086. inode->i_flags |= S_DEAD;
  1087. out_up_write:
  1088. up_write(&root->fs_info->subvol_sem);
  1089. out_unlock:
  1090. mutex_unlock(&inode->i_mutex);
  1091. if (!err) {
  1092. shrink_dcache_sb(root->fs_info->sb);
  1093. btrfs_invalidate_inodes(dest);
  1094. d_delete(dentry);
  1095. }
  1096. out_dput:
  1097. dput(dentry);
  1098. out_unlock_dir:
  1099. mutex_unlock(&dir->i_mutex);
  1100. mnt_drop_write(file->f_path.mnt);
  1101. out:
  1102. kfree(vol_args);
  1103. return err;
  1104. }
  1105. static int btrfs_ioctl_defrag(struct file *file)
  1106. {
  1107. struct inode *inode = fdentry(file)->d_inode;
  1108. struct btrfs_root *root = BTRFS_I(inode)->root;
  1109. int ret;
  1110. ret = mnt_want_write(file->f_path.mnt);
  1111. if (ret)
  1112. return ret;
  1113. switch (inode->i_mode & S_IFMT) {
  1114. case S_IFDIR:
  1115. if (!capable(CAP_SYS_ADMIN)) {
  1116. ret = -EPERM;
  1117. goto out;
  1118. }
  1119. btrfs_defrag_root(root, 0);
  1120. btrfs_defrag_root(root->fs_info->extent_root, 0);
  1121. break;
  1122. case S_IFREG:
  1123. if (!(file->f_mode & FMODE_WRITE)) {
  1124. ret = -EINVAL;
  1125. goto out;
  1126. }
  1127. btrfs_defrag_file(file);
  1128. break;
  1129. }
  1130. out:
  1131. mnt_drop_write(file->f_path.mnt);
  1132. return ret;
  1133. }
  1134. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1135. {
  1136. struct btrfs_ioctl_vol_args *vol_args;
  1137. int ret;
  1138. if (!capable(CAP_SYS_ADMIN))
  1139. return -EPERM;
  1140. vol_args = memdup_user(arg, sizeof(*vol_args));
  1141. if (IS_ERR(vol_args))
  1142. return PTR_ERR(vol_args);
  1143. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1144. ret = btrfs_init_new_device(root, vol_args->name);
  1145. kfree(vol_args);
  1146. return ret;
  1147. }
  1148. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1149. {
  1150. struct btrfs_ioctl_vol_args *vol_args;
  1151. int ret;
  1152. if (!capable(CAP_SYS_ADMIN))
  1153. return -EPERM;
  1154. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1155. return -EROFS;
  1156. vol_args = memdup_user(arg, sizeof(*vol_args));
  1157. if (IS_ERR(vol_args))
  1158. return PTR_ERR(vol_args);
  1159. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1160. ret = btrfs_rm_device(root, vol_args->name);
  1161. kfree(vol_args);
  1162. return ret;
  1163. }
  1164. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1165. u64 off, u64 olen, u64 destoff)
  1166. {
  1167. struct inode *inode = fdentry(file)->d_inode;
  1168. struct btrfs_root *root = BTRFS_I(inode)->root;
  1169. struct file *src_file;
  1170. struct inode *src;
  1171. struct btrfs_trans_handle *trans;
  1172. struct btrfs_path *path;
  1173. struct extent_buffer *leaf;
  1174. char *buf;
  1175. struct btrfs_key key;
  1176. u32 nritems;
  1177. int slot;
  1178. int ret;
  1179. u64 len = olen;
  1180. u64 bs = root->fs_info->sb->s_blocksize;
  1181. u64 hint_byte;
  1182. /*
  1183. * TODO:
  1184. * - split compressed inline extents. annoying: we need to
  1185. * decompress into destination's address_space (the file offset
  1186. * may change, so source mapping won't do), then recompress (or
  1187. * otherwise reinsert) a subrange.
  1188. * - allow ranges within the same file to be cloned (provided
  1189. * they don't overlap)?
  1190. */
  1191. /* the destination must be opened for writing */
  1192. if (!(file->f_mode & FMODE_WRITE))
  1193. return -EINVAL;
  1194. ret = mnt_want_write(file->f_path.mnt);
  1195. if (ret)
  1196. return ret;
  1197. src_file = fget(srcfd);
  1198. if (!src_file) {
  1199. ret = -EBADF;
  1200. goto out_drop_write;
  1201. }
  1202. src = src_file->f_dentry->d_inode;
  1203. ret = -EINVAL;
  1204. if (src == inode)
  1205. goto out_fput;
  1206. ret = -EISDIR;
  1207. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1208. goto out_fput;
  1209. ret = -EXDEV;
  1210. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1211. goto out_fput;
  1212. ret = -ENOMEM;
  1213. buf = vmalloc(btrfs_level_size(root, 0));
  1214. if (!buf)
  1215. goto out_fput;
  1216. path = btrfs_alloc_path();
  1217. if (!path) {
  1218. vfree(buf);
  1219. goto out_fput;
  1220. }
  1221. path->reada = 2;
  1222. if (inode < src) {
  1223. mutex_lock(&inode->i_mutex);
  1224. mutex_lock(&src->i_mutex);
  1225. } else {
  1226. mutex_lock(&src->i_mutex);
  1227. mutex_lock(&inode->i_mutex);
  1228. }
  1229. /* determine range to clone */
  1230. ret = -EINVAL;
  1231. if (off >= src->i_size || off + len > src->i_size)
  1232. goto out_unlock;
  1233. if (len == 0)
  1234. olen = len = src->i_size - off;
  1235. /* if we extend to eof, continue to block boundary */
  1236. if (off + len == src->i_size)
  1237. len = ((src->i_size + bs-1) & ~(bs-1))
  1238. - off;
  1239. /* verify the end result is block aligned */
  1240. if ((off & (bs-1)) ||
  1241. ((off + len) & (bs-1)))
  1242. goto out_unlock;
  1243. /* do any pending delalloc/csum calc on src, one way or
  1244. another, and lock file content */
  1245. while (1) {
  1246. struct btrfs_ordered_extent *ordered;
  1247. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1248. ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
  1249. if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
  1250. break;
  1251. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1252. if (ordered)
  1253. btrfs_put_ordered_extent(ordered);
  1254. btrfs_wait_ordered_range(src, off, off+len);
  1255. }
  1256. trans = btrfs_start_transaction(root, 1);
  1257. BUG_ON(!trans);
  1258. /* punch hole in destination first */
  1259. btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
  1260. /* clone data */
  1261. key.objectid = src->i_ino;
  1262. key.type = BTRFS_EXTENT_DATA_KEY;
  1263. key.offset = 0;
  1264. while (1) {
  1265. /*
  1266. * note the key will change type as we walk through the
  1267. * tree.
  1268. */
  1269. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  1270. if (ret < 0)
  1271. goto out;
  1272. nritems = btrfs_header_nritems(path->nodes[0]);
  1273. if (path->slots[0] >= nritems) {
  1274. ret = btrfs_next_leaf(root, path);
  1275. if (ret < 0)
  1276. goto out;
  1277. if (ret > 0)
  1278. break;
  1279. nritems = btrfs_header_nritems(path->nodes[0]);
  1280. }
  1281. leaf = path->nodes[0];
  1282. slot = path->slots[0];
  1283. btrfs_item_key_to_cpu(leaf, &key, slot);
  1284. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1285. key.objectid != src->i_ino)
  1286. break;
  1287. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1288. struct btrfs_file_extent_item *extent;
  1289. int type;
  1290. u32 size;
  1291. struct btrfs_key new_key;
  1292. u64 disko = 0, diskl = 0;
  1293. u64 datao = 0, datal = 0;
  1294. u8 comp;
  1295. size = btrfs_item_size_nr(leaf, slot);
  1296. read_extent_buffer(leaf, buf,
  1297. btrfs_item_ptr_offset(leaf, slot),
  1298. size);
  1299. extent = btrfs_item_ptr(leaf, slot,
  1300. struct btrfs_file_extent_item);
  1301. comp = btrfs_file_extent_compression(leaf, extent);
  1302. type = btrfs_file_extent_type(leaf, extent);
  1303. if (type == BTRFS_FILE_EXTENT_REG ||
  1304. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1305. disko = btrfs_file_extent_disk_bytenr(leaf,
  1306. extent);
  1307. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1308. extent);
  1309. datao = btrfs_file_extent_offset(leaf, extent);
  1310. datal = btrfs_file_extent_num_bytes(leaf,
  1311. extent);
  1312. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1313. /* take upper bound, may be compressed */
  1314. datal = btrfs_file_extent_ram_bytes(leaf,
  1315. extent);
  1316. }
  1317. btrfs_release_path(root, path);
  1318. if (key.offset + datal < off ||
  1319. key.offset >= off+len)
  1320. goto next;
  1321. memcpy(&new_key, &key, sizeof(new_key));
  1322. new_key.objectid = inode->i_ino;
  1323. new_key.offset = key.offset + destoff - off;
  1324. if (type == BTRFS_FILE_EXTENT_REG ||
  1325. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1326. ret = btrfs_insert_empty_item(trans, root, path,
  1327. &new_key, size);
  1328. if (ret)
  1329. goto out;
  1330. leaf = path->nodes[0];
  1331. slot = path->slots[0];
  1332. write_extent_buffer(leaf, buf,
  1333. btrfs_item_ptr_offset(leaf, slot),
  1334. size);
  1335. extent = btrfs_item_ptr(leaf, slot,
  1336. struct btrfs_file_extent_item);
  1337. if (off > key.offset) {
  1338. datao += off - key.offset;
  1339. datal -= off - key.offset;
  1340. }
  1341. if (key.offset + datal > off + len)
  1342. datal = off + len - key.offset;
  1343. /* disko == 0 means it's a hole */
  1344. if (!disko)
  1345. datao = 0;
  1346. btrfs_set_file_extent_offset(leaf, extent,
  1347. datao);
  1348. btrfs_set_file_extent_num_bytes(leaf, extent,
  1349. datal);
  1350. if (disko) {
  1351. inode_add_bytes(inode, datal);
  1352. ret = btrfs_inc_extent_ref(trans, root,
  1353. disko, diskl, 0,
  1354. root->root_key.objectid,
  1355. inode->i_ino,
  1356. new_key.offset - datao);
  1357. BUG_ON(ret);
  1358. }
  1359. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1360. u64 skip = 0;
  1361. u64 trim = 0;
  1362. if (off > key.offset) {
  1363. skip = off - key.offset;
  1364. new_key.offset += skip;
  1365. }
  1366. if (key.offset + datal > off+len)
  1367. trim = key.offset + datal - (off+len);
  1368. if (comp && (skip || trim)) {
  1369. ret = -EINVAL;
  1370. goto out;
  1371. }
  1372. size -= skip + trim;
  1373. datal -= skip + trim;
  1374. ret = btrfs_insert_empty_item(trans, root, path,
  1375. &new_key, size);
  1376. if (ret)
  1377. goto out;
  1378. if (skip) {
  1379. u32 start =
  1380. btrfs_file_extent_calc_inline_size(0);
  1381. memmove(buf+start, buf+start+skip,
  1382. datal);
  1383. }
  1384. leaf = path->nodes[0];
  1385. slot = path->slots[0];
  1386. write_extent_buffer(leaf, buf,
  1387. btrfs_item_ptr_offset(leaf, slot),
  1388. size);
  1389. inode_add_bytes(inode, datal);
  1390. }
  1391. btrfs_mark_buffer_dirty(leaf);
  1392. }
  1393. next:
  1394. btrfs_release_path(root, path);
  1395. key.offset++;
  1396. }
  1397. ret = 0;
  1398. out:
  1399. btrfs_release_path(root, path);
  1400. if (ret == 0) {
  1401. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1402. if (destoff + olen > inode->i_size)
  1403. btrfs_i_size_write(inode, destoff + olen);
  1404. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1405. ret = btrfs_update_inode(trans, root, inode);
  1406. }
  1407. btrfs_end_transaction(trans, root);
  1408. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1409. if (ret)
  1410. vmtruncate(inode, 0);
  1411. out_unlock:
  1412. mutex_unlock(&src->i_mutex);
  1413. mutex_unlock(&inode->i_mutex);
  1414. vfree(buf);
  1415. btrfs_free_path(path);
  1416. out_fput:
  1417. fput(src_file);
  1418. out_drop_write:
  1419. mnt_drop_write(file->f_path.mnt);
  1420. return ret;
  1421. }
  1422. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1423. {
  1424. struct btrfs_ioctl_clone_range_args args;
  1425. if (copy_from_user(&args, argp, sizeof(args)))
  1426. return -EFAULT;
  1427. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1428. args.src_length, args.dest_offset);
  1429. }
  1430. /*
  1431. * there are many ways the trans_start and trans_end ioctls can lead
  1432. * to deadlocks. They should only be used by applications that
  1433. * basically own the machine, and have a very in depth understanding
  1434. * of all the possible deadlocks and enospc problems.
  1435. */
  1436. static long btrfs_ioctl_trans_start(struct file *file)
  1437. {
  1438. struct inode *inode = fdentry(file)->d_inode;
  1439. struct btrfs_root *root = BTRFS_I(inode)->root;
  1440. struct btrfs_trans_handle *trans;
  1441. int ret;
  1442. ret = -EPERM;
  1443. if (!capable(CAP_SYS_ADMIN))
  1444. goto out;
  1445. ret = -EINPROGRESS;
  1446. if (file->private_data)
  1447. goto out;
  1448. ret = mnt_want_write(file->f_path.mnt);
  1449. if (ret)
  1450. goto out;
  1451. mutex_lock(&root->fs_info->trans_mutex);
  1452. root->fs_info->open_ioctl_trans++;
  1453. mutex_unlock(&root->fs_info->trans_mutex);
  1454. ret = -ENOMEM;
  1455. trans = btrfs_start_ioctl_transaction(root, 0);
  1456. if (!trans)
  1457. goto out_drop;
  1458. file->private_data = trans;
  1459. return 0;
  1460. out_drop:
  1461. mutex_lock(&root->fs_info->trans_mutex);
  1462. root->fs_info->open_ioctl_trans--;
  1463. mutex_unlock(&root->fs_info->trans_mutex);
  1464. mnt_drop_write(file->f_path.mnt);
  1465. out:
  1466. return ret;
  1467. }
  1468. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1469. {
  1470. struct inode *inode = fdentry(file)->d_inode;
  1471. struct btrfs_root *root = BTRFS_I(inode)->root;
  1472. struct btrfs_root *new_root;
  1473. struct btrfs_dir_item *di;
  1474. struct btrfs_trans_handle *trans;
  1475. struct btrfs_path *path;
  1476. struct btrfs_key location;
  1477. struct btrfs_disk_key disk_key;
  1478. struct btrfs_super_block *disk_super;
  1479. u64 features;
  1480. u64 objectid = 0;
  1481. u64 dir_id;
  1482. if (!capable(CAP_SYS_ADMIN))
  1483. return -EPERM;
  1484. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1485. return -EFAULT;
  1486. if (!objectid)
  1487. objectid = root->root_key.objectid;
  1488. location.objectid = objectid;
  1489. location.type = BTRFS_ROOT_ITEM_KEY;
  1490. location.offset = (u64)-1;
  1491. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1492. if (IS_ERR(new_root))
  1493. return PTR_ERR(new_root);
  1494. if (btrfs_root_refs(&new_root->root_item) == 0)
  1495. return -ENOENT;
  1496. path = btrfs_alloc_path();
  1497. if (!path)
  1498. return -ENOMEM;
  1499. path->leave_spinning = 1;
  1500. trans = btrfs_start_transaction(root, 1);
  1501. if (!trans) {
  1502. btrfs_free_path(path);
  1503. return -ENOMEM;
  1504. }
  1505. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1506. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1507. dir_id, "default", 7, 1);
  1508. if (!di) {
  1509. btrfs_free_path(path);
  1510. btrfs_end_transaction(trans, root);
  1511. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1512. "this isn't going to work\n");
  1513. return -ENOENT;
  1514. }
  1515. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1516. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1517. btrfs_mark_buffer_dirty(path->nodes[0]);
  1518. btrfs_free_path(path);
  1519. disk_super = &root->fs_info->super_copy;
  1520. features = btrfs_super_incompat_flags(disk_super);
  1521. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1522. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1523. btrfs_set_super_incompat_flags(disk_super, features);
  1524. }
  1525. btrfs_end_transaction(trans, root);
  1526. return 0;
  1527. }
  1528. /*
  1529. * there are many ways the trans_start and trans_end ioctls can lead
  1530. * to deadlocks. They should only be used by applications that
  1531. * basically own the machine, and have a very in depth understanding
  1532. * of all the possible deadlocks and enospc problems.
  1533. */
  1534. long btrfs_ioctl_trans_end(struct file *file)
  1535. {
  1536. struct inode *inode = fdentry(file)->d_inode;
  1537. struct btrfs_root *root = BTRFS_I(inode)->root;
  1538. struct btrfs_trans_handle *trans;
  1539. trans = file->private_data;
  1540. if (!trans)
  1541. return -EINVAL;
  1542. file->private_data = NULL;
  1543. btrfs_end_transaction(trans, root);
  1544. mutex_lock(&root->fs_info->trans_mutex);
  1545. root->fs_info->open_ioctl_trans--;
  1546. mutex_unlock(&root->fs_info->trans_mutex);
  1547. mnt_drop_write(file->f_path.mnt);
  1548. return 0;
  1549. }
  1550. long btrfs_ioctl(struct file *file, unsigned int
  1551. cmd, unsigned long arg)
  1552. {
  1553. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1554. void __user *argp = (void __user *)arg;
  1555. switch (cmd) {
  1556. case FS_IOC_GETFLAGS:
  1557. return btrfs_ioctl_getflags(file, argp);
  1558. case FS_IOC_SETFLAGS:
  1559. return btrfs_ioctl_setflags(file, argp);
  1560. case FS_IOC_GETVERSION:
  1561. return btrfs_ioctl_getversion(file, argp);
  1562. case BTRFS_IOC_SNAP_CREATE:
  1563. return btrfs_ioctl_snap_create(file, argp, 0);
  1564. case BTRFS_IOC_SUBVOL_CREATE:
  1565. return btrfs_ioctl_snap_create(file, argp, 1);
  1566. case BTRFS_IOC_SNAP_DESTROY:
  1567. return btrfs_ioctl_snap_destroy(file, argp);
  1568. case BTRFS_IOC_DEFAULT_SUBVOL:
  1569. return btrfs_ioctl_default_subvol(file, argp);
  1570. case BTRFS_IOC_DEFRAG:
  1571. return btrfs_ioctl_defrag(file);
  1572. case BTRFS_IOC_RESIZE:
  1573. return btrfs_ioctl_resize(root, argp);
  1574. case BTRFS_IOC_ADD_DEV:
  1575. return btrfs_ioctl_add_dev(root, argp);
  1576. case BTRFS_IOC_RM_DEV:
  1577. return btrfs_ioctl_rm_dev(root, argp);
  1578. case BTRFS_IOC_BALANCE:
  1579. return btrfs_balance(root->fs_info->dev_root);
  1580. case BTRFS_IOC_CLONE:
  1581. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1582. case BTRFS_IOC_CLONE_RANGE:
  1583. return btrfs_ioctl_clone_range(file, argp);
  1584. case BTRFS_IOC_TRANS_START:
  1585. return btrfs_ioctl_trans_start(file);
  1586. case BTRFS_IOC_TRANS_END:
  1587. return btrfs_ioctl_trans_end(file);
  1588. case BTRFS_IOC_TREE_SEARCH:
  1589. return btrfs_ioctl_tree_search(file, argp);
  1590. case BTRFS_IOC_INO_LOOKUP:
  1591. return btrfs_ioctl_ino_lookup(file, argp);
  1592. case BTRFS_IOC_SYNC:
  1593. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1594. return 0;
  1595. }
  1596. return -ENOTTY;
  1597. }