ioctl.c 56 KB

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