ioctl.c 70 KB

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