ioctl.c 59 KB

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