ioctl.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  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 <linux/blkdev.h>
  43. #include "compat.h"
  44. #include "ctree.h"
  45. #include "disk-io.h"
  46. #include "transaction.h"
  47. #include "btrfs_inode.h"
  48. #include "ioctl.h"
  49. #include "print-tree.h"
  50. #include "volumes.h"
  51. #include "locking.h"
  52. /* Mask out flags that are inappropriate for the given type of inode. */
  53. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  54. {
  55. if (S_ISDIR(mode))
  56. return flags;
  57. else if (S_ISREG(mode))
  58. return flags & ~FS_DIRSYNC_FL;
  59. else
  60. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  61. }
  62. /*
  63. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  64. */
  65. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  66. {
  67. unsigned int iflags = 0;
  68. if (flags & BTRFS_INODE_SYNC)
  69. iflags |= FS_SYNC_FL;
  70. if (flags & BTRFS_INODE_IMMUTABLE)
  71. iflags |= FS_IMMUTABLE_FL;
  72. if (flags & BTRFS_INODE_APPEND)
  73. iflags |= FS_APPEND_FL;
  74. if (flags & BTRFS_INODE_NODUMP)
  75. iflags |= FS_NODUMP_FL;
  76. if (flags & BTRFS_INODE_NOATIME)
  77. iflags |= FS_NOATIME_FL;
  78. if (flags & BTRFS_INODE_DIRSYNC)
  79. iflags |= FS_DIRSYNC_FL;
  80. if (flags & BTRFS_INODE_NODATACOW)
  81. iflags |= FS_NOCOW_FL;
  82. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  83. iflags |= FS_COMPR_FL;
  84. else if (flags & BTRFS_INODE_NOCOMPRESS)
  85. iflags |= FS_NOCOMP_FL;
  86. return iflags;
  87. }
  88. /*
  89. * Update inode->i_flags based on the btrfs internal flags.
  90. */
  91. void btrfs_update_iflags(struct inode *inode)
  92. {
  93. struct btrfs_inode *ip = BTRFS_I(inode);
  94. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  95. if (ip->flags & BTRFS_INODE_SYNC)
  96. inode->i_flags |= S_SYNC;
  97. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  98. inode->i_flags |= S_IMMUTABLE;
  99. if (ip->flags & BTRFS_INODE_APPEND)
  100. inode->i_flags |= S_APPEND;
  101. if (ip->flags & BTRFS_INODE_NOATIME)
  102. inode->i_flags |= S_NOATIME;
  103. if (ip->flags & BTRFS_INODE_DIRSYNC)
  104. inode->i_flags |= S_DIRSYNC;
  105. }
  106. /*
  107. * Inherit flags from the parent inode.
  108. *
  109. * Unlike extN we don't have any flags we don't want to inherit currently.
  110. */
  111. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  112. {
  113. unsigned int flags;
  114. if (!dir)
  115. return;
  116. flags = BTRFS_I(dir)->flags;
  117. if (S_ISREG(inode->i_mode))
  118. flags &= ~BTRFS_INODE_DIRSYNC;
  119. else if (!S_ISDIR(inode->i_mode))
  120. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  121. BTRFS_I(inode)->flags = flags;
  122. btrfs_update_iflags(inode);
  123. }
  124. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  125. {
  126. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  127. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  128. if (copy_to_user(arg, &flags, sizeof(flags)))
  129. return -EFAULT;
  130. return 0;
  131. }
  132. static int check_flags(unsigned int flags)
  133. {
  134. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  135. FS_NOATIME_FL | FS_NODUMP_FL | \
  136. FS_SYNC_FL | FS_DIRSYNC_FL | \
  137. FS_NOCOMP_FL | FS_COMPR_FL |
  138. FS_NOCOW_FL))
  139. return -EOPNOTSUPP;
  140. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  141. return -EINVAL;
  142. return 0;
  143. }
  144. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  145. {
  146. struct inode *inode = file->f_path.dentry->d_inode;
  147. struct btrfs_inode *ip = BTRFS_I(inode);
  148. struct btrfs_root *root = ip->root;
  149. struct btrfs_trans_handle *trans;
  150. unsigned int flags, oldflags;
  151. int ret;
  152. if (btrfs_root_readonly(root))
  153. return -EROFS;
  154. if (copy_from_user(&flags, arg, sizeof(flags)))
  155. return -EFAULT;
  156. ret = check_flags(flags);
  157. if (ret)
  158. return ret;
  159. if (!is_owner_or_cap(inode))
  160. return -EACCES;
  161. mutex_lock(&inode->i_mutex);
  162. flags = btrfs_mask_flags(inode->i_mode, flags);
  163. oldflags = btrfs_flags_to_ioctl(ip->flags);
  164. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  165. if (!capable(CAP_LINUX_IMMUTABLE)) {
  166. ret = -EPERM;
  167. goto out_unlock;
  168. }
  169. }
  170. ret = mnt_want_write(file->f_path.mnt);
  171. if (ret)
  172. goto out_unlock;
  173. if (flags & FS_SYNC_FL)
  174. ip->flags |= BTRFS_INODE_SYNC;
  175. else
  176. ip->flags &= ~BTRFS_INODE_SYNC;
  177. if (flags & FS_IMMUTABLE_FL)
  178. ip->flags |= BTRFS_INODE_IMMUTABLE;
  179. else
  180. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  181. if (flags & FS_APPEND_FL)
  182. ip->flags |= BTRFS_INODE_APPEND;
  183. else
  184. ip->flags &= ~BTRFS_INODE_APPEND;
  185. if (flags & FS_NODUMP_FL)
  186. ip->flags |= BTRFS_INODE_NODUMP;
  187. else
  188. ip->flags &= ~BTRFS_INODE_NODUMP;
  189. if (flags & FS_NOATIME_FL)
  190. ip->flags |= BTRFS_INODE_NOATIME;
  191. else
  192. ip->flags &= ~BTRFS_INODE_NOATIME;
  193. if (flags & FS_DIRSYNC_FL)
  194. ip->flags |= BTRFS_INODE_DIRSYNC;
  195. else
  196. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  197. if (flags & FS_NOCOW_FL)
  198. ip->flags |= BTRFS_INODE_NODATACOW;
  199. else
  200. ip->flags &= ~BTRFS_INODE_NODATACOW;
  201. /*
  202. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  203. * flag may be changed automatically if compression code won't make
  204. * things smaller.
  205. */
  206. if (flags & FS_NOCOMP_FL) {
  207. ip->flags &= ~BTRFS_INODE_COMPRESS;
  208. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  209. } else if (flags & FS_COMPR_FL) {
  210. ip->flags |= BTRFS_INODE_COMPRESS;
  211. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  212. }
  213. trans = btrfs_join_transaction(root, 1);
  214. BUG_ON(IS_ERR(trans));
  215. ret = btrfs_update_inode(trans, root, inode);
  216. BUG_ON(ret);
  217. btrfs_update_iflags(inode);
  218. inode->i_ctime = CURRENT_TIME;
  219. btrfs_end_transaction(trans, root);
  220. mnt_drop_write(file->f_path.mnt);
  221. ret = 0;
  222. out_unlock:
  223. mutex_unlock(&inode->i_mutex);
  224. return ret;
  225. }
  226. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  227. {
  228. struct inode *inode = file->f_path.dentry->d_inode;
  229. return put_user(inode->i_generation, arg);
  230. }
  231. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  232. {
  233. struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
  234. struct btrfs_fs_info *fs_info = root->fs_info;
  235. struct btrfs_device *device;
  236. struct request_queue *q;
  237. struct fstrim_range range;
  238. u64 minlen = ULLONG_MAX;
  239. u64 num_devices = 0;
  240. int ret;
  241. if (!capable(CAP_SYS_ADMIN))
  242. return -EPERM;
  243. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  244. list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
  245. if (!device->bdev)
  246. continue;
  247. q = bdev_get_queue(device->bdev);
  248. if (blk_queue_discard(q)) {
  249. num_devices++;
  250. minlen = min((u64)q->limits.discard_granularity,
  251. minlen);
  252. }
  253. }
  254. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  255. if (!num_devices)
  256. return -EOPNOTSUPP;
  257. if (copy_from_user(&range, arg, sizeof(range)))
  258. return -EFAULT;
  259. range.minlen = max(range.minlen, minlen);
  260. ret = btrfs_trim_fs(root, &range);
  261. if (ret < 0)
  262. return ret;
  263. if (copy_to_user(arg, &range, sizeof(range)))
  264. return -EFAULT;
  265. return 0;
  266. }
  267. static noinline int create_subvol(struct btrfs_root *root,
  268. struct dentry *dentry,
  269. char *name, int namelen,
  270. u64 *async_transid)
  271. {
  272. struct btrfs_trans_handle *trans;
  273. struct btrfs_key key;
  274. struct btrfs_root_item root_item;
  275. struct btrfs_inode_item *inode_item;
  276. struct extent_buffer *leaf;
  277. struct btrfs_root *new_root;
  278. struct dentry *parent = dget_parent(dentry);
  279. struct inode *dir;
  280. int ret;
  281. int err;
  282. u64 objectid;
  283. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  284. u64 index = 0;
  285. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  286. 0, &objectid);
  287. if (ret) {
  288. dput(parent);
  289. return ret;
  290. }
  291. dir = parent->d_inode;
  292. /*
  293. * 1 - inode item
  294. * 2 - refs
  295. * 1 - root item
  296. * 2 - dir items
  297. */
  298. trans = btrfs_start_transaction(root, 6);
  299. if (IS_ERR(trans)) {
  300. dput(parent);
  301. return PTR_ERR(trans);
  302. }
  303. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  304. 0, objectid, NULL, 0, 0, 0);
  305. if (IS_ERR(leaf)) {
  306. ret = PTR_ERR(leaf);
  307. goto fail;
  308. }
  309. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  310. btrfs_set_header_bytenr(leaf, leaf->start);
  311. btrfs_set_header_generation(leaf, trans->transid);
  312. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  313. btrfs_set_header_owner(leaf, objectid);
  314. write_extent_buffer(leaf, root->fs_info->fsid,
  315. (unsigned long)btrfs_header_fsid(leaf),
  316. BTRFS_FSID_SIZE);
  317. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  318. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  319. BTRFS_UUID_SIZE);
  320. btrfs_mark_buffer_dirty(leaf);
  321. inode_item = &root_item.inode;
  322. memset(inode_item, 0, sizeof(*inode_item));
  323. inode_item->generation = cpu_to_le64(1);
  324. inode_item->size = cpu_to_le64(3);
  325. inode_item->nlink = cpu_to_le32(1);
  326. inode_item->nbytes = cpu_to_le64(root->leafsize);
  327. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  328. root_item.flags = 0;
  329. root_item.byte_limit = 0;
  330. inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
  331. btrfs_set_root_bytenr(&root_item, leaf->start);
  332. btrfs_set_root_generation(&root_item, trans->transid);
  333. btrfs_set_root_level(&root_item, 0);
  334. btrfs_set_root_refs(&root_item, 1);
  335. btrfs_set_root_used(&root_item, leaf->len);
  336. btrfs_set_root_last_snapshot(&root_item, 0);
  337. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  338. root_item.drop_level = 0;
  339. btrfs_tree_unlock(leaf);
  340. free_extent_buffer(leaf);
  341. leaf = NULL;
  342. btrfs_set_root_dirid(&root_item, new_dirid);
  343. key.objectid = objectid;
  344. key.offset = 0;
  345. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  346. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  347. &root_item);
  348. if (ret)
  349. goto fail;
  350. key.offset = (u64)-1;
  351. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  352. BUG_ON(IS_ERR(new_root));
  353. btrfs_record_root_in_trans(trans, new_root);
  354. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  355. BTRFS_I(dir)->block_group);
  356. /*
  357. * insert the directory item
  358. */
  359. ret = btrfs_set_inode_index(dir, &index);
  360. BUG_ON(ret);
  361. ret = btrfs_insert_dir_item(trans, root,
  362. name, namelen, dir->i_ino, &key,
  363. BTRFS_FT_DIR, index);
  364. if (ret)
  365. goto fail;
  366. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  367. ret = btrfs_update_inode(trans, root, dir);
  368. BUG_ON(ret);
  369. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  370. objectid, root->root_key.objectid,
  371. dir->i_ino, index, name, namelen);
  372. BUG_ON(ret);
  373. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  374. fail:
  375. dput(parent);
  376. if (async_transid) {
  377. *async_transid = trans->transid;
  378. err = btrfs_commit_transaction_async(trans, root, 1);
  379. } else {
  380. err = btrfs_commit_transaction(trans, root);
  381. }
  382. if (err && !ret)
  383. ret = err;
  384. return ret;
  385. }
  386. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  387. char *name, int namelen, u64 *async_transid,
  388. bool readonly)
  389. {
  390. struct inode *inode;
  391. struct dentry *parent;
  392. struct btrfs_pending_snapshot *pending_snapshot;
  393. struct btrfs_trans_handle *trans;
  394. int ret;
  395. if (!root->ref_cows)
  396. return -EINVAL;
  397. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  398. if (!pending_snapshot)
  399. return -ENOMEM;
  400. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  401. pending_snapshot->dentry = dentry;
  402. pending_snapshot->root = root;
  403. pending_snapshot->readonly = readonly;
  404. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  405. if (IS_ERR(trans)) {
  406. ret = PTR_ERR(trans);
  407. goto fail;
  408. }
  409. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  410. BUG_ON(ret);
  411. list_add(&pending_snapshot->list,
  412. &trans->transaction->pending_snapshots);
  413. if (async_transid) {
  414. *async_transid = trans->transid;
  415. ret = btrfs_commit_transaction_async(trans,
  416. root->fs_info->extent_root, 1);
  417. } else {
  418. ret = btrfs_commit_transaction(trans,
  419. root->fs_info->extent_root);
  420. }
  421. BUG_ON(ret);
  422. ret = pending_snapshot->error;
  423. if (ret)
  424. goto fail;
  425. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  426. if (ret)
  427. goto fail;
  428. parent = dget_parent(dentry);
  429. inode = btrfs_lookup_dentry(parent->d_inode, dentry);
  430. dput(parent);
  431. if (IS_ERR(inode)) {
  432. ret = PTR_ERR(inode);
  433. goto fail;
  434. }
  435. BUG_ON(!inode);
  436. d_instantiate(dentry, inode);
  437. ret = 0;
  438. fail:
  439. kfree(pending_snapshot);
  440. return ret;
  441. }
  442. /* copy of check_sticky in fs/namei.c()
  443. * It's inline, so penalty for filesystems that don't use sticky bit is
  444. * minimal.
  445. */
  446. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  447. {
  448. uid_t fsuid = current_fsuid();
  449. if (!(dir->i_mode & S_ISVTX))
  450. return 0;
  451. if (inode->i_uid == fsuid)
  452. return 0;
  453. if (dir->i_uid == fsuid)
  454. return 0;
  455. return !capable(CAP_FOWNER);
  456. }
  457. /* copy of may_delete in fs/namei.c()
  458. * Check whether we can remove a link victim from directory dir, check
  459. * whether the type of victim is right.
  460. * 1. We can't do it if dir is read-only (done in permission())
  461. * 2. We should have write and exec permissions on dir
  462. * 3. We can't remove anything from append-only dir
  463. * 4. We can't do anything with immutable dir (done in permission())
  464. * 5. If the sticky bit on dir is set we should either
  465. * a. be owner of dir, or
  466. * b. be owner of victim, or
  467. * c. have CAP_FOWNER capability
  468. * 6. If the victim is append-only or immutable we can't do antyhing with
  469. * links pointing to it.
  470. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  471. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  472. * 9. We can't remove a root or mountpoint.
  473. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  474. * nfs_async_unlink().
  475. */
  476. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  477. {
  478. int error;
  479. if (!victim->d_inode)
  480. return -ENOENT;
  481. BUG_ON(victim->d_parent->d_inode != dir);
  482. audit_inode_child(victim, dir);
  483. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  484. if (error)
  485. return error;
  486. if (IS_APPEND(dir))
  487. return -EPERM;
  488. if (btrfs_check_sticky(dir, victim->d_inode)||
  489. IS_APPEND(victim->d_inode)||
  490. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  491. return -EPERM;
  492. if (isdir) {
  493. if (!S_ISDIR(victim->d_inode->i_mode))
  494. return -ENOTDIR;
  495. if (IS_ROOT(victim))
  496. return -EBUSY;
  497. } else if (S_ISDIR(victim->d_inode->i_mode))
  498. return -EISDIR;
  499. if (IS_DEADDIR(dir))
  500. return -ENOENT;
  501. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  502. return -EBUSY;
  503. return 0;
  504. }
  505. /* copy of may_create in fs/namei.c() */
  506. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  507. {
  508. if (child->d_inode)
  509. return -EEXIST;
  510. if (IS_DEADDIR(dir))
  511. return -ENOENT;
  512. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  513. }
  514. /*
  515. * Create a new subvolume below @parent. This is largely modeled after
  516. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  517. * inside this filesystem so it's quite a bit simpler.
  518. */
  519. static noinline int btrfs_mksubvol(struct path *parent,
  520. char *name, int namelen,
  521. struct btrfs_root *snap_src,
  522. u64 *async_transid, bool readonly)
  523. {
  524. struct inode *dir = parent->dentry->d_inode;
  525. struct dentry *dentry;
  526. int error;
  527. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  528. dentry = lookup_one_len(name, parent->dentry, namelen);
  529. error = PTR_ERR(dentry);
  530. if (IS_ERR(dentry))
  531. goto out_unlock;
  532. error = -EEXIST;
  533. if (dentry->d_inode)
  534. goto out_dput;
  535. error = mnt_want_write(parent->mnt);
  536. if (error)
  537. goto out_dput;
  538. error = btrfs_may_create(dir, dentry);
  539. if (error)
  540. goto out_drop_write;
  541. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  542. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  543. goto out_up_read;
  544. if (snap_src) {
  545. error = create_snapshot(snap_src, dentry,
  546. name, namelen, async_transid, readonly);
  547. } else {
  548. error = create_subvol(BTRFS_I(dir)->root, dentry,
  549. name, namelen, async_transid);
  550. }
  551. if (!error)
  552. fsnotify_mkdir(dir, dentry);
  553. out_up_read:
  554. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  555. out_drop_write:
  556. mnt_drop_write(parent->mnt);
  557. out_dput:
  558. dput(dentry);
  559. out_unlock:
  560. mutex_unlock(&dir->i_mutex);
  561. return error;
  562. }
  563. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  564. int thresh, u64 *last_len, u64 *skip,
  565. u64 *defrag_end)
  566. {
  567. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  568. struct extent_map *em = NULL;
  569. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  570. int ret = 1;
  571. if (thresh == 0)
  572. thresh = 256 * 1024;
  573. /*
  574. * make sure that once we start defragging and extent, we keep on
  575. * defragging it
  576. */
  577. if (start < *defrag_end)
  578. return 1;
  579. *skip = 0;
  580. /*
  581. * hopefully we have this extent in the tree already, try without
  582. * the full extent lock
  583. */
  584. read_lock(&em_tree->lock);
  585. em = lookup_extent_mapping(em_tree, start, len);
  586. read_unlock(&em_tree->lock);
  587. if (!em) {
  588. /* get the big lock and read metadata off disk */
  589. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  590. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  591. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  592. if (IS_ERR(em))
  593. return 0;
  594. }
  595. /* this will cover holes, and inline extents */
  596. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  597. ret = 0;
  598. /*
  599. * we hit a real extent, if it is big don't bother defragging it again
  600. */
  601. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  602. ret = 0;
  603. /*
  604. * last_len ends up being a counter of how many bytes we've defragged.
  605. * every time we choose not to defrag an extent, we reset *last_len
  606. * so that the next tiny extent will force a defrag.
  607. *
  608. * The end result of this is that tiny extents before a single big
  609. * extent will force at least part of that big extent to be defragged.
  610. */
  611. if (ret) {
  612. *last_len += len;
  613. *defrag_end = extent_map_end(em);
  614. } else {
  615. *last_len = 0;
  616. *skip = extent_map_end(em);
  617. *defrag_end = 0;
  618. }
  619. free_extent_map(em);
  620. return ret;
  621. }
  622. static int btrfs_defrag_file(struct file *file,
  623. struct btrfs_ioctl_defrag_range_args *range)
  624. {
  625. struct inode *inode = fdentry(file)->d_inode;
  626. struct btrfs_root *root = BTRFS_I(inode)->root;
  627. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  628. struct btrfs_ordered_extent *ordered;
  629. struct page *page;
  630. struct btrfs_super_block *disk_super;
  631. unsigned long last_index;
  632. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  633. unsigned long total_read = 0;
  634. u64 features;
  635. u64 page_start;
  636. u64 page_end;
  637. u64 last_len = 0;
  638. u64 skip = 0;
  639. u64 defrag_end = 0;
  640. unsigned long i;
  641. int ret;
  642. int compress_type = BTRFS_COMPRESS_ZLIB;
  643. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  644. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  645. return -EINVAL;
  646. if (range->compress_type)
  647. compress_type = range->compress_type;
  648. }
  649. if (inode->i_size == 0)
  650. return 0;
  651. if (range->start + range->len > range->start) {
  652. last_index = min_t(u64, inode->i_size - 1,
  653. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  654. } else {
  655. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  656. }
  657. i = range->start >> PAGE_CACHE_SHIFT;
  658. while (i <= last_index) {
  659. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  660. PAGE_CACHE_SIZE,
  661. range->extent_thresh,
  662. &last_len, &skip,
  663. &defrag_end)) {
  664. unsigned long next;
  665. /*
  666. * the should_defrag function tells us how much to skip
  667. * bump our counter by the suggested amount
  668. */
  669. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  670. i = max(i + 1, next);
  671. continue;
  672. }
  673. if (total_read % ra_pages == 0) {
  674. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  675. min(last_index, i + ra_pages - 1));
  676. }
  677. total_read++;
  678. mutex_lock(&inode->i_mutex);
  679. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  680. BTRFS_I(inode)->force_compress = compress_type;
  681. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  682. if (ret)
  683. goto err_unlock;
  684. again:
  685. if (inode->i_size == 0 ||
  686. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  687. ret = 0;
  688. goto err_reservations;
  689. }
  690. page = grab_cache_page(inode->i_mapping, i);
  691. if (!page) {
  692. ret = -ENOMEM;
  693. goto err_reservations;
  694. }
  695. if (!PageUptodate(page)) {
  696. btrfs_readpage(NULL, page);
  697. lock_page(page);
  698. if (!PageUptodate(page)) {
  699. unlock_page(page);
  700. page_cache_release(page);
  701. ret = -EIO;
  702. goto err_reservations;
  703. }
  704. }
  705. if (page->mapping != inode->i_mapping) {
  706. unlock_page(page);
  707. page_cache_release(page);
  708. goto again;
  709. }
  710. wait_on_page_writeback(page);
  711. if (PageDirty(page)) {
  712. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  713. goto loop_unlock;
  714. }
  715. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  716. page_end = page_start + PAGE_CACHE_SIZE - 1;
  717. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  718. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  719. if (ordered) {
  720. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  721. unlock_page(page);
  722. page_cache_release(page);
  723. btrfs_start_ordered_extent(inode, ordered, 1);
  724. btrfs_put_ordered_extent(ordered);
  725. goto again;
  726. }
  727. set_page_extent_mapped(page);
  728. /*
  729. * this makes sure page_mkwrite is called on the
  730. * page if it is dirtied again later
  731. */
  732. clear_page_dirty_for_io(page);
  733. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  734. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  735. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  736. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  737. ClearPageChecked(page);
  738. set_page_dirty(page);
  739. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  740. loop_unlock:
  741. unlock_page(page);
  742. page_cache_release(page);
  743. mutex_unlock(&inode->i_mutex);
  744. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  745. i++;
  746. }
  747. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  748. filemap_flush(inode->i_mapping);
  749. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  750. /* the filemap_flush will queue IO into the worker threads, but
  751. * we have to make sure the IO is actually started and that
  752. * ordered extents get created before we return
  753. */
  754. atomic_inc(&root->fs_info->async_submit_draining);
  755. while (atomic_read(&root->fs_info->nr_async_submits) ||
  756. atomic_read(&root->fs_info->async_delalloc_pages)) {
  757. wait_event(root->fs_info->async_submit_wait,
  758. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  759. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  760. }
  761. atomic_dec(&root->fs_info->async_submit_draining);
  762. mutex_lock(&inode->i_mutex);
  763. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  764. mutex_unlock(&inode->i_mutex);
  765. }
  766. disk_super = &root->fs_info->super_copy;
  767. features = btrfs_super_incompat_flags(disk_super);
  768. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  769. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  770. btrfs_set_super_incompat_flags(disk_super, features);
  771. }
  772. return 0;
  773. err_reservations:
  774. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  775. err_unlock:
  776. mutex_unlock(&inode->i_mutex);
  777. return ret;
  778. }
  779. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  780. void __user *arg)
  781. {
  782. u64 new_size;
  783. u64 old_size;
  784. u64 devid = 1;
  785. struct btrfs_ioctl_vol_args *vol_args;
  786. struct btrfs_trans_handle *trans;
  787. struct btrfs_device *device = NULL;
  788. char *sizestr;
  789. char *devstr = NULL;
  790. int ret = 0;
  791. int mod = 0;
  792. if (root->fs_info->sb->s_flags & MS_RDONLY)
  793. return -EROFS;
  794. if (!capable(CAP_SYS_ADMIN))
  795. return -EPERM;
  796. vol_args = memdup_user(arg, sizeof(*vol_args));
  797. if (IS_ERR(vol_args))
  798. return PTR_ERR(vol_args);
  799. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  800. mutex_lock(&root->fs_info->volume_mutex);
  801. sizestr = vol_args->name;
  802. devstr = strchr(sizestr, ':');
  803. if (devstr) {
  804. char *end;
  805. sizestr = devstr + 1;
  806. *devstr = '\0';
  807. devstr = vol_args->name;
  808. devid = simple_strtoull(devstr, &end, 10);
  809. printk(KERN_INFO "resizing devid %llu\n",
  810. (unsigned long long)devid);
  811. }
  812. device = btrfs_find_device(root, devid, NULL, NULL);
  813. if (!device) {
  814. printk(KERN_INFO "resizer unable to find device %llu\n",
  815. (unsigned long long)devid);
  816. ret = -EINVAL;
  817. goto out_unlock;
  818. }
  819. if (!strcmp(sizestr, "max"))
  820. new_size = device->bdev->bd_inode->i_size;
  821. else {
  822. if (sizestr[0] == '-') {
  823. mod = -1;
  824. sizestr++;
  825. } else if (sizestr[0] == '+') {
  826. mod = 1;
  827. sizestr++;
  828. }
  829. new_size = memparse(sizestr, NULL);
  830. if (new_size == 0) {
  831. ret = -EINVAL;
  832. goto out_unlock;
  833. }
  834. }
  835. old_size = device->total_bytes;
  836. if (mod < 0) {
  837. if (new_size > old_size) {
  838. ret = -EINVAL;
  839. goto out_unlock;
  840. }
  841. new_size = old_size - new_size;
  842. } else if (mod > 0) {
  843. new_size = old_size + new_size;
  844. }
  845. if (new_size < 256 * 1024 * 1024) {
  846. ret = -EINVAL;
  847. goto out_unlock;
  848. }
  849. if (new_size > device->bdev->bd_inode->i_size) {
  850. ret = -EFBIG;
  851. goto out_unlock;
  852. }
  853. do_div(new_size, root->sectorsize);
  854. new_size *= root->sectorsize;
  855. printk(KERN_INFO "new size for %s is %llu\n",
  856. device->name, (unsigned long long)new_size);
  857. if (new_size > old_size) {
  858. trans = btrfs_start_transaction(root, 0);
  859. if (IS_ERR(trans)) {
  860. ret = PTR_ERR(trans);
  861. goto out_unlock;
  862. }
  863. ret = btrfs_grow_device(trans, device, new_size);
  864. btrfs_commit_transaction(trans, root);
  865. } else {
  866. ret = btrfs_shrink_device(device, new_size);
  867. }
  868. out_unlock:
  869. mutex_unlock(&root->fs_info->volume_mutex);
  870. kfree(vol_args);
  871. return ret;
  872. }
  873. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  874. char *name,
  875. unsigned long fd,
  876. int subvol,
  877. u64 *transid,
  878. bool readonly)
  879. {
  880. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  881. struct file *src_file;
  882. int namelen;
  883. int ret = 0;
  884. if (root->fs_info->sb->s_flags & MS_RDONLY)
  885. return -EROFS;
  886. namelen = strlen(name);
  887. if (strchr(name, '/')) {
  888. ret = -EINVAL;
  889. goto out;
  890. }
  891. if (subvol) {
  892. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  893. NULL, transid, readonly);
  894. } else {
  895. struct inode *src_inode;
  896. src_file = fget(fd);
  897. if (!src_file) {
  898. ret = -EINVAL;
  899. goto out;
  900. }
  901. src_inode = src_file->f_path.dentry->d_inode;
  902. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  903. printk(KERN_INFO "btrfs: Snapshot src from "
  904. "another FS\n");
  905. ret = -EINVAL;
  906. fput(src_file);
  907. goto out;
  908. }
  909. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  910. BTRFS_I(src_inode)->root,
  911. transid, readonly);
  912. fput(src_file);
  913. }
  914. out:
  915. return ret;
  916. }
  917. static noinline int btrfs_ioctl_snap_create(struct file *file,
  918. void __user *arg, int subvol)
  919. {
  920. struct btrfs_ioctl_vol_args *vol_args;
  921. int ret;
  922. vol_args = memdup_user(arg, sizeof(*vol_args));
  923. if (IS_ERR(vol_args))
  924. return PTR_ERR(vol_args);
  925. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  926. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  927. vol_args->fd, subvol,
  928. NULL, false);
  929. kfree(vol_args);
  930. return ret;
  931. }
  932. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  933. void __user *arg, int subvol)
  934. {
  935. struct btrfs_ioctl_vol_args_v2 *vol_args;
  936. int ret;
  937. u64 transid = 0;
  938. u64 *ptr = NULL;
  939. bool readonly = false;
  940. vol_args = memdup_user(arg, sizeof(*vol_args));
  941. if (IS_ERR(vol_args))
  942. return PTR_ERR(vol_args);
  943. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  944. if (vol_args->flags &
  945. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  946. ret = -EOPNOTSUPP;
  947. goto out;
  948. }
  949. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  950. ptr = &transid;
  951. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  952. readonly = true;
  953. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  954. vol_args->fd, subvol,
  955. ptr, readonly);
  956. if (ret == 0 && ptr &&
  957. copy_to_user(arg +
  958. offsetof(struct btrfs_ioctl_vol_args_v2,
  959. transid), ptr, sizeof(*ptr)))
  960. ret = -EFAULT;
  961. out:
  962. kfree(vol_args);
  963. return ret;
  964. }
  965. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  966. void __user *arg)
  967. {
  968. struct inode *inode = fdentry(file)->d_inode;
  969. struct btrfs_root *root = BTRFS_I(inode)->root;
  970. int ret = 0;
  971. u64 flags = 0;
  972. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  973. return -EINVAL;
  974. down_read(&root->fs_info->subvol_sem);
  975. if (btrfs_root_readonly(root))
  976. flags |= BTRFS_SUBVOL_RDONLY;
  977. up_read(&root->fs_info->subvol_sem);
  978. if (copy_to_user(arg, &flags, sizeof(flags)))
  979. ret = -EFAULT;
  980. return ret;
  981. }
  982. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  983. void __user *arg)
  984. {
  985. struct inode *inode = fdentry(file)->d_inode;
  986. struct btrfs_root *root = BTRFS_I(inode)->root;
  987. struct btrfs_trans_handle *trans;
  988. u64 root_flags;
  989. u64 flags;
  990. int ret = 0;
  991. if (root->fs_info->sb->s_flags & MS_RDONLY)
  992. return -EROFS;
  993. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  994. return -EINVAL;
  995. if (copy_from_user(&flags, arg, sizeof(flags)))
  996. return -EFAULT;
  997. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  998. return -EINVAL;
  999. if (flags & ~BTRFS_SUBVOL_RDONLY)
  1000. return -EOPNOTSUPP;
  1001. if (!is_owner_or_cap(inode))
  1002. return -EACCES;
  1003. down_write(&root->fs_info->subvol_sem);
  1004. /* nothing to do */
  1005. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1006. goto out;
  1007. root_flags = btrfs_root_flags(&root->root_item);
  1008. if (flags & BTRFS_SUBVOL_RDONLY)
  1009. btrfs_set_root_flags(&root->root_item,
  1010. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1011. else
  1012. btrfs_set_root_flags(&root->root_item,
  1013. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1014. trans = btrfs_start_transaction(root, 1);
  1015. if (IS_ERR(trans)) {
  1016. ret = PTR_ERR(trans);
  1017. goto out_reset;
  1018. }
  1019. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1020. &root->root_key, &root->root_item);
  1021. btrfs_commit_transaction(trans, root);
  1022. out_reset:
  1023. if (ret)
  1024. btrfs_set_root_flags(&root->root_item, root_flags);
  1025. out:
  1026. up_write(&root->fs_info->subvol_sem);
  1027. return ret;
  1028. }
  1029. /*
  1030. * helper to check if the subvolume references other subvolumes
  1031. */
  1032. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1033. {
  1034. struct btrfs_path *path;
  1035. struct btrfs_key key;
  1036. int ret;
  1037. path = btrfs_alloc_path();
  1038. if (!path)
  1039. return -ENOMEM;
  1040. key.objectid = root->root_key.objectid;
  1041. key.type = BTRFS_ROOT_REF_KEY;
  1042. key.offset = (u64)-1;
  1043. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1044. &key, path, 0, 0);
  1045. if (ret < 0)
  1046. goto out;
  1047. BUG_ON(ret == 0);
  1048. ret = 0;
  1049. if (path->slots[0] > 0) {
  1050. path->slots[0]--;
  1051. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1052. if (key.objectid == root->root_key.objectid &&
  1053. key.type == BTRFS_ROOT_REF_KEY)
  1054. ret = -ENOTEMPTY;
  1055. }
  1056. out:
  1057. btrfs_free_path(path);
  1058. return ret;
  1059. }
  1060. static noinline int key_in_sk(struct btrfs_key *key,
  1061. struct btrfs_ioctl_search_key *sk)
  1062. {
  1063. struct btrfs_key test;
  1064. int ret;
  1065. test.objectid = sk->min_objectid;
  1066. test.type = sk->min_type;
  1067. test.offset = sk->min_offset;
  1068. ret = btrfs_comp_cpu_keys(key, &test);
  1069. if (ret < 0)
  1070. return 0;
  1071. test.objectid = sk->max_objectid;
  1072. test.type = sk->max_type;
  1073. test.offset = sk->max_offset;
  1074. ret = btrfs_comp_cpu_keys(key, &test);
  1075. if (ret > 0)
  1076. return 0;
  1077. return 1;
  1078. }
  1079. static noinline int copy_to_sk(struct btrfs_root *root,
  1080. struct btrfs_path *path,
  1081. struct btrfs_key *key,
  1082. struct btrfs_ioctl_search_key *sk,
  1083. char *buf,
  1084. unsigned long *sk_offset,
  1085. int *num_found)
  1086. {
  1087. u64 found_transid;
  1088. struct extent_buffer *leaf;
  1089. struct btrfs_ioctl_search_header sh;
  1090. unsigned long item_off;
  1091. unsigned long item_len;
  1092. int nritems;
  1093. int i;
  1094. int slot;
  1095. int found = 0;
  1096. int ret = 0;
  1097. leaf = path->nodes[0];
  1098. slot = path->slots[0];
  1099. nritems = btrfs_header_nritems(leaf);
  1100. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1101. i = nritems;
  1102. goto advance_key;
  1103. }
  1104. found_transid = btrfs_header_generation(leaf);
  1105. for (i = slot; i < nritems; i++) {
  1106. item_off = btrfs_item_ptr_offset(leaf, i);
  1107. item_len = btrfs_item_size_nr(leaf, i);
  1108. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1109. item_len = 0;
  1110. if (sizeof(sh) + item_len + *sk_offset >
  1111. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1112. ret = 1;
  1113. goto overflow;
  1114. }
  1115. btrfs_item_key_to_cpu(leaf, key, i);
  1116. if (!key_in_sk(key, sk))
  1117. continue;
  1118. sh.objectid = key->objectid;
  1119. sh.offset = key->offset;
  1120. sh.type = key->type;
  1121. sh.len = item_len;
  1122. sh.transid = found_transid;
  1123. /* copy search result header */
  1124. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1125. *sk_offset += sizeof(sh);
  1126. if (item_len) {
  1127. char *p = buf + *sk_offset;
  1128. /* copy the item */
  1129. read_extent_buffer(leaf, p,
  1130. item_off, item_len);
  1131. *sk_offset += item_len;
  1132. }
  1133. found++;
  1134. if (*num_found >= sk->nr_items)
  1135. break;
  1136. }
  1137. advance_key:
  1138. ret = 0;
  1139. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1140. key->offset++;
  1141. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1142. key->offset = 0;
  1143. key->type++;
  1144. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1145. key->offset = 0;
  1146. key->type = 0;
  1147. key->objectid++;
  1148. } else
  1149. ret = 1;
  1150. overflow:
  1151. *num_found += found;
  1152. return ret;
  1153. }
  1154. static noinline int search_ioctl(struct inode *inode,
  1155. struct btrfs_ioctl_search_args *args)
  1156. {
  1157. struct btrfs_root *root;
  1158. struct btrfs_key key;
  1159. struct btrfs_key max_key;
  1160. struct btrfs_path *path;
  1161. struct btrfs_ioctl_search_key *sk = &args->key;
  1162. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1163. int ret;
  1164. int num_found = 0;
  1165. unsigned long sk_offset = 0;
  1166. path = btrfs_alloc_path();
  1167. if (!path)
  1168. return -ENOMEM;
  1169. if (sk->tree_id == 0) {
  1170. /* search the root of the inode that was passed */
  1171. root = BTRFS_I(inode)->root;
  1172. } else {
  1173. key.objectid = sk->tree_id;
  1174. key.type = BTRFS_ROOT_ITEM_KEY;
  1175. key.offset = (u64)-1;
  1176. root = btrfs_read_fs_root_no_name(info, &key);
  1177. if (IS_ERR(root)) {
  1178. printk(KERN_ERR "could not find root %llu\n",
  1179. sk->tree_id);
  1180. btrfs_free_path(path);
  1181. return -ENOENT;
  1182. }
  1183. }
  1184. key.objectid = sk->min_objectid;
  1185. key.type = sk->min_type;
  1186. key.offset = sk->min_offset;
  1187. max_key.objectid = sk->max_objectid;
  1188. max_key.type = sk->max_type;
  1189. max_key.offset = sk->max_offset;
  1190. path->keep_locks = 1;
  1191. while(1) {
  1192. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1193. sk->min_transid);
  1194. if (ret != 0) {
  1195. if (ret > 0)
  1196. ret = 0;
  1197. goto err;
  1198. }
  1199. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1200. &sk_offset, &num_found);
  1201. btrfs_release_path(root, path);
  1202. if (ret || num_found >= sk->nr_items)
  1203. break;
  1204. }
  1205. ret = 0;
  1206. err:
  1207. sk->nr_items = num_found;
  1208. btrfs_free_path(path);
  1209. return ret;
  1210. }
  1211. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1212. void __user *argp)
  1213. {
  1214. struct btrfs_ioctl_search_args *args;
  1215. struct inode *inode;
  1216. int ret;
  1217. if (!capable(CAP_SYS_ADMIN))
  1218. return -EPERM;
  1219. args = memdup_user(argp, sizeof(*args));
  1220. if (IS_ERR(args))
  1221. return PTR_ERR(args);
  1222. inode = fdentry(file)->d_inode;
  1223. ret = search_ioctl(inode, args);
  1224. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1225. ret = -EFAULT;
  1226. kfree(args);
  1227. return ret;
  1228. }
  1229. /*
  1230. * Search INODE_REFs to identify path name of 'dirid' directory
  1231. * in a 'tree_id' tree. and sets path name to 'name'.
  1232. */
  1233. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1234. u64 tree_id, u64 dirid, char *name)
  1235. {
  1236. struct btrfs_root *root;
  1237. struct btrfs_key key;
  1238. char *ptr;
  1239. int ret = -1;
  1240. int slot;
  1241. int len;
  1242. int total_len = 0;
  1243. struct btrfs_inode_ref *iref;
  1244. struct extent_buffer *l;
  1245. struct btrfs_path *path;
  1246. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1247. name[0]='\0';
  1248. return 0;
  1249. }
  1250. path = btrfs_alloc_path();
  1251. if (!path)
  1252. return -ENOMEM;
  1253. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1254. key.objectid = tree_id;
  1255. key.type = BTRFS_ROOT_ITEM_KEY;
  1256. key.offset = (u64)-1;
  1257. root = btrfs_read_fs_root_no_name(info, &key);
  1258. if (IS_ERR(root)) {
  1259. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1260. ret = -ENOENT;
  1261. goto out;
  1262. }
  1263. key.objectid = dirid;
  1264. key.type = BTRFS_INODE_REF_KEY;
  1265. key.offset = (u64)-1;
  1266. while(1) {
  1267. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1268. if (ret < 0)
  1269. goto out;
  1270. l = path->nodes[0];
  1271. slot = path->slots[0];
  1272. if (ret > 0 && slot > 0)
  1273. slot--;
  1274. btrfs_item_key_to_cpu(l, &key, slot);
  1275. if (ret > 0 && (key.objectid != dirid ||
  1276. key.type != BTRFS_INODE_REF_KEY)) {
  1277. ret = -ENOENT;
  1278. goto out;
  1279. }
  1280. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1281. len = btrfs_inode_ref_name_len(l, iref);
  1282. ptr -= len + 1;
  1283. total_len += len + 1;
  1284. if (ptr < name)
  1285. goto out;
  1286. *(ptr + len) = '/';
  1287. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1288. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1289. break;
  1290. btrfs_release_path(root, path);
  1291. key.objectid = key.offset;
  1292. key.offset = (u64)-1;
  1293. dirid = key.objectid;
  1294. }
  1295. if (ptr < name)
  1296. goto out;
  1297. memcpy(name, ptr, total_len);
  1298. name[total_len]='\0';
  1299. ret = 0;
  1300. out:
  1301. btrfs_free_path(path);
  1302. return ret;
  1303. }
  1304. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1305. void __user *argp)
  1306. {
  1307. struct btrfs_ioctl_ino_lookup_args *args;
  1308. struct inode *inode;
  1309. int ret;
  1310. if (!capable(CAP_SYS_ADMIN))
  1311. return -EPERM;
  1312. args = memdup_user(argp, sizeof(*args));
  1313. if (IS_ERR(args))
  1314. return PTR_ERR(args);
  1315. inode = fdentry(file)->d_inode;
  1316. if (args->treeid == 0)
  1317. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1318. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1319. args->treeid, args->objectid,
  1320. args->name);
  1321. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1322. ret = -EFAULT;
  1323. kfree(args);
  1324. return ret;
  1325. }
  1326. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1327. void __user *arg)
  1328. {
  1329. struct dentry *parent = fdentry(file);
  1330. struct dentry *dentry;
  1331. struct inode *dir = parent->d_inode;
  1332. struct inode *inode;
  1333. struct btrfs_root *root = BTRFS_I(dir)->root;
  1334. struct btrfs_root *dest = NULL;
  1335. struct btrfs_ioctl_vol_args *vol_args;
  1336. struct btrfs_trans_handle *trans;
  1337. int namelen;
  1338. int ret;
  1339. int err = 0;
  1340. vol_args = memdup_user(arg, sizeof(*vol_args));
  1341. if (IS_ERR(vol_args))
  1342. return PTR_ERR(vol_args);
  1343. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1344. namelen = strlen(vol_args->name);
  1345. if (strchr(vol_args->name, '/') ||
  1346. strncmp(vol_args->name, "..", namelen) == 0) {
  1347. err = -EINVAL;
  1348. goto out;
  1349. }
  1350. err = mnt_want_write(file->f_path.mnt);
  1351. if (err)
  1352. goto out;
  1353. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1354. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1355. if (IS_ERR(dentry)) {
  1356. err = PTR_ERR(dentry);
  1357. goto out_unlock_dir;
  1358. }
  1359. if (!dentry->d_inode) {
  1360. err = -ENOENT;
  1361. goto out_dput;
  1362. }
  1363. inode = dentry->d_inode;
  1364. dest = BTRFS_I(inode)->root;
  1365. if (!capable(CAP_SYS_ADMIN)){
  1366. /*
  1367. * Regular user. Only allow this with a special mount
  1368. * option, when the user has write+exec access to the
  1369. * subvol root, and when rmdir(2) would have been
  1370. * allowed.
  1371. *
  1372. * Note that this is _not_ check that the subvol is
  1373. * empty or doesn't contain data that we wouldn't
  1374. * otherwise be able to delete.
  1375. *
  1376. * Users who want to delete empty subvols should try
  1377. * rmdir(2).
  1378. */
  1379. err = -EPERM;
  1380. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1381. goto out_dput;
  1382. /*
  1383. * Do not allow deletion if the parent dir is the same
  1384. * as the dir to be deleted. That means the ioctl
  1385. * must be called on the dentry referencing the root
  1386. * of the subvol, not a random directory contained
  1387. * within it.
  1388. */
  1389. err = -EINVAL;
  1390. if (root == dest)
  1391. goto out_dput;
  1392. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1393. if (err)
  1394. goto out_dput;
  1395. /* check if subvolume may be deleted by a non-root user */
  1396. err = btrfs_may_delete(dir, dentry, 1);
  1397. if (err)
  1398. goto out_dput;
  1399. }
  1400. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1401. err = -EINVAL;
  1402. goto out_dput;
  1403. }
  1404. mutex_lock(&inode->i_mutex);
  1405. err = d_invalidate(dentry);
  1406. if (err)
  1407. goto out_unlock;
  1408. down_write(&root->fs_info->subvol_sem);
  1409. err = may_destroy_subvol(dest);
  1410. if (err)
  1411. goto out_up_write;
  1412. trans = btrfs_start_transaction(root, 0);
  1413. if (IS_ERR(trans)) {
  1414. err = PTR_ERR(trans);
  1415. goto out_up_write;
  1416. }
  1417. trans->block_rsv = &root->fs_info->global_block_rsv;
  1418. ret = btrfs_unlink_subvol(trans, root, dir,
  1419. dest->root_key.objectid,
  1420. dentry->d_name.name,
  1421. dentry->d_name.len);
  1422. BUG_ON(ret);
  1423. btrfs_record_root_in_trans(trans, dest);
  1424. memset(&dest->root_item.drop_progress, 0,
  1425. sizeof(dest->root_item.drop_progress));
  1426. dest->root_item.drop_level = 0;
  1427. btrfs_set_root_refs(&dest->root_item, 0);
  1428. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1429. ret = btrfs_insert_orphan_item(trans,
  1430. root->fs_info->tree_root,
  1431. dest->root_key.objectid);
  1432. BUG_ON(ret);
  1433. }
  1434. ret = btrfs_end_transaction(trans, root);
  1435. BUG_ON(ret);
  1436. inode->i_flags |= S_DEAD;
  1437. out_up_write:
  1438. up_write(&root->fs_info->subvol_sem);
  1439. out_unlock:
  1440. mutex_unlock(&inode->i_mutex);
  1441. if (!err) {
  1442. shrink_dcache_sb(root->fs_info->sb);
  1443. btrfs_invalidate_inodes(dest);
  1444. d_delete(dentry);
  1445. }
  1446. out_dput:
  1447. dput(dentry);
  1448. out_unlock_dir:
  1449. mutex_unlock(&dir->i_mutex);
  1450. mnt_drop_write(file->f_path.mnt);
  1451. out:
  1452. kfree(vol_args);
  1453. return err;
  1454. }
  1455. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1456. {
  1457. struct inode *inode = fdentry(file)->d_inode;
  1458. struct btrfs_root *root = BTRFS_I(inode)->root;
  1459. struct btrfs_ioctl_defrag_range_args *range;
  1460. int ret;
  1461. if (btrfs_root_readonly(root))
  1462. return -EROFS;
  1463. ret = mnt_want_write(file->f_path.mnt);
  1464. if (ret)
  1465. return ret;
  1466. switch (inode->i_mode & S_IFMT) {
  1467. case S_IFDIR:
  1468. if (!capable(CAP_SYS_ADMIN)) {
  1469. ret = -EPERM;
  1470. goto out;
  1471. }
  1472. ret = btrfs_defrag_root(root, 0);
  1473. if (ret)
  1474. goto out;
  1475. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1476. break;
  1477. case S_IFREG:
  1478. if (!(file->f_mode & FMODE_WRITE)) {
  1479. ret = -EINVAL;
  1480. goto out;
  1481. }
  1482. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1483. if (!range) {
  1484. ret = -ENOMEM;
  1485. goto out;
  1486. }
  1487. if (argp) {
  1488. if (copy_from_user(range, argp,
  1489. sizeof(*range))) {
  1490. ret = -EFAULT;
  1491. kfree(range);
  1492. goto out;
  1493. }
  1494. /* compression requires us to start the IO */
  1495. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1496. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1497. range->extent_thresh = (u32)-1;
  1498. }
  1499. } else {
  1500. /* the rest are all set to zero by kzalloc */
  1501. range->len = (u64)-1;
  1502. }
  1503. ret = btrfs_defrag_file(file, range);
  1504. kfree(range);
  1505. break;
  1506. default:
  1507. ret = -EINVAL;
  1508. }
  1509. out:
  1510. mnt_drop_write(file->f_path.mnt);
  1511. return ret;
  1512. }
  1513. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1514. {
  1515. struct btrfs_ioctl_vol_args *vol_args;
  1516. int ret;
  1517. if (!capable(CAP_SYS_ADMIN))
  1518. return -EPERM;
  1519. vol_args = memdup_user(arg, sizeof(*vol_args));
  1520. if (IS_ERR(vol_args))
  1521. return PTR_ERR(vol_args);
  1522. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1523. ret = btrfs_init_new_device(root, vol_args->name);
  1524. kfree(vol_args);
  1525. return ret;
  1526. }
  1527. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1528. {
  1529. struct btrfs_ioctl_vol_args *vol_args;
  1530. int ret;
  1531. if (!capable(CAP_SYS_ADMIN))
  1532. return -EPERM;
  1533. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1534. return -EROFS;
  1535. vol_args = memdup_user(arg, sizeof(*vol_args));
  1536. if (IS_ERR(vol_args))
  1537. return PTR_ERR(vol_args);
  1538. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1539. ret = btrfs_rm_device(root, vol_args->name);
  1540. kfree(vol_args);
  1541. return ret;
  1542. }
  1543. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1544. u64 off, u64 olen, u64 destoff)
  1545. {
  1546. struct inode *inode = fdentry(file)->d_inode;
  1547. struct btrfs_root *root = BTRFS_I(inode)->root;
  1548. struct file *src_file;
  1549. struct inode *src;
  1550. struct btrfs_trans_handle *trans;
  1551. struct btrfs_path *path;
  1552. struct extent_buffer *leaf;
  1553. char *buf;
  1554. struct btrfs_key key;
  1555. u32 nritems;
  1556. int slot;
  1557. int ret;
  1558. u64 len = olen;
  1559. u64 bs = root->fs_info->sb->s_blocksize;
  1560. u64 hint_byte;
  1561. /*
  1562. * TODO:
  1563. * - split compressed inline extents. annoying: we need to
  1564. * decompress into destination's address_space (the file offset
  1565. * may change, so source mapping won't do), then recompress (or
  1566. * otherwise reinsert) a subrange.
  1567. * - allow ranges within the same file to be cloned (provided
  1568. * they don't overlap)?
  1569. */
  1570. /* the destination must be opened for writing */
  1571. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1572. return -EINVAL;
  1573. if (btrfs_root_readonly(root))
  1574. return -EROFS;
  1575. ret = mnt_want_write(file->f_path.mnt);
  1576. if (ret)
  1577. return ret;
  1578. src_file = fget(srcfd);
  1579. if (!src_file) {
  1580. ret = -EBADF;
  1581. goto out_drop_write;
  1582. }
  1583. src = src_file->f_dentry->d_inode;
  1584. ret = -EINVAL;
  1585. if (src == inode)
  1586. goto out_fput;
  1587. /* the src must be open for reading */
  1588. if (!(src_file->f_mode & FMODE_READ))
  1589. goto out_fput;
  1590. ret = -EISDIR;
  1591. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1592. goto out_fput;
  1593. ret = -EXDEV;
  1594. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1595. goto out_fput;
  1596. ret = -ENOMEM;
  1597. buf = vmalloc(btrfs_level_size(root, 0));
  1598. if (!buf)
  1599. goto out_fput;
  1600. path = btrfs_alloc_path();
  1601. if (!path) {
  1602. vfree(buf);
  1603. goto out_fput;
  1604. }
  1605. path->reada = 2;
  1606. if (inode < src) {
  1607. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1608. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1609. } else {
  1610. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1611. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1612. }
  1613. /* determine range to clone */
  1614. ret = -EINVAL;
  1615. if (off + len > src->i_size || off + len < off)
  1616. goto out_unlock;
  1617. if (len == 0)
  1618. olen = len = src->i_size - off;
  1619. /* if we extend to eof, continue to block boundary */
  1620. if (off + len == src->i_size)
  1621. len = ALIGN(src->i_size, bs) - off;
  1622. /* verify the end result is block aligned */
  1623. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1624. !IS_ALIGNED(destoff, bs))
  1625. goto out_unlock;
  1626. /* do any pending delalloc/csum calc on src, one way or
  1627. another, and lock file content */
  1628. while (1) {
  1629. struct btrfs_ordered_extent *ordered;
  1630. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1631. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1632. if (!ordered &&
  1633. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1634. EXTENT_DELALLOC, 0, NULL))
  1635. break;
  1636. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1637. if (ordered)
  1638. btrfs_put_ordered_extent(ordered);
  1639. btrfs_wait_ordered_range(src, off, len);
  1640. }
  1641. /* clone data */
  1642. key.objectid = src->i_ino;
  1643. key.type = BTRFS_EXTENT_DATA_KEY;
  1644. key.offset = 0;
  1645. while (1) {
  1646. /*
  1647. * note the key will change type as we walk through the
  1648. * tree.
  1649. */
  1650. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1651. if (ret < 0)
  1652. goto out;
  1653. nritems = btrfs_header_nritems(path->nodes[0]);
  1654. if (path->slots[0] >= nritems) {
  1655. ret = btrfs_next_leaf(root, path);
  1656. if (ret < 0)
  1657. goto out;
  1658. if (ret > 0)
  1659. break;
  1660. nritems = btrfs_header_nritems(path->nodes[0]);
  1661. }
  1662. leaf = path->nodes[0];
  1663. slot = path->slots[0];
  1664. btrfs_item_key_to_cpu(leaf, &key, slot);
  1665. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1666. key.objectid != src->i_ino)
  1667. break;
  1668. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1669. struct btrfs_file_extent_item *extent;
  1670. int type;
  1671. u32 size;
  1672. struct btrfs_key new_key;
  1673. u64 disko = 0, diskl = 0;
  1674. u64 datao = 0, datal = 0;
  1675. u8 comp;
  1676. u64 endoff;
  1677. size = btrfs_item_size_nr(leaf, slot);
  1678. read_extent_buffer(leaf, buf,
  1679. btrfs_item_ptr_offset(leaf, slot),
  1680. size);
  1681. extent = btrfs_item_ptr(leaf, slot,
  1682. struct btrfs_file_extent_item);
  1683. comp = btrfs_file_extent_compression(leaf, extent);
  1684. type = btrfs_file_extent_type(leaf, extent);
  1685. if (type == BTRFS_FILE_EXTENT_REG ||
  1686. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1687. disko = btrfs_file_extent_disk_bytenr(leaf,
  1688. extent);
  1689. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1690. extent);
  1691. datao = btrfs_file_extent_offset(leaf, extent);
  1692. datal = btrfs_file_extent_num_bytes(leaf,
  1693. extent);
  1694. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1695. /* take upper bound, may be compressed */
  1696. datal = btrfs_file_extent_ram_bytes(leaf,
  1697. extent);
  1698. }
  1699. btrfs_release_path(root, path);
  1700. if (key.offset + datal <= off ||
  1701. key.offset >= off+len)
  1702. goto next;
  1703. memcpy(&new_key, &key, sizeof(new_key));
  1704. new_key.objectid = inode->i_ino;
  1705. if (off <= key.offset)
  1706. new_key.offset = key.offset + destoff - off;
  1707. else
  1708. new_key.offset = destoff;
  1709. trans = btrfs_start_transaction(root, 1);
  1710. if (IS_ERR(trans)) {
  1711. ret = PTR_ERR(trans);
  1712. goto out;
  1713. }
  1714. if (type == BTRFS_FILE_EXTENT_REG ||
  1715. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1716. if (off > key.offset) {
  1717. datao += off - key.offset;
  1718. datal -= off - key.offset;
  1719. }
  1720. if (key.offset + datal > off + len)
  1721. datal = off + len - key.offset;
  1722. ret = btrfs_drop_extents(trans, inode,
  1723. new_key.offset,
  1724. new_key.offset + datal,
  1725. &hint_byte, 1);
  1726. BUG_ON(ret);
  1727. ret = btrfs_insert_empty_item(trans, root, path,
  1728. &new_key, size);
  1729. BUG_ON(ret);
  1730. leaf = path->nodes[0];
  1731. slot = path->slots[0];
  1732. write_extent_buffer(leaf, buf,
  1733. btrfs_item_ptr_offset(leaf, slot),
  1734. size);
  1735. extent = btrfs_item_ptr(leaf, slot,
  1736. struct btrfs_file_extent_item);
  1737. /* disko == 0 means it's a hole */
  1738. if (!disko)
  1739. datao = 0;
  1740. btrfs_set_file_extent_offset(leaf, extent,
  1741. datao);
  1742. btrfs_set_file_extent_num_bytes(leaf, extent,
  1743. datal);
  1744. if (disko) {
  1745. inode_add_bytes(inode, datal);
  1746. ret = btrfs_inc_extent_ref(trans, root,
  1747. disko, diskl, 0,
  1748. root->root_key.objectid,
  1749. inode->i_ino,
  1750. new_key.offset - datao);
  1751. BUG_ON(ret);
  1752. }
  1753. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1754. u64 skip = 0;
  1755. u64 trim = 0;
  1756. if (off > key.offset) {
  1757. skip = off - key.offset;
  1758. new_key.offset += skip;
  1759. }
  1760. if (key.offset + datal > off+len)
  1761. trim = key.offset + datal - (off+len);
  1762. if (comp && (skip || trim)) {
  1763. ret = -EINVAL;
  1764. btrfs_end_transaction(trans, root);
  1765. goto out;
  1766. }
  1767. size -= skip + trim;
  1768. datal -= skip + trim;
  1769. ret = btrfs_drop_extents(trans, inode,
  1770. new_key.offset,
  1771. new_key.offset + datal,
  1772. &hint_byte, 1);
  1773. BUG_ON(ret);
  1774. ret = btrfs_insert_empty_item(trans, root, path,
  1775. &new_key, size);
  1776. BUG_ON(ret);
  1777. if (skip) {
  1778. u32 start =
  1779. btrfs_file_extent_calc_inline_size(0);
  1780. memmove(buf+start, buf+start+skip,
  1781. datal);
  1782. }
  1783. leaf = path->nodes[0];
  1784. slot = path->slots[0];
  1785. write_extent_buffer(leaf, buf,
  1786. btrfs_item_ptr_offset(leaf, slot),
  1787. size);
  1788. inode_add_bytes(inode, datal);
  1789. }
  1790. btrfs_mark_buffer_dirty(leaf);
  1791. btrfs_release_path(root, path);
  1792. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1793. /*
  1794. * we round up to the block size at eof when
  1795. * determining which extents to clone above,
  1796. * but shouldn't round up the file size
  1797. */
  1798. endoff = new_key.offset + datal;
  1799. if (endoff > destoff+olen)
  1800. endoff = destoff+olen;
  1801. if (endoff > inode->i_size)
  1802. btrfs_i_size_write(inode, endoff);
  1803. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1804. ret = btrfs_update_inode(trans, root, inode);
  1805. BUG_ON(ret);
  1806. btrfs_end_transaction(trans, root);
  1807. }
  1808. next:
  1809. btrfs_release_path(root, path);
  1810. key.offset++;
  1811. }
  1812. ret = 0;
  1813. out:
  1814. btrfs_release_path(root, path);
  1815. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1816. out_unlock:
  1817. mutex_unlock(&src->i_mutex);
  1818. mutex_unlock(&inode->i_mutex);
  1819. vfree(buf);
  1820. btrfs_free_path(path);
  1821. out_fput:
  1822. fput(src_file);
  1823. out_drop_write:
  1824. mnt_drop_write(file->f_path.mnt);
  1825. return ret;
  1826. }
  1827. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1828. {
  1829. struct btrfs_ioctl_clone_range_args args;
  1830. if (copy_from_user(&args, argp, sizeof(args)))
  1831. return -EFAULT;
  1832. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1833. args.src_length, args.dest_offset);
  1834. }
  1835. /*
  1836. * there are many ways the trans_start and trans_end ioctls can lead
  1837. * to deadlocks. They should only be used by applications that
  1838. * basically own the machine, and have a very in depth understanding
  1839. * of all the possible deadlocks and enospc problems.
  1840. */
  1841. static long btrfs_ioctl_trans_start(struct file *file)
  1842. {
  1843. struct inode *inode = fdentry(file)->d_inode;
  1844. struct btrfs_root *root = BTRFS_I(inode)->root;
  1845. struct btrfs_trans_handle *trans;
  1846. int ret;
  1847. ret = -EPERM;
  1848. if (!capable(CAP_SYS_ADMIN))
  1849. goto out;
  1850. ret = -EINPROGRESS;
  1851. if (file->private_data)
  1852. goto out;
  1853. ret = -EROFS;
  1854. if (btrfs_root_readonly(root))
  1855. goto out;
  1856. ret = mnt_want_write(file->f_path.mnt);
  1857. if (ret)
  1858. goto out;
  1859. mutex_lock(&root->fs_info->trans_mutex);
  1860. root->fs_info->open_ioctl_trans++;
  1861. mutex_unlock(&root->fs_info->trans_mutex);
  1862. ret = -ENOMEM;
  1863. trans = btrfs_start_ioctl_transaction(root, 0);
  1864. if (IS_ERR(trans))
  1865. goto out_drop;
  1866. file->private_data = trans;
  1867. return 0;
  1868. out_drop:
  1869. mutex_lock(&root->fs_info->trans_mutex);
  1870. root->fs_info->open_ioctl_trans--;
  1871. mutex_unlock(&root->fs_info->trans_mutex);
  1872. mnt_drop_write(file->f_path.mnt);
  1873. out:
  1874. return ret;
  1875. }
  1876. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1877. {
  1878. struct inode *inode = fdentry(file)->d_inode;
  1879. struct btrfs_root *root = BTRFS_I(inode)->root;
  1880. struct btrfs_root *new_root;
  1881. struct btrfs_dir_item *di;
  1882. struct btrfs_trans_handle *trans;
  1883. struct btrfs_path *path;
  1884. struct btrfs_key location;
  1885. struct btrfs_disk_key disk_key;
  1886. struct btrfs_super_block *disk_super;
  1887. u64 features;
  1888. u64 objectid = 0;
  1889. u64 dir_id;
  1890. if (!capable(CAP_SYS_ADMIN))
  1891. return -EPERM;
  1892. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1893. return -EFAULT;
  1894. if (!objectid)
  1895. objectid = root->root_key.objectid;
  1896. location.objectid = objectid;
  1897. location.type = BTRFS_ROOT_ITEM_KEY;
  1898. location.offset = (u64)-1;
  1899. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1900. if (IS_ERR(new_root))
  1901. return PTR_ERR(new_root);
  1902. if (btrfs_root_refs(&new_root->root_item) == 0)
  1903. return -ENOENT;
  1904. path = btrfs_alloc_path();
  1905. if (!path)
  1906. return -ENOMEM;
  1907. path->leave_spinning = 1;
  1908. trans = btrfs_start_transaction(root, 1);
  1909. if (IS_ERR(trans)) {
  1910. btrfs_free_path(path);
  1911. return PTR_ERR(trans);
  1912. }
  1913. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1914. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1915. dir_id, "default", 7, 1);
  1916. if (IS_ERR_OR_NULL(di)) {
  1917. btrfs_free_path(path);
  1918. btrfs_end_transaction(trans, root);
  1919. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1920. "this isn't going to work\n");
  1921. return -ENOENT;
  1922. }
  1923. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1924. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1925. btrfs_mark_buffer_dirty(path->nodes[0]);
  1926. btrfs_free_path(path);
  1927. disk_super = &root->fs_info->super_copy;
  1928. features = btrfs_super_incompat_flags(disk_super);
  1929. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1930. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1931. btrfs_set_super_incompat_flags(disk_super, features);
  1932. }
  1933. btrfs_end_transaction(trans, root);
  1934. return 0;
  1935. }
  1936. static void get_block_group_info(struct list_head *groups_list,
  1937. struct btrfs_ioctl_space_info *space)
  1938. {
  1939. struct btrfs_block_group_cache *block_group;
  1940. space->total_bytes = 0;
  1941. space->used_bytes = 0;
  1942. space->flags = 0;
  1943. list_for_each_entry(block_group, groups_list, list) {
  1944. space->flags = block_group->flags;
  1945. space->total_bytes += block_group->key.offset;
  1946. space->used_bytes +=
  1947. btrfs_block_group_used(&block_group->item);
  1948. }
  1949. }
  1950. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1951. {
  1952. struct btrfs_ioctl_space_args space_args;
  1953. struct btrfs_ioctl_space_info space;
  1954. struct btrfs_ioctl_space_info *dest;
  1955. struct btrfs_ioctl_space_info *dest_orig;
  1956. struct btrfs_ioctl_space_info __user *user_dest;
  1957. struct btrfs_space_info *info;
  1958. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1959. BTRFS_BLOCK_GROUP_SYSTEM,
  1960. BTRFS_BLOCK_GROUP_METADATA,
  1961. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1962. int num_types = 4;
  1963. int alloc_size;
  1964. int ret = 0;
  1965. u64 slot_count = 0;
  1966. int i, c;
  1967. if (copy_from_user(&space_args,
  1968. (struct btrfs_ioctl_space_args __user *)arg,
  1969. sizeof(space_args)))
  1970. return -EFAULT;
  1971. for (i = 0; i < num_types; i++) {
  1972. struct btrfs_space_info *tmp;
  1973. info = NULL;
  1974. rcu_read_lock();
  1975. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1976. list) {
  1977. if (tmp->flags == types[i]) {
  1978. info = tmp;
  1979. break;
  1980. }
  1981. }
  1982. rcu_read_unlock();
  1983. if (!info)
  1984. continue;
  1985. down_read(&info->groups_sem);
  1986. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1987. if (!list_empty(&info->block_groups[c]))
  1988. slot_count++;
  1989. }
  1990. up_read(&info->groups_sem);
  1991. }
  1992. /* space_slots == 0 means they are asking for a count */
  1993. if (space_args.space_slots == 0) {
  1994. space_args.total_spaces = slot_count;
  1995. goto out;
  1996. }
  1997. slot_count = min_t(u64, space_args.space_slots, slot_count);
  1998. alloc_size = sizeof(*dest) * slot_count;
  1999. /* we generally have at most 6 or so space infos, one for each raid
  2000. * level. So, a whole page should be more than enough for everyone
  2001. */
  2002. if (alloc_size > PAGE_CACHE_SIZE)
  2003. return -ENOMEM;
  2004. space_args.total_spaces = 0;
  2005. dest = kmalloc(alloc_size, GFP_NOFS);
  2006. if (!dest)
  2007. return -ENOMEM;
  2008. dest_orig = dest;
  2009. /* now we have a buffer to copy into */
  2010. for (i = 0; i < num_types; i++) {
  2011. struct btrfs_space_info *tmp;
  2012. if (!slot_count)
  2013. break;
  2014. info = NULL;
  2015. rcu_read_lock();
  2016. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  2017. list) {
  2018. if (tmp->flags == types[i]) {
  2019. info = tmp;
  2020. break;
  2021. }
  2022. }
  2023. rcu_read_unlock();
  2024. if (!info)
  2025. continue;
  2026. down_read(&info->groups_sem);
  2027. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2028. if (!list_empty(&info->block_groups[c])) {
  2029. get_block_group_info(&info->block_groups[c],
  2030. &space);
  2031. memcpy(dest, &space, sizeof(space));
  2032. dest++;
  2033. space_args.total_spaces++;
  2034. slot_count--;
  2035. }
  2036. if (!slot_count)
  2037. break;
  2038. }
  2039. up_read(&info->groups_sem);
  2040. }
  2041. user_dest = (struct btrfs_ioctl_space_info *)
  2042. (arg + sizeof(struct btrfs_ioctl_space_args));
  2043. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2044. ret = -EFAULT;
  2045. kfree(dest_orig);
  2046. out:
  2047. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2048. ret = -EFAULT;
  2049. return ret;
  2050. }
  2051. /*
  2052. * there are many ways the trans_start and trans_end ioctls can lead
  2053. * to deadlocks. They should only be used by applications that
  2054. * basically own the machine, and have a very in depth understanding
  2055. * of all the possible deadlocks and enospc problems.
  2056. */
  2057. long btrfs_ioctl_trans_end(struct file *file)
  2058. {
  2059. struct inode *inode = fdentry(file)->d_inode;
  2060. struct btrfs_root *root = BTRFS_I(inode)->root;
  2061. struct btrfs_trans_handle *trans;
  2062. trans = file->private_data;
  2063. if (!trans)
  2064. return -EINVAL;
  2065. file->private_data = NULL;
  2066. btrfs_end_transaction(trans, root);
  2067. mutex_lock(&root->fs_info->trans_mutex);
  2068. root->fs_info->open_ioctl_trans--;
  2069. mutex_unlock(&root->fs_info->trans_mutex);
  2070. mnt_drop_write(file->f_path.mnt);
  2071. return 0;
  2072. }
  2073. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  2074. {
  2075. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2076. struct btrfs_trans_handle *trans;
  2077. u64 transid;
  2078. int ret;
  2079. trans = btrfs_start_transaction(root, 0);
  2080. if (IS_ERR(trans))
  2081. return PTR_ERR(trans);
  2082. transid = trans->transid;
  2083. ret = btrfs_commit_transaction_async(trans, root, 0);
  2084. if (ret) {
  2085. btrfs_end_transaction(trans, root);
  2086. return ret;
  2087. }
  2088. if (argp)
  2089. if (copy_to_user(argp, &transid, sizeof(transid)))
  2090. return -EFAULT;
  2091. return 0;
  2092. }
  2093. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2094. {
  2095. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2096. u64 transid;
  2097. if (argp) {
  2098. if (copy_from_user(&transid, argp, sizeof(transid)))
  2099. return -EFAULT;
  2100. } else {
  2101. transid = 0; /* current trans */
  2102. }
  2103. return btrfs_wait_for_commit(root, transid);
  2104. }
  2105. long btrfs_ioctl(struct file *file, unsigned int
  2106. cmd, unsigned long arg)
  2107. {
  2108. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2109. void __user *argp = (void __user *)arg;
  2110. switch (cmd) {
  2111. case FS_IOC_GETFLAGS:
  2112. return btrfs_ioctl_getflags(file, argp);
  2113. case FS_IOC_SETFLAGS:
  2114. return btrfs_ioctl_setflags(file, argp);
  2115. case FS_IOC_GETVERSION:
  2116. return btrfs_ioctl_getversion(file, argp);
  2117. case FITRIM:
  2118. return btrfs_ioctl_fitrim(file, argp);
  2119. case BTRFS_IOC_SNAP_CREATE:
  2120. return btrfs_ioctl_snap_create(file, argp, 0);
  2121. case BTRFS_IOC_SNAP_CREATE_V2:
  2122. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2123. case BTRFS_IOC_SUBVOL_CREATE:
  2124. return btrfs_ioctl_snap_create(file, argp, 1);
  2125. case BTRFS_IOC_SNAP_DESTROY:
  2126. return btrfs_ioctl_snap_destroy(file, argp);
  2127. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2128. return btrfs_ioctl_subvol_getflags(file, argp);
  2129. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2130. return btrfs_ioctl_subvol_setflags(file, argp);
  2131. case BTRFS_IOC_DEFAULT_SUBVOL:
  2132. return btrfs_ioctl_default_subvol(file, argp);
  2133. case BTRFS_IOC_DEFRAG:
  2134. return btrfs_ioctl_defrag(file, NULL);
  2135. case BTRFS_IOC_DEFRAG_RANGE:
  2136. return btrfs_ioctl_defrag(file, argp);
  2137. case BTRFS_IOC_RESIZE:
  2138. return btrfs_ioctl_resize(root, argp);
  2139. case BTRFS_IOC_ADD_DEV:
  2140. return btrfs_ioctl_add_dev(root, argp);
  2141. case BTRFS_IOC_RM_DEV:
  2142. return btrfs_ioctl_rm_dev(root, argp);
  2143. case BTRFS_IOC_BALANCE:
  2144. return btrfs_balance(root->fs_info->dev_root);
  2145. case BTRFS_IOC_CLONE:
  2146. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2147. case BTRFS_IOC_CLONE_RANGE:
  2148. return btrfs_ioctl_clone_range(file, argp);
  2149. case BTRFS_IOC_TRANS_START:
  2150. return btrfs_ioctl_trans_start(file);
  2151. case BTRFS_IOC_TRANS_END:
  2152. return btrfs_ioctl_trans_end(file);
  2153. case BTRFS_IOC_TREE_SEARCH:
  2154. return btrfs_ioctl_tree_search(file, argp);
  2155. case BTRFS_IOC_INO_LOOKUP:
  2156. return btrfs_ioctl_ino_lookup(file, argp);
  2157. case BTRFS_IOC_SPACE_INFO:
  2158. return btrfs_ioctl_space_info(root, argp);
  2159. case BTRFS_IOC_SYNC:
  2160. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2161. return 0;
  2162. case BTRFS_IOC_START_SYNC:
  2163. return btrfs_ioctl_start_sync(file, argp);
  2164. case BTRFS_IOC_WAIT_SYNC:
  2165. return btrfs_ioctl_wait_sync(file, argp);
  2166. }
  2167. return -ENOTTY;
  2168. }