ioctl.c 48 KB

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