ioctl.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  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 check_sticky in fs/namei.c()
  353. * It's inline, so penalty for filesystems that don't use sticky bit is
  354. * minimal.
  355. */
  356. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  357. {
  358. uid_t fsuid = current_fsuid();
  359. if (!(dir->i_mode & S_ISVTX))
  360. return 0;
  361. if (inode->i_uid == fsuid)
  362. return 0;
  363. if (dir->i_uid == fsuid)
  364. return 0;
  365. return !capable(CAP_FOWNER);
  366. }
  367. /* copy of may_delete in fs/namei.c()
  368. * Check whether we can remove a link victim from directory dir, check
  369. * whether the type of victim is right.
  370. * 1. We can't do it if dir is read-only (done in permission())
  371. * 2. We should have write and exec permissions on dir
  372. * 3. We can't remove anything from append-only dir
  373. * 4. We can't do anything with immutable dir (done in permission())
  374. * 5. If the sticky bit on dir is set we should either
  375. * a. be owner of dir, or
  376. * b. be owner of victim, or
  377. * c. have CAP_FOWNER capability
  378. * 6. If the victim is append-only or immutable we can't do antyhing with
  379. * links pointing to it.
  380. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  381. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  382. * 9. We can't remove a root or mountpoint.
  383. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  384. * nfs_async_unlink().
  385. */
  386. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  387. {
  388. int error;
  389. if (!victim->d_inode)
  390. return -ENOENT;
  391. BUG_ON(victim->d_parent->d_inode != dir);
  392. audit_inode_child(victim, dir);
  393. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  394. if (error)
  395. return error;
  396. if (IS_APPEND(dir))
  397. return -EPERM;
  398. if (btrfs_check_sticky(dir, victim->d_inode)||
  399. IS_APPEND(victim->d_inode)||
  400. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  401. return -EPERM;
  402. if (isdir) {
  403. if (!S_ISDIR(victim->d_inode->i_mode))
  404. return -ENOTDIR;
  405. if (IS_ROOT(victim))
  406. return -EBUSY;
  407. } else if (S_ISDIR(victim->d_inode->i_mode))
  408. return -EISDIR;
  409. if (IS_DEADDIR(dir))
  410. return -ENOENT;
  411. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  412. return -EBUSY;
  413. return 0;
  414. }
  415. /* copy of may_create in fs/namei.c() */
  416. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  417. {
  418. if (child->d_inode)
  419. return -EEXIST;
  420. if (IS_DEADDIR(dir))
  421. return -ENOENT;
  422. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  423. }
  424. /*
  425. * Create a new subvolume below @parent. This is largely modeled after
  426. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  427. * inside this filesystem so it's quite a bit simpler.
  428. */
  429. static noinline int btrfs_mksubvol(struct path *parent,
  430. char *name, int namelen,
  431. struct btrfs_root *snap_src,
  432. u64 *async_transid)
  433. {
  434. struct inode *dir = parent->dentry->d_inode;
  435. struct dentry *dentry;
  436. int error;
  437. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  438. dentry = lookup_one_len(name, parent->dentry, namelen);
  439. error = PTR_ERR(dentry);
  440. if (IS_ERR(dentry))
  441. goto out_unlock;
  442. error = -EEXIST;
  443. if (dentry->d_inode)
  444. goto out_dput;
  445. error = mnt_want_write(parent->mnt);
  446. if (error)
  447. goto out_dput;
  448. error = btrfs_may_create(dir, dentry);
  449. if (error)
  450. goto out_drop_write;
  451. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  452. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  453. goto out_up_read;
  454. if (snap_src) {
  455. error = create_snapshot(snap_src, dentry,
  456. name, namelen, async_transid);
  457. } else {
  458. error = create_subvol(BTRFS_I(dir)->root, dentry,
  459. name, namelen, async_transid);
  460. }
  461. if (!error)
  462. fsnotify_mkdir(dir, dentry);
  463. out_up_read:
  464. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  465. out_drop_write:
  466. mnt_drop_write(parent->mnt);
  467. out_dput:
  468. dput(dentry);
  469. out_unlock:
  470. mutex_unlock(&dir->i_mutex);
  471. return error;
  472. }
  473. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  474. int thresh, u64 *last_len, u64 *skip,
  475. u64 *defrag_end)
  476. {
  477. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  478. struct extent_map *em = NULL;
  479. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  480. int ret = 1;
  481. if (thresh == 0)
  482. thresh = 256 * 1024;
  483. /*
  484. * make sure that once we start defragging and extent, we keep on
  485. * defragging it
  486. */
  487. if (start < *defrag_end)
  488. return 1;
  489. *skip = 0;
  490. /*
  491. * hopefully we have this extent in the tree already, try without
  492. * the full extent lock
  493. */
  494. read_lock(&em_tree->lock);
  495. em = lookup_extent_mapping(em_tree, start, len);
  496. read_unlock(&em_tree->lock);
  497. if (!em) {
  498. /* get the big lock and read metadata off disk */
  499. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  500. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  501. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  502. if (IS_ERR(em))
  503. return 0;
  504. }
  505. /* this will cover holes, and inline extents */
  506. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  507. ret = 0;
  508. /*
  509. * we hit a real extent, if it is big don't bother defragging it again
  510. */
  511. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  512. ret = 0;
  513. /*
  514. * last_len ends up being a counter of how many bytes we've defragged.
  515. * every time we choose not to defrag an extent, we reset *last_len
  516. * so that the next tiny extent will force a defrag.
  517. *
  518. * The end result of this is that tiny extents before a single big
  519. * extent will force at least part of that big extent to be defragged.
  520. */
  521. if (ret) {
  522. *last_len += len;
  523. *defrag_end = extent_map_end(em);
  524. } else {
  525. *last_len = 0;
  526. *skip = extent_map_end(em);
  527. *defrag_end = 0;
  528. }
  529. free_extent_map(em);
  530. return ret;
  531. }
  532. static int btrfs_defrag_file(struct file *file,
  533. struct btrfs_ioctl_defrag_range_args *range)
  534. {
  535. struct inode *inode = fdentry(file)->d_inode;
  536. struct btrfs_root *root = BTRFS_I(inode)->root;
  537. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  538. struct btrfs_ordered_extent *ordered;
  539. struct page *page;
  540. unsigned long last_index;
  541. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  542. unsigned long total_read = 0;
  543. u64 page_start;
  544. u64 page_end;
  545. u64 last_len = 0;
  546. u64 skip = 0;
  547. u64 defrag_end = 0;
  548. unsigned long i;
  549. int ret;
  550. if (inode->i_size == 0)
  551. return 0;
  552. if (range->start + range->len > range->start) {
  553. last_index = min_t(u64, inode->i_size - 1,
  554. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  555. } else {
  556. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  557. }
  558. i = range->start >> PAGE_CACHE_SHIFT;
  559. while (i <= last_index) {
  560. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  561. PAGE_CACHE_SIZE,
  562. range->extent_thresh,
  563. &last_len, &skip,
  564. &defrag_end)) {
  565. unsigned long next;
  566. /*
  567. * the should_defrag function tells us how much to skip
  568. * bump our counter by the suggested amount
  569. */
  570. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  571. i = max(i + 1, next);
  572. continue;
  573. }
  574. if (total_read % ra_pages == 0) {
  575. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  576. min(last_index, i + ra_pages - 1));
  577. }
  578. total_read++;
  579. mutex_lock(&inode->i_mutex);
  580. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  581. BTRFS_I(inode)->force_compress = 1;
  582. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  583. if (ret)
  584. goto err_unlock;
  585. again:
  586. if (inode->i_size == 0 ||
  587. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  588. ret = 0;
  589. goto err_reservations;
  590. }
  591. page = grab_cache_page(inode->i_mapping, i);
  592. if (!page) {
  593. ret = -ENOMEM;
  594. goto err_reservations;
  595. }
  596. if (!PageUptodate(page)) {
  597. btrfs_readpage(NULL, page);
  598. lock_page(page);
  599. if (!PageUptodate(page)) {
  600. unlock_page(page);
  601. page_cache_release(page);
  602. ret = -EIO;
  603. goto err_reservations;
  604. }
  605. }
  606. if (page->mapping != inode->i_mapping) {
  607. unlock_page(page);
  608. page_cache_release(page);
  609. goto again;
  610. }
  611. wait_on_page_writeback(page);
  612. if (PageDirty(page)) {
  613. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  614. goto loop_unlock;
  615. }
  616. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  617. page_end = page_start + PAGE_CACHE_SIZE - 1;
  618. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  619. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  620. if (ordered) {
  621. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  622. unlock_page(page);
  623. page_cache_release(page);
  624. btrfs_start_ordered_extent(inode, ordered, 1);
  625. btrfs_put_ordered_extent(ordered);
  626. goto again;
  627. }
  628. set_page_extent_mapped(page);
  629. /*
  630. * this makes sure page_mkwrite is called on the
  631. * page if it is dirtied again later
  632. */
  633. clear_page_dirty_for_io(page);
  634. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  635. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  636. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  637. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  638. ClearPageChecked(page);
  639. set_page_dirty(page);
  640. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  641. loop_unlock:
  642. unlock_page(page);
  643. page_cache_release(page);
  644. mutex_unlock(&inode->i_mutex);
  645. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  646. i++;
  647. }
  648. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  649. filemap_flush(inode->i_mapping);
  650. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  651. /* the filemap_flush will queue IO into the worker threads, but
  652. * we have to make sure the IO is actually started and that
  653. * ordered extents get created before we return
  654. */
  655. atomic_inc(&root->fs_info->async_submit_draining);
  656. while (atomic_read(&root->fs_info->nr_async_submits) ||
  657. atomic_read(&root->fs_info->async_delalloc_pages)) {
  658. wait_event(root->fs_info->async_submit_wait,
  659. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  660. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  661. }
  662. atomic_dec(&root->fs_info->async_submit_draining);
  663. mutex_lock(&inode->i_mutex);
  664. BTRFS_I(inode)->force_compress = 0;
  665. mutex_unlock(&inode->i_mutex);
  666. }
  667. return 0;
  668. err_reservations:
  669. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  670. err_unlock:
  671. mutex_unlock(&inode->i_mutex);
  672. return ret;
  673. }
  674. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  675. void __user *arg)
  676. {
  677. u64 new_size;
  678. u64 old_size;
  679. u64 devid = 1;
  680. struct btrfs_ioctl_vol_args *vol_args;
  681. struct btrfs_trans_handle *trans;
  682. struct btrfs_device *device = NULL;
  683. char *sizestr;
  684. char *devstr = NULL;
  685. int ret = 0;
  686. int mod = 0;
  687. if (root->fs_info->sb->s_flags & MS_RDONLY)
  688. return -EROFS;
  689. if (!capable(CAP_SYS_ADMIN))
  690. return -EPERM;
  691. vol_args = memdup_user(arg, sizeof(*vol_args));
  692. if (IS_ERR(vol_args))
  693. return PTR_ERR(vol_args);
  694. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  695. mutex_lock(&root->fs_info->volume_mutex);
  696. sizestr = vol_args->name;
  697. devstr = strchr(sizestr, ':');
  698. if (devstr) {
  699. char *end;
  700. sizestr = devstr + 1;
  701. *devstr = '\0';
  702. devstr = vol_args->name;
  703. devid = simple_strtoull(devstr, &end, 10);
  704. printk(KERN_INFO "resizing devid %llu\n",
  705. (unsigned long long)devid);
  706. }
  707. device = btrfs_find_device(root, devid, NULL, NULL);
  708. if (!device) {
  709. printk(KERN_INFO "resizer unable to find device %llu\n",
  710. (unsigned long long)devid);
  711. ret = -EINVAL;
  712. goto out_unlock;
  713. }
  714. if (!strcmp(sizestr, "max"))
  715. new_size = device->bdev->bd_inode->i_size;
  716. else {
  717. if (sizestr[0] == '-') {
  718. mod = -1;
  719. sizestr++;
  720. } else if (sizestr[0] == '+') {
  721. mod = 1;
  722. sizestr++;
  723. }
  724. new_size = memparse(sizestr, NULL);
  725. if (new_size == 0) {
  726. ret = -EINVAL;
  727. goto out_unlock;
  728. }
  729. }
  730. old_size = device->total_bytes;
  731. if (mod < 0) {
  732. if (new_size > old_size) {
  733. ret = -EINVAL;
  734. goto out_unlock;
  735. }
  736. new_size = old_size - new_size;
  737. } else if (mod > 0) {
  738. new_size = old_size + new_size;
  739. }
  740. if (new_size < 256 * 1024 * 1024) {
  741. ret = -EINVAL;
  742. goto out_unlock;
  743. }
  744. if (new_size > device->bdev->bd_inode->i_size) {
  745. ret = -EFBIG;
  746. goto out_unlock;
  747. }
  748. do_div(new_size, root->sectorsize);
  749. new_size *= root->sectorsize;
  750. printk(KERN_INFO "new size for %s is %llu\n",
  751. device->name, (unsigned long long)new_size);
  752. if (new_size > old_size) {
  753. trans = btrfs_start_transaction(root, 0);
  754. ret = btrfs_grow_device(trans, device, new_size);
  755. btrfs_commit_transaction(trans, root);
  756. } else {
  757. ret = btrfs_shrink_device(device, new_size);
  758. }
  759. out_unlock:
  760. mutex_unlock(&root->fs_info->volume_mutex);
  761. kfree(vol_args);
  762. return ret;
  763. }
  764. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  765. char *name,
  766. unsigned long fd,
  767. int subvol,
  768. u64 *transid)
  769. {
  770. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  771. struct file *src_file;
  772. int namelen;
  773. int ret = 0;
  774. if (root->fs_info->sb->s_flags & MS_RDONLY)
  775. return -EROFS;
  776. namelen = strlen(name);
  777. if (strchr(name, '/')) {
  778. ret = -EINVAL;
  779. goto out;
  780. }
  781. if (subvol) {
  782. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  783. NULL, transid);
  784. } else {
  785. struct inode *src_inode;
  786. src_file = fget(fd);
  787. if (!src_file) {
  788. ret = -EINVAL;
  789. goto out;
  790. }
  791. src_inode = src_file->f_path.dentry->d_inode;
  792. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  793. printk(KERN_INFO "btrfs: Snapshot src from "
  794. "another FS\n");
  795. ret = -EINVAL;
  796. fput(src_file);
  797. goto out;
  798. }
  799. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  800. BTRFS_I(src_inode)->root,
  801. transid);
  802. fput(src_file);
  803. }
  804. out:
  805. return ret;
  806. }
  807. static noinline int btrfs_ioctl_snap_create(struct file *file,
  808. void __user *arg, int subvol,
  809. int async)
  810. {
  811. struct btrfs_ioctl_vol_args *vol_args = NULL;
  812. struct btrfs_ioctl_async_vol_args *async_vol_args = NULL;
  813. char *name;
  814. u64 fd;
  815. u64 transid = 0;
  816. int ret;
  817. if (async) {
  818. async_vol_args = memdup_user(arg, sizeof(*async_vol_args));
  819. if (IS_ERR(async_vol_args))
  820. return PTR_ERR(async_vol_args);
  821. name = async_vol_args->name;
  822. fd = async_vol_args->fd;
  823. async_vol_args->name[BTRFS_SNAPSHOT_NAME_MAX] = '\0';
  824. } else {
  825. vol_args = memdup_user(arg, sizeof(*vol_args));
  826. if (IS_ERR(vol_args))
  827. return PTR_ERR(vol_args);
  828. name = vol_args->name;
  829. fd = vol_args->fd;
  830. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  831. }
  832. ret = btrfs_ioctl_snap_create_transid(file, name, fd,
  833. subvol, &transid);
  834. if (!ret && async) {
  835. if (copy_to_user(arg +
  836. offsetof(struct btrfs_ioctl_async_vol_args,
  837. transid), &transid, sizeof(transid)))
  838. return -EFAULT;
  839. }
  840. kfree(vol_args);
  841. kfree(async_vol_args);
  842. return ret;
  843. }
  844. /*
  845. * helper to check if the subvolume references other subvolumes
  846. */
  847. static noinline int may_destroy_subvol(struct btrfs_root *root)
  848. {
  849. struct btrfs_path *path;
  850. struct btrfs_key key;
  851. int ret;
  852. path = btrfs_alloc_path();
  853. if (!path)
  854. return -ENOMEM;
  855. key.objectid = root->root_key.objectid;
  856. key.type = BTRFS_ROOT_REF_KEY;
  857. key.offset = (u64)-1;
  858. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  859. &key, path, 0, 0);
  860. if (ret < 0)
  861. goto out;
  862. BUG_ON(ret == 0);
  863. ret = 0;
  864. if (path->slots[0] > 0) {
  865. path->slots[0]--;
  866. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  867. if (key.objectid == root->root_key.objectid &&
  868. key.type == BTRFS_ROOT_REF_KEY)
  869. ret = -ENOTEMPTY;
  870. }
  871. out:
  872. btrfs_free_path(path);
  873. return ret;
  874. }
  875. static noinline int key_in_sk(struct btrfs_key *key,
  876. struct btrfs_ioctl_search_key *sk)
  877. {
  878. struct btrfs_key test;
  879. int ret;
  880. test.objectid = sk->min_objectid;
  881. test.type = sk->min_type;
  882. test.offset = sk->min_offset;
  883. ret = btrfs_comp_cpu_keys(key, &test);
  884. if (ret < 0)
  885. return 0;
  886. test.objectid = sk->max_objectid;
  887. test.type = sk->max_type;
  888. test.offset = sk->max_offset;
  889. ret = btrfs_comp_cpu_keys(key, &test);
  890. if (ret > 0)
  891. return 0;
  892. return 1;
  893. }
  894. static noinline int copy_to_sk(struct btrfs_root *root,
  895. struct btrfs_path *path,
  896. struct btrfs_key *key,
  897. struct btrfs_ioctl_search_key *sk,
  898. char *buf,
  899. unsigned long *sk_offset,
  900. int *num_found)
  901. {
  902. u64 found_transid;
  903. struct extent_buffer *leaf;
  904. struct btrfs_ioctl_search_header sh;
  905. unsigned long item_off;
  906. unsigned long item_len;
  907. int nritems;
  908. int i;
  909. int slot;
  910. int found = 0;
  911. int ret = 0;
  912. leaf = path->nodes[0];
  913. slot = path->slots[0];
  914. nritems = btrfs_header_nritems(leaf);
  915. if (btrfs_header_generation(leaf) > sk->max_transid) {
  916. i = nritems;
  917. goto advance_key;
  918. }
  919. found_transid = btrfs_header_generation(leaf);
  920. for (i = slot; i < nritems; i++) {
  921. item_off = btrfs_item_ptr_offset(leaf, i);
  922. item_len = btrfs_item_size_nr(leaf, i);
  923. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  924. item_len = 0;
  925. if (sizeof(sh) + item_len + *sk_offset >
  926. BTRFS_SEARCH_ARGS_BUFSIZE) {
  927. ret = 1;
  928. goto overflow;
  929. }
  930. btrfs_item_key_to_cpu(leaf, key, i);
  931. if (!key_in_sk(key, sk))
  932. continue;
  933. sh.objectid = key->objectid;
  934. sh.offset = key->offset;
  935. sh.type = key->type;
  936. sh.len = item_len;
  937. sh.transid = found_transid;
  938. /* copy search result header */
  939. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  940. *sk_offset += sizeof(sh);
  941. if (item_len) {
  942. char *p = buf + *sk_offset;
  943. /* copy the item */
  944. read_extent_buffer(leaf, p,
  945. item_off, item_len);
  946. *sk_offset += item_len;
  947. }
  948. found++;
  949. if (*num_found >= sk->nr_items)
  950. break;
  951. }
  952. advance_key:
  953. ret = 0;
  954. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  955. key->offset++;
  956. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  957. key->offset = 0;
  958. key->type++;
  959. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  960. key->offset = 0;
  961. key->type = 0;
  962. key->objectid++;
  963. } else
  964. ret = 1;
  965. overflow:
  966. *num_found += found;
  967. return ret;
  968. }
  969. static noinline int search_ioctl(struct inode *inode,
  970. struct btrfs_ioctl_search_args *args)
  971. {
  972. struct btrfs_root *root;
  973. struct btrfs_key key;
  974. struct btrfs_key max_key;
  975. struct btrfs_path *path;
  976. struct btrfs_ioctl_search_key *sk = &args->key;
  977. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  978. int ret;
  979. int num_found = 0;
  980. unsigned long sk_offset = 0;
  981. path = btrfs_alloc_path();
  982. if (!path)
  983. return -ENOMEM;
  984. if (sk->tree_id == 0) {
  985. /* search the root of the inode that was passed */
  986. root = BTRFS_I(inode)->root;
  987. } else {
  988. key.objectid = sk->tree_id;
  989. key.type = BTRFS_ROOT_ITEM_KEY;
  990. key.offset = (u64)-1;
  991. root = btrfs_read_fs_root_no_name(info, &key);
  992. if (IS_ERR(root)) {
  993. printk(KERN_ERR "could not find root %llu\n",
  994. sk->tree_id);
  995. btrfs_free_path(path);
  996. return -ENOENT;
  997. }
  998. }
  999. key.objectid = sk->min_objectid;
  1000. key.type = sk->min_type;
  1001. key.offset = sk->min_offset;
  1002. max_key.objectid = sk->max_objectid;
  1003. max_key.type = sk->max_type;
  1004. max_key.offset = sk->max_offset;
  1005. path->keep_locks = 1;
  1006. while(1) {
  1007. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1008. sk->min_transid);
  1009. if (ret != 0) {
  1010. if (ret > 0)
  1011. ret = 0;
  1012. goto err;
  1013. }
  1014. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1015. &sk_offset, &num_found);
  1016. btrfs_release_path(root, path);
  1017. if (ret || num_found >= sk->nr_items)
  1018. break;
  1019. }
  1020. ret = 0;
  1021. err:
  1022. sk->nr_items = num_found;
  1023. btrfs_free_path(path);
  1024. return ret;
  1025. }
  1026. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1027. void __user *argp)
  1028. {
  1029. struct btrfs_ioctl_search_args *args;
  1030. struct inode *inode;
  1031. int ret;
  1032. if (!capable(CAP_SYS_ADMIN))
  1033. return -EPERM;
  1034. args = memdup_user(argp, sizeof(*args));
  1035. if (IS_ERR(args))
  1036. return PTR_ERR(args);
  1037. inode = fdentry(file)->d_inode;
  1038. ret = search_ioctl(inode, args);
  1039. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1040. ret = -EFAULT;
  1041. kfree(args);
  1042. return ret;
  1043. }
  1044. /*
  1045. * Search INODE_REFs to identify path name of 'dirid' directory
  1046. * in a 'tree_id' tree. and sets path name to 'name'.
  1047. */
  1048. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1049. u64 tree_id, u64 dirid, char *name)
  1050. {
  1051. struct btrfs_root *root;
  1052. struct btrfs_key key;
  1053. char *ptr;
  1054. int ret = -1;
  1055. int slot;
  1056. int len;
  1057. int total_len = 0;
  1058. struct btrfs_inode_ref *iref;
  1059. struct extent_buffer *l;
  1060. struct btrfs_path *path;
  1061. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1062. name[0]='\0';
  1063. return 0;
  1064. }
  1065. path = btrfs_alloc_path();
  1066. if (!path)
  1067. return -ENOMEM;
  1068. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1069. key.objectid = tree_id;
  1070. key.type = BTRFS_ROOT_ITEM_KEY;
  1071. key.offset = (u64)-1;
  1072. root = btrfs_read_fs_root_no_name(info, &key);
  1073. if (IS_ERR(root)) {
  1074. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1075. ret = -ENOENT;
  1076. goto out;
  1077. }
  1078. key.objectid = dirid;
  1079. key.type = BTRFS_INODE_REF_KEY;
  1080. key.offset = (u64)-1;
  1081. while(1) {
  1082. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1083. if (ret < 0)
  1084. goto out;
  1085. l = path->nodes[0];
  1086. slot = path->slots[0];
  1087. if (ret > 0 && slot > 0)
  1088. slot--;
  1089. btrfs_item_key_to_cpu(l, &key, slot);
  1090. if (ret > 0 && (key.objectid != dirid ||
  1091. key.type != BTRFS_INODE_REF_KEY)) {
  1092. ret = -ENOENT;
  1093. goto out;
  1094. }
  1095. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1096. len = btrfs_inode_ref_name_len(l, iref);
  1097. ptr -= len + 1;
  1098. total_len += len + 1;
  1099. if (ptr < name)
  1100. goto out;
  1101. *(ptr + len) = '/';
  1102. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1103. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1104. break;
  1105. btrfs_release_path(root, path);
  1106. key.objectid = key.offset;
  1107. key.offset = (u64)-1;
  1108. dirid = key.objectid;
  1109. }
  1110. if (ptr < name)
  1111. goto out;
  1112. memcpy(name, ptr, total_len);
  1113. name[total_len]='\0';
  1114. ret = 0;
  1115. out:
  1116. btrfs_free_path(path);
  1117. return ret;
  1118. }
  1119. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1120. void __user *argp)
  1121. {
  1122. struct btrfs_ioctl_ino_lookup_args *args;
  1123. struct inode *inode;
  1124. int ret;
  1125. if (!capable(CAP_SYS_ADMIN))
  1126. return -EPERM;
  1127. args = memdup_user(argp, sizeof(*args));
  1128. if (IS_ERR(args))
  1129. return PTR_ERR(args);
  1130. inode = fdentry(file)->d_inode;
  1131. if (args->treeid == 0)
  1132. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1133. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1134. args->treeid, args->objectid,
  1135. args->name);
  1136. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1137. ret = -EFAULT;
  1138. kfree(args);
  1139. return ret;
  1140. }
  1141. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1142. void __user *arg)
  1143. {
  1144. struct dentry *parent = fdentry(file);
  1145. struct dentry *dentry;
  1146. struct inode *dir = parent->d_inode;
  1147. struct inode *inode;
  1148. struct btrfs_root *root = BTRFS_I(dir)->root;
  1149. struct btrfs_root *dest = NULL;
  1150. struct btrfs_ioctl_vol_args *vol_args;
  1151. struct btrfs_trans_handle *trans;
  1152. int namelen;
  1153. int ret;
  1154. int err = 0;
  1155. vol_args = memdup_user(arg, sizeof(*vol_args));
  1156. if (IS_ERR(vol_args))
  1157. return PTR_ERR(vol_args);
  1158. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1159. namelen = strlen(vol_args->name);
  1160. if (strchr(vol_args->name, '/') ||
  1161. strncmp(vol_args->name, "..", namelen) == 0) {
  1162. err = -EINVAL;
  1163. goto out;
  1164. }
  1165. err = mnt_want_write(file->f_path.mnt);
  1166. if (err)
  1167. goto out;
  1168. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1169. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1170. if (IS_ERR(dentry)) {
  1171. err = PTR_ERR(dentry);
  1172. goto out_unlock_dir;
  1173. }
  1174. if (!dentry->d_inode) {
  1175. err = -ENOENT;
  1176. goto out_dput;
  1177. }
  1178. inode = dentry->d_inode;
  1179. dest = BTRFS_I(inode)->root;
  1180. if (!capable(CAP_SYS_ADMIN)){
  1181. /*
  1182. * Regular user. Only allow this with a special mount
  1183. * option, when the user has write+exec access to the
  1184. * subvol root, and when rmdir(2) would have been
  1185. * allowed.
  1186. *
  1187. * Note that this is _not_ check that the subvol is
  1188. * empty or doesn't contain data that we wouldn't
  1189. * otherwise be able to delete.
  1190. *
  1191. * Users who want to delete empty subvols should try
  1192. * rmdir(2).
  1193. */
  1194. err = -EPERM;
  1195. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1196. goto out_dput;
  1197. /*
  1198. * Do not allow deletion if the parent dir is the same
  1199. * as the dir to be deleted. That means the ioctl
  1200. * must be called on the dentry referencing the root
  1201. * of the subvol, not a random directory contained
  1202. * within it.
  1203. */
  1204. err = -EINVAL;
  1205. if (root == dest)
  1206. goto out_dput;
  1207. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1208. if (err)
  1209. goto out_dput;
  1210. /* check if subvolume may be deleted by a non-root user */
  1211. err = btrfs_may_delete(dir, dentry, 1);
  1212. if (err)
  1213. goto out_dput;
  1214. }
  1215. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1216. err = -EINVAL;
  1217. goto out_dput;
  1218. }
  1219. mutex_lock(&inode->i_mutex);
  1220. err = d_invalidate(dentry);
  1221. if (err)
  1222. goto out_unlock;
  1223. down_write(&root->fs_info->subvol_sem);
  1224. err = may_destroy_subvol(dest);
  1225. if (err)
  1226. goto out_up_write;
  1227. trans = btrfs_start_transaction(root, 0);
  1228. if (IS_ERR(trans)) {
  1229. err = PTR_ERR(trans);
  1230. goto out_up_write;
  1231. }
  1232. trans->block_rsv = &root->fs_info->global_block_rsv;
  1233. ret = btrfs_unlink_subvol(trans, root, dir,
  1234. dest->root_key.objectid,
  1235. dentry->d_name.name,
  1236. dentry->d_name.len);
  1237. BUG_ON(ret);
  1238. btrfs_record_root_in_trans(trans, dest);
  1239. memset(&dest->root_item.drop_progress, 0,
  1240. sizeof(dest->root_item.drop_progress));
  1241. dest->root_item.drop_level = 0;
  1242. btrfs_set_root_refs(&dest->root_item, 0);
  1243. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1244. ret = btrfs_insert_orphan_item(trans,
  1245. root->fs_info->tree_root,
  1246. dest->root_key.objectid);
  1247. BUG_ON(ret);
  1248. }
  1249. ret = btrfs_end_transaction(trans, root);
  1250. BUG_ON(ret);
  1251. inode->i_flags |= S_DEAD;
  1252. out_up_write:
  1253. up_write(&root->fs_info->subvol_sem);
  1254. out_unlock:
  1255. mutex_unlock(&inode->i_mutex);
  1256. if (!err) {
  1257. shrink_dcache_sb(root->fs_info->sb);
  1258. btrfs_invalidate_inodes(dest);
  1259. d_delete(dentry);
  1260. }
  1261. out_dput:
  1262. dput(dentry);
  1263. out_unlock_dir:
  1264. mutex_unlock(&dir->i_mutex);
  1265. mnt_drop_write(file->f_path.mnt);
  1266. out:
  1267. kfree(vol_args);
  1268. return err;
  1269. }
  1270. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1271. {
  1272. struct inode *inode = fdentry(file)->d_inode;
  1273. struct btrfs_root *root = BTRFS_I(inode)->root;
  1274. struct btrfs_ioctl_defrag_range_args *range;
  1275. int ret;
  1276. ret = mnt_want_write(file->f_path.mnt);
  1277. if (ret)
  1278. return ret;
  1279. switch (inode->i_mode & S_IFMT) {
  1280. case S_IFDIR:
  1281. if (!capable(CAP_SYS_ADMIN)) {
  1282. ret = -EPERM;
  1283. goto out;
  1284. }
  1285. ret = btrfs_defrag_root(root, 0);
  1286. if (ret)
  1287. goto out;
  1288. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1289. break;
  1290. case S_IFREG:
  1291. if (!(file->f_mode & FMODE_WRITE)) {
  1292. ret = -EINVAL;
  1293. goto out;
  1294. }
  1295. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1296. if (!range) {
  1297. ret = -ENOMEM;
  1298. goto out;
  1299. }
  1300. if (argp) {
  1301. if (copy_from_user(range, argp,
  1302. sizeof(*range))) {
  1303. ret = -EFAULT;
  1304. kfree(range);
  1305. goto out;
  1306. }
  1307. /* compression requires us to start the IO */
  1308. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1309. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1310. range->extent_thresh = (u32)-1;
  1311. }
  1312. } else {
  1313. /* the rest are all set to zero by kzalloc */
  1314. range->len = (u64)-1;
  1315. }
  1316. ret = btrfs_defrag_file(file, range);
  1317. kfree(range);
  1318. break;
  1319. default:
  1320. ret = -EINVAL;
  1321. }
  1322. out:
  1323. mnt_drop_write(file->f_path.mnt);
  1324. return ret;
  1325. }
  1326. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1327. {
  1328. struct btrfs_ioctl_vol_args *vol_args;
  1329. int ret;
  1330. if (!capable(CAP_SYS_ADMIN))
  1331. return -EPERM;
  1332. vol_args = memdup_user(arg, sizeof(*vol_args));
  1333. if (IS_ERR(vol_args))
  1334. return PTR_ERR(vol_args);
  1335. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1336. ret = btrfs_init_new_device(root, vol_args->name);
  1337. kfree(vol_args);
  1338. return ret;
  1339. }
  1340. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1341. {
  1342. struct btrfs_ioctl_vol_args *vol_args;
  1343. int ret;
  1344. if (!capable(CAP_SYS_ADMIN))
  1345. return -EPERM;
  1346. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1347. return -EROFS;
  1348. vol_args = memdup_user(arg, sizeof(*vol_args));
  1349. if (IS_ERR(vol_args))
  1350. return PTR_ERR(vol_args);
  1351. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1352. ret = btrfs_rm_device(root, vol_args->name);
  1353. kfree(vol_args);
  1354. return ret;
  1355. }
  1356. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1357. u64 off, u64 olen, u64 destoff)
  1358. {
  1359. struct inode *inode = fdentry(file)->d_inode;
  1360. struct btrfs_root *root = BTRFS_I(inode)->root;
  1361. struct file *src_file;
  1362. struct inode *src;
  1363. struct btrfs_trans_handle *trans;
  1364. struct btrfs_path *path;
  1365. struct extent_buffer *leaf;
  1366. char *buf;
  1367. struct btrfs_key key;
  1368. u32 nritems;
  1369. int slot;
  1370. int ret;
  1371. u64 len = olen;
  1372. u64 bs = root->fs_info->sb->s_blocksize;
  1373. u64 hint_byte;
  1374. /*
  1375. * TODO:
  1376. * - split compressed inline extents. annoying: we need to
  1377. * decompress into destination's address_space (the file offset
  1378. * may change, so source mapping won't do), then recompress (or
  1379. * otherwise reinsert) a subrange.
  1380. * - allow ranges within the same file to be cloned (provided
  1381. * they don't overlap)?
  1382. */
  1383. /* the destination must be opened for writing */
  1384. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1385. return -EINVAL;
  1386. ret = mnt_want_write(file->f_path.mnt);
  1387. if (ret)
  1388. return ret;
  1389. src_file = fget(srcfd);
  1390. if (!src_file) {
  1391. ret = -EBADF;
  1392. goto out_drop_write;
  1393. }
  1394. src = src_file->f_dentry->d_inode;
  1395. ret = -EINVAL;
  1396. if (src == inode)
  1397. goto out_fput;
  1398. /* the src must be open for reading */
  1399. if (!(src_file->f_mode & FMODE_READ))
  1400. goto out_fput;
  1401. ret = -EISDIR;
  1402. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1403. goto out_fput;
  1404. ret = -EXDEV;
  1405. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1406. goto out_fput;
  1407. ret = -ENOMEM;
  1408. buf = vmalloc(btrfs_level_size(root, 0));
  1409. if (!buf)
  1410. goto out_fput;
  1411. path = btrfs_alloc_path();
  1412. if (!path) {
  1413. vfree(buf);
  1414. goto out_fput;
  1415. }
  1416. path->reada = 2;
  1417. if (inode < src) {
  1418. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1419. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1420. } else {
  1421. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1422. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1423. }
  1424. /* determine range to clone */
  1425. ret = -EINVAL;
  1426. if (off + len > src->i_size || off + len < off)
  1427. goto out_unlock;
  1428. if (len == 0)
  1429. olen = len = src->i_size - off;
  1430. /* if we extend to eof, continue to block boundary */
  1431. if (off + len == src->i_size)
  1432. len = ((src->i_size + bs-1) & ~(bs-1))
  1433. - off;
  1434. /* verify the end result is block aligned */
  1435. if ((off & (bs-1)) ||
  1436. ((off + len) & (bs-1)))
  1437. goto out_unlock;
  1438. /* do any pending delalloc/csum calc on src, one way or
  1439. another, and lock file content */
  1440. while (1) {
  1441. struct btrfs_ordered_extent *ordered;
  1442. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1443. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1444. if (!ordered &&
  1445. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1446. EXTENT_DELALLOC, 0, NULL))
  1447. break;
  1448. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1449. if (ordered)
  1450. btrfs_put_ordered_extent(ordered);
  1451. btrfs_wait_ordered_range(src, off, len);
  1452. }
  1453. /* clone data */
  1454. key.objectid = src->i_ino;
  1455. key.type = BTRFS_EXTENT_DATA_KEY;
  1456. key.offset = 0;
  1457. while (1) {
  1458. /*
  1459. * note the key will change type as we walk through the
  1460. * tree.
  1461. */
  1462. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1463. if (ret < 0)
  1464. goto out;
  1465. nritems = btrfs_header_nritems(path->nodes[0]);
  1466. if (path->slots[0] >= nritems) {
  1467. ret = btrfs_next_leaf(root, path);
  1468. if (ret < 0)
  1469. goto out;
  1470. if (ret > 0)
  1471. break;
  1472. nritems = btrfs_header_nritems(path->nodes[0]);
  1473. }
  1474. leaf = path->nodes[0];
  1475. slot = path->slots[0];
  1476. btrfs_item_key_to_cpu(leaf, &key, slot);
  1477. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1478. key.objectid != src->i_ino)
  1479. break;
  1480. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1481. struct btrfs_file_extent_item *extent;
  1482. int type;
  1483. u32 size;
  1484. struct btrfs_key new_key;
  1485. u64 disko = 0, diskl = 0;
  1486. u64 datao = 0, datal = 0;
  1487. u8 comp;
  1488. u64 endoff;
  1489. size = btrfs_item_size_nr(leaf, slot);
  1490. read_extent_buffer(leaf, buf,
  1491. btrfs_item_ptr_offset(leaf, slot),
  1492. size);
  1493. extent = btrfs_item_ptr(leaf, slot,
  1494. struct btrfs_file_extent_item);
  1495. comp = btrfs_file_extent_compression(leaf, extent);
  1496. type = btrfs_file_extent_type(leaf, extent);
  1497. if (type == BTRFS_FILE_EXTENT_REG ||
  1498. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1499. disko = btrfs_file_extent_disk_bytenr(leaf,
  1500. extent);
  1501. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1502. extent);
  1503. datao = btrfs_file_extent_offset(leaf, extent);
  1504. datal = btrfs_file_extent_num_bytes(leaf,
  1505. extent);
  1506. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1507. /* take upper bound, may be compressed */
  1508. datal = btrfs_file_extent_ram_bytes(leaf,
  1509. extent);
  1510. }
  1511. btrfs_release_path(root, path);
  1512. if (key.offset + datal <= off ||
  1513. key.offset >= off+len)
  1514. goto next;
  1515. memcpy(&new_key, &key, sizeof(new_key));
  1516. new_key.objectid = inode->i_ino;
  1517. new_key.offset = key.offset + destoff - off;
  1518. trans = btrfs_start_transaction(root, 1);
  1519. if (IS_ERR(trans)) {
  1520. ret = PTR_ERR(trans);
  1521. goto out;
  1522. }
  1523. if (type == BTRFS_FILE_EXTENT_REG ||
  1524. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1525. if (off > key.offset) {
  1526. datao += off - key.offset;
  1527. datal -= off - key.offset;
  1528. }
  1529. if (key.offset + datal > off + len)
  1530. datal = off + len - key.offset;
  1531. ret = btrfs_drop_extents(trans, inode,
  1532. new_key.offset,
  1533. new_key.offset + datal,
  1534. &hint_byte, 1);
  1535. BUG_ON(ret);
  1536. ret = btrfs_insert_empty_item(trans, root, path,
  1537. &new_key, size);
  1538. BUG_ON(ret);
  1539. leaf = path->nodes[0];
  1540. slot = path->slots[0];
  1541. write_extent_buffer(leaf, buf,
  1542. btrfs_item_ptr_offset(leaf, slot),
  1543. size);
  1544. extent = btrfs_item_ptr(leaf, slot,
  1545. struct btrfs_file_extent_item);
  1546. /* disko == 0 means it's a hole */
  1547. if (!disko)
  1548. datao = 0;
  1549. btrfs_set_file_extent_offset(leaf, extent,
  1550. datao);
  1551. btrfs_set_file_extent_num_bytes(leaf, extent,
  1552. datal);
  1553. if (disko) {
  1554. inode_add_bytes(inode, datal);
  1555. ret = btrfs_inc_extent_ref(trans, root,
  1556. disko, diskl, 0,
  1557. root->root_key.objectid,
  1558. inode->i_ino,
  1559. new_key.offset - datao);
  1560. BUG_ON(ret);
  1561. }
  1562. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1563. u64 skip = 0;
  1564. u64 trim = 0;
  1565. if (off > key.offset) {
  1566. skip = off - key.offset;
  1567. new_key.offset += skip;
  1568. }
  1569. if (key.offset + datal > off+len)
  1570. trim = key.offset + datal - (off+len);
  1571. if (comp && (skip || trim)) {
  1572. ret = -EINVAL;
  1573. btrfs_end_transaction(trans, root);
  1574. goto out;
  1575. }
  1576. size -= skip + trim;
  1577. datal -= skip + trim;
  1578. ret = btrfs_drop_extents(trans, inode,
  1579. new_key.offset,
  1580. new_key.offset + datal,
  1581. &hint_byte, 1);
  1582. BUG_ON(ret);
  1583. ret = btrfs_insert_empty_item(trans, root, path,
  1584. &new_key, size);
  1585. BUG_ON(ret);
  1586. if (skip) {
  1587. u32 start =
  1588. btrfs_file_extent_calc_inline_size(0);
  1589. memmove(buf+start, buf+start+skip,
  1590. datal);
  1591. }
  1592. leaf = path->nodes[0];
  1593. slot = path->slots[0];
  1594. write_extent_buffer(leaf, buf,
  1595. btrfs_item_ptr_offset(leaf, slot),
  1596. size);
  1597. inode_add_bytes(inode, datal);
  1598. }
  1599. btrfs_mark_buffer_dirty(leaf);
  1600. btrfs_release_path(root, path);
  1601. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1602. /*
  1603. * we round up to the block size at eof when
  1604. * determining which extents to clone above,
  1605. * but shouldn't round up the file size
  1606. */
  1607. endoff = new_key.offset + datal;
  1608. if (endoff > off+olen)
  1609. endoff = off+olen;
  1610. if (endoff > inode->i_size)
  1611. btrfs_i_size_write(inode, endoff);
  1612. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1613. ret = btrfs_update_inode(trans, root, inode);
  1614. BUG_ON(ret);
  1615. btrfs_end_transaction(trans, root);
  1616. }
  1617. next:
  1618. btrfs_release_path(root, path);
  1619. key.offset++;
  1620. }
  1621. ret = 0;
  1622. out:
  1623. btrfs_release_path(root, path);
  1624. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1625. out_unlock:
  1626. mutex_unlock(&src->i_mutex);
  1627. mutex_unlock(&inode->i_mutex);
  1628. vfree(buf);
  1629. btrfs_free_path(path);
  1630. out_fput:
  1631. fput(src_file);
  1632. out_drop_write:
  1633. mnt_drop_write(file->f_path.mnt);
  1634. return ret;
  1635. }
  1636. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1637. {
  1638. struct btrfs_ioctl_clone_range_args args;
  1639. if (copy_from_user(&args, argp, sizeof(args)))
  1640. return -EFAULT;
  1641. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1642. args.src_length, args.dest_offset);
  1643. }
  1644. /*
  1645. * there are many ways the trans_start and trans_end ioctls can lead
  1646. * to deadlocks. They should only be used by applications that
  1647. * basically own the machine, and have a very in depth understanding
  1648. * of all the possible deadlocks and enospc problems.
  1649. */
  1650. static long btrfs_ioctl_trans_start(struct file *file)
  1651. {
  1652. struct inode *inode = fdentry(file)->d_inode;
  1653. struct btrfs_root *root = BTRFS_I(inode)->root;
  1654. struct btrfs_trans_handle *trans;
  1655. int ret;
  1656. ret = -EPERM;
  1657. if (!capable(CAP_SYS_ADMIN))
  1658. goto out;
  1659. ret = -EINPROGRESS;
  1660. if (file->private_data)
  1661. goto out;
  1662. ret = mnt_want_write(file->f_path.mnt);
  1663. if (ret)
  1664. goto out;
  1665. mutex_lock(&root->fs_info->trans_mutex);
  1666. root->fs_info->open_ioctl_trans++;
  1667. mutex_unlock(&root->fs_info->trans_mutex);
  1668. ret = -ENOMEM;
  1669. trans = btrfs_start_ioctl_transaction(root, 0);
  1670. if (!trans)
  1671. goto out_drop;
  1672. file->private_data = trans;
  1673. return 0;
  1674. out_drop:
  1675. mutex_lock(&root->fs_info->trans_mutex);
  1676. root->fs_info->open_ioctl_trans--;
  1677. mutex_unlock(&root->fs_info->trans_mutex);
  1678. mnt_drop_write(file->f_path.mnt);
  1679. out:
  1680. return ret;
  1681. }
  1682. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1683. {
  1684. struct inode *inode = fdentry(file)->d_inode;
  1685. struct btrfs_root *root = BTRFS_I(inode)->root;
  1686. struct btrfs_root *new_root;
  1687. struct btrfs_dir_item *di;
  1688. struct btrfs_trans_handle *trans;
  1689. struct btrfs_path *path;
  1690. struct btrfs_key location;
  1691. struct btrfs_disk_key disk_key;
  1692. struct btrfs_super_block *disk_super;
  1693. u64 features;
  1694. u64 objectid = 0;
  1695. u64 dir_id;
  1696. if (!capable(CAP_SYS_ADMIN))
  1697. return -EPERM;
  1698. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1699. return -EFAULT;
  1700. if (!objectid)
  1701. objectid = root->root_key.objectid;
  1702. location.objectid = objectid;
  1703. location.type = BTRFS_ROOT_ITEM_KEY;
  1704. location.offset = (u64)-1;
  1705. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1706. if (IS_ERR(new_root))
  1707. return PTR_ERR(new_root);
  1708. if (btrfs_root_refs(&new_root->root_item) == 0)
  1709. return -ENOENT;
  1710. path = btrfs_alloc_path();
  1711. if (!path)
  1712. return -ENOMEM;
  1713. path->leave_spinning = 1;
  1714. trans = btrfs_start_transaction(root, 1);
  1715. if (!trans) {
  1716. btrfs_free_path(path);
  1717. return -ENOMEM;
  1718. }
  1719. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1720. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1721. dir_id, "default", 7, 1);
  1722. if (IS_ERR_OR_NULL(di)) {
  1723. btrfs_free_path(path);
  1724. btrfs_end_transaction(trans, root);
  1725. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1726. "this isn't going to work\n");
  1727. return -ENOENT;
  1728. }
  1729. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1730. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1731. btrfs_mark_buffer_dirty(path->nodes[0]);
  1732. btrfs_free_path(path);
  1733. disk_super = &root->fs_info->super_copy;
  1734. features = btrfs_super_incompat_flags(disk_super);
  1735. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1736. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1737. btrfs_set_super_incompat_flags(disk_super, features);
  1738. }
  1739. btrfs_end_transaction(trans, root);
  1740. return 0;
  1741. }
  1742. static void get_block_group_info(struct list_head *groups_list,
  1743. struct btrfs_ioctl_space_info *space)
  1744. {
  1745. struct btrfs_block_group_cache *block_group;
  1746. space->total_bytes = 0;
  1747. space->used_bytes = 0;
  1748. space->flags = 0;
  1749. list_for_each_entry(block_group, groups_list, list) {
  1750. space->flags = block_group->flags;
  1751. space->total_bytes += block_group->key.offset;
  1752. space->used_bytes +=
  1753. btrfs_block_group_used(&block_group->item);
  1754. }
  1755. }
  1756. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1757. {
  1758. struct btrfs_ioctl_space_args space_args;
  1759. struct btrfs_ioctl_space_info space;
  1760. struct btrfs_ioctl_space_info *dest;
  1761. struct btrfs_ioctl_space_info *dest_orig;
  1762. struct btrfs_ioctl_space_info *user_dest;
  1763. struct btrfs_space_info *info;
  1764. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1765. BTRFS_BLOCK_GROUP_SYSTEM,
  1766. BTRFS_BLOCK_GROUP_METADATA,
  1767. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1768. int num_types = 4;
  1769. int alloc_size;
  1770. int ret = 0;
  1771. int slot_count = 0;
  1772. int i, c;
  1773. if (copy_from_user(&space_args,
  1774. (struct btrfs_ioctl_space_args __user *)arg,
  1775. sizeof(space_args)))
  1776. return -EFAULT;
  1777. for (i = 0; i < num_types; i++) {
  1778. struct btrfs_space_info *tmp;
  1779. info = NULL;
  1780. rcu_read_lock();
  1781. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1782. list) {
  1783. if (tmp->flags == types[i]) {
  1784. info = tmp;
  1785. break;
  1786. }
  1787. }
  1788. rcu_read_unlock();
  1789. if (!info)
  1790. continue;
  1791. down_read(&info->groups_sem);
  1792. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1793. if (!list_empty(&info->block_groups[c]))
  1794. slot_count++;
  1795. }
  1796. up_read(&info->groups_sem);
  1797. }
  1798. /* space_slots == 0 means they are asking for a count */
  1799. if (space_args.space_slots == 0) {
  1800. space_args.total_spaces = slot_count;
  1801. goto out;
  1802. }
  1803. slot_count = min_t(int, space_args.space_slots, slot_count);
  1804. alloc_size = sizeof(*dest) * slot_count;
  1805. /* we generally have at most 6 or so space infos, one for each raid
  1806. * level. So, a whole page should be more than enough for everyone
  1807. */
  1808. if (alloc_size > PAGE_CACHE_SIZE)
  1809. return -ENOMEM;
  1810. space_args.total_spaces = 0;
  1811. dest = kmalloc(alloc_size, GFP_NOFS);
  1812. if (!dest)
  1813. return -ENOMEM;
  1814. dest_orig = dest;
  1815. /* now we have a buffer to copy into */
  1816. for (i = 0; i < num_types; i++) {
  1817. struct btrfs_space_info *tmp;
  1818. info = NULL;
  1819. rcu_read_lock();
  1820. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1821. list) {
  1822. if (tmp->flags == types[i]) {
  1823. info = tmp;
  1824. break;
  1825. }
  1826. }
  1827. rcu_read_unlock();
  1828. if (!info)
  1829. continue;
  1830. down_read(&info->groups_sem);
  1831. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1832. if (!list_empty(&info->block_groups[c])) {
  1833. get_block_group_info(&info->block_groups[c],
  1834. &space);
  1835. memcpy(dest, &space, sizeof(space));
  1836. dest++;
  1837. space_args.total_spaces++;
  1838. }
  1839. }
  1840. up_read(&info->groups_sem);
  1841. }
  1842. user_dest = (struct btrfs_ioctl_space_info *)
  1843. (arg + sizeof(struct btrfs_ioctl_space_args));
  1844. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1845. ret = -EFAULT;
  1846. kfree(dest_orig);
  1847. out:
  1848. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1849. ret = -EFAULT;
  1850. return ret;
  1851. }
  1852. /*
  1853. * there are many ways the trans_start and trans_end ioctls can lead
  1854. * to deadlocks. They should only be used by applications that
  1855. * basically own the machine, and have a very in depth understanding
  1856. * of all the possible deadlocks and enospc problems.
  1857. */
  1858. long btrfs_ioctl_trans_end(struct file *file)
  1859. {
  1860. struct inode *inode = fdentry(file)->d_inode;
  1861. struct btrfs_root *root = BTRFS_I(inode)->root;
  1862. struct btrfs_trans_handle *trans;
  1863. trans = file->private_data;
  1864. if (!trans)
  1865. return -EINVAL;
  1866. file->private_data = NULL;
  1867. btrfs_end_transaction(trans, root);
  1868. mutex_lock(&root->fs_info->trans_mutex);
  1869. root->fs_info->open_ioctl_trans--;
  1870. mutex_unlock(&root->fs_info->trans_mutex);
  1871. mnt_drop_write(file->f_path.mnt);
  1872. return 0;
  1873. }
  1874. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1875. {
  1876. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1877. struct btrfs_trans_handle *trans;
  1878. u64 transid;
  1879. trans = btrfs_start_transaction(root, 0);
  1880. transid = trans->transid;
  1881. btrfs_commit_transaction_async(trans, root, 0);
  1882. if (argp)
  1883. if (copy_to_user(argp, &transid, sizeof(transid)))
  1884. return -EFAULT;
  1885. return 0;
  1886. }
  1887. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  1888. {
  1889. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1890. u64 transid;
  1891. if (argp) {
  1892. if (copy_from_user(&transid, argp, sizeof(transid)))
  1893. return -EFAULT;
  1894. } else {
  1895. transid = 0; /* current trans */
  1896. }
  1897. return btrfs_wait_for_commit(root, transid);
  1898. }
  1899. long btrfs_ioctl(struct file *file, unsigned int
  1900. cmd, unsigned long arg)
  1901. {
  1902. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  1903. void __user *argp = (void __user *)arg;
  1904. switch (cmd) {
  1905. case FS_IOC_GETFLAGS:
  1906. return btrfs_ioctl_getflags(file, argp);
  1907. case FS_IOC_SETFLAGS:
  1908. return btrfs_ioctl_setflags(file, argp);
  1909. case FS_IOC_GETVERSION:
  1910. return btrfs_ioctl_getversion(file, argp);
  1911. case BTRFS_IOC_SNAP_CREATE:
  1912. return btrfs_ioctl_snap_create(file, argp, 0, 0);
  1913. case BTRFS_IOC_SNAP_CREATE_ASYNC:
  1914. return btrfs_ioctl_snap_create(file, argp, 0, 1);
  1915. case BTRFS_IOC_SUBVOL_CREATE:
  1916. return btrfs_ioctl_snap_create(file, argp, 1, 0);
  1917. case BTRFS_IOC_SNAP_DESTROY:
  1918. return btrfs_ioctl_snap_destroy(file, argp);
  1919. case BTRFS_IOC_DEFAULT_SUBVOL:
  1920. return btrfs_ioctl_default_subvol(file, argp);
  1921. case BTRFS_IOC_DEFRAG:
  1922. return btrfs_ioctl_defrag(file, NULL);
  1923. case BTRFS_IOC_DEFRAG_RANGE:
  1924. return btrfs_ioctl_defrag(file, argp);
  1925. case BTRFS_IOC_RESIZE:
  1926. return btrfs_ioctl_resize(root, argp);
  1927. case BTRFS_IOC_ADD_DEV:
  1928. return btrfs_ioctl_add_dev(root, argp);
  1929. case BTRFS_IOC_RM_DEV:
  1930. return btrfs_ioctl_rm_dev(root, argp);
  1931. case BTRFS_IOC_BALANCE:
  1932. return btrfs_balance(root->fs_info->dev_root);
  1933. case BTRFS_IOC_CLONE:
  1934. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  1935. case BTRFS_IOC_CLONE_RANGE:
  1936. return btrfs_ioctl_clone_range(file, argp);
  1937. case BTRFS_IOC_TRANS_START:
  1938. return btrfs_ioctl_trans_start(file);
  1939. case BTRFS_IOC_TRANS_END:
  1940. return btrfs_ioctl_trans_end(file);
  1941. case BTRFS_IOC_TREE_SEARCH:
  1942. return btrfs_ioctl_tree_search(file, argp);
  1943. case BTRFS_IOC_INO_LOOKUP:
  1944. return btrfs_ioctl_ino_lookup(file, argp);
  1945. case BTRFS_IOC_SPACE_INFO:
  1946. return btrfs_ioctl_space_info(root, argp);
  1947. case BTRFS_IOC_SYNC:
  1948. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  1949. return 0;
  1950. case BTRFS_IOC_START_SYNC:
  1951. return btrfs_ioctl_start_sync(file, argp);
  1952. case BTRFS_IOC_WAIT_SYNC:
  1953. return btrfs_ioctl_wait_sync(file, argp);
  1954. }
  1955. return -ENOTTY;
  1956. }