ioctl.c 55 KB

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