ioctl.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  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. u64 *async_transid)
  198. {
  199. struct btrfs_trans_handle *trans;
  200. struct btrfs_key key;
  201. struct btrfs_root_item root_item;
  202. struct btrfs_inode_item *inode_item;
  203. struct extent_buffer *leaf;
  204. struct btrfs_root *new_root;
  205. struct inode *dir = dentry->d_parent->d_inode;
  206. int ret;
  207. int err;
  208. u64 objectid;
  209. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  210. u64 index = 0;
  211. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  212. 0, &objectid);
  213. if (ret)
  214. return ret;
  215. /*
  216. * 1 - inode item
  217. * 2 - refs
  218. * 1 - root item
  219. * 2 - dir items
  220. */
  221. trans = btrfs_start_transaction(root, 6);
  222. if (IS_ERR(trans))
  223. return PTR_ERR(trans);
  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. if (async_transid) {
  294. *async_transid = trans->transid;
  295. err = btrfs_commit_transaction_async(trans, root, 1);
  296. } else {
  297. err = btrfs_commit_transaction(trans, root);
  298. }
  299. if (err && !ret)
  300. ret = err;
  301. return ret;
  302. }
  303. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  304. char *name, int namelen, u64 *async_transid)
  305. {
  306. struct inode *inode;
  307. struct btrfs_pending_snapshot *pending_snapshot;
  308. struct btrfs_trans_handle *trans;
  309. int ret;
  310. if (!root->ref_cows)
  311. return -EINVAL;
  312. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  313. if (!pending_snapshot)
  314. return -ENOMEM;
  315. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  316. pending_snapshot->dentry = dentry;
  317. pending_snapshot->root = root;
  318. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  319. if (IS_ERR(trans)) {
  320. ret = PTR_ERR(trans);
  321. goto fail;
  322. }
  323. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  324. BUG_ON(ret);
  325. list_add(&pending_snapshot->list,
  326. &trans->transaction->pending_snapshots);
  327. if (async_transid) {
  328. *async_transid = trans->transid;
  329. ret = btrfs_commit_transaction_async(trans,
  330. root->fs_info->extent_root, 1);
  331. } else {
  332. ret = btrfs_commit_transaction(trans,
  333. root->fs_info->extent_root);
  334. }
  335. BUG_ON(ret);
  336. ret = pending_snapshot->error;
  337. if (ret)
  338. goto fail;
  339. btrfs_orphan_cleanup(pending_snapshot->snap);
  340. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  341. if (IS_ERR(inode)) {
  342. ret = PTR_ERR(inode);
  343. goto fail;
  344. }
  345. BUG_ON(!inode);
  346. d_instantiate(dentry, inode);
  347. ret = 0;
  348. fail:
  349. kfree(pending_snapshot);
  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. u64 *async_transid)
  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, async_transid);
  394. } else {
  395. error = create_subvol(BTRFS_I(dir)->root, dentry,
  396. name, namelen, async_transid);
  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_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  520. if (ret)
  521. goto err_unlock;
  522. again:
  523. if (inode->i_size == 0 ||
  524. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  525. ret = 0;
  526. goto err_reservations;
  527. }
  528. page = grab_cache_page(inode->i_mapping, i);
  529. if (!page) {
  530. ret = -ENOMEM;
  531. goto err_reservations;
  532. }
  533. if (!PageUptodate(page)) {
  534. btrfs_readpage(NULL, page);
  535. lock_page(page);
  536. if (!PageUptodate(page)) {
  537. unlock_page(page);
  538. page_cache_release(page);
  539. ret = -EIO;
  540. goto err_reservations;
  541. }
  542. }
  543. if (page->mapping != inode->i_mapping) {
  544. unlock_page(page);
  545. page_cache_release(page);
  546. goto again;
  547. }
  548. wait_on_page_writeback(page);
  549. if (PageDirty(page)) {
  550. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  551. goto loop_unlock;
  552. }
  553. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  554. page_end = page_start + PAGE_CACHE_SIZE - 1;
  555. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  556. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  557. if (ordered) {
  558. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  559. unlock_page(page);
  560. page_cache_release(page);
  561. btrfs_start_ordered_extent(inode, ordered, 1);
  562. btrfs_put_ordered_extent(ordered);
  563. goto again;
  564. }
  565. set_page_extent_mapped(page);
  566. /*
  567. * this makes sure page_mkwrite is called on the
  568. * page if it is dirtied again later
  569. */
  570. clear_page_dirty_for_io(page);
  571. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  572. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  573. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  574. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  575. ClearPageChecked(page);
  576. set_page_dirty(page);
  577. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  578. loop_unlock:
  579. unlock_page(page);
  580. page_cache_release(page);
  581. mutex_unlock(&inode->i_mutex);
  582. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  583. i++;
  584. }
  585. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  586. filemap_flush(inode->i_mapping);
  587. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  588. /* the filemap_flush will queue IO into the worker threads, but
  589. * we have to make sure the IO is actually started and that
  590. * ordered extents get created before we return
  591. */
  592. atomic_inc(&root->fs_info->async_submit_draining);
  593. while (atomic_read(&root->fs_info->nr_async_submits) ||
  594. atomic_read(&root->fs_info->async_delalloc_pages)) {
  595. wait_event(root->fs_info->async_submit_wait,
  596. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  597. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  598. }
  599. atomic_dec(&root->fs_info->async_submit_draining);
  600. mutex_lock(&inode->i_mutex);
  601. BTRFS_I(inode)->force_compress = 0;
  602. mutex_unlock(&inode->i_mutex);
  603. }
  604. return 0;
  605. err_reservations:
  606. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  607. err_unlock:
  608. mutex_unlock(&inode->i_mutex);
  609. return ret;
  610. }
  611. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  612. void __user *arg)
  613. {
  614. u64 new_size;
  615. u64 old_size;
  616. u64 devid = 1;
  617. struct btrfs_ioctl_vol_args *vol_args;
  618. struct btrfs_trans_handle *trans;
  619. struct btrfs_device *device = NULL;
  620. char *sizestr;
  621. char *devstr = NULL;
  622. int ret = 0;
  623. int mod = 0;
  624. if (root->fs_info->sb->s_flags & MS_RDONLY)
  625. return -EROFS;
  626. if (!capable(CAP_SYS_ADMIN))
  627. return -EPERM;
  628. vol_args = memdup_user(arg, sizeof(*vol_args));
  629. if (IS_ERR(vol_args))
  630. return PTR_ERR(vol_args);
  631. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  632. mutex_lock(&root->fs_info->volume_mutex);
  633. sizestr = vol_args->name;
  634. devstr = strchr(sizestr, ':');
  635. if (devstr) {
  636. char *end;
  637. sizestr = devstr + 1;
  638. *devstr = '\0';
  639. devstr = vol_args->name;
  640. devid = simple_strtoull(devstr, &end, 10);
  641. printk(KERN_INFO "resizing devid %llu\n",
  642. (unsigned long long)devid);
  643. }
  644. device = btrfs_find_device(root, devid, NULL, NULL);
  645. if (!device) {
  646. printk(KERN_INFO "resizer unable to find device %llu\n",
  647. (unsigned long long)devid);
  648. ret = -EINVAL;
  649. goto out_unlock;
  650. }
  651. if (!strcmp(sizestr, "max"))
  652. new_size = device->bdev->bd_inode->i_size;
  653. else {
  654. if (sizestr[0] == '-') {
  655. mod = -1;
  656. sizestr++;
  657. } else if (sizestr[0] == '+') {
  658. mod = 1;
  659. sizestr++;
  660. }
  661. new_size = memparse(sizestr, NULL);
  662. if (new_size == 0) {
  663. ret = -EINVAL;
  664. goto out_unlock;
  665. }
  666. }
  667. old_size = device->total_bytes;
  668. if (mod < 0) {
  669. if (new_size > old_size) {
  670. ret = -EINVAL;
  671. goto out_unlock;
  672. }
  673. new_size = old_size - new_size;
  674. } else if (mod > 0) {
  675. new_size = old_size + new_size;
  676. }
  677. if (new_size < 256 * 1024 * 1024) {
  678. ret = -EINVAL;
  679. goto out_unlock;
  680. }
  681. if (new_size > device->bdev->bd_inode->i_size) {
  682. ret = -EFBIG;
  683. goto out_unlock;
  684. }
  685. do_div(new_size, root->sectorsize);
  686. new_size *= root->sectorsize;
  687. printk(KERN_INFO "new size for %s is %llu\n",
  688. device->name, (unsigned long long)new_size);
  689. if (new_size > old_size) {
  690. trans = btrfs_start_transaction(root, 0);
  691. ret = btrfs_grow_device(trans, device, new_size);
  692. btrfs_commit_transaction(trans, root);
  693. } else {
  694. ret = btrfs_shrink_device(device, new_size);
  695. }
  696. out_unlock:
  697. mutex_unlock(&root->fs_info->volume_mutex);
  698. kfree(vol_args);
  699. return ret;
  700. }
  701. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  702. char *name,
  703. unsigned long fd,
  704. int subvol,
  705. u64 *transid)
  706. {
  707. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  708. struct file *src_file;
  709. int namelen;
  710. int ret = 0;
  711. if (root->fs_info->sb->s_flags & MS_RDONLY)
  712. return -EROFS;
  713. namelen = strlen(name);
  714. if (strchr(name, '/')) {
  715. ret = -EINVAL;
  716. goto out;
  717. }
  718. if (subvol) {
  719. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  720. NULL, transid);
  721. } else {
  722. struct inode *src_inode;
  723. src_file = fget(fd);
  724. if (!src_file) {
  725. ret = -EINVAL;
  726. goto out;
  727. }
  728. src_inode = src_file->f_path.dentry->d_inode;
  729. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  730. printk(KERN_INFO "btrfs: Snapshot src from "
  731. "another FS\n");
  732. ret = -EINVAL;
  733. fput(src_file);
  734. goto out;
  735. }
  736. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  737. BTRFS_I(src_inode)->root,
  738. transid);
  739. fput(src_file);
  740. }
  741. out:
  742. return ret;
  743. }
  744. static noinline int btrfs_ioctl_snap_create(struct file *file,
  745. void __user *arg, int subvol,
  746. int async)
  747. {
  748. struct btrfs_ioctl_vol_args *vol_args = NULL;
  749. struct btrfs_ioctl_async_vol_args *async_vol_args = NULL;
  750. char *name;
  751. u64 fd;
  752. u64 transid = 0;
  753. int ret;
  754. if (async) {
  755. async_vol_args = memdup_user(arg, sizeof(*async_vol_args));
  756. if (IS_ERR(async_vol_args))
  757. return PTR_ERR(async_vol_args);
  758. name = async_vol_args->name;
  759. fd = async_vol_args->fd;
  760. async_vol_args->name[BTRFS_SNAPSHOT_NAME_MAX] = '\0';
  761. } else {
  762. vol_args = memdup_user(arg, sizeof(*vol_args));
  763. if (IS_ERR(vol_args))
  764. return PTR_ERR(vol_args);
  765. name = vol_args->name;
  766. fd = vol_args->fd;
  767. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  768. }
  769. ret = btrfs_ioctl_snap_create_transid(file, name, fd,
  770. subvol, &transid);
  771. if (!ret && async) {
  772. if (copy_to_user(arg +
  773. offsetof(struct btrfs_ioctl_async_vol_args,
  774. transid), &transid, sizeof(transid)))
  775. return -EFAULT;
  776. }
  777. kfree(vol_args);
  778. kfree(async_vol_args);
  779. return ret;
  780. }
  781. /*
  782. * helper to check if the subvolume references other subvolumes
  783. */
  784. static noinline int may_destroy_subvol(struct btrfs_root *root)
  785. {
  786. struct btrfs_path *path;
  787. struct btrfs_key key;
  788. int ret;
  789. path = btrfs_alloc_path();
  790. if (!path)
  791. return -ENOMEM;
  792. key.objectid = root->root_key.objectid;
  793. key.type = BTRFS_ROOT_REF_KEY;
  794. key.offset = (u64)-1;
  795. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  796. &key, path, 0, 0);
  797. if (ret < 0)
  798. goto out;
  799. BUG_ON(ret == 0);
  800. ret = 0;
  801. if (path->slots[0] > 0) {
  802. path->slots[0]--;
  803. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  804. if (key.objectid == root->root_key.objectid &&
  805. key.type == BTRFS_ROOT_REF_KEY)
  806. ret = -ENOTEMPTY;
  807. }
  808. out:
  809. btrfs_free_path(path);
  810. return ret;
  811. }
  812. static noinline int key_in_sk(struct btrfs_key *key,
  813. struct btrfs_ioctl_search_key *sk)
  814. {
  815. struct btrfs_key test;
  816. int ret;
  817. test.objectid = sk->min_objectid;
  818. test.type = sk->min_type;
  819. test.offset = sk->min_offset;
  820. ret = btrfs_comp_cpu_keys(key, &test);
  821. if (ret < 0)
  822. return 0;
  823. test.objectid = sk->max_objectid;
  824. test.type = sk->max_type;
  825. test.offset = sk->max_offset;
  826. ret = btrfs_comp_cpu_keys(key, &test);
  827. if (ret > 0)
  828. return 0;
  829. return 1;
  830. }
  831. static noinline int copy_to_sk(struct btrfs_root *root,
  832. struct btrfs_path *path,
  833. struct btrfs_key *key,
  834. struct btrfs_ioctl_search_key *sk,
  835. char *buf,
  836. unsigned long *sk_offset,
  837. int *num_found)
  838. {
  839. u64 found_transid;
  840. struct extent_buffer *leaf;
  841. struct btrfs_ioctl_search_header sh;
  842. unsigned long item_off;
  843. unsigned long item_len;
  844. int nritems;
  845. int i;
  846. int slot;
  847. int found = 0;
  848. int ret = 0;
  849. leaf = path->nodes[0];
  850. slot = path->slots[0];
  851. nritems = btrfs_header_nritems(leaf);
  852. if (btrfs_header_generation(leaf) > sk->max_transid) {
  853. i = nritems;
  854. goto advance_key;
  855. }
  856. found_transid = btrfs_header_generation(leaf);
  857. for (i = slot; i < nritems; i++) {
  858. item_off = btrfs_item_ptr_offset(leaf, i);
  859. item_len = btrfs_item_size_nr(leaf, i);
  860. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  861. item_len = 0;
  862. if (sizeof(sh) + item_len + *sk_offset >
  863. BTRFS_SEARCH_ARGS_BUFSIZE) {
  864. ret = 1;
  865. goto overflow;
  866. }
  867. btrfs_item_key_to_cpu(leaf, key, i);
  868. if (!key_in_sk(key, sk))
  869. continue;
  870. sh.objectid = key->objectid;
  871. sh.offset = key->offset;
  872. sh.type = key->type;
  873. sh.len = item_len;
  874. sh.transid = found_transid;
  875. /* copy search result header */
  876. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  877. *sk_offset += sizeof(sh);
  878. if (item_len) {
  879. char *p = buf + *sk_offset;
  880. /* copy the item */
  881. read_extent_buffer(leaf, p,
  882. item_off, item_len);
  883. *sk_offset += item_len;
  884. }
  885. found++;
  886. if (*num_found >= sk->nr_items)
  887. break;
  888. }
  889. advance_key:
  890. ret = 0;
  891. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  892. key->offset++;
  893. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  894. key->offset = 0;
  895. key->type++;
  896. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  897. key->offset = 0;
  898. key->type = 0;
  899. key->objectid++;
  900. } else
  901. ret = 1;
  902. overflow:
  903. *num_found += found;
  904. return ret;
  905. }
  906. static noinline int search_ioctl(struct inode *inode,
  907. struct btrfs_ioctl_search_args *args)
  908. {
  909. struct btrfs_root *root;
  910. struct btrfs_key key;
  911. struct btrfs_key max_key;
  912. struct btrfs_path *path;
  913. struct btrfs_ioctl_search_key *sk = &args->key;
  914. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  915. int ret;
  916. int num_found = 0;
  917. unsigned long sk_offset = 0;
  918. path = btrfs_alloc_path();
  919. if (!path)
  920. return -ENOMEM;
  921. if (sk->tree_id == 0) {
  922. /* search the root of the inode that was passed */
  923. root = BTRFS_I(inode)->root;
  924. } else {
  925. key.objectid = sk->tree_id;
  926. key.type = BTRFS_ROOT_ITEM_KEY;
  927. key.offset = (u64)-1;
  928. root = btrfs_read_fs_root_no_name(info, &key);
  929. if (IS_ERR(root)) {
  930. printk(KERN_ERR "could not find root %llu\n",
  931. sk->tree_id);
  932. btrfs_free_path(path);
  933. return -ENOENT;
  934. }
  935. }
  936. key.objectid = sk->min_objectid;
  937. key.type = sk->min_type;
  938. key.offset = sk->min_offset;
  939. max_key.objectid = sk->max_objectid;
  940. max_key.type = sk->max_type;
  941. max_key.offset = sk->max_offset;
  942. path->keep_locks = 1;
  943. while(1) {
  944. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  945. sk->min_transid);
  946. if (ret != 0) {
  947. if (ret > 0)
  948. ret = 0;
  949. goto err;
  950. }
  951. ret = copy_to_sk(root, path, &key, sk, args->buf,
  952. &sk_offset, &num_found);
  953. btrfs_release_path(root, path);
  954. if (ret || num_found >= sk->nr_items)
  955. break;
  956. }
  957. ret = 0;
  958. err:
  959. sk->nr_items = num_found;
  960. btrfs_free_path(path);
  961. return ret;
  962. }
  963. static noinline int btrfs_ioctl_tree_search(struct file *file,
  964. void __user *argp)
  965. {
  966. struct btrfs_ioctl_search_args *args;
  967. struct inode *inode;
  968. int ret;
  969. if (!capable(CAP_SYS_ADMIN))
  970. return -EPERM;
  971. args = memdup_user(argp, sizeof(*args));
  972. if (IS_ERR(args))
  973. return PTR_ERR(args);
  974. inode = fdentry(file)->d_inode;
  975. ret = search_ioctl(inode, args);
  976. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  977. ret = -EFAULT;
  978. kfree(args);
  979. return ret;
  980. }
  981. /*
  982. * Search INODE_REFs to identify path name of 'dirid' directory
  983. * in a 'tree_id' tree. and sets path name to 'name'.
  984. */
  985. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  986. u64 tree_id, u64 dirid, char *name)
  987. {
  988. struct btrfs_root *root;
  989. struct btrfs_key key;
  990. char *ptr;
  991. int ret = -1;
  992. int slot;
  993. int len;
  994. int total_len = 0;
  995. struct btrfs_inode_ref *iref;
  996. struct extent_buffer *l;
  997. struct btrfs_path *path;
  998. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  999. name[0]='\0';
  1000. return 0;
  1001. }
  1002. path = btrfs_alloc_path();
  1003. if (!path)
  1004. return -ENOMEM;
  1005. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1006. key.objectid = tree_id;
  1007. key.type = BTRFS_ROOT_ITEM_KEY;
  1008. key.offset = (u64)-1;
  1009. root = btrfs_read_fs_root_no_name(info, &key);
  1010. if (IS_ERR(root)) {
  1011. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1012. ret = -ENOENT;
  1013. goto out;
  1014. }
  1015. key.objectid = dirid;
  1016. key.type = BTRFS_INODE_REF_KEY;
  1017. key.offset = (u64)-1;
  1018. while(1) {
  1019. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1020. if (ret < 0)
  1021. goto out;
  1022. l = path->nodes[0];
  1023. slot = path->slots[0];
  1024. if (ret > 0 && slot > 0)
  1025. slot--;
  1026. btrfs_item_key_to_cpu(l, &key, slot);
  1027. if (ret > 0 && (key.objectid != dirid ||
  1028. key.type != BTRFS_INODE_REF_KEY)) {
  1029. ret = -ENOENT;
  1030. goto out;
  1031. }
  1032. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1033. len = btrfs_inode_ref_name_len(l, iref);
  1034. ptr -= len + 1;
  1035. total_len += len + 1;
  1036. if (ptr < name)
  1037. goto out;
  1038. *(ptr + len) = '/';
  1039. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1040. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1041. break;
  1042. btrfs_release_path(root, path);
  1043. key.objectid = key.offset;
  1044. key.offset = (u64)-1;
  1045. dirid = key.objectid;
  1046. }
  1047. if (ptr < name)
  1048. goto out;
  1049. memcpy(name, ptr, total_len);
  1050. name[total_len]='\0';
  1051. ret = 0;
  1052. out:
  1053. btrfs_free_path(path);
  1054. return ret;
  1055. }
  1056. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1057. void __user *argp)
  1058. {
  1059. struct btrfs_ioctl_ino_lookup_args *args;
  1060. struct inode *inode;
  1061. int ret;
  1062. if (!capable(CAP_SYS_ADMIN))
  1063. return -EPERM;
  1064. args = memdup_user(argp, sizeof(*args));
  1065. if (IS_ERR(args))
  1066. return PTR_ERR(args);
  1067. inode = fdentry(file)->d_inode;
  1068. if (args->treeid == 0)
  1069. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1070. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1071. args->treeid, args->objectid,
  1072. args->name);
  1073. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1074. ret = -EFAULT;
  1075. kfree(args);
  1076. return ret;
  1077. }
  1078. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1079. void __user *arg)
  1080. {
  1081. struct dentry *parent = fdentry(file);
  1082. struct dentry *dentry;
  1083. struct inode *dir = parent->d_inode;
  1084. struct inode *inode;
  1085. struct btrfs_root *root = BTRFS_I(dir)->root;
  1086. struct btrfs_root *dest = NULL;
  1087. struct btrfs_ioctl_vol_args *vol_args;
  1088. struct btrfs_trans_handle *trans;
  1089. int namelen;
  1090. int ret;
  1091. int err = 0;
  1092. if (!capable(CAP_SYS_ADMIN))
  1093. return -EPERM;
  1094. vol_args = memdup_user(arg, sizeof(*vol_args));
  1095. if (IS_ERR(vol_args))
  1096. return PTR_ERR(vol_args);
  1097. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1098. namelen = strlen(vol_args->name);
  1099. if (strchr(vol_args->name, '/') ||
  1100. strncmp(vol_args->name, "..", namelen) == 0) {
  1101. err = -EINVAL;
  1102. goto out;
  1103. }
  1104. err = mnt_want_write(file->f_path.mnt);
  1105. if (err)
  1106. goto out;
  1107. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1108. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1109. if (IS_ERR(dentry)) {
  1110. err = PTR_ERR(dentry);
  1111. goto out_unlock_dir;
  1112. }
  1113. if (!dentry->d_inode) {
  1114. err = -ENOENT;
  1115. goto out_dput;
  1116. }
  1117. inode = dentry->d_inode;
  1118. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1119. err = -EINVAL;
  1120. goto out_dput;
  1121. }
  1122. dest = BTRFS_I(inode)->root;
  1123. mutex_lock(&inode->i_mutex);
  1124. err = d_invalidate(dentry);
  1125. if (err)
  1126. goto out_unlock;
  1127. down_write(&root->fs_info->subvol_sem);
  1128. err = may_destroy_subvol(dest);
  1129. if (err)
  1130. goto out_up_write;
  1131. trans = btrfs_start_transaction(root, 0);
  1132. if (IS_ERR(trans)) {
  1133. err = PTR_ERR(trans);
  1134. goto out_up_write;
  1135. }
  1136. trans->block_rsv = &root->fs_info->global_block_rsv;
  1137. ret = btrfs_unlink_subvol(trans, root, dir,
  1138. dest->root_key.objectid,
  1139. dentry->d_name.name,
  1140. dentry->d_name.len);
  1141. BUG_ON(ret);
  1142. btrfs_record_root_in_trans(trans, dest);
  1143. memset(&dest->root_item.drop_progress, 0,
  1144. sizeof(dest->root_item.drop_progress));
  1145. dest->root_item.drop_level = 0;
  1146. btrfs_set_root_refs(&dest->root_item, 0);
  1147. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1148. ret = btrfs_insert_orphan_item(trans,
  1149. root->fs_info->tree_root,
  1150. dest->root_key.objectid);
  1151. BUG_ON(ret);
  1152. }
  1153. ret = btrfs_commit_transaction(trans, root);
  1154. BUG_ON(ret);
  1155. inode->i_flags |= S_DEAD;
  1156. out_up_write:
  1157. up_write(&root->fs_info->subvol_sem);
  1158. out_unlock:
  1159. mutex_unlock(&inode->i_mutex);
  1160. if (!err) {
  1161. shrink_dcache_sb(root->fs_info->sb);
  1162. btrfs_invalidate_inodes(dest);
  1163. d_delete(dentry);
  1164. }
  1165. out_dput:
  1166. dput(dentry);
  1167. out_unlock_dir:
  1168. mutex_unlock(&dir->i_mutex);
  1169. mnt_drop_write(file->f_path.mnt);
  1170. out:
  1171. kfree(vol_args);
  1172. return err;
  1173. }
  1174. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1175. {
  1176. struct inode *inode = fdentry(file)->d_inode;
  1177. struct btrfs_root *root = BTRFS_I(inode)->root;
  1178. struct btrfs_ioctl_defrag_range_args *range;
  1179. int ret;
  1180. ret = mnt_want_write(file->f_path.mnt);
  1181. if (ret)
  1182. return ret;
  1183. switch (inode->i_mode & S_IFMT) {
  1184. case S_IFDIR:
  1185. if (!capable(CAP_SYS_ADMIN)) {
  1186. ret = -EPERM;
  1187. goto out;
  1188. }
  1189. ret = btrfs_defrag_root(root, 0);
  1190. if (ret)
  1191. goto out;
  1192. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1193. break;
  1194. case S_IFREG:
  1195. if (!(file->f_mode & FMODE_WRITE)) {
  1196. ret = -EINVAL;
  1197. goto out;
  1198. }
  1199. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1200. if (!range) {
  1201. ret = -ENOMEM;
  1202. goto out;
  1203. }
  1204. if (argp) {
  1205. if (copy_from_user(range, argp,
  1206. sizeof(*range))) {
  1207. ret = -EFAULT;
  1208. kfree(range);
  1209. goto out;
  1210. }
  1211. /* compression requires us to start the IO */
  1212. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1213. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1214. range->extent_thresh = (u32)-1;
  1215. }
  1216. } else {
  1217. /* the rest are all set to zero by kzalloc */
  1218. range->len = (u64)-1;
  1219. }
  1220. ret = btrfs_defrag_file(file, range);
  1221. kfree(range);
  1222. break;
  1223. default:
  1224. ret = -EINVAL;
  1225. }
  1226. out:
  1227. mnt_drop_write(file->f_path.mnt);
  1228. return ret;
  1229. }
  1230. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1231. {
  1232. struct btrfs_ioctl_vol_args *vol_args;
  1233. int ret;
  1234. if (!capable(CAP_SYS_ADMIN))
  1235. return -EPERM;
  1236. vol_args = memdup_user(arg, sizeof(*vol_args));
  1237. if (IS_ERR(vol_args))
  1238. return PTR_ERR(vol_args);
  1239. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1240. ret = btrfs_init_new_device(root, vol_args->name);
  1241. kfree(vol_args);
  1242. return ret;
  1243. }
  1244. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1245. {
  1246. struct btrfs_ioctl_vol_args *vol_args;
  1247. int ret;
  1248. if (!capable(CAP_SYS_ADMIN))
  1249. return -EPERM;
  1250. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1251. return -EROFS;
  1252. vol_args = memdup_user(arg, sizeof(*vol_args));
  1253. if (IS_ERR(vol_args))
  1254. return PTR_ERR(vol_args);
  1255. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1256. ret = btrfs_rm_device(root, vol_args->name);
  1257. kfree(vol_args);
  1258. return ret;
  1259. }
  1260. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1261. u64 off, u64 olen, u64 destoff)
  1262. {
  1263. struct inode *inode = fdentry(file)->d_inode;
  1264. struct btrfs_root *root = BTRFS_I(inode)->root;
  1265. struct file *src_file;
  1266. struct inode *src;
  1267. struct btrfs_trans_handle *trans;
  1268. struct btrfs_path *path;
  1269. struct extent_buffer *leaf;
  1270. char *buf;
  1271. struct btrfs_key key;
  1272. u32 nritems;
  1273. int slot;
  1274. int ret;
  1275. u64 len = olen;
  1276. u64 bs = root->fs_info->sb->s_blocksize;
  1277. u64 hint_byte;
  1278. /*
  1279. * TODO:
  1280. * - split compressed inline extents. annoying: we need to
  1281. * decompress into destination's address_space (the file offset
  1282. * may change, so source mapping won't do), then recompress (or
  1283. * otherwise reinsert) a subrange.
  1284. * - allow ranges within the same file to be cloned (provided
  1285. * they don't overlap)?
  1286. */
  1287. /* the destination must be opened for writing */
  1288. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1289. return -EINVAL;
  1290. ret = mnt_want_write(file->f_path.mnt);
  1291. if (ret)
  1292. return ret;
  1293. src_file = fget(srcfd);
  1294. if (!src_file) {
  1295. ret = -EBADF;
  1296. goto out_drop_write;
  1297. }
  1298. src = src_file->f_dentry->d_inode;
  1299. ret = -EINVAL;
  1300. if (src == inode)
  1301. goto out_fput;
  1302. /* the src must be open for reading */
  1303. if (!(src_file->f_mode & FMODE_READ))
  1304. goto out_fput;
  1305. ret = -EISDIR;
  1306. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1307. goto out_fput;
  1308. ret = -EXDEV;
  1309. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1310. goto out_fput;
  1311. ret = -ENOMEM;
  1312. buf = vmalloc(btrfs_level_size(root, 0));
  1313. if (!buf)
  1314. goto out_fput;
  1315. path = btrfs_alloc_path();
  1316. if (!path) {
  1317. vfree(buf);
  1318. goto out_fput;
  1319. }
  1320. path->reada = 2;
  1321. if (inode < src) {
  1322. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1323. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1324. } else {
  1325. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1326. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1327. }
  1328. /* determine range to clone */
  1329. ret = -EINVAL;
  1330. if (off + len > src->i_size || off + len < off)
  1331. goto out_unlock;
  1332. if (len == 0)
  1333. olen = len = src->i_size - off;
  1334. /* if we extend to eof, continue to block boundary */
  1335. if (off + len == src->i_size)
  1336. len = ((src->i_size + bs-1) & ~(bs-1))
  1337. - off;
  1338. /* verify the end result is block aligned */
  1339. if ((off & (bs-1)) ||
  1340. ((off + len) & (bs-1)))
  1341. goto out_unlock;
  1342. /* do any pending delalloc/csum calc on src, one way or
  1343. another, and lock file content */
  1344. while (1) {
  1345. struct btrfs_ordered_extent *ordered;
  1346. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1347. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1348. if (!ordered &&
  1349. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1350. EXTENT_DELALLOC, 0, NULL))
  1351. break;
  1352. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1353. if (ordered)
  1354. btrfs_put_ordered_extent(ordered);
  1355. btrfs_wait_ordered_range(src, off, len);
  1356. }
  1357. /* clone data */
  1358. key.objectid = src->i_ino;
  1359. key.type = BTRFS_EXTENT_DATA_KEY;
  1360. key.offset = 0;
  1361. while (1) {
  1362. /*
  1363. * note the key will change type as we walk through the
  1364. * tree.
  1365. */
  1366. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1367. if (ret < 0)
  1368. goto out;
  1369. nritems = btrfs_header_nritems(path->nodes[0]);
  1370. if (path->slots[0] >= nritems) {
  1371. ret = btrfs_next_leaf(root, path);
  1372. if (ret < 0)
  1373. goto out;
  1374. if (ret > 0)
  1375. break;
  1376. nritems = btrfs_header_nritems(path->nodes[0]);
  1377. }
  1378. leaf = path->nodes[0];
  1379. slot = path->slots[0];
  1380. btrfs_item_key_to_cpu(leaf, &key, slot);
  1381. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1382. key.objectid != src->i_ino)
  1383. break;
  1384. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1385. struct btrfs_file_extent_item *extent;
  1386. int type;
  1387. u32 size;
  1388. struct btrfs_key new_key;
  1389. u64 disko = 0, diskl = 0;
  1390. u64 datao = 0, datal = 0;
  1391. u8 comp;
  1392. u64 endoff;
  1393. size = btrfs_item_size_nr(leaf, slot);
  1394. read_extent_buffer(leaf, buf,
  1395. btrfs_item_ptr_offset(leaf, slot),
  1396. size);
  1397. extent = btrfs_item_ptr(leaf, slot,
  1398. struct btrfs_file_extent_item);
  1399. comp = btrfs_file_extent_compression(leaf, extent);
  1400. type = btrfs_file_extent_type(leaf, extent);
  1401. if (type == BTRFS_FILE_EXTENT_REG ||
  1402. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1403. disko = btrfs_file_extent_disk_bytenr(leaf,
  1404. extent);
  1405. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1406. extent);
  1407. datao = btrfs_file_extent_offset(leaf, extent);
  1408. datal = btrfs_file_extent_num_bytes(leaf,
  1409. extent);
  1410. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1411. /* take upper bound, may be compressed */
  1412. datal = btrfs_file_extent_ram_bytes(leaf,
  1413. extent);
  1414. }
  1415. btrfs_release_path(root, path);
  1416. if (key.offset + datal <= off ||
  1417. key.offset >= off+len)
  1418. goto next;
  1419. memcpy(&new_key, &key, sizeof(new_key));
  1420. new_key.objectid = inode->i_ino;
  1421. new_key.offset = key.offset + destoff - off;
  1422. trans = btrfs_start_transaction(root, 1);
  1423. if (IS_ERR(trans)) {
  1424. ret = PTR_ERR(trans);
  1425. goto out;
  1426. }
  1427. if (type == BTRFS_FILE_EXTENT_REG ||
  1428. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1429. if (off > key.offset) {
  1430. datao += off - key.offset;
  1431. datal -= off - key.offset;
  1432. }
  1433. if (key.offset + datal > off + len)
  1434. datal = off + len - key.offset;
  1435. ret = btrfs_drop_extents(trans, inode,
  1436. new_key.offset,
  1437. new_key.offset + datal,
  1438. &hint_byte, 1);
  1439. BUG_ON(ret);
  1440. ret = btrfs_insert_empty_item(trans, root, path,
  1441. &new_key, size);
  1442. BUG_ON(ret);
  1443. leaf = path->nodes[0];
  1444. slot = path->slots[0];
  1445. write_extent_buffer(leaf, buf,
  1446. btrfs_item_ptr_offset(leaf, slot),
  1447. size);
  1448. extent = btrfs_item_ptr(leaf, slot,
  1449. struct btrfs_file_extent_item);
  1450. /* disko == 0 means it's a hole */
  1451. if (!disko)
  1452. datao = 0;
  1453. btrfs_set_file_extent_offset(leaf, extent,
  1454. datao);
  1455. btrfs_set_file_extent_num_bytes(leaf, extent,
  1456. datal);
  1457. if (disko) {
  1458. inode_add_bytes(inode, datal);
  1459. ret = btrfs_inc_extent_ref(trans, root,
  1460. disko, diskl, 0,
  1461. root->root_key.objectid,
  1462. inode->i_ino,
  1463. new_key.offset - datao);
  1464. BUG_ON(ret);
  1465. }
  1466. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1467. u64 skip = 0;
  1468. u64 trim = 0;
  1469. if (off > key.offset) {
  1470. skip = off - key.offset;
  1471. new_key.offset += skip;
  1472. }
  1473. if (key.offset + datal > off+len)
  1474. trim = key.offset + datal - (off+len);
  1475. if (comp && (skip || trim)) {
  1476. ret = -EINVAL;
  1477. btrfs_end_transaction(trans, root);
  1478. goto out;
  1479. }
  1480. size -= skip + trim;
  1481. datal -= skip + trim;
  1482. ret = btrfs_drop_extents(trans, inode,
  1483. new_key.offset,
  1484. new_key.offset + datal,
  1485. &hint_byte, 1);
  1486. BUG_ON(ret);
  1487. ret = btrfs_insert_empty_item(trans, root, path,
  1488. &new_key, size);
  1489. BUG_ON(ret);
  1490. if (skip) {
  1491. u32 start =
  1492. btrfs_file_extent_calc_inline_size(0);
  1493. memmove(buf+start, buf+start+skip,
  1494. datal);
  1495. }
  1496. leaf = path->nodes[0];
  1497. slot = path->slots[0];
  1498. write_extent_buffer(leaf, buf,
  1499. btrfs_item_ptr_offset(leaf, slot),
  1500. size);
  1501. inode_add_bytes(inode, datal);
  1502. }
  1503. btrfs_mark_buffer_dirty(leaf);
  1504. btrfs_release_path(root, path);
  1505. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1506. /*
  1507. * we round up to the block size at eof when
  1508. * determining which extents to clone above,
  1509. * but shouldn't round up the file size
  1510. */
  1511. endoff = new_key.offset + datal;
  1512. if (endoff > off+olen)
  1513. endoff = off+olen;
  1514. if (endoff > inode->i_size)
  1515. btrfs_i_size_write(inode, endoff);
  1516. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1517. ret = btrfs_update_inode(trans, root, inode);
  1518. BUG_ON(ret);
  1519. btrfs_end_transaction(trans, root);
  1520. }
  1521. next:
  1522. btrfs_release_path(root, path);
  1523. key.offset++;
  1524. }
  1525. ret = 0;
  1526. out:
  1527. btrfs_release_path(root, path);
  1528. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1529. out_unlock:
  1530. mutex_unlock(&src->i_mutex);
  1531. mutex_unlock(&inode->i_mutex);
  1532. vfree(buf);
  1533. btrfs_free_path(path);
  1534. out_fput:
  1535. fput(src_file);
  1536. out_drop_write:
  1537. mnt_drop_write(file->f_path.mnt);
  1538. return ret;
  1539. }
  1540. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1541. {
  1542. struct btrfs_ioctl_clone_range_args args;
  1543. if (copy_from_user(&args, argp, sizeof(args)))
  1544. return -EFAULT;
  1545. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1546. args.src_length, args.dest_offset);
  1547. }
  1548. /*
  1549. * there are many ways the trans_start and trans_end ioctls can lead
  1550. * to deadlocks. They should only be used by applications that
  1551. * basically own the machine, and have a very in depth understanding
  1552. * of all the possible deadlocks and enospc problems.
  1553. */
  1554. static long btrfs_ioctl_trans_start(struct file *file)
  1555. {
  1556. struct inode *inode = fdentry(file)->d_inode;
  1557. struct btrfs_root *root = BTRFS_I(inode)->root;
  1558. struct btrfs_trans_handle *trans;
  1559. int ret;
  1560. ret = -EPERM;
  1561. if (!capable(CAP_SYS_ADMIN))
  1562. goto out;
  1563. ret = -EINPROGRESS;
  1564. if (file->private_data)
  1565. goto out;
  1566. ret = mnt_want_write(file->f_path.mnt);
  1567. if (ret)
  1568. goto out;
  1569. mutex_lock(&root->fs_info->trans_mutex);
  1570. root->fs_info->open_ioctl_trans++;
  1571. mutex_unlock(&root->fs_info->trans_mutex);
  1572. ret = -ENOMEM;
  1573. trans = btrfs_start_ioctl_transaction(root, 0);
  1574. if (!trans)
  1575. goto out_drop;
  1576. file->private_data = trans;
  1577. return 0;
  1578. out_drop:
  1579. mutex_lock(&root->fs_info->trans_mutex);
  1580. root->fs_info->open_ioctl_trans--;
  1581. mutex_unlock(&root->fs_info->trans_mutex);
  1582. mnt_drop_write(file->f_path.mnt);
  1583. out:
  1584. return ret;
  1585. }
  1586. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1587. {
  1588. struct inode *inode = fdentry(file)->d_inode;
  1589. struct btrfs_root *root = BTRFS_I(inode)->root;
  1590. struct btrfs_root *new_root;
  1591. struct btrfs_dir_item *di;
  1592. struct btrfs_trans_handle *trans;
  1593. struct btrfs_path *path;
  1594. struct btrfs_key location;
  1595. struct btrfs_disk_key disk_key;
  1596. struct btrfs_super_block *disk_super;
  1597. u64 features;
  1598. u64 objectid = 0;
  1599. u64 dir_id;
  1600. if (!capable(CAP_SYS_ADMIN))
  1601. return -EPERM;
  1602. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1603. return -EFAULT;
  1604. if (!objectid)
  1605. objectid = root->root_key.objectid;
  1606. location.objectid = objectid;
  1607. location.type = BTRFS_ROOT_ITEM_KEY;
  1608. location.offset = (u64)-1;
  1609. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1610. if (IS_ERR(new_root))
  1611. return PTR_ERR(new_root);
  1612. if (btrfs_root_refs(&new_root->root_item) == 0)
  1613. return -ENOENT;
  1614. path = btrfs_alloc_path();
  1615. if (!path)
  1616. return -ENOMEM;
  1617. path->leave_spinning = 1;
  1618. trans = btrfs_start_transaction(root, 1);
  1619. if (!trans) {
  1620. btrfs_free_path(path);
  1621. return -ENOMEM;
  1622. }
  1623. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1624. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1625. dir_id, "default", 7, 1);
  1626. if (IS_ERR_OR_NULL(di)) {
  1627. btrfs_free_path(path);
  1628. btrfs_end_transaction(trans, root);
  1629. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1630. "this isn't going to work\n");
  1631. return -ENOENT;
  1632. }
  1633. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1634. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1635. btrfs_mark_buffer_dirty(path->nodes[0]);
  1636. btrfs_free_path(path);
  1637. disk_super = &root->fs_info->super_copy;
  1638. features = btrfs_super_incompat_flags(disk_super);
  1639. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1640. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1641. btrfs_set_super_incompat_flags(disk_super, features);
  1642. }
  1643. btrfs_end_transaction(trans, root);
  1644. return 0;
  1645. }
  1646. static void get_block_group_info(struct list_head *groups_list,
  1647. struct btrfs_ioctl_space_info *space)
  1648. {
  1649. struct btrfs_block_group_cache *block_group;
  1650. space->total_bytes = 0;
  1651. space->used_bytes = 0;
  1652. space->flags = 0;
  1653. list_for_each_entry(block_group, groups_list, list) {
  1654. space->flags = block_group->flags;
  1655. space->total_bytes += block_group->key.offset;
  1656. space->used_bytes +=
  1657. btrfs_block_group_used(&block_group->item);
  1658. }
  1659. }
  1660. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1661. {
  1662. struct btrfs_ioctl_space_args space_args;
  1663. struct btrfs_ioctl_space_info space;
  1664. struct btrfs_ioctl_space_info *dest;
  1665. struct btrfs_ioctl_space_info *dest_orig;
  1666. struct btrfs_ioctl_space_info *user_dest;
  1667. struct btrfs_space_info *info;
  1668. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1669. BTRFS_BLOCK_GROUP_SYSTEM,
  1670. BTRFS_BLOCK_GROUP_METADATA,
  1671. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1672. int num_types = 4;
  1673. int alloc_size;
  1674. int ret = 0;
  1675. int slot_count = 0;
  1676. int i, c;
  1677. if (copy_from_user(&space_args,
  1678. (struct btrfs_ioctl_space_args __user *)arg,
  1679. sizeof(space_args)))
  1680. return -EFAULT;
  1681. for (i = 0; i < num_types; i++) {
  1682. struct btrfs_space_info *tmp;
  1683. info = NULL;
  1684. rcu_read_lock();
  1685. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1686. list) {
  1687. if (tmp->flags == types[i]) {
  1688. info = tmp;
  1689. break;
  1690. }
  1691. }
  1692. rcu_read_unlock();
  1693. if (!info)
  1694. continue;
  1695. down_read(&info->groups_sem);
  1696. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1697. if (!list_empty(&info->block_groups[c]))
  1698. slot_count++;
  1699. }
  1700. up_read(&info->groups_sem);
  1701. }
  1702. /* space_slots == 0 means they are asking for a count */
  1703. if (space_args.space_slots == 0) {
  1704. space_args.total_spaces = slot_count;
  1705. goto out;
  1706. }
  1707. slot_count = min_t(int, space_args.space_slots, slot_count);
  1708. alloc_size = sizeof(*dest) * slot_count;
  1709. /* we generally have at most 6 or so space infos, one for each raid
  1710. * level. So, a whole page should be more than enough for everyone
  1711. */
  1712. if (alloc_size > PAGE_CACHE_SIZE)
  1713. return -ENOMEM;
  1714. space_args.total_spaces = 0;
  1715. dest = kmalloc(alloc_size, GFP_NOFS);
  1716. if (!dest)
  1717. return -ENOMEM;
  1718. dest_orig = dest;
  1719. /* now we have a buffer to copy into */
  1720. for (i = 0; i < num_types; i++) {
  1721. struct btrfs_space_info *tmp;
  1722. info = NULL;
  1723. rcu_read_lock();
  1724. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1725. list) {
  1726. if (tmp->flags == types[i]) {
  1727. info = tmp;
  1728. break;
  1729. }
  1730. }
  1731. rcu_read_unlock();
  1732. if (!info)
  1733. continue;
  1734. down_read(&info->groups_sem);
  1735. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1736. if (!list_empty(&info->block_groups[c])) {
  1737. get_block_group_info(&info->block_groups[c],
  1738. &space);
  1739. memcpy(dest, &space, sizeof(space));
  1740. dest++;
  1741. space_args.total_spaces++;
  1742. }
  1743. }
  1744. up_read(&info->groups_sem);
  1745. }
  1746. user_dest = (struct btrfs_ioctl_space_info *)
  1747. (arg + sizeof(struct btrfs_ioctl_space_args));
  1748. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1749. ret = -EFAULT;
  1750. kfree(dest_orig);
  1751. out:
  1752. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1753. ret = -EFAULT;
  1754. return ret;
  1755. }
  1756. /*
  1757. * there are many ways the trans_start and trans_end ioctls can lead
  1758. * to deadlocks. They should only be used by applications that
  1759. * basically own the machine, and have a very in depth understanding
  1760. * of all the possible deadlocks and enospc problems.
  1761. */
  1762. long btrfs_ioctl_trans_end(struct file *file)
  1763. {
  1764. struct inode *inode = fdentry(file)->d_inode;
  1765. struct btrfs_root *root = BTRFS_I(inode)->root;
  1766. struct btrfs_trans_handle *trans;
  1767. trans = file->private_data;
  1768. if (!trans)
  1769. return -EINVAL;
  1770. file->private_data = NULL;
  1771. btrfs_end_transaction(trans, root);
  1772. mutex_lock(&root->fs_info->trans_mutex);
  1773. root->fs_info->open_ioctl_trans--;
  1774. mutex_unlock(&root->fs_info->trans_mutex);
  1775. mnt_drop_write(file->f_path.mnt);
  1776. return 0;
  1777. }
  1778. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1779. {
  1780. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1781. struct btrfs_trans_handle *trans;
  1782. u64 transid;
  1783. trans = btrfs_start_transaction(root, 0);
  1784. transid = trans->transid;
  1785. btrfs_commit_transaction_async(trans, root, 0);
  1786. if (argp)
  1787. if (copy_to_user(argp, &transid, sizeof(transid)))
  1788. return -EFAULT;
  1789. return 0;
  1790. }
  1791. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  1792. {
  1793. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1794. u64 transid;
  1795. if (argp) {
  1796. if (copy_from_user(&transid, argp, sizeof(transid)))
  1797. return -EFAULT;
  1798. } else {
  1799. transid = 0; /* current trans */
  1800. }
  1801. return btrfs_wait_for_commit(root, transid);
  1802. }
  1803. long btrfs_ioctl(struct file *file, unsigned int
  1804. cmd, unsigned long arg)
  1805. {
  1806. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1807. void __user *argp = (void __user *)arg;
  1808. switch (cmd) {
  1809. case FS_IOC_GETFLAGS:
  1810. return btrfs_ioctl_getflags(file, argp);
  1811. case FS_IOC_SETFLAGS:
  1812. return btrfs_ioctl_setflags(file, argp);
  1813. case FS_IOC_GETVERSION:
  1814. return btrfs_ioctl_getversion(file, argp);
  1815. case BTRFS_IOC_SNAP_CREATE:
  1816. return btrfs_ioctl_snap_create(file, argp, 0, 0);
  1817. case BTRFS_IOC_SNAP_CREATE_ASYNC:
  1818. return btrfs_ioctl_snap_create(file, argp, 0, 1);
  1819. case BTRFS_IOC_SUBVOL_CREATE:
  1820. return btrfs_ioctl_snap_create(file, argp, 1, 0);
  1821. case BTRFS_IOC_SNAP_DESTROY:
  1822. return btrfs_ioctl_snap_destroy(file, argp);
  1823. case BTRFS_IOC_DEFAULT_SUBVOL:
  1824. return btrfs_ioctl_default_subvol(file, argp);
  1825. case BTRFS_IOC_DEFRAG:
  1826. return btrfs_ioctl_defrag(file, NULL);
  1827. case BTRFS_IOC_DEFRAG_RANGE:
  1828. return btrfs_ioctl_defrag(file, argp);
  1829. case BTRFS_IOC_RESIZE:
  1830. return btrfs_ioctl_resize(root, argp);
  1831. case BTRFS_IOC_ADD_DEV:
  1832. return btrfs_ioctl_add_dev(root, argp);
  1833. case BTRFS_IOC_RM_DEV:
  1834. return btrfs_ioctl_rm_dev(root, argp);
  1835. case BTRFS_IOC_BALANCE:
  1836. return btrfs_balance(root->fs_info->dev_root);
  1837. case BTRFS_IOC_CLONE:
  1838. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1839. case BTRFS_IOC_CLONE_RANGE:
  1840. return btrfs_ioctl_clone_range(file, argp);
  1841. case BTRFS_IOC_TRANS_START:
  1842. return btrfs_ioctl_trans_start(file);
  1843. case BTRFS_IOC_TRANS_END:
  1844. return btrfs_ioctl_trans_end(file);
  1845. case BTRFS_IOC_TREE_SEARCH:
  1846. return btrfs_ioctl_tree_search(file, argp);
  1847. case BTRFS_IOC_INO_LOOKUP:
  1848. return btrfs_ioctl_ino_lookup(file, argp);
  1849. case BTRFS_IOC_SPACE_INFO:
  1850. return btrfs_ioctl_space_info(root, argp);
  1851. case BTRFS_IOC_SYNC:
  1852. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1853. return 0;
  1854. case BTRFS_IOC_START_SYNC:
  1855. return btrfs_ioctl_start_sync(file, argp);
  1856. case BTRFS_IOC_WAIT_SYNC:
  1857. return btrfs_ioctl_wait_sync(file, argp);
  1858. }
  1859. return -ENOTTY;
  1860. }