ioctl.c 60 KB

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