ioctl.c 96 KB

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