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