ioctl.c 71 KB

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