ioctl.c 55 KB

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