ioctl.c 70 KB

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