ioctl.c 48 KB

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