ioctl.c 92 KB

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