ioctl.c 82 KB

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