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 (!inode_owner_or_capable(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. } else {
  213. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  214. }
  215. trans = btrfs_join_transaction(root, 1);
  216. BUG_ON(IS_ERR(trans));
  217. ret = btrfs_update_inode(trans, root, inode);
  218. BUG_ON(ret);
  219. btrfs_update_iflags(inode);
  220. inode->i_ctime = CURRENT_TIME;
  221. btrfs_end_transaction(trans, root);
  222. mnt_drop_write(file->f_path.mnt);
  223. ret = 0;
  224. out_unlock:
  225. mutex_unlock(&inode->i_mutex);
  226. return ret;
  227. }
  228. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  229. {
  230. struct inode *inode = file->f_path.dentry->d_inode;
  231. return put_user(inode->i_generation, arg);
  232. }
  233. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  234. {
  235. struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
  236. struct btrfs_fs_info *fs_info = root->fs_info;
  237. struct btrfs_device *device;
  238. struct request_queue *q;
  239. struct fstrim_range range;
  240. u64 minlen = ULLONG_MAX;
  241. u64 num_devices = 0;
  242. int ret;
  243. if (!capable(CAP_SYS_ADMIN))
  244. return -EPERM;
  245. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  246. list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
  247. if (!device->bdev)
  248. continue;
  249. q = bdev_get_queue(device->bdev);
  250. if (blk_queue_discard(q)) {
  251. num_devices++;
  252. minlen = min((u64)q->limits.discard_granularity,
  253. minlen);
  254. }
  255. }
  256. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  257. if (!num_devices)
  258. return -EOPNOTSUPP;
  259. if (copy_from_user(&range, arg, sizeof(range)))
  260. return -EFAULT;
  261. range.minlen = max(range.minlen, minlen);
  262. ret = btrfs_trim_fs(root, &range);
  263. if (ret < 0)
  264. return ret;
  265. if (copy_to_user(arg, &range, sizeof(range)))
  266. return -EFAULT;
  267. return 0;
  268. }
  269. static noinline int create_subvol(struct btrfs_root *root,
  270. struct dentry *dentry,
  271. char *name, int namelen,
  272. u64 *async_transid)
  273. {
  274. struct btrfs_trans_handle *trans;
  275. struct btrfs_key key;
  276. struct btrfs_root_item root_item;
  277. struct btrfs_inode_item *inode_item;
  278. struct extent_buffer *leaf;
  279. struct btrfs_root *new_root;
  280. struct dentry *parent = dget_parent(dentry);
  281. struct inode *dir;
  282. int ret;
  283. int err;
  284. u64 objectid;
  285. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  286. u64 index = 0;
  287. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  288. 0, &objectid);
  289. if (ret) {
  290. dput(parent);
  291. return ret;
  292. }
  293. dir = parent->d_inode;
  294. /*
  295. * 1 - inode item
  296. * 2 - refs
  297. * 1 - root item
  298. * 2 - dir items
  299. */
  300. trans = btrfs_start_transaction(root, 6);
  301. if (IS_ERR(trans)) {
  302. dput(parent);
  303. return PTR_ERR(trans);
  304. }
  305. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  306. 0, objectid, NULL, 0, 0, 0);
  307. if (IS_ERR(leaf)) {
  308. ret = PTR_ERR(leaf);
  309. goto fail;
  310. }
  311. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  312. btrfs_set_header_bytenr(leaf, leaf->start);
  313. btrfs_set_header_generation(leaf, trans->transid);
  314. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  315. btrfs_set_header_owner(leaf, objectid);
  316. write_extent_buffer(leaf, root->fs_info->fsid,
  317. (unsigned long)btrfs_header_fsid(leaf),
  318. BTRFS_FSID_SIZE);
  319. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  320. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  321. BTRFS_UUID_SIZE);
  322. btrfs_mark_buffer_dirty(leaf);
  323. inode_item = &root_item.inode;
  324. memset(inode_item, 0, sizeof(*inode_item));
  325. inode_item->generation = cpu_to_le64(1);
  326. inode_item->size = cpu_to_le64(3);
  327. inode_item->nlink = cpu_to_le32(1);
  328. inode_item->nbytes = cpu_to_le64(root->leafsize);
  329. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  330. root_item.flags = 0;
  331. root_item.byte_limit = 0;
  332. inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
  333. btrfs_set_root_bytenr(&root_item, leaf->start);
  334. btrfs_set_root_generation(&root_item, trans->transid);
  335. btrfs_set_root_level(&root_item, 0);
  336. btrfs_set_root_refs(&root_item, 1);
  337. btrfs_set_root_used(&root_item, leaf->len);
  338. btrfs_set_root_last_snapshot(&root_item, 0);
  339. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  340. root_item.drop_level = 0;
  341. btrfs_tree_unlock(leaf);
  342. free_extent_buffer(leaf);
  343. leaf = NULL;
  344. btrfs_set_root_dirid(&root_item, new_dirid);
  345. key.objectid = objectid;
  346. key.offset = 0;
  347. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  348. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  349. &root_item);
  350. if (ret)
  351. goto fail;
  352. key.offset = (u64)-1;
  353. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  354. BUG_ON(IS_ERR(new_root));
  355. btrfs_record_root_in_trans(trans, new_root);
  356. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  357. BTRFS_I(dir)->block_group);
  358. /*
  359. * insert the directory item
  360. */
  361. ret = btrfs_set_inode_index(dir, &index);
  362. BUG_ON(ret);
  363. ret = btrfs_insert_dir_item(trans, root,
  364. name, namelen, dir->i_ino, &key,
  365. BTRFS_FT_DIR, index);
  366. if (ret)
  367. goto fail;
  368. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  369. ret = btrfs_update_inode(trans, root, dir);
  370. BUG_ON(ret);
  371. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  372. objectid, root->root_key.objectid,
  373. dir->i_ino, index, name, namelen);
  374. BUG_ON(ret);
  375. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  376. fail:
  377. dput(parent);
  378. if (async_transid) {
  379. *async_transid = trans->transid;
  380. err = btrfs_commit_transaction_async(trans, root, 1);
  381. } else {
  382. err = btrfs_commit_transaction(trans, root);
  383. }
  384. if (err && !ret)
  385. ret = err;
  386. return ret;
  387. }
  388. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  389. char *name, int namelen, u64 *async_transid,
  390. bool readonly)
  391. {
  392. struct inode *inode;
  393. struct dentry *parent;
  394. struct btrfs_pending_snapshot *pending_snapshot;
  395. struct btrfs_trans_handle *trans;
  396. int ret;
  397. if (!root->ref_cows)
  398. return -EINVAL;
  399. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  400. if (!pending_snapshot)
  401. return -ENOMEM;
  402. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  403. pending_snapshot->dentry = dentry;
  404. pending_snapshot->root = root;
  405. pending_snapshot->readonly = readonly;
  406. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  407. if (IS_ERR(trans)) {
  408. ret = PTR_ERR(trans);
  409. goto fail;
  410. }
  411. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  412. BUG_ON(ret);
  413. list_add(&pending_snapshot->list,
  414. &trans->transaction->pending_snapshots);
  415. if (async_transid) {
  416. *async_transid = trans->transid;
  417. ret = btrfs_commit_transaction_async(trans,
  418. root->fs_info->extent_root, 1);
  419. } else {
  420. ret = btrfs_commit_transaction(trans,
  421. root->fs_info->extent_root);
  422. }
  423. BUG_ON(ret);
  424. ret = pending_snapshot->error;
  425. if (ret)
  426. goto fail;
  427. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  428. if (ret)
  429. goto fail;
  430. parent = dget_parent(dentry);
  431. inode = btrfs_lookup_dentry(parent->d_inode, dentry);
  432. dput(parent);
  433. if (IS_ERR(inode)) {
  434. ret = PTR_ERR(inode);
  435. goto fail;
  436. }
  437. BUG_ON(!inode);
  438. d_instantiate(dentry, inode);
  439. ret = 0;
  440. fail:
  441. kfree(pending_snapshot);
  442. return ret;
  443. }
  444. /* copy of check_sticky in fs/namei.c()
  445. * It's inline, so penalty for filesystems that don't use sticky bit is
  446. * minimal.
  447. */
  448. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  449. {
  450. uid_t fsuid = current_fsuid();
  451. if (!(dir->i_mode & S_ISVTX))
  452. return 0;
  453. if (inode->i_uid == fsuid)
  454. return 0;
  455. if (dir->i_uid == fsuid)
  456. return 0;
  457. return !capable(CAP_FOWNER);
  458. }
  459. /* copy of may_delete in fs/namei.c()
  460. * Check whether we can remove a link victim from directory dir, check
  461. * whether the type of victim is right.
  462. * 1. We can't do it if dir is read-only (done in permission())
  463. * 2. We should have write and exec permissions on dir
  464. * 3. We can't remove anything from append-only dir
  465. * 4. We can't do anything with immutable dir (done in permission())
  466. * 5. If the sticky bit on dir is set we should either
  467. * a. be owner of dir, or
  468. * b. be owner of victim, or
  469. * c. have CAP_FOWNER capability
  470. * 6. If the victim is append-only or immutable we can't do antyhing with
  471. * links pointing to it.
  472. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  473. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  474. * 9. We can't remove a root or mountpoint.
  475. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  476. * nfs_async_unlink().
  477. */
  478. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  479. {
  480. int error;
  481. if (!victim->d_inode)
  482. return -ENOENT;
  483. BUG_ON(victim->d_parent->d_inode != dir);
  484. audit_inode_child(victim, dir);
  485. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  486. if (error)
  487. return error;
  488. if (IS_APPEND(dir))
  489. return -EPERM;
  490. if (btrfs_check_sticky(dir, victim->d_inode)||
  491. IS_APPEND(victim->d_inode)||
  492. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  493. return -EPERM;
  494. if (isdir) {
  495. if (!S_ISDIR(victim->d_inode->i_mode))
  496. return -ENOTDIR;
  497. if (IS_ROOT(victim))
  498. return -EBUSY;
  499. } else if (S_ISDIR(victim->d_inode->i_mode))
  500. return -EISDIR;
  501. if (IS_DEADDIR(dir))
  502. return -ENOENT;
  503. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  504. return -EBUSY;
  505. return 0;
  506. }
  507. /* copy of may_create in fs/namei.c() */
  508. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  509. {
  510. if (child->d_inode)
  511. return -EEXIST;
  512. if (IS_DEADDIR(dir))
  513. return -ENOENT;
  514. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  515. }
  516. /*
  517. * Create a new subvolume below @parent. This is largely modeled after
  518. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  519. * inside this filesystem so it's quite a bit simpler.
  520. */
  521. static noinline int btrfs_mksubvol(struct path *parent,
  522. char *name, int namelen,
  523. struct btrfs_root *snap_src,
  524. u64 *async_transid, bool readonly)
  525. {
  526. struct inode *dir = parent->dentry->d_inode;
  527. struct dentry *dentry;
  528. int error;
  529. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  530. dentry = lookup_one_len(name, parent->dentry, namelen);
  531. error = PTR_ERR(dentry);
  532. if (IS_ERR(dentry))
  533. goto out_unlock;
  534. error = -EEXIST;
  535. if (dentry->d_inode)
  536. goto out_dput;
  537. error = mnt_want_write(parent->mnt);
  538. if (error)
  539. goto out_dput;
  540. error = btrfs_may_create(dir, dentry);
  541. if (error)
  542. goto out_drop_write;
  543. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  544. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  545. goto out_up_read;
  546. if (snap_src) {
  547. error = create_snapshot(snap_src, dentry,
  548. name, namelen, async_transid, readonly);
  549. } else {
  550. error = create_subvol(BTRFS_I(dir)->root, dentry,
  551. name, namelen, async_transid);
  552. }
  553. if (!error)
  554. fsnotify_mkdir(dir, dentry);
  555. out_up_read:
  556. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  557. out_drop_write:
  558. mnt_drop_write(parent->mnt);
  559. out_dput:
  560. dput(dentry);
  561. out_unlock:
  562. mutex_unlock(&dir->i_mutex);
  563. return error;
  564. }
  565. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  566. int thresh, u64 *last_len, u64 *skip,
  567. u64 *defrag_end)
  568. {
  569. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  570. struct extent_map *em = NULL;
  571. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  572. int ret = 1;
  573. if (thresh == 0)
  574. thresh = 256 * 1024;
  575. /*
  576. * make sure that once we start defragging and extent, we keep on
  577. * defragging it
  578. */
  579. if (start < *defrag_end)
  580. return 1;
  581. *skip = 0;
  582. /*
  583. * hopefully we have this extent in the tree already, try without
  584. * the full extent lock
  585. */
  586. read_lock(&em_tree->lock);
  587. em = lookup_extent_mapping(em_tree, start, len);
  588. read_unlock(&em_tree->lock);
  589. if (!em) {
  590. /* get the big lock and read metadata off disk */
  591. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  592. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  593. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  594. if (IS_ERR(em))
  595. return 0;
  596. }
  597. /* this will cover holes, and inline extents */
  598. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  599. ret = 0;
  600. /*
  601. * we hit a real extent, if it is big don't bother defragging it again
  602. */
  603. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  604. ret = 0;
  605. /*
  606. * last_len ends up being a counter of how many bytes we've defragged.
  607. * every time we choose not to defrag an extent, we reset *last_len
  608. * so that the next tiny extent will force a defrag.
  609. *
  610. * The end result of this is that tiny extents before a single big
  611. * extent will force at least part of that big extent to be defragged.
  612. */
  613. if (ret) {
  614. *last_len += len;
  615. *defrag_end = extent_map_end(em);
  616. } else {
  617. *last_len = 0;
  618. *skip = extent_map_end(em);
  619. *defrag_end = 0;
  620. }
  621. free_extent_map(em);
  622. return ret;
  623. }
  624. static int btrfs_defrag_file(struct file *file,
  625. struct btrfs_ioctl_defrag_range_args *range)
  626. {
  627. struct inode *inode = fdentry(file)->d_inode;
  628. struct btrfs_root *root = BTRFS_I(inode)->root;
  629. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  630. struct btrfs_ordered_extent *ordered;
  631. struct page *page;
  632. struct btrfs_super_block *disk_super;
  633. unsigned long last_index;
  634. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  635. unsigned long total_read = 0;
  636. u64 features;
  637. u64 page_start;
  638. u64 page_end;
  639. u64 last_len = 0;
  640. u64 skip = 0;
  641. u64 defrag_end = 0;
  642. unsigned long i;
  643. int ret;
  644. int compress_type = BTRFS_COMPRESS_ZLIB;
  645. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  646. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  647. return -EINVAL;
  648. if (range->compress_type)
  649. compress_type = range->compress_type;
  650. }
  651. if (inode->i_size == 0)
  652. return 0;
  653. if (range->start + range->len > range->start) {
  654. last_index = min_t(u64, inode->i_size - 1,
  655. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  656. } else {
  657. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  658. }
  659. i = range->start >> PAGE_CACHE_SHIFT;
  660. while (i <= last_index) {
  661. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  662. PAGE_CACHE_SIZE,
  663. range->extent_thresh,
  664. &last_len, &skip,
  665. &defrag_end)) {
  666. unsigned long next;
  667. /*
  668. * the should_defrag function tells us how much to skip
  669. * bump our counter by the suggested amount
  670. */
  671. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  672. i = max(i + 1, next);
  673. continue;
  674. }
  675. if (total_read % ra_pages == 0) {
  676. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  677. min(last_index, i + ra_pages - 1));
  678. }
  679. total_read++;
  680. mutex_lock(&inode->i_mutex);
  681. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  682. BTRFS_I(inode)->force_compress = compress_type;
  683. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  684. if (ret)
  685. goto err_unlock;
  686. again:
  687. if (inode->i_size == 0 ||
  688. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  689. ret = 0;
  690. goto err_reservations;
  691. }
  692. page = grab_cache_page(inode->i_mapping, i);
  693. if (!page) {
  694. ret = -ENOMEM;
  695. goto err_reservations;
  696. }
  697. if (!PageUptodate(page)) {
  698. btrfs_readpage(NULL, page);
  699. lock_page(page);
  700. if (!PageUptodate(page)) {
  701. unlock_page(page);
  702. page_cache_release(page);
  703. ret = -EIO;
  704. goto err_reservations;
  705. }
  706. }
  707. if (page->mapping != inode->i_mapping) {
  708. unlock_page(page);
  709. page_cache_release(page);
  710. goto again;
  711. }
  712. wait_on_page_writeback(page);
  713. if (PageDirty(page)) {
  714. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  715. goto loop_unlock;
  716. }
  717. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  718. page_end = page_start + PAGE_CACHE_SIZE - 1;
  719. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  720. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  721. if (ordered) {
  722. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  723. unlock_page(page);
  724. page_cache_release(page);
  725. btrfs_start_ordered_extent(inode, ordered, 1);
  726. btrfs_put_ordered_extent(ordered);
  727. goto again;
  728. }
  729. set_page_extent_mapped(page);
  730. /*
  731. * this makes sure page_mkwrite is called on the
  732. * page if it is dirtied again later
  733. */
  734. clear_page_dirty_for_io(page);
  735. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  736. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  737. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  738. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  739. ClearPageChecked(page);
  740. set_page_dirty(page);
  741. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  742. loop_unlock:
  743. unlock_page(page);
  744. page_cache_release(page);
  745. mutex_unlock(&inode->i_mutex);
  746. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  747. i++;
  748. }
  749. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  750. filemap_flush(inode->i_mapping);
  751. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  752. /* the filemap_flush will queue IO into the worker threads, but
  753. * we have to make sure the IO is actually started and that
  754. * ordered extents get created before we return
  755. */
  756. atomic_inc(&root->fs_info->async_submit_draining);
  757. while (atomic_read(&root->fs_info->nr_async_submits) ||
  758. atomic_read(&root->fs_info->async_delalloc_pages)) {
  759. wait_event(root->fs_info->async_submit_wait,
  760. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  761. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  762. }
  763. atomic_dec(&root->fs_info->async_submit_draining);
  764. mutex_lock(&inode->i_mutex);
  765. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  766. mutex_unlock(&inode->i_mutex);
  767. }
  768. disk_super = &root->fs_info->super_copy;
  769. features = btrfs_super_incompat_flags(disk_super);
  770. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  771. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  772. btrfs_set_super_incompat_flags(disk_super, features);
  773. }
  774. return 0;
  775. err_reservations:
  776. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  777. err_unlock:
  778. mutex_unlock(&inode->i_mutex);
  779. return ret;
  780. }
  781. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  782. void __user *arg)
  783. {
  784. u64 new_size;
  785. u64 old_size;
  786. u64 devid = 1;
  787. struct btrfs_ioctl_vol_args *vol_args;
  788. struct btrfs_trans_handle *trans;
  789. struct btrfs_device *device = NULL;
  790. char *sizestr;
  791. char *devstr = NULL;
  792. int ret = 0;
  793. int mod = 0;
  794. if (root->fs_info->sb->s_flags & MS_RDONLY)
  795. return -EROFS;
  796. if (!capable(CAP_SYS_ADMIN))
  797. return -EPERM;
  798. vol_args = memdup_user(arg, sizeof(*vol_args));
  799. if (IS_ERR(vol_args))
  800. return PTR_ERR(vol_args);
  801. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  802. mutex_lock(&root->fs_info->volume_mutex);
  803. sizestr = vol_args->name;
  804. devstr = strchr(sizestr, ':');
  805. if (devstr) {
  806. char *end;
  807. sizestr = devstr + 1;
  808. *devstr = '\0';
  809. devstr = vol_args->name;
  810. devid = simple_strtoull(devstr, &end, 10);
  811. printk(KERN_INFO "resizing devid %llu\n",
  812. (unsigned long long)devid);
  813. }
  814. device = btrfs_find_device(root, devid, NULL, NULL);
  815. if (!device) {
  816. printk(KERN_INFO "resizer unable to find device %llu\n",
  817. (unsigned long long)devid);
  818. ret = -EINVAL;
  819. goto out_unlock;
  820. }
  821. if (!strcmp(sizestr, "max"))
  822. new_size = device->bdev->bd_inode->i_size;
  823. else {
  824. if (sizestr[0] == '-') {
  825. mod = -1;
  826. sizestr++;
  827. } else if (sizestr[0] == '+') {
  828. mod = 1;
  829. sizestr++;
  830. }
  831. new_size = memparse(sizestr, NULL);
  832. if (new_size == 0) {
  833. ret = -EINVAL;
  834. goto out_unlock;
  835. }
  836. }
  837. old_size = device->total_bytes;
  838. if (mod < 0) {
  839. if (new_size > old_size) {
  840. ret = -EINVAL;
  841. goto out_unlock;
  842. }
  843. new_size = old_size - new_size;
  844. } else if (mod > 0) {
  845. new_size = old_size + new_size;
  846. }
  847. if (new_size < 256 * 1024 * 1024) {
  848. ret = -EINVAL;
  849. goto out_unlock;
  850. }
  851. if (new_size > device->bdev->bd_inode->i_size) {
  852. ret = -EFBIG;
  853. goto out_unlock;
  854. }
  855. do_div(new_size, root->sectorsize);
  856. new_size *= root->sectorsize;
  857. printk(KERN_INFO "new size for %s is %llu\n",
  858. device->name, (unsigned long long)new_size);
  859. if (new_size > old_size) {
  860. trans = btrfs_start_transaction(root, 0);
  861. if (IS_ERR(trans)) {
  862. ret = PTR_ERR(trans);
  863. goto out_unlock;
  864. }
  865. ret = btrfs_grow_device(trans, device, new_size);
  866. btrfs_commit_transaction(trans, root);
  867. } else {
  868. ret = btrfs_shrink_device(device, new_size);
  869. }
  870. out_unlock:
  871. mutex_unlock(&root->fs_info->volume_mutex);
  872. kfree(vol_args);
  873. return ret;
  874. }
  875. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  876. char *name,
  877. unsigned long fd,
  878. int subvol,
  879. u64 *transid,
  880. bool readonly)
  881. {
  882. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  883. struct file *src_file;
  884. int namelen;
  885. int ret = 0;
  886. if (root->fs_info->sb->s_flags & MS_RDONLY)
  887. return -EROFS;
  888. namelen = strlen(name);
  889. if (strchr(name, '/')) {
  890. ret = -EINVAL;
  891. goto out;
  892. }
  893. if (subvol) {
  894. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  895. NULL, transid, readonly);
  896. } else {
  897. struct inode *src_inode;
  898. src_file = fget(fd);
  899. if (!src_file) {
  900. ret = -EINVAL;
  901. goto out;
  902. }
  903. src_inode = src_file->f_path.dentry->d_inode;
  904. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  905. printk(KERN_INFO "btrfs: Snapshot src from "
  906. "another FS\n");
  907. ret = -EINVAL;
  908. fput(src_file);
  909. goto out;
  910. }
  911. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  912. BTRFS_I(src_inode)->root,
  913. transid, readonly);
  914. fput(src_file);
  915. }
  916. out:
  917. return ret;
  918. }
  919. static noinline int btrfs_ioctl_snap_create(struct file *file,
  920. void __user *arg, int subvol)
  921. {
  922. struct btrfs_ioctl_vol_args *vol_args;
  923. int ret;
  924. vol_args = memdup_user(arg, sizeof(*vol_args));
  925. if (IS_ERR(vol_args))
  926. return PTR_ERR(vol_args);
  927. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  928. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  929. vol_args->fd, subvol,
  930. NULL, false);
  931. kfree(vol_args);
  932. return ret;
  933. }
  934. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  935. void __user *arg, int subvol)
  936. {
  937. struct btrfs_ioctl_vol_args_v2 *vol_args;
  938. int ret;
  939. u64 transid = 0;
  940. u64 *ptr = NULL;
  941. bool readonly = false;
  942. vol_args = memdup_user(arg, sizeof(*vol_args));
  943. if (IS_ERR(vol_args))
  944. return PTR_ERR(vol_args);
  945. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  946. if (vol_args->flags &
  947. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  948. ret = -EOPNOTSUPP;
  949. goto out;
  950. }
  951. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  952. ptr = &transid;
  953. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  954. readonly = true;
  955. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  956. vol_args->fd, subvol,
  957. ptr, readonly);
  958. if (ret == 0 && ptr &&
  959. copy_to_user(arg +
  960. offsetof(struct btrfs_ioctl_vol_args_v2,
  961. transid), ptr, sizeof(*ptr)))
  962. ret = -EFAULT;
  963. out:
  964. kfree(vol_args);
  965. return ret;
  966. }
  967. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  968. void __user *arg)
  969. {
  970. struct inode *inode = fdentry(file)->d_inode;
  971. struct btrfs_root *root = BTRFS_I(inode)->root;
  972. int ret = 0;
  973. u64 flags = 0;
  974. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  975. return -EINVAL;
  976. down_read(&root->fs_info->subvol_sem);
  977. if (btrfs_root_readonly(root))
  978. flags |= BTRFS_SUBVOL_RDONLY;
  979. up_read(&root->fs_info->subvol_sem);
  980. if (copy_to_user(arg, &flags, sizeof(flags)))
  981. ret = -EFAULT;
  982. return ret;
  983. }
  984. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  985. void __user *arg)
  986. {
  987. struct inode *inode = fdentry(file)->d_inode;
  988. struct btrfs_root *root = BTRFS_I(inode)->root;
  989. struct btrfs_trans_handle *trans;
  990. u64 root_flags;
  991. u64 flags;
  992. int ret = 0;
  993. if (root->fs_info->sb->s_flags & MS_RDONLY)
  994. return -EROFS;
  995. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  996. return -EINVAL;
  997. if (copy_from_user(&flags, arg, sizeof(flags)))
  998. return -EFAULT;
  999. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1000. return -EINVAL;
  1001. if (flags & ~BTRFS_SUBVOL_RDONLY)
  1002. return -EOPNOTSUPP;
  1003. if (!inode_owner_or_capable(inode))
  1004. return -EACCES;
  1005. down_write(&root->fs_info->subvol_sem);
  1006. /* nothing to do */
  1007. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1008. goto out;
  1009. root_flags = btrfs_root_flags(&root->root_item);
  1010. if (flags & BTRFS_SUBVOL_RDONLY)
  1011. btrfs_set_root_flags(&root->root_item,
  1012. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1013. else
  1014. btrfs_set_root_flags(&root->root_item,
  1015. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1016. trans = btrfs_start_transaction(root, 1);
  1017. if (IS_ERR(trans)) {
  1018. ret = PTR_ERR(trans);
  1019. goto out_reset;
  1020. }
  1021. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1022. &root->root_key, &root->root_item);
  1023. btrfs_commit_transaction(trans, root);
  1024. out_reset:
  1025. if (ret)
  1026. btrfs_set_root_flags(&root->root_item, root_flags);
  1027. out:
  1028. up_write(&root->fs_info->subvol_sem);
  1029. return ret;
  1030. }
  1031. /*
  1032. * helper to check if the subvolume references other subvolumes
  1033. */
  1034. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1035. {
  1036. struct btrfs_path *path;
  1037. struct btrfs_key key;
  1038. int ret;
  1039. path = btrfs_alloc_path();
  1040. if (!path)
  1041. return -ENOMEM;
  1042. key.objectid = root->root_key.objectid;
  1043. key.type = BTRFS_ROOT_REF_KEY;
  1044. key.offset = (u64)-1;
  1045. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1046. &key, path, 0, 0);
  1047. if (ret < 0)
  1048. goto out;
  1049. BUG_ON(ret == 0);
  1050. ret = 0;
  1051. if (path->slots[0] > 0) {
  1052. path->slots[0]--;
  1053. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1054. if (key.objectid == root->root_key.objectid &&
  1055. key.type == BTRFS_ROOT_REF_KEY)
  1056. ret = -ENOTEMPTY;
  1057. }
  1058. out:
  1059. btrfs_free_path(path);
  1060. return ret;
  1061. }
  1062. static noinline int key_in_sk(struct btrfs_key *key,
  1063. struct btrfs_ioctl_search_key *sk)
  1064. {
  1065. struct btrfs_key test;
  1066. int ret;
  1067. test.objectid = sk->min_objectid;
  1068. test.type = sk->min_type;
  1069. test.offset = sk->min_offset;
  1070. ret = btrfs_comp_cpu_keys(key, &test);
  1071. if (ret < 0)
  1072. return 0;
  1073. test.objectid = sk->max_objectid;
  1074. test.type = sk->max_type;
  1075. test.offset = sk->max_offset;
  1076. ret = btrfs_comp_cpu_keys(key, &test);
  1077. if (ret > 0)
  1078. return 0;
  1079. return 1;
  1080. }
  1081. static noinline int copy_to_sk(struct btrfs_root *root,
  1082. struct btrfs_path *path,
  1083. struct btrfs_key *key,
  1084. struct btrfs_ioctl_search_key *sk,
  1085. char *buf,
  1086. unsigned long *sk_offset,
  1087. int *num_found)
  1088. {
  1089. u64 found_transid;
  1090. struct extent_buffer *leaf;
  1091. struct btrfs_ioctl_search_header sh;
  1092. unsigned long item_off;
  1093. unsigned long item_len;
  1094. int nritems;
  1095. int i;
  1096. int slot;
  1097. int ret = 0;
  1098. leaf = path->nodes[0];
  1099. slot = path->slots[0];
  1100. nritems = btrfs_header_nritems(leaf);
  1101. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1102. i = nritems;
  1103. goto advance_key;
  1104. }
  1105. found_transid = btrfs_header_generation(leaf);
  1106. for (i = slot; i < nritems; i++) {
  1107. item_off = btrfs_item_ptr_offset(leaf, i);
  1108. item_len = btrfs_item_size_nr(leaf, i);
  1109. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1110. item_len = 0;
  1111. if (sizeof(sh) + item_len + *sk_offset >
  1112. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1113. ret = 1;
  1114. goto overflow;
  1115. }
  1116. btrfs_item_key_to_cpu(leaf, key, i);
  1117. if (!key_in_sk(key, sk))
  1118. continue;
  1119. sh.objectid = key->objectid;
  1120. sh.offset = key->offset;
  1121. sh.type = key->type;
  1122. sh.len = item_len;
  1123. sh.transid = found_transid;
  1124. /* copy search result header */
  1125. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1126. *sk_offset += sizeof(sh);
  1127. if (item_len) {
  1128. char *p = buf + *sk_offset;
  1129. /* copy the item */
  1130. read_extent_buffer(leaf, p,
  1131. item_off, item_len);
  1132. *sk_offset += item_len;
  1133. }
  1134. (*num_found)++;
  1135. if (*num_found >= sk->nr_items)
  1136. break;
  1137. }
  1138. advance_key:
  1139. ret = 0;
  1140. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1141. key->offset++;
  1142. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1143. key->offset = 0;
  1144. key->type++;
  1145. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1146. key->offset = 0;
  1147. key->type = 0;
  1148. key->objectid++;
  1149. } else
  1150. ret = 1;
  1151. overflow:
  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. }