ioctl.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345
  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 <linux/uaccess.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. #include "props.h"
  59. #include "sysfs.h"
  60. #include "qgroup.h"
  61. #ifdef CONFIG_64BIT
  62. /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
  63. * structures are incorrect, as the timespec structure from userspace
  64. * is 4 bytes too small. We define these alternatives here to teach
  65. * the kernel about the 32-bit struct packing.
  66. */
  67. struct btrfs_ioctl_timespec_32 {
  68. __u64 sec;
  69. __u32 nsec;
  70. } __attribute__ ((__packed__));
  71. struct btrfs_ioctl_received_subvol_args_32 {
  72. char uuid[BTRFS_UUID_SIZE]; /* in */
  73. __u64 stransid; /* in */
  74. __u64 rtransid; /* out */
  75. struct btrfs_ioctl_timespec_32 stime; /* in */
  76. struct btrfs_ioctl_timespec_32 rtime; /* out */
  77. __u64 flags; /* in */
  78. __u64 reserved[16]; /* in */
  79. } __attribute__ ((__packed__));
  80. #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
  81. struct btrfs_ioctl_received_subvol_args_32)
  82. #endif
  83. static int btrfs_clone(struct inode *src, struct inode *inode,
  84. u64 off, u64 olen, u64 olen_aligned, u64 destoff);
  85. /* Mask out flags that are inappropriate for the given type of inode. */
  86. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  87. {
  88. if (S_ISDIR(mode))
  89. return flags;
  90. else if (S_ISREG(mode))
  91. return flags & ~FS_DIRSYNC_FL;
  92. else
  93. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  94. }
  95. /*
  96. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  97. */
  98. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  99. {
  100. unsigned int iflags = 0;
  101. if (flags & BTRFS_INODE_SYNC)
  102. iflags |= FS_SYNC_FL;
  103. if (flags & BTRFS_INODE_IMMUTABLE)
  104. iflags |= FS_IMMUTABLE_FL;
  105. if (flags & BTRFS_INODE_APPEND)
  106. iflags |= FS_APPEND_FL;
  107. if (flags & BTRFS_INODE_NODUMP)
  108. iflags |= FS_NODUMP_FL;
  109. if (flags & BTRFS_INODE_NOATIME)
  110. iflags |= FS_NOATIME_FL;
  111. if (flags & BTRFS_INODE_DIRSYNC)
  112. iflags |= FS_DIRSYNC_FL;
  113. if (flags & BTRFS_INODE_NODATACOW)
  114. iflags |= FS_NOCOW_FL;
  115. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  116. iflags |= FS_COMPR_FL;
  117. else if (flags & BTRFS_INODE_NOCOMPRESS)
  118. iflags |= FS_NOCOMP_FL;
  119. return iflags;
  120. }
  121. /*
  122. * Update inode->i_flags based on the btrfs internal flags.
  123. */
  124. void btrfs_update_iflags(struct inode *inode)
  125. {
  126. struct btrfs_inode *ip = BTRFS_I(inode);
  127. unsigned int new_fl = 0;
  128. if (ip->flags & BTRFS_INODE_SYNC)
  129. new_fl |= S_SYNC;
  130. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  131. new_fl |= S_IMMUTABLE;
  132. if (ip->flags & BTRFS_INODE_APPEND)
  133. new_fl |= S_APPEND;
  134. if (ip->flags & BTRFS_INODE_NOATIME)
  135. new_fl |= S_NOATIME;
  136. if (ip->flags & BTRFS_INODE_DIRSYNC)
  137. new_fl |= S_DIRSYNC;
  138. set_mask_bits(&inode->i_flags,
  139. S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
  140. new_fl);
  141. }
  142. /*
  143. * Inherit flags from the parent inode.
  144. *
  145. * Currently only the compression flags and the cow flags are inherited.
  146. */
  147. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  148. {
  149. unsigned int flags;
  150. if (!dir)
  151. return;
  152. flags = BTRFS_I(dir)->flags;
  153. if (flags & BTRFS_INODE_NOCOMPRESS) {
  154. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  155. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  156. } else if (flags & BTRFS_INODE_COMPRESS) {
  157. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  158. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  159. }
  160. if (flags & BTRFS_INODE_NODATACOW) {
  161. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  162. if (S_ISREG(inode->i_mode))
  163. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  164. }
  165. btrfs_update_iflags(inode);
  166. }
  167. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  168. {
  169. struct btrfs_inode *ip = BTRFS_I(file_inode(file));
  170. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  171. if (copy_to_user(arg, &flags, sizeof(flags)))
  172. return -EFAULT;
  173. return 0;
  174. }
  175. static int check_flags(unsigned int flags)
  176. {
  177. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  178. FS_NOATIME_FL | FS_NODUMP_FL | \
  179. FS_SYNC_FL | FS_DIRSYNC_FL | \
  180. FS_NOCOMP_FL | FS_COMPR_FL |
  181. FS_NOCOW_FL))
  182. return -EOPNOTSUPP;
  183. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  184. return -EINVAL;
  185. return 0;
  186. }
  187. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  188. {
  189. struct inode *inode = file_inode(file);
  190. struct btrfs_inode *ip = BTRFS_I(inode);
  191. struct btrfs_root *root = ip->root;
  192. struct btrfs_trans_handle *trans;
  193. unsigned int flags, oldflags;
  194. int ret;
  195. u64 ip_oldflags;
  196. unsigned int i_oldflags;
  197. umode_t mode;
  198. if (!inode_owner_or_capable(inode))
  199. return -EPERM;
  200. if (btrfs_root_readonly(root))
  201. return -EROFS;
  202. if (copy_from_user(&flags, arg, sizeof(flags)))
  203. return -EFAULT;
  204. ret = check_flags(flags);
  205. if (ret)
  206. return ret;
  207. ret = mnt_want_write_file(file);
  208. if (ret)
  209. return ret;
  210. mutex_lock(&inode->i_mutex);
  211. ip_oldflags = ip->flags;
  212. i_oldflags = inode->i_flags;
  213. mode = inode->i_mode;
  214. flags = btrfs_mask_flags(inode->i_mode, flags);
  215. oldflags = btrfs_flags_to_ioctl(ip->flags);
  216. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  217. if (!capable(CAP_LINUX_IMMUTABLE)) {
  218. ret = -EPERM;
  219. goto out_unlock;
  220. }
  221. }
  222. if (flags & FS_SYNC_FL)
  223. ip->flags |= BTRFS_INODE_SYNC;
  224. else
  225. ip->flags &= ~BTRFS_INODE_SYNC;
  226. if (flags & FS_IMMUTABLE_FL)
  227. ip->flags |= BTRFS_INODE_IMMUTABLE;
  228. else
  229. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  230. if (flags & FS_APPEND_FL)
  231. ip->flags |= BTRFS_INODE_APPEND;
  232. else
  233. ip->flags &= ~BTRFS_INODE_APPEND;
  234. if (flags & FS_NODUMP_FL)
  235. ip->flags |= BTRFS_INODE_NODUMP;
  236. else
  237. ip->flags &= ~BTRFS_INODE_NODUMP;
  238. if (flags & FS_NOATIME_FL)
  239. ip->flags |= BTRFS_INODE_NOATIME;
  240. else
  241. ip->flags &= ~BTRFS_INODE_NOATIME;
  242. if (flags & FS_DIRSYNC_FL)
  243. ip->flags |= BTRFS_INODE_DIRSYNC;
  244. else
  245. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  246. if (flags & FS_NOCOW_FL) {
  247. if (S_ISREG(mode)) {
  248. /*
  249. * It's safe to turn csums off here, no extents exist.
  250. * Otherwise we want the flag to reflect the real COW
  251. * status of the file and will not set it.
  252. */
  253. if (inode->i_size == 0)
  254. ip->flags |= BTRFS_INODE_NODATACOW
  255. | BTRFS_INODE_NODATASUM;
  256. } else {
  257. ip->flags |= BTRFS_INODE_NODATACOW;
  258. }
  259. } else {
  260. /*
  261. * Revert back under same assuptions as above
  262. */
  263. if (S_ISREG(mode)) {
  264. if (inode->i_size == 0)
  265. ip->flags &= ~(BTRFS_INODE_NODATACOW
  266. | BTRFS_INODE_NODATASUM);
  267. } else {
  268. ip->flags &= ~BTRFS_INODE_NODATACOW;
  269. }
  270. }
  271. /*
  272. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  273. * flag may be changed automatically if compression code won't make
  274. * things smaller.
  275. */
  276. if (flags & FS_NOCOMP_FL) {
  277. ip->flags &= ~BTRFS_INODE_COMPRESS;
  278. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  279. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  280. if (ret && ret != -ENODATA)
  281. goto out_drop;
  282. } else if (flags & FS_COMPR_FL) {
  283. const char *comp;
  284. ip->flags |= BTRFS_INODE_COMPRESS;
  285. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  286. if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
  287. comp = "lzo";
  288. else
  289. comp = "zlib";
  290. ret = btrfs_set_prop(inode, "btrfs.compression",
  291. comp, strlen(comp), 0);
  292. if (ret)
  293. goto out_drop;
  294. } else {
  295. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  296. }
  297. trans = btrfs_start_transaction(root, 1);
  298. if (IS_ERR(trans)) {
  299. ret = PTR_ERR(trans);
  300. goto out_drop;
  301. }
  302. btrfs_update_iflags(inode);
  303. inode_inc_iversion(inode);
  304. inode->i_ctime = CURRENT_TIME;
  305. ret = btrfs_update_inode(trans, root, inode);
  306. btrfs_end_transaction(trans, root);
  307. out_drop:
  308. if (ret) {
  309. ip->flags = ip_oldflags;
  310. inode->i_flags = i_oldflags;
  311. }
  312. out_unlock:
  313. mutex_unlock(&inode->i_mutex);
  314. mnt_drop_write_file(file);
  315. return ret;
  316. }
  317. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  318. {
  319. struct inode *inode = file_inode(file);
  320. return put_user(inode->i_generation, arg);
  321. }
  322. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  323. {
  324. struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
  325. struct btrfs_device *device;
  326. struct request_queue *q;
  327. struct fstrim_range range;
  328. u64 minlen = ULLONG_MAX;
  329. u64 num_devices = 0;
  330. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  331. int ret;
  332. if (!capable(CAP_SYS_ADMIN))
  333. return -EPERM;
  334. rcu_read_lock();
  335. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  336. dev_list) {
  337. if (!device->bdev)
  338. continue;
  339. q = bdev_get_queue(device->bdev);
  340. if (blk_queue_discard(q)) {
  341. num_devices++;
  342. minlen = min((u64)q->limits.discard_granularity,
  343. minlen);
  344. }
  345. }
  346. rcu_read_unlock();
  347. if (!num_devices)
  348. return -EOPNOTSUPP;
  349. if (copy_from_user(&range, arg, sizeof(range)))
  350. return -EFAULT;
  351. if (range.start > total_bytes ||
  352. range.len < fs_info->sb->s_blocksize)
  353. return -EINVAL;
  354. range.len = min(range.len, total_bytes - range.start);
  355. range.minlen = max(range.minlen, minlen);
  356. ret = btrfs_trim_fs(fs_info->tree_root, &range);
  357. if (ret < 0)
  358. return ret;
  359. if (copy_to_user(arg, &range, sizeof(range)))
  360. return -EFAULT;
  361. return 0;
  362. }
  363. int btrfs_is_empty_uuid(u8 *uuid)
  364. {
  365. int i;
  366. for (i = 0; i < BTRFS_UUID_SIZE; i++) {
  367. if (uuid[i])
  368. return 0;
  369. }
  370. return 1;
  371. }
  372. static noinline int create_subvol(struct inode *dir,
  373. struct dentry *dentry,
  374. char *name, int namelen,
  375. u64 *async_transid,
  376. struct btrfs_qgroup_inherit *inherit)
  377. {
  378. struct btrfs_trans_handle *trans;
  379. struct btrfs_key key;
  380. struct btrfs_root_item root_item;
  381. struct btrfs_inode_item *inode_item;
  382. struct extent_buffer *leaf;
  383. struct btrfs_root *root = BTRFS_I(dir)->root;
  384. struct btrfs_root *new_root;
  385. struct btrfs_block_rsv block_rsv;
  386. struct timespec cur_time = CURRENT_TIME;
  387. struct inode *inode;
  388. int ret;
  389. int err;
  390. u64 objectid;
  391. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  392. u64 index = 0;
  393. u64 qgroup_reserved;
  394. uuid_le new_uuid;
  395. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  396. if (ret)
  397. return ret;
  398. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  399. /*
  400. * The same as the snapshot creation, please see the comment
  401. * of create_snapshot().
  402. */
  403. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  404. 8, &qgroup_reserved, false);
  405. if (ret)
  406. return ret;
  407. trans = btrfs_start_transaction(root, 0);
  408. if (IS_ERR(trans)) {
  409. ret = PTR_ERR(trans);
  410. btrfs_subvolume_release_metadata(root, &block_rsv,
  411. qgroup_reserved);
  412. return ret;
  413. }
  414. trans->block_rsv = &block_rsv;
  415. trans->bytes_reserved = block_rsv.size;
  416. ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
  417. if (ret)
  418. goto fail;
  419. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  420. 0, objectid, NULL, 0, 0, 0);
  421. if (IS_ERR(leaf)) {
  422. ret = PTR_ERR(leaf);
  423. goto fail;
  424. }
  425. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  426. btrfs_set_header_bytenr(leaf, leaf->start);
  427. btrfs_set_header_generation(leaf, trans->transid);
  428. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  429. btrfs_set_header_owner(leaf, objectid);
  430. write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
  431. BTRFS_FSID_SIZE);
  432. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  433. btrfs_header_chunk_tree_uuid(leaf),
  434. BTRFS_UUID_SIZE);
  435. btrfs_mark_buffer_dirty(leaf);
  436. memset(&root_item, 0, sizeof(root_item));
  437. inode_item = &root_item.inode;
  438. btrfs_set_stack_inode_generation(inode_item, 1);
  439. btrfs_set_stack_inode_size(inode_item, 3);
  440. btrfs_set_stack_inode_nlink(inode_item, 1);
  441. btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
  442. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  443. btrfs_set_root_flags(&root_item, 0);
  444. btrfs_set_root_limit(&root_item, 0);
  445. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  446. btrfs_set_root_bytenr(&root_item, leaf->start);
  447. btrfs_set_root_generation(&root_item, trans->transid);
  448. btrfs_set_root_level(&root_item, 0);
  449. btrfs_set_root_refs(&root_item, 1);
  450. btrfs_set_root_used(&root_item, leaf->len);
  451. btrfs_set_root_last_snapshot(&root_item, 0);
  452. btrfs_set_root_generation_v2(&root_item,
  453. btrfs_root_generation(&root_item));
  454. uuid_le_gen(&new_uuid);
  455. memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
  456. btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
  457. btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
  458. root_item.ctime = root_item.otime;
  459. btrfs_set_root_ctransid(&root_item, trans->transid);
  460. btrfs_set_root_otransid(&root_item, trans->transid);
  461. btrfs_tree_unlock(leaf);
  462. free_extent_buffer(leaf);
  463. leaf = NULL;
  464. btrfs_set_root_dirid(&root_item, new_dirid);
  465. key.objectid = objectid;
  466. key.offset = 0;
  467. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  468. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  469. &root_item);
  470. if (ret)
  471. goto fail;
  472. key.offset = (u64)-1;
  473. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  474. if (IS_ERR(new_root)) {
  475. btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
  476. ret = PTR_ERR(new_root);
  477. goto fail;
  478. }
  479. btrfs_record_root_in_trans(trans, new_root);
  480. ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
  481. if (ret) {
  482. /* We potentially lose an unused inode item here */
  483. btrfs_abort_transaction(trans, root, ret);
  484. goto fail;
  485. }
  486. /*
  487. * insert the directory item
  488. */
  489. ret = btrfs_set_inode_index(dir, &index);
  490. if (ret) {
  491. btrfs_abort_transaction(trans, root, ret);
  492. goto fail;
  493. }
  494. ret = btrfs_insert_dir_item(trans, root,
  495. name, namelen, dir, &key,
  496. BTRFS_FT_DIR, index);
  497. if (ret) {
  498. btrfs_abort_transaction(trans, root, ret);
  499. goto fail;
  500. }
  501. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  502. ret = btrfs_update_inode(trans, root, dir);
  503. BUG_ON(ret);
  504. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  505. objectid, root->root_key.objectid,
  506. btrfs_ino(dir), index, name, namelen);
  507. BUG_ON(ret);
  508. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  509. root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  510. objectid);
  511. if (ret)
  512. btrfs_abort_transaction(trans, root, ret);
  513. fail:
  514. trans->block_rsv = NULL;
  515. trans->bytes_reserved = 0;
  516. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  517. if (async_transid) {
  518. *async_transid = trans->transid;
  519. err = btrfs_commit_transaction_async(trans, root, 1);
  520. if (err)
  521. err = btrfs_commit_transaction(trans, root);
  522. } else {
  523. err = btrfs_commit_transaction(trans, root);
  524. }
  525. if (err && !ret)
  526. ret = err;
  527. if (!ret) {
  528. inode = btrfs_lookup_dentry(dir, dentry);
  529. if (IS_ERR(inode))
  530. return PTR_ERR(inode);
  531. d_instantiate(dentry, inode);
  532. }
  533. return ret;
  534. }
  535. static void btrfs_wait_nocow_write(struct btrfs_root *root)
  536. {
  537. s64 writers;
  538. DEFINE_WAIT(wait);
  539. do {
  540. prepare_to_wait(&root->subv_writers->wait, &wait,
  541. TASK_UNINTERRUPTIBLE);
  542. writers = percpu_counter_sum(&root->subv_writers->counter);
  543. if (writers)
  544. schedule();
  545. finish_wait(&root->subv_writers->wait, &wait);
  546. } while (writers);
  547. }
  548. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  549. struct dentry *dentry, char *name, int namelen,
  550. u64 *async_transid, bool readonly,
  551. struct btrfs_qgroup_inherit *inherit)
  552. {
  553. struct inode *inode;
  554. struct btrfs_pending_snapshot *pending_snapshot;
  555. struct btrfs_trans_handle *trans;
  556. int ret;
  557. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  558. return -EINVAL;
  559. atomic_inc(&root->will_be_snapshoted);
  560. smp_mb__after_atomic();
  561. btrfs_wait_nocow_write(root);
  562. ret = btrfs_start_delalloc_inodes(root, 0);
  563. if (ret)
  564. goto out;
  565. btrfs_wait_ordered_extents(root, -1);
  566. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  567. if (!pending_snapshot) {
  568. ret = -ENOMEM;
  569. goto out;
  570. }
  571. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  572. BTRFS_BLOCK_RSV_TEMP);
  573. /*
  574. * 1 - parent dir inode
  575. * 2 - dir entries
  576. * 1 - root item
  577. * 2 - root ref/backref
  578. * 1 - root of snapshot
  579. * 1 - UUID item
  580. */
  581. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
  582. &pending_snapshot->block_rsv, 8,
  583. &pending_snapshot->qgroup_reserved,
  584. false);
  585. if (ret)
  586. goto free;
  587. pending_snapshot->dentry = dentry;
  588. pending_snapshot->root = root;
  589. pending_snapshot->readonly = readonly;
  590. pending_snapshot->dir = dir;
  591. pending_snapshot->inherit = inherit;
  592. trans = btrfs_start_transaction(root, 0);
  593. if (IS_ERR(trans)) {
  594. ret = PTR_ERR(trans);
  595. goto fail;
  596. }
  597. spin_lock(&root->fs_info->trans_lock);
  598. list_add(&pending_snapshot->list,
  599. &trans->transaction->pending_snapshots);
  600. spin_unlock(&root->fs_info->trans_lock);
  601. if (async_transid) {
  602. *async_transid = trans->transid;
  603. ret = btrfs_commit_transaction_async(trans,
  604. root->fs_info->extent_root, 1);
  605. if (ret)
  606. ret = btrfs_commit_transaction(trans, root);
  607. } else {
  608. ret = btrfs_commit_transaction(trans,
  609. root->fs_info->extent_root);
  610. }
  611. if (ret)
  612. goto fail;
  613. ret = pending_snapshot->error;
  614. if (ret)
  615. goto fail;
  616. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  617. if (IS_ERR(inode)) {
  618. ret = PTR_ERR(inode);
  619. goto fail;
  620. }
  621. d_instantiate(dentry, inode);
  622. ret = 0;
  623. fail:
  624. btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
  625. &pending_snapshot->block_rsv,
  626. pending_snapshot->qgroup_reserved);
  627. free:
  628. kfree(pending_snapshot);
  629. out:
  630. atomic_dec(&root->will_be_snapshoted);
  631. return ret;
  632. }
  633. /* copy of check_sticky in fs/namei.c()
  634. * It's inline, so penalty for filesystems that don't use sticky bit is
  635. * minimal.
  636. */
  637. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  638. {
  639. kuid_t fsuid = current_fsuid();
  640. if (!(dir->i_mode & S_ISVTX))
  641. return 0;
  642. if (uid_eq(inode->i_uid, fsuid))
  643. return 0;
  644. if (uid_eq(dir->i_uid, fsuid))
  645. return 0;
  646. return !capable(CAP_FOWNER);
  647. }
  648. /* copy of may_delete in fs/namei.c()
  649. * Check whether we can remove a link victim from directory dir, check
  650. * whether the type of victim is right.
  651. * 1. We can't do it if dir is read-only (done in permission())
  652. * 2. We should have write and exec permissions on dir
  653. * 3. We can't remove anything from append-only dir
  654. * 4. We can't do anything with immutable dir (done in permission())
  655. * 5. If the sticky bit on dir is set we should either
  656. * a. be owner of dir, or
  657. * b. be owner of victim, or
  658. * c. have CAP_FOWNER capability
  659. * 6. If the victim is append-only or immutable we can't do antyhing with
  660. * links pointing to it.
  661. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  662. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  663. * 9. We can't remove a root or mountpoint.
  664. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  665. * nfs_async_unlink().
  666. */
  667. static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
  668. {
  669. int error;
  670. if (!victim->d_inode)
  671. return -ENOENT;
  672. BUG_ON(victim->d_parent->d_inode != dir);
  673. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  674. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  675. if (error)
  676. return error;
  677. if (IS_APPEND(dir))
  678. return -EPERM;
  679. if (btrfs_check_sticky(dir, victim->d_inode)||
  680. IS_APPEND(victim->d_inode)||
  681. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  682. return -EPERM;
  683. if (isdir) {
  684. if (!S_ISDIR(victim->d_inode->i_mode))
  685. return -ENOTDIR;
  686. if (IS_ROOT(victim))
  687. return -EBUSY;
  688. } else if (S_ISDIR(victim->d_inode->i_mode))
  689. return -EISDIR;
  690. if (IS_DEADDIR(dir))
  691. return -ENOENT;
  692. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  693. return -EBUSY;
  694. return 0;
  695. }
  696. /* copy of may_create in fs/namei.c() */
  697. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  698. {
  699. if (child->d_inode)
  700. return -EEXIST;
  701. if (IS_DEADDIR(dir))
  702. return -ENOENT;
  703. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  704. }
  705. /*
  706. * Create a new subvolume below @parent. This is largely modeled after
  707. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  708. * inside this filesystem so it's quite a bit simpler.
  709. */
  710. static noinline int btrfs_mksubvol(struct path *parent,
  711. char *name, int namelen,
  712. struct btrfs_root *snap_src,
  713. u64 *async_transid, bool readonly,
  714. struct btrfs_qgroup_inherit *inherit)
  715. {
  716. struct inode *dir = parent->dentry->d_inode;
  717. struct dentry *dentry;
  718. int error;
  719. error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  720. if (error == -EINTR)
  721. return error;
  722. dentry = lookup_one_len(name, parent->dentry, namelen);
  723. error = PTR_ERR(dentry);
  724. if (IS_ERR(dentry))
  725. goto out_unlock;
  726. error = -EEXIST;
  727. if (dentry->d_inode)
  728. goto out_dput;
  729. error = btrfs_may_create(dir, dentry);
  730. if (error)
  731. goto out_dput;
  732. /*
  733. * even if this name doesn't exist, we may get hash collisions.
  734. * check for them now when we can safely fail
  735. */
  736. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  737. dir->i_ino, name,
  738. namelen);
  739. if (error)
  740. goto out_dput;
  741. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  742. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  743. goto out_up_read;
  744. if (snap_src) {
  745. error = create_snapshot(snap_src, dir, dentry, name, namelen,
  746. async_transid, readonly, inherit);
  747. } else {
  748. error = create_subvol(dir, dentry, name, namelen,
  749. async_transid, inherit);
  750. }
  751. if (!error)
  752. fsnotify_mkdir(dir, dentry);
  753. out_up_read:
  754. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  755. out_dput:
  756. dput(dentry);
  757. out_unlock:
  758. mutex_unlock(&dir->i_mutex);
  759. return error;
  760. }
  761. /*
  762. * When we're defragging a range, we don't want to kick it off again
  763. * if it is really just waiting for delalloc to send it down.
  764. * If we find a nice big extent or delalloc range for the bytes in the
  765. * file you want to defrag, we return 0 to let you know to skip this
  766. * part of the file
  767. */
  768. static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
  769. {
  770. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  771. struct extent_map *em = NULL;
  772. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  773. u64 end;
  774. read_lock(&em_tree->lock);
  775. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  776. read_unlock(&em_tree->lock);
  777. if (em) {
  778. end = extent_map_end(em);
  779. free_extent_map(em);
  780. if (end - offset > thresh)
  781. return 0;
  782. }
  783. /* if we already have a nice delalloc here, just stop */
  784. thresh /= 2;
  785. end = count_range_bits(io_tree, &offset, offset + thresh,
  786. thresh, EXTENT_DELALLOC, 1);
  787. if (end >= thresh)
  788. return 0;
  789. return 1;
  790. }
  791. /*
  792. * helper function to walk through a file and find extents
  793. * newer than a specific transid, and smaller than thresh.
  794. *
  795. * This is used by the defragging code to find new and small
  796. * extents
  797. */
  798. static int find_new_extents(struct btrfs_root *root,
  799. struct inode *inode, u64 newer_than,
  800. u64 *off, int thresh)
  801. {
  802. struct btrfs_path *path;
  803. struct btrfs_key min_key;
  804. struct extent_buffer *leaf;
  805. struct btrfs_file_extent_item *extent;
  806. int type;
  807. int ret;
  808. u64 ino = btrfs_ino(inode);
  809. path = btrfs_alloc_path();
  810. if (!path)
  811. return -ENOMEM;
  812. min_key.objectid = ino;
  813. min_key.type = BTRFS_EXTENT_DATA_KEY;
  814. min_key.offset = *off;
  815. while (1) {
  816. path->keep_locks = 1;
  817. ret = btrfs_search_forward(root, &min_key, path, newer_than);
  818. if (ret != 0)
  819. goto none;
  820. path->keep_locks = 0;
  821. btrfs_unlock_up_safe(path, 1);
  822. process_slot:
  823. if (min_key.objectid != ino)
  824. goto none;
  825. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  826. goto none;
  827. leaf = path->nodes[0];
  828. extent = btrfs_item_ptr(leaf, path->slots[0],
  829. struct btrfs_file_extent_item);
  830. type = btrfs_file_extent_type(leaf, extent);
  831. if (type == BTRFS_FILE_EXTENT_REG &&
  832. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  833. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  834. *off = min_key.offset;
  835. btrfs_free_path(path);
  836. return 0;
  837. }
  838. path->slots[0]++;
  839. if (path->slots[0] < btrfs_header_nritems(leaf)) {
  840. btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
  841. goto process_slot;
  842. }
  843. if (min_key.offset == (u64)-1)
  844. goto none;
  845. min_key.offset++;
  846. btrfs_release_path(path);
  847. }
  848. none:
  849. btrfs_free_path(path);
  850. return -ENOENT;
  851. }
  852. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  853. {
  854. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  855. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  856. struct extent_map *em;
  857. u64 len = PAGE_CACHE_SIZE;
  858. /*
  859. * hopefully we have this extent in the tree already, try without
  860. * the full extent lock
  861. */
  862. read_lock(&em_tree->lock);
  863. em = lookup_extent_mapping(em_tree, start, len);
  864. read_unlock(&em_tree->lock);
  865. if (!em) {
  866. struct extent_state *cached = NULL;
  867. u64 end = start + len - 1;
  868. /* get the big lock and read metadata off disk */
  869. lock_extent_bits(io_tree, start, end, 0, &cached);
  870. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  871. unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
  872. if (IS_ERR(em))
  873. return NULL;
  874. }
  875. return em;
  876. }
  877. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  878. {
  879. struct extent_map *next;
  880. bool ret = true;
  881. /* this is the last extent */
  882. if (em->start + em->len >= i_size_read(inode))
  883. return false;
  884. next = defrag_lookup_extent(inode, em->start + em->len);
  885. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  886. ret = false;
  887. else if ((em->block_start + em->block_len == next->block_start) &&
  888. (em->block_len > 128 * 1024 && next->block_len > 128 * 1024))
  889. ret = false;
  890. free_extent_map(next);
  891. return ret;
  892. }
  893. static int should_defrag_range(struct inode *inode, u64 start, int thresh,
  894. u64 *last_len, u64 *skip, u64 *defrag_end,
  895. int compress)
  896. {
  897. struct extent_map *em;
  898. int ret = 1;
  899. bool next_mergeable = true;
  900. /*
  901. * make sure that once we start defragging an extent, we keep on
  902. * defragging it
  903. */
  904. if (start < *defrag_end)
  905. return 1;
  906. *skip = 0;
  907. em = defrag_lookup_extent(inode, start);
  908. if (!em)
  909. return 0;
  910. /* this will cover holes, and inline extents */
  911. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  912. ret = 0;
  913. goto out;
  914. }
  915. next_mergeable = defrag_check_next_extent(inode, em);
  916. /*
  917. * we hit a real extent, if it is big or the next extent is not a
  918. * real extent, don't bother defragging it
  919. */
  920. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  921. (em->len >= thresh || !next_mergeable))
  922. ret = 0;
  923. out:
  924. /*
  925. * last_len ends up being a counter of how many bytes we've defragged.
  926. * every time we choose not to defrag an extent, we reset *last_len
  927. * so that the next tiny extent will force a defrag.
  928. *
  929. * The end result of this is that tiny extents before a single big
  930. * extent will force at least part of that big extent to be defragged.
  931. */
  932. if (ret) {
  933. *defrag_end = extent_map_end(em);
  934. } else {
  935. *last_len = 0;
  936. *skip = extent_map_end(em);
  937. *defrag_end = 0;
  938. }
  939. free_extent_map(em);
  940. return ret;
  941. }
  942. /*
  943. * it doesn't do much good to defrag one or two pages
  944. * at a time. This pulls in a nice chunk of pages
  945. * to COW and defrag.
  946. *
  947. * It also makes sure the delalloc code has enough
  948. * dirty data to avoid making new small extents as part
  949. * of the defrag
  950. *
  951. * It's a good idea to start RA on this range
  952. * before calling this.
  953. */
  954. static int cluster_pages_for_defrag(struct inode *inode,
  955. struct page **pages,
  956. unsigned long start_index,
  957. unsigned long num_pages)
  958. {
  959. unsigned long file_end;
  960. u64 isize = i_size_read(inode);
  961. u64 page_start;
  962. u64 page_end;
  963. u64 page_cnt;
  964. int ret;
  965. int i;
  966. int i_done;
  967. struct btrfs_ordered_extent *ordered;
  968. struct extent_state *cached_state = NULL;
  969. struct extent_io_tree *tree;
  970. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  971. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  972. if (!isize || start_index > file_end)
  973. return 0;
  974. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  975. ret = btrfs_delalloc_reserve_space(inode,
  976. page_cnt << PAGE_CACHE_SHIFT);
  977. if (ret)
  978. return ret;
  979. i_done = 0;
  980. tree = &BTRFS_I(inode)->io_tree;
  981. /* step one, lock all the pages */
  982. for (i = 0; i < page_cnt; i++) {
  983. struct page *page;
  984. again:
  985. page = find_or_create_page(inode->i_mapping,
  986. start_index + i, mask);
  987. if (!page)
  988. break;
  989. page_start = page_offset(page);
  990. page_end = page_start + PAGE_CACHE_SIZE - 1;
  991. while (1) {
  992. lock_extent_bits(tree, page_start, page_end,
  993. 0, &cached_state);
  994. ordered = btrfs_lookup_ordered_extent(inode,
  995. page_start);
  996. unlock_extent_cached(tree, page_start, page_end,
  997. &cached_state, GFP_NOFS);
  998. if (!ordered)
  999. break;
  1000. unlock_page(page);
  1001. btrfs_start_ordered_extent(inode, ordered, 1);
  1002. btrfs_put_ordered_extent(ordered);
  1003. lock_page(page);
  1004. /*
  1005. * we unlocked the page above, so we need check if
  1006. * it was released or not.
  1007. */
  1008. if (page->mapping != inode->i_mapping) {
  1009. unlock_page(page);
  1010. page_cache_release(page);
  1011. goto again;
  1012. }
  1013. }
  1014. if (!PageUptodate(page)) {
  1015. btrfs_readpage(NULL, page);
  1016. lock_page(page);
  1017. if (!PageUptodate(page)) {
  1018. unlock_page(page);
  1019. page_cache_release(page);
  1020. ret = -EIO;
  1021. break;
  1022. }
  1023. }
  1024. if (page->mapping != inode->i_mapping) {
  1025. unlock_page(page);
  1026. page_cache_release(page);
  1027. goto again;
  1028. }
  1029. pages[i] = page;
  1030. i_done++;
  1031. }
  1032. if (!i_done || ret)
  1033. goto out;
  1034. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1035. goto out;
  1036. /*
  1037. * so now we have a nice long stream of locked
  1038. * and up to date pages, lets wait on them
  1039. */
  1040. for (i = 0; i < i_done; i++)
  1041. wait_on_page_writeback(pages[i]);
  1042. page_start = page_offset(pages[0]);
  1043. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  1044. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1045. page_start, page_end - 1, 0, &cached_state);
  1046. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  1047. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1048. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  1049. &cached_state, GFP_NOFS);
  1050. if (i_done != page_cnt) {
  1051. spin_lock(&BTRFS_I(inode)->lock);
  1052. BTRFS_I(inode)->outstanding_extents++;
  1053. spin_unlock(&BTRFS_I(inode)->lock);
  1054. btrfs_delalloc_release_space(inode,
  1055. (page_cnt - i_done) << PAGE_CACHE_SHIFT);
  1056. }
  1057. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  1058. &cached_state, GFP_NOFS);
  1059. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1060. page_start, page_end - 1, &cached_state,
  1061. GFP_NOFS);
  1062. for (i = 0; i < i_done; i++) {
  1063. clear_page_dirty_for_io(pages[i]);
  1064. ClearPageChecked(pages[i]);
  1065. set_page_extent_mapped(pages[i]);
  1066. set_page_dirty(pages[i]);
  1067. unlock_page(pages[i]);
  1068. page_cache_release(pages[i]);
  1069. }
  1070. return i_done;
  1071. out:
  1072. for (i = 0; i < i_done; i++) {
  1073. unlock_page(pages[i]);
  1074. page_cache_release(pages[i]);
  1075. }
  1076. btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
  1077. return ret;
  1078. }
  1079. int btrfs_defrag_file(struct inode *inode, struct file *file,
  1080. struct btrfs_ioctl_defrag_range_args *range,
  1081. u64 newer_than, unsigned long max_to_defrag)
  1082. {
  1083. struct btrfs_root *root = BTRFS_I(inode)->root;
  1084. struct file_ra_state *ra = NULL;
  1085. unsigned long last_index;
  1086. u64 isize = i_size_read(inode);
  1087. u64 last_len = 0;
  1088. u64 skip = 0;
  1089. u64 defrag_end = 0;
  1090. u64 newer_off = range->start;
  1091. unsigned long i;
  1092. unsigned long ra_index = 0;
  1093. int ret;
  1094. int defrag_count = 0;
  1095. int compress_type = BTRFS_COMPRESS_ZLIB;
  1096. int extent_thresh = range->extent_thresh;
  1097. unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  1098. unsigned long cluster = max_cluster;
  1099. u64 new_align = ~((u64)128 * 1024 - 1);
  1100. struct page **pages = NULL;
  1101. if (isize == 0)
  1102. return 0;
  1103. if (range->start >= isize)
  1104. return -EINVAL;
  1105. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1106. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  1107. return -EINVAL;
  1108. if (range->compress_type)
  1109. compress_type = range->compress_type;
  1110. }
  1111. if (extent_thresh == 0)
  1112. extent_thresh = 256 * 1024;
  1113. /*
  1114. * if we were not given a file, allocate a readahead
  1115. * context
  1116. */
  1117. if (!file) {
  1118. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  1119. if (!ra)
  1120. return -ENOMEM;
  1121. file_ra_state_init(ra, inode->i_mapping);
  1122. } else {
  1123. ra = &file->f_ra;
  1124. }
  1125. pages = kmalloc_array(max_cluster, sizeof(struct page *),
  1126. GFP_NOFS);
  1127. if (!pages) {
  1128. ret = -ENOMEM;
  1129. goto out_ra;
  1130. }
  1131. /* find the last page to defrag */
  1132. if (range->start + range->len > range->start) {
  1133. last_index = min_t(u64, isize - 1,
  1134. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  1135. } else {
  1136. last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1137. }
  1138. if (newer_than) {
  1139. ret = find_new_extents(root, inode, newer_than,
  1140. &newer_off, 64 * 1024);
  1141. if (!ret) {
  1142. range->start = newer_off;
  1143. /*
  1144. * we always align our defrag to help keep
  1145. * the extents in the file evenly spaced
  1146. */
  1147. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1148. } else
  1149. goto out_ra;
  1150. } else {
  1151. i = range->start >> PAGE_CACHE_SHIFT;
  1152. }
  1153. if (!max_to_defrag)
  1154. max_to_defrag = last_index + 1;
  1155. /*
  1156. * make writeback starts from i, so the defrag range can be
  1157. * written sequentially.
  1158. */
  1159. if (i < inode->i_mapping->writeback_index)
  1160. inode->i_mapping->writeback_index = i;
  1161. while (i <= last_index && defrag_count < max_to_defrag &&
  1162. (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  1163. PAGE_CACHE_SHIFT)) {
  1164. /*
  1165. * make sure we stop running if someone unmounts
  1166. * the FS
  1167. */
  1168. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1169. break;
  1170. if (btrfs_defrag_cancelled(root->fs_info)) {
  1171. printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
  1172. ret = -EAGAIN;
  1173. break;
  1174. }
  1175. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  1176. extent_thresh, &last_len, &skip,
  1177. &defrag_end, range->flags &
  1178. BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1179. unsigned long next;
  1180. /*
  1181. * the should_defrag function tells us how much to skip
  1182. * bump our counter by the suggested amount
  1183. */
  1184. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1185. i = max(i + 1, next);
  1186. continue;
  1187. }
  1188. if (!newer_than) {
  1189. cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
  1190. PAGE_CACHE_SHIFT) - i;
  1191. cluster = min(cluster, max_cluster);
  1192. } else {
  1193. cluster = max_cluster;
  1194. }
  1195. if (i + cluster > ra_index) {
  1196. ra_index = max(i, ra_index);
  1197. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  1198. cluster);
  1199. ra_index += max_cluster;
  1200. }
  1201. mutex_lock(&inode->i_mutex);
  1202. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  1203. BTRFS_I(inode)->force_compress = compress_type;
  1204. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1205. if (ret < 0) {
  1206. mutex_unlock(&inode->i_mutex);
  1207. goto out_ra;
  1208. }
  1209. defrag_count += ret;
  1210. balance_dirty_pages_ratelimited(inode->i_mapping);
  1211. mutex_unlock(&inode->i_mutex);
  1212. if (newer_than) {
  1213. if (newer_off == (u64)-1)
  1214. break;
  1215. if (ret > 0)
  1216. i += ret;
  1217. newer_off = max(newer_off + 1,
  1218. (u64)i << PAGE_CACHE_SHIFT);
  1219. ret = find_new_extents(root, inode,
  1220. newer_than, &newer_off,
  1221. 64 * 1024);
  1222. if (!ret) {
  1223. range->start = newer_off;
  1224. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1225. } else {
  1226. break;
  1227. }
  1228. } else {
  1229. if (ret > 0) {
  1230. i += ret;
  1231. last_len += ret << PAGE_CACHE_SHIFT;
  1232. } else {
  1233. i++;
  1234. last_len = 0;
  1235. }
  1236. }
  1237. }
  1238. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
  1239. filemap_flush(inode->i_mapping);
  1240. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1241. &BTRFS_I(inode)->runtime_flags))
  1242. filemap_flush(inode->i_mapping);
  1243. }
  1244. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1245. /* the filemap_flush will queue IO into the worker threads, but
  1246. * we have to make sure the IO is actually started and that
  1247. * ordered extents get created before we return
  1248. */
  1249. atomic_inc(&root->fs_info->async_submit_draining);
  1250. while (atomic_read(&root->fs_info->nr_async_submits) ||
  1251. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1252. wait_event(root->fs_info->async_submit_wait,
  1253. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  1254. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  1255. }
  1256. atomic_dec(&root->fs_info->async_submit_draining);
  1257. }
  1258. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1259. btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
  1260. }
  1261. ret = defrag_count;
  1262. out_ra:
  1263. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1264. mutex_lock(&inode->i_mutex);
  1265. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1266. mutex_unlock(&inode->i_mutex);
  1267. }
  1268. if (!file)
  1269. kfree(ra);
  1270. kfree(pages);
  1271. return ret;
  1272. }
  1273. static noinline int btrfs_ioctl_resize(struct file *file,
  1274. void __user *arg)
  1275. {
  1276. u64 new_size;
  1277. u64 old_size;
  1278. u64 devid = 1;
  1279. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  1280. struct btrfs_ioctl_vol_args *vol_args;
  1281. struct btrfs_trans_handle *trans;
  1282. struct btrfs_device *device = NULL;
  1283. char *sizestr;
  1284. char *retptr;
  1285. char *devstr = NULL;
  1286. int ret = 0;
  1287. int mod = 0;
  1288. if (!capable(CAP_SYS_ADMIN))
  1289. return -EPERM;
  1290. ret = mnt_want_write_file(file);
  1291. if (ret)
  1292. return ret;
  1293. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1294. 1)) {
  1295. mnt_drop_write_file(file);
  1296. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  1297. }
  1298. mutex_lock(&root->fs_info->volume_mutex);
  1299. vol_args = memdup_user(arg, sizeof(*vol_args));
  1300. if (IS_ERR(vol_args)) {
  1301. ret = PTR_ERR(vol_args);
  1302. goto out;
  1303. }
  1304. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1305. sizestr = vol_args->name;
  1306. devstr = strchr(sizestr, ':');
  1307. if (devstr) {
  1308. sizestr = devstr + 1;
  1309. *devstr = '\0';
  1310. devstr = vol_args->name;
  1311. ret = kstrtoull(devstr, 10, &devid);
  1312. if (ret)
  1313. goto out_free;
  1314. if (!devid) {
  1315. ret = -EINVAL;
  1316. goto out_free;
  1317. }
  1318. btrfs_info(root->fs_info, "resizing devid %llu", devid);
  1319. }
  1320. device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  1321. if (!device) {
  1322. btrfs_info(root->fs_info, "resizer unable to find device %llu",
  1323. devid);
  1324. ret = -ENODEV;
  1325. goto out_free;
  1326. }
  1327. if (!device->writeable) {
  1328. btrfs_info(root->fs_info,
  1329. "resizer unable to apply on readonly device %llu",
  1330. devid);
  1331. ret = -EPERM;
  1332. goto out_free;
  1333. }
  1334. if (!strcmp(sizestr, "max"))
  1335. new_size = device->bdev->bd_inode->i_size;
  1336. else {
  1337. if (sizestr[0] == '-') {
  1338. mod = -1;
  1339. sizestr++;
  1340. } else if (sizestr[0] == '+') {
  1341. mod = 1;
  1342. sizestr++;
  1343. }
  1344. new_size = memparse(sizestr, &retptr);
  1345. if (*retptr != '\0' || new_size == 0) {
  1346. ret = -EINVAL;
  1347. goto out_free;
  1348. }
  1349. }
  1350. if (device->is_tgtdev_for_dev_replace) {
  1351. ret = -EPERM;
  1352. goto out_free;
  1353. }
  1354. old_size = device->total_bytes;
  1355. if (mod < 0) {
  1356. if (new_size > old_size) {
  1357. ret = -EINVAL;
  1358. goto out_free;
  1359. }
  1360. new_size = old_size - new_size;
  1361. } else if (mod > 0) {
  1362. if (new_size > ULLONG_MAX - old_size) {
  1363. ret = -ERANGE;
  1364. goto out_free;
  1365. }
  1366. new_size = old_size + new_size;
  1367. }
  1368. if (new_size < 256 * 1024 * 1024) {
  1369. ret = -EINVAL;
  1370. goto out_free;
  1371. }
  1372. if (new_size > device->bdev->bd_inode->i_size) {
  1373. ret = -EFBIG;
  1374. goto out_free;
  1375. }
  1376. do_div(new_size, root->sectorsize);
  1377. new_size *= root->sectorsize;
  1378. printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
  1379. rcu_str_deref(device->name), new_size);
  1380. if (new_size > old_size) {
  1381. trans = btrfs_start_transaction(root, 0);
  1382. if (IS_ERR(trans)) {
  1383. ret = PTR_ERR(trans);
  1384. goto out_free;
  1385. }
  1386. ret = btrfs_grow_device(trans, device, new_size);
  1387. btrfs_commit_transaction(trans, root);
  1388. } else if (new_size < old_size) {
  1389. ret = btrfs_shrink_device(device, new_size);
  1390. } /* equal, nothing need to do */
  1391. out_free:
  1392. kfree(vol_args);
  1393. out:
  1394. mutex_unlock(&root->fs_info->volume_mutex);
  1395. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1396. mnt_drop_write_file(file);
  1397. return ret;
  1398. }
  1399. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1400. char *name, unsigned long fd, int subvol,
  1401. u64 *transid, bool readonly,
  1402. struct btrfs_qgroup_inherit *inherit)
  1403. {
  1404. int namelen;
  1405. int ret = 0;
  1406. ret = mnt_want_write_file(file);
  1407. if (ret)
  1408. goto out;
  1409. namelen = strlen(name);
  1410. if (strchr(name, '/')) {
  1411. ret = -EINVAL;
  1412. goto out_drop_write;
  1413. }
  1414. if (name[0] == '.' &&
  1415. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1416. ret = -EEXIST;
  1417. goto out_drop_write;
  1418. }
  1419. if (subvol) {
  1420. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1421. NULL, transid, readonly, inherit);
  1422. } else {
  1423. struct fd src = fdget(fd);
  1424. struct inode *src_inode;
  1425. if (!src.file) {
  1426. ret = -EINVAL;
  1427. goto out_drop_write;
  1428. }
  1429. src_inode = file_inode(src.file);
  1430. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1431. btrfs_info(BTRFS_I(src_inode)->root->fs_info,
  1432. "Snapshot src from another FS");
  1433. ret = -EXDEV;
  1434. } else if (!inode_owner_or_capable(src_inode)) {
  1435. /*
  1436. * Subvolume creation is not restricted, but snapshots
  1437. * are limited to own subvolumes only
  1438. */
  1439. ret = -EPERM;
  1440. } else {
  1441. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1442. BTRFS_I(src_inode)->root,
  1443. transid, readonly, inherit);
  1444. }
  1445. fdput(src);
  1446. }
  1447. out_drop_write:
  1448. mnt_drop_write_file(file);
  1449. out:
  1450. return ret;
  1451. }
  1452. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1453. void __user *arg, int subvol)
  1454. {
  1455. struct btrfs_ioctl_vol_args *vol_args;
  1456. int ret;
  1457. vol_args = memdup_user(arg, sizeof(*vol_args));
  1458. if (IS_ERR(vol_args))
  1459. return PTR_ERR(vol_args);
  1460. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1461. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1462. vol_args->fd, subvol,
  1463. NULL, false, NULL);
  1464. kfree(vol_args);
  1465. return ret;
  1466. }
  1467. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1468. void __user *arg, int subvol)
  1469. {
  1470. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1471. int ret;
  1472. u64 transid = 0;
  1473. u64 *ptr = NULL;
  1474. bool readonly = false;
  1475. struct btrfs_qgroup_inherit *inherit = NULL;
  1476. vol_args = memdup_user(arg, sizeof(*vol_args));
  1477. if (IS_ERR(vol_args))
  1478. return PTR_ERR(vol_args);
  1479. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1480. if (vol_args->flags &
  1481. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1482. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1483. ret = -EOPNOTSUPP;
  1484. goto free_args;
  1485. }
  1486. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1487. ptr = &transid;
  1488. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1489. readonly = true;
  1490. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1491. if (vol_args->size > PAGE_CACHE_SIZE) {
  1492. ret = -EINVAL;
  1493. goto free_args;
  1494. }
  1495. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1496. if (IS_ERR(inherit)) {
  1497. ret = PTR_ERR(inherit);
  1498. goto free_args;
  1499. }
  1500. }
  1501. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1502. vol_args->fd, subvol, ptr,
  1503. readonly, inherit);
  1504. if (ret)
  1505. goto free_inherit;
  1506. if (ptr && copy_to_user(arg +
  1507. offsetof(struct btrfs_ioctl_vol_args_v2,
  1508. transid),
  1509. ptr, sizeof(*ptr)))
  1510. ret = -EFAULT;
  1511. free_inherit:
  1512. kfree(inherit);
  1513. free_args:
  1514. kfree(vol_args);
  1515. return ret;
  1516. }
  1517. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1518. void __user *arg)
  1519. {
  1520. struct inode *inode = file_inode(file);
  1521. struct btrfs_root *root = BTRFS_I(inode)->root;
  1522. int ret = 0;
  1523. u64 flags = 0;
  1524. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1525. return -EINVAL;
  1526. down_read(&root->fs_info->subvol_sem);
  1527. if (btrfs_root_readonly(root))
  1528. flags |= BTRFS_SUBVOL_RDONLY;
  1529. up_read(&root->fs_info->subvol_sem);
  1530. if (copy_to_user(arg, &flags, sizeof(flags)))
  1531. ret = -EFAULT;
  1532. return ret;
  1533. }
  1534. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1535. void __user *arg)
  1536. {
  1537. struct inode *inode = file_inode(file);
  1538. struct btrfs_root *root = BTRFS_I(inode)->root;
  1539. struct btrfs_trans_handle *trans;
  1540. u64 root_flags;
  1541. u64 flags;
  1542. int ret = 0;
  1543. if (!inode_owner_or_capable(inode))
  1544. return -EPERM;
  1545. ret = mnt_want_write_file(file);
  1546. if (ret)
  1547. goto out;
  1548. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1549. ret = -EINVAL;
  1550. goto out_drop_write;
  1551. }
  1552. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1553. ret = -EFAULT;
  1554. goto out_drop_write;
  1555. }
  1556. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1557. ret = -EINVAL;
  1558. goto out_drop_write;
  1559. }
  1560. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1561. ret = -EOPNOTSUPP;
  1562. goto out_drop_write;
  1563. }
  1564. down_write(&root->fs_info->subvol_sem);
  1565. /* nothing to do */
  1566. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1567. goto out_drop_sem;
  1568. root_flags = btrfs_root_flags(&root->root_item);
  1569. if (flags & BTRFS_SUBVOL_RDONLY) {
  1570. btrfs_set_root_flags(&root->root_item,
  1571. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1572. } else {
  1573. /*
  1574. * Block RO -> RW transition if this subvolume is involved in
  1575. * send
  1576. */
  1577. spin_lock(&root->root_item_lock);
  1578. if (root->send_in_progress == 0) {
  1579. btrfs_set_root_flags(&root->root_item,
  1580. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1581. spin_unlock(&root->root_item_lock);
  1582. } else {
  1583. spin_unlock(&root->root_item_lock);
  1584. btrfs_warn(root->fs_info,
  1585. "Attempt to set subvolume %llu read-write during send",
  1586. root->root_key.objectid);
  1587. ret = -EPERM;
  1588. goto out_drop_sem;
  1589. }
  1590. }
  1591. trans = btrfs_start_transaction(root, 1);
  1592. if (IS_ERR(trans)) {
  1593. ret = PTR_ERR(trans);
  1594. goto out_reset;
  1595. }
  1596. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1597. &root->root_key, &root->root_item);
  1598. btrfs_commit_transaction(trans, root);
  1599. out_reset:
  1600. if (ret)
  1601. btrfs_set_root_flags(&root->root_item, root_flags);
  1602. out_drop_sem:
  1603. up_write(&root->fs_info->subvol_sem);
  1604. out_drop_write:
  1605. mnt_drop_write_file(file);
  1606. out:
  1607. return ret;
  1608. }
  1609. /*
  1610. * helper to check if the subvolume references other subvolumes
  1611. */
  1612. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1613. {
  1614. struct btrfs_path *path;
  1615. struct btrfs_dir_item *di;
  1616. struct btrfs_key key;
  1617. u64 dir_id;
  1618. int ret;
  1619. path = btrfs_alloc_path();
  1620. if (!path)
  1621. return -ENOMEM;
  1622. /* Make sure this root isn't set as the default subvol */
  1623. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  1624. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
  1625. dir_id, "default", 7, 0);
  1626. if (di && !IS_ERR(di)) {
  1627. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1628. if (key.objectid == root->root_key.objectid) {
  1629. ret = -EPERM;
  1630. btrfs_err(root->fs_info, "deleting default subvolume "
  1631. "%llu is not allowed", key.objectid);
  1632. goto out;
  1633. }
  1634. btrfs_release_path(path);
  1635. }
  1636. key.objectid = root->root_key.objectid;
  1637. key.type = BTRFS_ROOT_REF_KEY;
  1638. key.offset = (u64)-1;
  1639. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1640. &key, path, 0, 0);
  1641. if (ret < 0)
  1642. goto out;
  1643. BUG_ON(ret == 0);
  1644. ret = 0;
  1645. if (path->slots[0] > 0) {
  1646. path->slots[0]--;
  1647. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1648. if (key.objectid == root->root_key.objectid &&
  1649. key.type == BTRFS_ROOT_REF_KEY)
  1650. ret = -ENOTEMPTY;
  1651. }
  1652. out:
  1653. btrfs_free_path(path);
  1654. return ret;
  1655. }
  1656. static noinline int key_in_sk(struct btrfs_key *key,
  1657. struct btrfs_ioctl_search_key *sk)
  1658. {
  1659. struct btrfs_key test;
  1660. int ret;
  1661. test.objectid = sk->min_objectid;
  1662. test.type = sk->min_type;
  1663. test.offset = sk->min_offset;
  1664. ret = btrfs_comp_cpu_keys(key, &test);
  1665. if (ret < 0)
  1666. return 0;
  1667. test.objectid = sk->max_objectid;
  1668. test.type = sk->max_type;
  1669. test.offset = sk->max_offset;
  1670. ret = btrfs_comp_cpu_keys(key, &test);
  1671. if (ret > 0)
  1672. return 0;
  1673. return 1;
  1674. }
  1675. static noinline int copy_to_sk(struct btrfs_root *root,
  1676. struct btrfs_path *path,
  1677. struct btrfs_key *key,
  1678. struct btrfs_ioctl_search_key *sk,
  1679. size_t *buf_size,
  1680. char __user *ubuf,
  1681. unsigned long *sk_offset,
  1682. int *num_found)
  1683. {
  1684. u64 found_transid;
  1685. struct extent_buffer *leaf;
  1686. struct btrfs_ioctl_search_header sh;
  1687. unsigned long item_off;
  1688. unsigned long item_len;
  1689. int nritems;
  1690. int i;
  1691. int slot;
  1692. int ret = 0;
  1693. leaf = path->nodes[0];
  1694. slot = path->slots[0];
  1695. nritems = btrfs_header_nritems(leaf);
  1696. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1697. i = nritems;
  1698. goto advance_key;
  1699. }
  1700. found_transid = btrfs_header_generation(leaf);
  1701. for (i = slot; i < nritems; i++) {
  1702. item_off = btrfs_item_ptr_offset(leaf, i);
  1703. item_len = btrfs_item_size_nr(leaf, i);
  1704. btrfs_item_key_to_cpu(leaf, key, i);
  1705. if (!key_in_sk(key, sk))
  1706. continue;
  1707. if (sizeof(sh) + item_len > *buf_size) {
  1708. if (*num_found) {
  1709. ret = 1;
  1710. goto out;
  1711. }
  1712. /*
  1713. * return one empty item back for v1, which does not
  1714. * handle -EOVERFLOW
  1715. */
  1716. *buf_size = sizeof(sh) + item_len;
  1717. item_len = 0;
  1718. ret = -EOVERFLOW;
  1719. }
  1720. if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
  1721. ret = 1;
  1722. goto out;
  1723. }
  1724. sh.objectid = key->objectid;
  1725. sh.offset = key->offset;
  1726. sh.type = key->type;
  1727. sh.len = item_len;
  1728. sh.transid = found_transid;
  1729. /* copy search result header */
  1730. if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
  1731. ret = -EFAULT;
  1732. goto out;
  1733. }
  1734. *sk_offset += sizeof(sh);
  1735. if (item_len) {
  1736. char __user *up = ubuf + *sk_offset;
  1737. /* copy the item */
  1738. if (read_extent_buffer_to_user(leaf, up,
  1739. item_off, item_len)) {
  1740. ret = -EFAULT;
  1741. goto out;
  1742. }
  1743. *sk_offset += item_len;
  1744. }
  1745. (*num_found)++;
  1746. if (ret) /* -EOVERFLOW from above */
  1747. goto out;
  1748. if (*num_found >= sk->nr_items) {
  1749. ret = 1;
  1750. goto out;
  1751. }
  1752. }
  1753. advance_key:
  1754. ret = 0;
  1755. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1756. key->offset++;
  1757. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1758. key->offset = 0;
  1759. key->type++;
  1760. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1761. key->offset = 0;
  1762. key->type = 0;
  1763. key->objectid++;
  1764. } else
  1765. ret = 1;
  1766. out:
  1767. /*
  1768. * 0: all items from this leaf copied, continue with next
  1769. * 1: * more items can be copied, but unused buffer is too small
  1770. * * all items were found
  1771. * Either way, it will stops the loop which iterates to the next
  1772. * leaf
  1773. * -EOVERFLOW: item was to large for buffer
  1774. * -EFAULT: could not copy extent buffer back to userspace
  1775. */
  1776. return ret;
  1777. }
  1778. static noinline int search_ioctl(struct inode *inode,
  1779. struct btrfs_ioctl_search_key *sk,
  1780. size_t *buf_size,
  1781. char __user *ubuf)
  1782. {
  1783. struct btrfs_root *root;
  1784. struct btrfs_key key;
  1785. struct btrfs_path *path;
  1786. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1787. int ret;
  1788. int num_found = 0;
  1789. unsigned long sk_offset = 0;
  1790. if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
  1791. *buf_size = sizeof(struct btrfs_ioctl_search_header);
  1792. return -EOVERFLOW;
  1793. }
  1794. path = btrfs_alloc_path();
  1795. if (!path)
  1796. return -ENOMEM;
  1797. if (sk->tree_id == 0) {
  1798. /* search the root of the inode that was passed */
  1799. root = BTRFS_I(inode)->root;
  1800. } else {
  1801. key.objectid = sk->tree_id;
  1802. key.type = BTRFS_ROOT_ITEM_KEY;
  1803. key.offset = (u64)-1;
  1804. root = btrfs_read_fs_root_no_name(info, &key);
  1805. if (IS_ERR(root)) {
  1806. printk(KERN_ERR "BTRFS: could not find root %llu\n",
  1807. sk->tree_id);
  1808. btrfs_free_path(path);
  1809. return -ENOENT;
  1810. }
  1811. }
  1812. key.objectid = sk->min_objectid;
  1813. key.type = sk->min_type;
  1814. key.offset = sk->min_offset;
  1815. path->keep_locks = 1;
  1816. while (1) {
  1817. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1818. if (ret != 0) {
  1819. if (ret > 0)
  1820. ret = 0;
  1821. goto err;
  1822. }
  1823. ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
  1824. &sk_offset, &num_found);
  1825. btrfs_release_path(path);
  1826. if (ret)
  1827. break;
  1828. }
  1829. if (ret > 0)
  1830. ret = 0;
  1831. err:
  1832. sk->nr_items = num_found;
  1833. btrfs_free_path(path);
  1834. return ret;
  1835. }
  1836. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1837. void __user *argp)
  1838. {
  1839. struct btrfs_ioctl_search_args __user *uargs;
  1840. struct btrfs_ioctl_search_key sk;
  1841. struct inode *inode;
  1842. int ret;
  1843. size_t buf_size;
  1844. if (!capable(CAP_SYS_ADMIN))
  1845. return -EPERM;
  1846. uargs = (struct btrfs_ioctl_search_args __user *)argp;
  1847. if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
  1848. return -EFAULT;
  1849. buf_size = sizeof(uargs->buf);
  1850. inode = file_inode(file);
  1851. ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
  1852. /*
  1853. * In the origin implementation an overflow is handled by returning a
  1854. * search header with a len of zero, so reset ret.
  1855. */
  1856. if (ret == -EOVERFLOW)
  1857. ret = 0;
  1858. if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
  1859. ret = -EFAULT;
  1860. return ret;
  1861. }
  1862. static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
  1863. void __user *argp)
  1864. {
  1865. struct btrfs_ioctl_search_args_v2 __user *uarg;
  1866. struct btrfs_ioctl_search_args_v2 args;
  1867. struct inode *inode;
  1868. int ret;
  1869. size_t buf_size;
  1870. const size_t buf_limit = 16 * 1024 * 1024;
  1871. if (!capable(CAP_SYS_ADMIN))
  1872. return -EPERM;
  1873. /* copy search header and buffer size */
  1874. uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
  1875. if (copy_from_user(&args, uarg, sizeof(args)))
  1876. return -EFAULT;
  1877. buf_size = args.buf_size;
  1878. if (buf_size < sizeof(struct btrfs_ioctl_search_header))
  1879. return -EOVERFLOW;
  1880. /* limit result size to 16MB */
  1881. if (buf_size > buf_limit)
  1882. buf_size = buf_limit;
  1883. inode = file_inode(file);
  1884. ret = search_ioctl(inode, &args.key, &buf_size,
  1885. (char *)(&uarg->buf[0]));
  1886. if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
  1887. ret = -EFAULT;
  1888. else if (ret == -EOVERFLOW &&
  1889. copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
  1890. ret = -EFAULT;
  1891. return ret;
  1892. }
  1893. /*
  1894. * Search INODE_REFs to identify path name of 'dirid' directory
  1895. * in a 'tree_id' tree. and sets path name to 'name'.
  1896. */
  1897. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1898. u64 tree_id, u64 dirid, char *name)
  1899. {
  1900. struct btrfs_root *root;
  1901. struct btrfs_key key;
  1902. char *ptr;
  1903. int ret = -1;
  1904. int slot;
  1905. int len;
  1906. int total_len = 0;
  1907. struct btrfs_inode_ref *iref;
  1908. struct extent_buffer *l;
  1909. struct btrfs_path *path;
  1910. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1911. name[0]='\0';
  1912. return 0;
  1913. }
  1914. path = btrfs_alloc_path();
  1915. if (!path)
  1916. return -ENOMEM;
  1917. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1918. key.objectid = tree_id;
  1919. key.type = BTRFS_ROOT_ITEM_KEY;
  1920. key.offset = (u64)-1;
  1921. root = btrfs_read_fs_root_no_name(info, &key);
  1922. if (IS_ERR(root)) {
  1923. printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
  1924. ret = -ENOENT;
  1925. goto out;
  1926. }
  1927. key.objectid = dirid;
  1928. key.type = BTRFS_INODE_REF_KEY;
  1929. key.offset = (u64)-1;
  1930. while (1) {
  1931. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1932. if (ret < 0)
  1933. goto out;
  1934. else if (ret > 0) {
  1935. ret = btrfs_previous_item(root, path, dirid,
  1936. BTRFS_INODE_REF_KEY);
  1937. if (ret < 0)
  1938. goto out;
  1939. else if (ret > 0) {
  1940. ret = -ENOENT;
  1941. goto out;
  1942. }
  1943. }
  1944. l = path->nodes[0];
  1945. slot = path->slots[0];
  1946. btrfs_item_key_to_cpu(l, &key, slot);
  1947. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1948. len = btrfs_inode_ref_name_len(l, iref);
  1949. ptr -= len + 1;
  1950. total_len += len + 1;
  1951. if (ptr < name) {
  1952. ret = -ENAMETOOLONG;
  1953. goto out;
  1954. }
  1955. *(ptr + len) = '/';
  1956. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  1957. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1958. break;
  1959. btrfs_release_path(path);
  1960. key.objectid = key.offset;
  1961. key.offset = (u64)-1;
  1962. dirid = key.objectid;
  1963. }
  1964. memmove(name, ptr, total_len);
  1965. name[total_len] = '\0';
  1966. ret = 0;
  1967. out:
  1968. btrfs_free_path(path);
  1969. return ret;
  1970. }
  1971. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1972. void __user *argp)
  1973. {
  1974. struct btrfs_ioctl_ino_lookup_args *args;
  1975. struct inode *inode;
  1976. int ret;
  1977. if (!capable(CAP_SYS_ADMIN))
  1978. return -EPERM;
  1979. args = memdup_user(argp, sizeof(*args));
  1980. if (IS_ERR(args))
  1981. return PTR_ERR(args);
  1982. inode = file_inode(file);
  1983. if (args->treeid == 0)
  1984. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1985. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1986. args->treeid, args->objectid,
  1987. args->name);
  1988. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1989. ret = -EFAULT;
  1990. kfree(args);
  1991. return ret;
  1992. }
  1993. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1994. void __user *arg)
  1995. {
  1996. struct dentry *parent = file->f_path.dentry;
  1997. struct dentry *dentry;
  1998. struct inode *dir = parent->d_inode;
  1999. struct inode *inode;
  2000. struct btrfs_root *root = BTRFS_I(dir)->root;
  2001. struct btrfs_root *dest = NULL;
  2002. struct btrfs_ioctl_vol_args *vol_args;
  2003. struct btrfs_trans_handle *trans;
  2004. struct btrfs_block_rsv block_rsv;
  2005. u64 root_flags;
  2006. u64 qgroup_reserved;
  2007. int namelen;
  2008. int ret;
  2009. int err = 0;
  2010. vol_args = memdup_user(arg, sizeof(*vol_args));
  2011. if (IS_ERR(vol_args))
  2012. return PTR_ERR(vol_args);
  2013. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2014. namelen = strlen(vol_args->name);
  2015. if (strchr(vol_args->name, '/') ||
  2016. strncmp(vol_args->name, "..", namelen) == 0) {
  2017. err = -EINVAL;
  2018. goto out;
  2019. }
  2020. err = mnt_want_write_file(file);
  2021. if (err)
  2022. goto out;
  2023. err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  2024. if (err == -EINTR)
  2025. goto out_drop_write;
  2026. dentry = lookup_one_len(vol_args->name, parent, namelen);
  2027. if (IS_ERR(dentry)) {
  2028. err = PTR_ERR(dentry);
  2029. goto out_unlock_dir;
  2030. }
  2031. if (!dentry->d_inode) {
  2032. err = -ENOENT;
  2033. goto out_dput;
  2034. }
  2035. inode = dentry->d_inode;
  2036. dest = BTRFS_I(inode)->root;
  2037. if (!capable(CAP_SYS_ADMIN)) {
  2038. /*
  2039. * Regular user. Only allow this with a special mount
  2040. * option, when the user has write+exec access to the
  2041. * subvol root, and when rmdir(2) would have been
  2042. * allowed.
  2043. *
  2044. * Note that this is _not_ check that the subvol is
  2045. * empty or doesn't contain data that we wouldn't
  2046. * otherwise be able to delete.
  2047. *
  2048. * Users who want to delete empty subvols should try
  2049. * rmdir(2).
  2050. */
  2051. err = -EPERM;
  2052. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  2053. goto out_dput;
  2054. /*
  2055. * Do not allow deletion if the parent dir is the same
  2056. * as the dir to be deleted. That means the ioctl
  2057. * must be called on the dentry referencing the root
  2058. * of the subvol, not a random directory contained
  2059. * within it.
  2060. */
  2061. err = -EINVAL;
  2062. if (root == dest)
  2063. goto out_dput;
  2064. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  2065. if (err)
  2066. goto out_dput;
  2067. }
  2068. /* check if subvolume may be deleted by a user */
  2069. err = btrfs_may_delete(dir, dentry, 1);
  2070. if (err)
  2071. goto out_dput;
  2072. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  2073. err = -EINVAL;
  2074. goto out_dput;
  2075. }
  2076. mutex_lock(&inode->i_mutex);
  2077. /*
  2078. * Don't allow to delete a subvolume with send in progress. This is
  2079. * inside the i_mutex so the error handling that has to drop the bit
  2080. * again is not run concurrently.
  2081. */
  2082. spin_lock(&dest->root_item_lock);
  2083. root_flags = btrfs_root_flags(&dest->root_item);
  2084. if (dest->send_in_progress == 0) {
  2085. btrfs_set_root_flags(&dest->root_item,
  2086. root_flags | BTRFS_ROOT_SUBVOL_DEAD);
  2087. spin_unlock(&dest->root_item_lock);
  2088. } else {
  2089. spin_unlock(&dest->root_item_lock);
  2090. btrfs_warn(root->fs_info,
  2091. "Attempt to delete subvolume %llu during send",
  2092. dest->root_key.objectid);
  2093. err = -EPERM;
  2094. goto out_dput;
  2095. }
  2096. err = d_invalidate(dentry);
  2097. if (err)
  2098. goto out_unlock;
  2099. down_write(&root->fs_info->subvol_sem);
  2100. err = may_destroy_subvol(dest);
  2101. if (err)
  2102. goto out_up_write;
  2103. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  2104. /*
  2105. * One for dir inode, two for dir entries, two for root
  2106. * ref/backref.
  2107. */
  2108. err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  2109. 5, &qgroup_reserved, true);
  2110. if (err)
  2111. goto out_up_write;
  2112. trans = btrfs_start_transaction(root, 0);
  2113. if (IS_ERR(trans)) {
  2114. err = PTR_ERR(trans);
  2115. goto out_release;
  2116. }
  2117. trans->block_rsv = &block_rsv;
  2118. trans->bytes_reserved = block_rsv.size;
  2119. ret = btrfs_unlink_subvol(trans, root, dir,
  2120. dest->root_key.objectid,
  2121. dentry->d_name.name,
  2122. dentry->d_name.len);
  2123. if (ret) {
  2124. err = ret;
  2125. btrfs_abort_transaction(trans, root, ret);
  2126. goto out_end_trans;
  2127. }
  2128. btrfs_record_root_in_trans(trans, dest);
  2129. memset(&dest->root_item.drop_progress, 0,
  2130. sizeof(dest->root_item.drop_progress));
  2131. dest->root_item.drop_level = 0;
  2132. btrfs_set_root_refs(&dest->root_item, 0);
  2133. if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
  2134. ret = btrfs_insert_orphan_item(trans,
  2135. root->fs_info->tree_root,
  2136. dest->root_key.objectid);
  2137. if (ret) {
  2138. btrfs_abort_transaction(trans, root, ret);
  2139. err = ret;
  2140. goto out_end_trans;
  2141. }
  2142. }
  2143. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  2144. dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  2145. dest->root_key.objectid);
  2146. if (ret && ret != -ENOENT) {
  2147. btrfs_abort_transaction(trans, root, ret);
  2148. err = ret;
  2149. goto out_end_trans;
  2150. }
  2151. if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
  2152. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  2153. dest->root_item.received_uuid,
  2154. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  2155. dest->root_key.objectid);
  2156. if (ret && ret != -ENOENT) {
  2157. btrfs_abort_transaction(trans, root, ret);
  2158. err = ret;
  2159. goto out_end_trans;
  2160. }
  2161. }
  2162. out_end_trans:
  2163. trans->block_rsv = NULL;
  2164. trans->bytes_reserved = 0;
  2165. ret = btrfs_end_transaction(trans, root);
  2166. if (ret && !err)
  2167. err = ret;
  2168. inode->i_flags |= S_DEAD;
  2169. out_release:
  2170. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  2171. out_up_write:
  2172. up_write(&root->fs_info->subvol_sem);
  2173. out_unlock:
  2174. if (err) {
  2175. spin_lock(&dest->root_item_lock);
  2176. root_flags = btrfs_root_flags(&dest->root_item);
  2177. btrfs_set_root_flags(&dest->root_item,
  2178. root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
  2179. spin_unlock(&dest->root_item_lock);
  2180. }
  2181. mutex_unlock(&inode->i_mutex);
  2182. if (!err) {
  2183. shrink_dcache_sb(root->fs_info->sb);
  2184. btrfs_invalidate_inodes(dest);
  2185. d_delete(dentry);
  2186. ASSERT(dest->send_in_progress == 0);
  2187. /* the last ref */
  2188. if (dest->cache_inode) {
  2189. iput(dest->cache_inode);
  2190. dest->cache_inode = NULL;
  2191. }
  2192. }
  2193. out_dput:
  2194. dput(dentry);
  2195. out_unlock_dir:
  2196. mutex_unlock(&dir->i_mutex);
  2197. out_drop_write:
  2198. mnt_drop_write_file(file);
  2199. out:
  2200. kfree(vol_args);
  2201. return err;
  2202. }
  2203. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2204. {
  2205. struct inode *inode = file_inode(file);
  2206. struct btrfs_root *root = BTRFS_I(inode)->root;
  2207. struct btrfs_ioctl_defrag_range_args *range;
  2208. int ret;
  2209. ret = mnt_want_write_file(file);
  2210. if (ret)
  2211. return ret;
  2212. if (btrfs_root_readonly(root)) {
  2213. ret = -EROFS;
  2214. goto out;
  2215. }
  2216. switch (inode->i_mode & S_IFMT) {
  2217. case S_IFDIR:
  2218. if (!capable(CAP_SYS_ADMIN)) {
  2219. ret = -EPERM;
  2220. goto out;
  2221. }
  2222. ret = btrfs_defrag_root(root);
  2223. if (ret)
  2224. goto out;
  2225. ret = btrfs_defrag_root(root->fs_info->extent_root);
  2226. break;
  2227. case S_IFREG:
  2228. if (!(file->f_mode & FMODE_WRITE)) {
  2229. ret = -EINVAL;
  2230. goto out;
  2231. }
  2232. range = kzalloc(sizeof(*range), GFP_KERNEL);
  2233. if (!range) {
  2234. ret = -ENOMEM;
  2235. goto out;
  2236. }
  2237. if (argp) {
  2238. if (copy_from_user(range, argp,
  2239. sizeof(*range))) {
  2240. ret = -EFAULT;
  2241. kfree(range);
  2242. goto out;
  2243. }
  2244. /* compression requires us to start the IO */
  2245. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2246. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2247. range->extent_thresh = (u32)-1;
  2248. }
  2249. } else {
  2250. /* the rest are all set to zero by kzalloc */
  2251. range->len = (u64)-1;
  2252. }
  2253. ret = btrfs_defrag_file(file_inode(file), file,
  2254. range, 0, 0);
  2255. if (ret > 0)
  2256. ret = 0;
  2257. kfree(range);
  2258. break;
  2259. default:
  2260. ret = -EINVAL;
  2261. }
  2262. out:
  2263. mnt_drop_write_file(file);
  2264. return ret;
  2265. }
  2266. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  2267. {
  2268. struct btrfs_ioctl_vol_args *vol_args;
  2269. int ret;
  2270. if (!capable(CAP_SYS_ADMIN))
  2271. return -EPERM;
  2272. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2273. 1)) {
  2274. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2275. }
  2276. mutex_lock(&root->fs_info->volume_mutex);
  2277. vol_args = memdup_user(arg, sizeof(*vol_args));
  2278. if (IS_ERR(vol_args)) {
  2279. ret = PTR_ERR(vol_args);
  2280. goto out;
  2281. }
  2282. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2283. ret = btrfs_init_new_device(root, vol_args->name);
  2284. kfree(vol_args);
  2285. out:
  2286. mutex_unlock(&root->fs_info->volume_mutex);
  2287. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2288. return ret;
  2289. }
  2290. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2291. {
  2292. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2293. struct btrfs_ioctl_vol_args *vol_args;
  2294. int ret;
  2295. if (!capable(CAP_SYS_ADMIN))
  2296. return -EPERM;
  2297. ret = mnt_want_write_file(file);
  2298. if (ret)
  2299. return ret;
  2300. vol_args = memdup_user(arg, sizeof(*vol_args));
  2301. if (IS_ERR(vol_args)) {
  2302. ret = PTR_ERR(vol_args);
  2303. goto err_drop;
  2304. }
  2305. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2306. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2307. 1)) {
  2308. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2309. goto out;
  2310. }
  2311. mutex_lock(&root->fs_info->volume_mutex);
  2312. ret = btrfs_rm_device(root, vol_args->name);
  2313. mutex_unlock(&root->fs_info->volume_mutex);
  2314. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2315. out:
  2316. kfree(vol_args);
  2317. err_drop:
  2318. mnt_drop_write_file(file);
  2319. return ret;
  2320. }
  2321. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2322. {
  2323. struct btrfs_ioctl_fs_info_args *fi_args;
  2324. struct btrfs_device *device;
  2325. struct btrfs_device *next;
  2326. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2327. int ret = 0;
  2328. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2329. if (!fi_args)
  2330. return -ENOMEM;
  2331. mutex_lock(&fs_devices->device_list_mutex);
  2332. fi_args->num_devices = fs_devices->num_devices;
  2333. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2334. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2335. if (device->devid > fi_args->max_id)
  2336. fi_args->max_id = device->devid;
  2337. }
  2338. mutex_unlock(&fs_devices->device_list_mutex);
  2339. fi_args->nodesize = root->fs_info->super_copy->nodesize;
  2340. fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
  2341. fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
  2342. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2343. ret = -EFAULT;
  2344. kfree(fi_args);
  2345. return ret;
  2346. }
  2347. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2348. {
  2349. struct btrfs_ioctl_dev_info_args *di_args;
  2350. struct btrfs_device *dev;
  2351. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2352. int ret = 0;
  2353. char *s_uuid = NULL;
  2354. di_args = memdup_user(arg, sizeof(*di_args));
  2355. if (IS_ERR(di_args))
  2356. return PTR_ERR(di_args);
  2357. if (!btrfs_is_empty_uuid(di_args->uuid))
  2358. s_uuid = di_args->uuid;
  2359. mutex_lock(&fs_devices->device_list_mutex);
  2360. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2361. if (!dev) {
  2362. ret = -ENODEV;
  2363. goto out;
  2364. }
  2365. di_args->devid = dev->devid;
  2366. di_args->bytes_used = dev->bytes_used;
  2367. di_args->total_bytes = dev->total_bytes;
  2368. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2369. if (dev->name) {
  2370. struct rcu_string *name;
  2371. rcu_read_lock();
  2372. name = rcu_dereference(dev->name);
  2373. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2374. rcu_read_unlock();
  2375. di_args->path[sizeof(di_args->path) - 1] = 0;
  2376. } else {
  2377. di_args->path[0] = '\0';
  2378. }
  2379. out:
  2380. mutex_unlock(&fs_devices->device_list_mutex);
  2381. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2382. ret = -EFAULT;
  2383. kfree(di_args);
  2384. return ret;
  2385. }
  2386. static struct page *extent_same_get_page(struct inode *inode, u64 off)
  2387. {
  2388. struct page *page;
  2389. pgoff_t index;
  2390. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2391. index = off >> PAGE_CACHE_SHIFT;
  2392. page = grab_cache_page(inode->i_mapping, index);
  2393. if (!page)
  2394. return NULL;
  2395. if (!PageUptodate(page)) {
  2396. if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
  2397. 0))
  2398. return NULL;
  2399. lock_page(page);
  2400. if (!PageUptodate(page)) {
  2401. unlock_page(page);
  2402. page_cache_release(page);
  2403. return NULL;
  2404. }
  2405. }
  2406. unlock_page(page);
  2407. return page;
  2408. }
  2409. static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
  2410. {
  2411. /* do any pending delalloc/csum calc on src, one way or
  2412. another, and lock file content */
  2413. while (1) {
  2414. struct btrfs_ordered_extent *ordered;
  2415. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2416. ordered = btrfs_lookup_first_ordered_extent(inode,
  2417. off + len - 1);
  2418. if ((!ordered ||
  2419. ordered->file_offset + ordered->len <= off ||
  2420. ordered->file_offset >= off + len) &&
  2421. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2422. off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
  2423. if (ordered)
  2424. btrfs_put_ordered_extent(ordered);
  2425. break;
  2426. }
  2427. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2428. if (ordered)
  2429. btrfs_put_ordered_extent(ordered);
  2430. btrfs_wait_ordered_range(inode, off, len);
  2431. }
  2432. }
  2433. static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
  2434. struct inode *inode2, u64 loff2, u64 len)
  2435. {
  2436. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2437. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2438. mutex_unlock(&inode1->i_mutex);
  2439. mutex_unlock(&inode2->i_mutex);
  2440. }
  2441. static void btrfs_double_lock(struct inode *inode1, u64 loff1,
  2442. struct inode *inode2, u64 loff2, u64 len)
  2443. {
  2444. if (inode1 < inode2) {
  2445. swap(inode1, inode2);
  2446. swap(loff1, loff2);
  2447. }
  2448. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  2449. lock_extent_range(inode1, loff1, len);
  2450. if (inode1 != inode2) {
  2451. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  2452. lock_extent_range(inode2, loff2, len);
  2453. }
  2454. }
  2455. static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
  2456. u64 dst_loff, u64 len)
  2457. {
  2458. int ret = 0;
  2459. struct page *src_page, *dst_page;
  2460. unsigned int cmp_len = PAGE_CACHE_SIZE;
  2461. void *addr, *dst_addr;
  2462. while (len) {
  2463. if (len < PAGE_CACHE_SIZE)
  2464. cmp_len = len;
  2465. src_page = extent_same_get_page(src, loff);
  2466. if (!src_page)
  2467. return -EINVAL;
  2468. dst_page = extent_same_get_page(dst, dst_loff);
  2469. if (!dst_page) {
  2470. page_cache_release(src_page);
  2471. return -EINVAL;
  2472. }
  2473. addr = kmap_atomic(src_page);
  2474. dst_addr = kmap_atomic(dst_page);
  2475. flush_dcache_page(src_page);
  2476. flush_dcache_page(dst_page);
  2477. if (memcmp(addr, dst_addr, cmp_len))
  2478. ret = BTRFS_SAME_DATA_DIFFERS;
  2479. kunmap_atomic(addr);
  2480. kunmap_atomic(dst_addr);
  2481. page_cache_release(src_page);
  2482. page_cache_release(dst_page);
  2483. if (ret)
  2484. break;
  2485. loff += cmp_len;
  2486. dst_loff += cmp_len;
  2487. len -= cmp_len;
  2488. }
  2489. return ret;
  2490. }
  2491. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
  2492. {
  2493. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2494. if (off + len > inode->i_size || off + len < off)
  2495. return -EINVAL;
  2496. /* Check that we are block aligned - btrfs_clone() requires this */
  2497. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2498. return -EINVAL;
  2499. return 0;
  2500. }
  2501. static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
  2502. struct inode *dst, u64 dst_loff)
  2503. {
  2504. int ret;
  2505. /*
  2506. * btrfs_clone() can't handle extents in the same file
  2507. * yet. Once that works, we can drop this check and replace it
  2508. * with a check for the same inode, but overlapping extents.
  2509. */
  2510. if (src == dst)
  2511. return -EINVAL;
  2512. btrfs_double_lock(src, loff, dst, dst_loff, len);
  2513. ret = extent_same_check_offsets(src, loff, len);
  2514. if (ret)
  2515. goto out_unlock;
  2516. ret = extent_same_check_offsets(dst, dst_loff, len);
  2517. if (ret)
  2518. goto out_unlock;
  2519. /* don't make the dst file partly checksummed */
  2520. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2521. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  2522. ret = -EINVAL;
  2523. goto out_unlock;
  2524. }
  2525. ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
  2526. if (ret == 0)
  2527. ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
  2528. out_unlock:
  2529. btrfs_double_unlock(src, loff, dst, dst_loff, len);
  2530. return ret;
  2531. }
  2532. #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
  2533. static long btrfs_ioctl_file_extent_same(struct file *file,
  2534. struct btrfs_ioctl_same_args __user *argp)
  2535. {
  2536. struct btrfs_ioctl_same_args *same;
  2537. struct btrfs_ioctl_same_extent_info *info;
  2538. struct inode *src = file_inode(file);
  2539. u64 off;
  2540. u64 len;
  2541. int i;
  2542. int ret;
  2543. unsigned long size;
  2544. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  2545. bool is_admin = capable(CAP_SYS_ADMIN);
  2546. u16 count;
  2547. if (!(file->f_mode & FMODE_READ))
  2548. return -EINVAL;
  2549. ret = mnt_want_write_file(file);
  2550. if (ret)
  2551. return ret;
  2552. if (get_user(count, &argp->dest_count)) {
  2553. ret = -EFAULT;
  2554. goto out;
  2555. }
  2556. size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
  2557. same = memdup_user(argp, size);
  2558. if (IS_ERR(same)) {
  2559. ret = PTR_ERR(same);
  2560. goto out;
  2561. }
  2562. off = same->logical_offset;
  2563. len = same->length;
  2564. /*
  2565. * Limit the total length we will dedupe for each operation.
  2566. * This is intended to bound the total time spent in this
  2567. * ioctl to something sane.
  2568. */
  2569. if (len > BTRFS_MAX_DEDUPE_LEN)
  2570. len = BTRFS_MAX_DEDUPE_LEN;
  2571. if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
  2572. /*
  2573. * Btrfs does not support blocksize < page_size. As a
  2574. * result, btrfs_cmp_data() won't correctly handle
  2575. * this situation without an update.
  2576. */
  2577. ret = -EINVAL;
  2578. goto out;
  2579. }
  2580. ret = -EISDIR;
  2581. if (S_ISDIR(src->i_mode))
  2582. goto out;
  2583. ret = -EACCES;
  2584. if (!S_ISREG(src->i_mode))
  2585. goto out;
  2586. /* pre-format output fields to sane values */
  2587. for (i = 0; i < count; i++) {
  2588. same->info[i].bytes_deduped = 0ULL;
  2589. same->info[i].status = 0;
  2590. }
  2591. for (i = 0, info = same->info; i < count; i++, info++) {
  2592. struct inode *dst;
  2593. struct fd dst_file = fdget(info->fd);
  2594. if (!dst_file.file) {
  2595. info->status = -EBADF;
  2596. continue;
  2597. }
  2598. dst = file_inode(dst_file.file);
  2599. if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
  2600. info->status = -EINVAL;
  2601. } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
  2602. info->status = -EXDEV;
  2603. } else if (S_ISDIR(dst->i_mode)) {
  2604. info->status = -EISDIR;
  2605. } else if (!S_ISREG(dst->i_mode)) {
  2606. info->status = -EACCES;
  2607. } else {
  2608. info->status = btrfs_extent_same(src, off, len, dst,
  2609. info->logical_offset);
  2610. if (info->status == 0)
  2611. info->bytes_deduped += len;
  2612. }
  2613. fdput(dst_file);
  2614. }
  2615. ret = copy_to_user(argp, same, size);
  2616. if (ret)
  2617. ret = -EFAULT;
  2618. out:
  2619. mnt_drop_write_file(file);
  2620. return ret;
  2621. }
  2622. /* Helper to check and see if this root currently has a ref on the given disk
  2623. * bytenr. If it does then we need to update the quota for this root. This
  2624. * doesn't do anything if quotas aren't enabled.
  2625. */
  2626. static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2627. u64 disko)
  2628. {
  2629. struct seq_list tree_mod_seq_elem = {};
  2630. struct ulist *roots;
  2631. struct ulist_iterator uiter;
  2632. struct ulist_node *root_node = NULL;
  2633. int ret;
  2634. if (!root->fs_info->quota_enabled)
  2635. return 1;
  2636. btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
  2637. ret = btrfs_find_all_roots(trans, root->fs_info, disko,
  2638. tree_mod_seq_elem.seq, &roots);
  2639. if (ret < 0)
  2640. goto out;
  2641. ret = 0;
  2642. ULIST_ITER_INIT(&uiter);
  2643. while ((root_node = ulist_next(roots, &uiter))) {
  2644. if (root_node->val == root->objectid) {
  2645. ret = 1;
  2646. break;
  2647. }
  2648. }
  2649. ulist_free(roots);
  2650. out:
  2651. btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
  2652. return ret;
  2653. }
  2654. static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
  2655. struct inode *inode,
  2656. u64 endoff,
  2657. const u64 destoff,
  2658. const u64 olen)
  2659. {
  2660. struct btrfs_root *root = BTRFS_I(inode)->root;
  2661. int ret;
  2662. inode_inc_iversion(inode);
  2663. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2664. /*
  2665. * We round up to the block size at eof when determining which
  2666. * extents to clone above, but shouldn't round up the file size.
  2667. */
  2668. if (endoff > destoff + olen)
  2669. endoff = destoff + olen;
  2670. if (endoff > inode->i_size)
  2671. btrfs_i_size_write(inode, endoff);
  2672. ret = btrfs_update_inode(trans, root, inode);
  2673. if (ret) {
  2674. btrfs_abort_transaction(trans, root, ret);
  2675. btrfs_end_transaction(trans, root);
  2676. goto out;
  2677. }
  2678. ret = btrfs_end_transaction(trans, root);
  2679. out:
  2680. return ret;
  2681. }
  2682. static void clone_update_extent_map(struct inode *inode,
  2683. const struct btrfs_trans_handle *trans,
  2684. const struct btrfs_path *path,
  2685. const u64 hole_offset,
  2686. const u64 hole_len)
  2687. {
  2688. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2689. struct extent_map *em;
  2690. int ret;
  2691. em = alloc_extent_map();
  2692. if (!em) {
  2693. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2694. &BTRFS_I(inode)->runtime_flags);
  2695. return;
  2696. }
  2697. if (path) {
  2698. struct btrfs_file_extent_item *fi;
  2699. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2700. struct btrfs_file_extent_item);
  2701. btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
  2702. em->generation = -1;
  2703. if (btrfs_file_extent_type(path->nodes[0], fi) ==
  2704. BTRFS_FILE_EXTENT_INLINE)
  2705. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2706. &BTRFS_I(inode)->runtime_flags);
  2707. } else {
  2708. em->start = hole_offset;
  2709. em->len = hole_len;
  2710. em->ram_bytes = em->len;
  2711. em->orig_start = hole_offset;
  2712. em->block_start = EXTENT_MAP_HOLE;
  2713. em->block_len = 0;
  2714. em->orig_block_len = 0;
  2715. em->compress_type = BTRFS_COMPRESS_NONE;
  2716. em->generation = trans->transid;
  2717. }
  2718. while (1) {
  2719. write_lock(&em_tree->lock);
  2720. ret = add_extent_mapping(em_tree, em, 1);
  2721. write_unlock(&em_tree->lock);
  2722. if (ret != -EEXIST) {
  2723. free_extent_map(em);
  2724. break;
  2725. }
  2726. btrfs_drop_extent_cache(inode, em->start,
  2727. em->start + em->len - 1, 0);
  2728. }
  2729. if (unlikely(ret))
  2730. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2731. &BTRFS_I(inode)->runtime_flags);
  2732. }
  2733. /**
  2734. * btrfs_clone() - clone a range from inode file to another
  2735. *
  2736. * @src: Inode to clone from
  2737. * @inode: Inode to clone to
  2738. * @off: Offset within source to start clone from
  2739. * @olen: Original length, passed by user, of range to clone
  2740. * @olen_aligned: Block-aligned value of olen, extent_same uses
  2741. * identical values here
  2742. * @destoff: Offset within @inode to start clone
  2743. */
  2744. static int btrfs_clone(struct inode *src, struct inode *inode,
  2745. const u64 off, const u64 olen, const u64 olen_aligned,
  2746. const u64 destoff)
  2747. {
  2748. struct btrfs_root *root = BTRFS_I(inode)->root;
  2749. struct btrfs_path *path = NULL;
  2750. struct extent_buffer *leaf;
  2751. struct btrfs_trans_handle *trans;
  2752. char *buf = NULL;
  2753. struct btrfs_key key;
  2754. u32 nritems;
  2755. int slot;
  2756. int ret;
  2757. int no_quota;
  2758. const u64 len = olen_aligned;
  2759. u64 last_disko = 0;
  2760. u64 last_dest_end = destoff;
  2761. ret = -ENOMEM;
  2762. buf = vmalloc(btrfs_level_size(root, 0));
  2763. if (!buf)
  2764. return ret;
  2765. path = btrfs_alloc_path();
  2766. if (!path) {
  2767. vfree(buf);
  2768. return ret;
  2769. }
  2770. path->reada = 2;
  2771. /* clone data */
  2772. key.objectid = btrfs_ino(src);
  2773. key.type = BTRFS_EXTENT_DATA_KEY;
  2774. key.offset = off;
  2775. while (1) {
  2776. /*
  2777. * note the key will change type as we walk through the
  2778. * tree.
  2779. */
  2780. path->leave_spinning = 1;
  2781. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2782. 0, 0);
  2783. if (ret < 0)
  2784. goto out;
  2785. /*
  2786. * First search, if no extent item that starts at offset off was
  2787. * found but the previous item is an extent item, it's possible
  2788. * it might overlap our target range, therefore process it.
  2789. */
  2790. if (key.offset == off && ret > 0 && path->slots[0] > 0) {
  2791. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2792. path->slots[0] - 1);
  2793. if (key.type == BTRFS_EXTENT_DATA_KEY)
  2794. path->slots[0]--;
  2795. }
  2796. nritems = btrfs_header_nritems(path->nodes[0]);
  2797. process_slot:
  2798. no_quota = 1;
  2799. if (path->slots[0] >= nritems) {
  2800. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2801. if (ret < 0)
  2802. goto out;
  2803. if (ret > 0)
  2804. break;
  2805. nritems = btrfs_header_nritems(path->nodes[0]);
  2806. }
  2807. leaf = path->nodes[0];
  2808. slot = path->slots[0];
  2809. btrfs_item_key_to_cpu(leaf, &key, slot);
  2810. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  2811. key.objectid != btrfs_ino(src))
  2812. break;
  2813. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  2814. struct btrfs_file_extent_item *extent;
  2815. int type;
  2816. u32 size;
  2817. struct btrfs_key new_key;
  2818. u64 disko = 0, diskl = 0;
  2819. u64 datao = 0, datal = 0;
  2820. u8 comp;
  2821. u64 drop_start;
  2822. extent = btrfs_item_ptr(leaf, slot,
  2823. struct btrfs_file_extent_item);
  2824. comp = btrfs_file_extent_compression(leaf, extent);
  2825. type = btrfs_file_extent_type(leaf, extent);
  2826. if (type == BTRFS_FILE_EXTENT_REG ||
  2827. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2828. disko = btrfs_file_extent_disk_bytenr(leaf,
  2829. extent);
  2830. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2831. extent);
  2832. datao = btrfs_file_extent_offset(leaf, extent);
  2833. datal = btrfs_file_extent_num_bytes(leaf,
  2834. extent);
  2835. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2836. /* take upper bound, may be compressed */
  2837. datal = btrfs_file_extent_ram_bytes(leaf,
  2838. extent);
  2839. }
  2840. /*
  2841. * The first search might have left us at an extent
  2842. * item that ends before our target range's start, can
  2843. * happen if we have holes and NO_HOLES feature enabled.
  2844. */
  2845. if (key.offset + datal <= off) {
  2846. path->slots[0]++;
  2847. goto process_slot;
  2848. } else if (key.offset >= off + len) {
  2849. break;
  2850. }
  2851. size = btrfs_item_size_nr(leaf, slot);
  2852. read_extent_buffer(leaf, buf,
  2853. btrfs_item_ptr_offset(leaf, slot),
  2854. size);
  2855. btrfs_release_path(path);
  2856. path->leave_spinning = 0;
  2857. memcpy(&new_key, &key, sizeof(new_key));
  2858. new_key.objectid = btrfs_ino(inode);
  2859. if (off <= key.offset)
  2860. new_key.offset = key.offset + destoff - off;
  2861. else
  2862. new_key.offset = destoff;
  2863. /*
  2864. * Deal with a hole that doesn't have an extent item
  2865. * that represents it (NO_HOLES feature enabled).
  2866. * This hole is either in the middle of the cloning
  2867. * range or at the beginning (fully overlaps it or
  2868. * partially overlaps it).
  2869. */
  2870. if (new_key.offset != last_dest_end)
  2871. drop_start = last_dest_end;
  2872. else
  2873. drop_start = new_key.offset;
  2874. /*
  2875. * 1 - adjusting old extent (we may have to split it)
  2876. * 1 - add new extent
  2877. * 1 - inode update
  2878. */
  2879. trans = btrfs_start_transaction(root, 3);
  2880. if (IS_ERR(trans)) {
  2881. ret = PTR_ERR(trans);
  2882. goto out;
  2883. }
  2884. if (type == BTRFS_FILE_EXTENT_REG ||
  2885. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2886. /*
  2887. * a | --- range to clone ---| b
  2888. * | ------------- extent ------------- |
  2889. */
  2890. /* subtract range b */
  2891. if (key.offset + datal > off + len)
  2892. datal = off + len - key.offset;
  2893. /* subtract range a */
  2894. if (off > key.offset) {
  2895. datao += off - key.offset;
  2896. datal -= off - key.offset;
  2897. }
  2898. ret = btrfs_drop_extents(trans, root, inode,
  2899. drop_start,
  2900. new_key.offset + datal,
  2901. 1);
  2902. if (ret) {
  2903. if (ret != -EOPNOTSUPP)
  2904. btrfs_abort_transaction(trans,
  2905. root, ret);
  2906. btrfs_end_transaction(trans, root);
  2907. goto out;
  2908. }
  2909. ret = btrfs_insert_empty_item(trans, root, path,
  2910. &new_key, size);
  2911. if (ret) {
  2912. btrfs_abort_transaction(trans, root,
  2913. ret);
  2914. btrfs_end_transaction(trans, root);
  2915. goto out;
  2916. }
  2917. leaf = path->nodes[0];
  2918. slot = path->slots[0];
  2919. write_extent_buffer(leaf, buf,
  2920. btrfs_item_ptr_offset(leaf, slot),
  2921. size);
  2922. extent = btrfs_item_ptr(leaf, slot,
  2923. struct btrfs_file_extent_item);
  2924. /* disko == 0 means it's a hole */
  2925. if (!disko)
  2926. datao = 0;
  2927. btrfs_set_file_extent_offset(leaf, extent,
  2928. datao);
  2929. btrfs_set_file_extent_num_bytes(leaf, extent,
  2930. datal);
  2931. /*
  2932. * We need to look up the roots that point at
  2933. * this bytenr and see if the new root does. If
  2934. * it does not we need to make sure we update
  2935. * quotas appropriately.
  2936. */
  2937. if (disko && root != BTRFS_I(src)->root &&
  2938. disko != last_disko) {
  2939. no_quota = check_ref(trans, root,
  2940. disko);
  2941. if (no_quota < 0) {
  2942. btrfs_abort_transaction(trans,
  2943. root,
  2944. ret);
  2945. btrfs_end_transaction(trans,
  2946. root);
  2947. ret = no_quota;
  2948. goto out;
  2949. }
  2950. }
  2951. if (disko) {
  2952. inode_add_bytes(inode, datal);
  2953. ret = btrfs_inc_extent_ref(trans, root,
  2954. disko, diskl, 0,
  2955. root->root_key.objectid,
  2956. btrfs_ino(inode),
  2957. new_key.offset - datao,
  2958. no_quota);
  2959. if (ret) {
  2960. btrfs_abort_transaction(trans,
  2961. root,
  2962. ret);
  2963. btrfs_end_transaction(trans,
  2964. root);
  2965. goto out;
  2966. }
  2967. }
  2968. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2969. u64 skip = 0;
  2970. u64 trim = 0;
  2971. u64 aligned_end = 0;
  2972. if (off > key.offset) {
  2973. skip = off - key.offset;
  2974. new_key.offset += skip;
  2975. }
  2976. if (key.offset + datal > off + len)
  2977. trim = key.offset + datal - (off + len);
  2978. if (comp && (skip || trim)) {
  2979. ret = -EINVAL;
  2980. btrfs_end_transaction(trans, root);
  2981. goto out;
  2982. }
  2983. size -= skip + trim;
  2984. datal -= skip + trim;
  2985. aligned_end = ALIGN(new_key.offset + datal,
  2986. root->sectorsize);
  2987. ret = btrfs_drop_extents(trans, root, inode,
  2988. drop_start,
  2989. aligned_end,
  2990. 1);
  2991. if (ret) {
  2992. if (ret != -EOPNOTSUPP)
  2993. btrfs_abort_transaction(trans,
  2994. root, ret);
  2995. btrfs_end_transaction(trans, root);
  2996. goto out;
  2997. }
  2998. ret = btrfs_insert_empty_item(trans, root, path,
  2999. &new_key, size);
  3000. if (ret) {
  3001. btrfs_abort_transaction(trans, root,
  3002. ret);
  3003. btrfs_end_transaction(trans, root);
  3004. goto out;
  3005. }
  3006. if (skip) {
  3007. u32 start =
  3008. btrfs_file_extent_calc_inline_size(0);
  3009. memmove(buf+start, buf+start+skip,
  3010. datal);
  3011. }
  3012. leaf = path->nodes[0];
  3013. slot = path->slots[0];
  3014. write_extent_buffer(leaf, buf,
  3015. btrfs_item_ptr_offset(leaf, slot),
  3016. size);
  3017. inode_add_bytes(inode, datal);
  3018. }
  3019. /* If we have an implicit hole (NO_HOLES feature). */
  3020. if (drop_start < new_key.offset)
  3021. clone_update_extent_map(inode, trans,
  3022. NULL, drop_start,
  3023. new_key.offset - drop_start);
  3024. clone_update_extent_map(inode, trans, path, 0, 0);
  3025. btrfs_mark_buffer_dirty(leaf);
  3026. btrfs_release_path(path);
  3027. last_dest_end = ALIGN(new_key.offset + datal,
  3028. root->sectorsize);
  3029. ret = clone_finish_inode_update(trans, inode,
  3030. last_dest_end,
  3031. destoff, olen);
  3032. if (ret)
  3033. goto out;
  3034. if (new_key.offset + datal >= destoff + len)
  3035. break;
  3036. }
  3037. btrfs_release_path(path);
  3038. key.offset++;
  3039. }
  3040. ret = 0;
  3041. if (last_dest_end < destoff + len) {
  3042. /*
  3043. * We have an implicit hole (NO_HOLES feature is enabled) that
  3044. * fully or partially overlaps our cloning range at its end.
  3045. */
  3046. btrfs_release_path(path);
  3047. /*
  3048. * 1 - remove extent(s)
  3049. * 1 - inode update
  3050. */
  3051. trans = btrfs_start_transaction(root, 2);
  3052. if (IS_ERR(trans)) {
  3053. ret = PTR_ERR(trans);
  3054. goto out;
  3055. }
  3056. ret = btrfs_drop_extents(trans, root, inode,
  3057. last_dest_end, destoff + len, 1);
  3058. if (ret) {
  3059. if (ret != -EOPNOTSUPP)
  3060. btrfs_abort_transaction(trans, root, ret);
  3061. btrfs_end_transaction(trans, root);
  3062. goto out;
  3063. }
  3064. clone_update_extent_map(inode, trans, NULL, last_dest_end,
  3065. destoff + len - last_dest_end);
  3066. ret = clone_finish_inode_update(trans, inode, destoff + len,
  3067. destoff, olen);
  3068. }
  3069. out:
  3070. btrfs_free_path(path);
  3071. vfree(buf);
  3072. return ret;
  3073. }
  3074. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  3075. u64 off, u64 olen, u64 destoff)
  3076. {
  3077. struct inode *inode = file_inode(file);
  3078. struct btrfs_root *root = BTRFS_I(inode)->root;
  3079. struct fd src_file;
  3080. struct inode *src;
  3081. int ret;
  3082. u64 len = olen;
  3083. u64 bs = root->fs_info->sb->s_blocksize;
  3084. int same_inode = 0;
  3085. /*
  3086. * TODO:
  3087. * - split compressed inline extents. annoying: we need to
  3088. * decompress into destination's address_space (the file offset
  3089. * may change, so source mapping won't do), then recompress (or
  3090. * otherwise reinsert) a subrange.
  3091. *
  3092. * - split destination inode's inline extents. The inline extents can
  3093. * be either compressed or non-compressed.
  3094. */
  3095. /* the destination must be opened for writing */
  3096. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  3097. return -EINVAL;
  3098. if (btrfs_root_readonly(root))
  3099. return -EROFS;
  3100. ret = mnt_want_write_file(file);
  3101. if (ret)
  3102. return ret;
  3103. src_file = fdget(srcfd);
  3104. if (!src_file.file) {
  3105. ret = -EBADF;
  3106. goto out_drop_write;
  3107. }
  3108. ret = -EXDEV;
  3109. if (src_file.file->f_path.mnt != file->f_path.mnt)
  3110. goto out_fput;
  3111. src = file_inode(src_file.file);
  3112. ret = -EINVAL;
  3113. if (src == inode)
  3114. same_inode = 1;
  3115. /* the src must be open for reading */
  3116. if (!(src_file.file->f_mode & FMODE_READ))
  3117. goto out_fput;
  3118. /* don't make the dst file partly checksummed */
  3119. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3120. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  3121. goto out_fput;
  3122. ret = -EISDIR;
  3123. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  3124. goto out_fput;
  3125. ret = -EXDEV;
  3126. if (src->i_sb != inode->i_sb)
  3127. goto out_fput;
  3128. if (!same_inode) {
  3129. if (inode < src) {
  3130. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  3131. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  3132. } else {
  3133. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  3134. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  3135. }
  3136. } else {
  3137. mutex_lock(&src->i_mutex);
  3138. }
  3139. /* determine range to clone */
  3140. ret = -EINVAL;
  3141. if (off + len > src->i_size || off + len < off)
  3142. goto out_unlock;
  3143. if (len == 0)
  3144. olen = len = src->i_size - off;
  3145. /* if we extend to eof, continue to block boundary */
  3146. if (off + len == src->i_size)
  3147. len = ALIGN(src->i_size, bs) - off;
  3148. /* verify the end result is block aligned */
  3149. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  3150. !IS_ALIGNED(destoff, bs))
  3151. goto out_unlock;
  3152. /* verify if ranges are overlapped within the same file */
  3153. if (same_inode) {
  3154. if (destoff + len > off && destoff < off + len)
  3155. goto out_unlock;
  3156. }
  3157. if (destoff > inode->i_size) {
  3158. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  3159. if (ret)
  3160. goto out_unlock;
  3161. }
  3162. /*
  3163. * Lock the target range too. Right after we replace the file extent
  3164. * items in the fs tree (which now point to the cloned data), we might
  3165. * have a worker replace them with extent items relative to a write
  3166. * operation that was issued before this clone operation (i.e. confront
  3167. * with inode.c:btrfs_finish_ordered_io).
  3168. */
  3169. if (same_inode) {
  3170. u64 lock_start = min_t(u64, off, destoff);
  3171. u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
  3172. lock_extent_range(src, lock_start, lock_len);
  3173. } else {
  3174. lock_extent_range(src, off, len);
  3175. lock_extent_range(inode, destoff, len);
  3176. }
  3177. ret = btrfs_clone(src, inode, off, olen, len, destoff);
  3178. if (same_inode) {
  3179. u64 lock_start = min_t(u64, off, destoff);
  3180. u64 lock_end = max_t(u64, off, destoff) + len - 1;
  3181. unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
  3182. } else {
  3183. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  3184. unlock_extent(&BTRFS_I(inode)->io_tree, destoff,
  3185. destoff + len - 1);
  3186. }
  3187. /*
  3188. * Truncate page cache pages so that future reads will see the cloned
  3189. * data immediately and not the previous data.
  3190. */
  3191. truncate_inode_pages_range(&inode->i_data, destoff,
  3192. PAGE_CACHE_ALIGN(destoff + len) - 1);
  3193. out_unlock:
  3194. if (!same_inode) {
  3195. if (inode < src) {
  3196. mutex_unlock(&src->i_mutex);
  3197. mutex_unlock(&inode->i_mutex);
  3198. } else {
  3199. mutex_unlock(&inode->i_mutex);
  3200. mutex_unlock(&src->i_mutex);
  3201. }
  3202. } else {
  3203. mutex_unlock(&src->i_mutex);
  3204. }
  3205. out_fput:
  3206. fdput(src_file);
  3207. out_drop_write:
  3208. mnt_drop_write_file(file);
  3209. return ret;
  3210. }
  3211. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  3212. {
  3213. struct btrfs_ioctl_clone_range_args args;
  3214. if (copy_from_user(&args, argp, sizeof(args)))
  3215. return -EFAULT;
  3216. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  3217. args.src_length, args.dest_offset);
  3218. }
  3219. /*
  3220. * there are many ways the trans_start and trans_end ioctls can lead
  3221. * to deadlocks. They should only be used by applications that
  3222. * basically own the machine, and have a very in depth understanding
  3223. * of all the possible deadlocks and enospc problems.
  3224. */
  3225. static long btrfs_ioctl_trans_start(struct file *file)
  3226. {
  3227. struct inode *inode = file_inode(file);
  3228. struct btrfs_root *root = BTRFS_I(inode)->root;
  3229. struct btrfs_trans_handle *trans;
  3230. int ret;
  3231. ret = -EPERM;
  3232. if (!capable(CAP_SYS_ADMIN))
  3233. goto out;
  3234. ret = -EINPROGRESS;
  3235. if (file->private_data)
  3236. goto out;
  3237. ret = -EROFS;
  3238. if (btrfs_root_readonly(root))
  3239. goto out;
  3240. ret = mnt_want_write_file(file);
  3241. if (ret)
  3242. goto out;
  3243. atomic_inc(&root->fs_info->open_ioctl_trans);
  3244. ret = -ENOMEM;
  3245. trans = btrfs_start_ioctl_transaction(root);
  3246. if (IS_ERR(trans))
  3247. goto out_drop;
  3248. file->private_data = trans;
  3249. return 0;
  3250. out_drop:
  3251. atomic_dec(&root->fs_info->open_ioctl_trans);
  3252. mnt_drop_write_file(file);
  3253. out:
  3254. return ret;
  3255. }
  3256. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  3257. {
  3258. struct inode *inode = file_inode(file);
  3259. struct btrfs_root *root = BTRFS_I(inode)->root;
  3260. struct btrfs_root *new_root;
  3261. struct btrfs_dir_item *di;
  3262. struct btrfs_trans_handle *trans;
  3263. struct btrfs_path *path;
  3264. struct btrfs_key location;
  3265. struct btrfs_disk_key disk_key;
  3266. u64 objectid = 0;
  3267. u64 dir_id;
  3268. int ret;
  3269. if (!capable(CAP_SYS_ADMIN))
  3270. return -EPERM;
  3271. ret = mnt_want_write_file(file);
  3272. if (ret)
  3273. return ret;
  3274. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  3275. ret = -EFAULT;
  3276. goto out;
  3277. }
  3278. if (!objectid)
  3279. objectid = BTRFS_FS_TREE_OBJECTID;
  3280. location.objectid = objectid;
  3281. location.type = BTRFS_ROOT_ITEM_KEY;
  3282. location.offset = (u64)-1;
  3283. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  3284. if (IS_ERR(new_root)) {
  3285. ret = PTR_ERR(new_root);
  3286. goto out;
  3287. }
  3288. path = btrfs_alloc_path();
  3289. if (!path) {
  3290. ret = -ENOMEM;
  3291. goto out;
  3292. }
  3293. path->leave_spinning = 1;
  3294. trans = btrfs_start_transaction(root, 1);
  3295. if (IS_ERR(trans)) {
  3296. btrfs_free_path(path);
  3297. ret = PTR_ERR(trans);
  3298. goto out;
  3299. }
  3300. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  3301. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  3302. dir_id, "default", 7, 1);
  3303. if (IS_ERR_OR_NULL(di)) {
  3304. btrfs_free_path(path);
  3305. btrfs_end_transaction(trans, root);
  3306. btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
  3307. "item, this isn't going to work");
  3308. ret = -ENOENT;
  3309. goto out;
  3310. }
  3311. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  3312. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  3313. btrfs_mark_buffer_dirty(path->nodes[0]);
  3314. btrfs_free_path(path);
  3315. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  3316. btrfs_end_transaction(trans, root);
  3317. out:
  3318. mnt_drop_write_file(file);
  3319. return ret;
  3320. }
  3321. void btrfs_get_block_group_info(struct list_head *groups_list,
  3322. struct btrfs_ioctl_space_info *space)
  3323. {
  3324. struct btrfs_block_group_cache *block_group;
  3325. space->total_bytes = 0;
  3326. space->used_bytes = 0;
  3327. space->flags = 0;
  3328. list_for_each_entry(block_group, groups_list, list) {
  3329. space->flags = block_group->flags;
  3330. space->total_bytes += block_group->key.offset;
  3331. space->used_bytes +=
  3332. btrfs_block_group_used(&block_group->item);
  3333. }
  3334. }
  3335. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  3336. {
  3337. struct btrfs_ioctl_space_args space_args;
  3338. struct btrfs_ioctl_space_info space;
  3339. struct btrfs_ioctl_space_info *dest;
  3340. struct btrfs_ioctl_space_info *dest_orig;
  3341. struct btrfs_ioctl_space_info __user *user_dest;
  3342. struct btrfs_space_info *info;
  3343. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  3344. BTRFS_BLOCK_GROUP_SYSTEM,
  3345. BTRFS_BLOCK_GROUP_METADATA,
  3346. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  3347. int num_types = 4;
  3348. int alloc_size;
  3349. int ret = 0;
  3350. u64 slot_count = 0;
  3351. int i, c;
  3352. if (copy_from_user(&space_args,
  3353. (struct btrfs_ioctl_space_args __user *)arg,
  3354. sizeof(space_args)))
  3355. return -EFAULT;
  3356. for (i = 0; i < num_types; i++) {
  3357. struct btrfs_space_info *tmp;
  3358. info = NULL;
  3359. rcu_read_lock();
  3360. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3361. list) {
  3362. if (tmp->flags == types[i]) {
  3363. info = tmp;
  3364. break;
  3365. }
  3366. }
  3367. rcu_read_unlock();
  3368. if (!info)
  3369. continue;
  3370. down_read(&info->groups_sem);
  3371. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3372. if (!list_empty(&info->block_groups[c]))
  3373. slot_count++;
  3374. }
  3375. up_read(&info->groups_sem);
  3376. }
  3377. /*
  3378. * Global block reserve, exported as a space_info
  3379. */
  3380. slot_count++;
  3381. /* space_slots == 0 means they are asking for a count */
  3382. if (space_args.space_slots == 0) {
  3383. space_args.total_spaces = slot_count;
  3384. goto out;
  3385. }
  3386. slot_count = min_t(u64, space_args.space_slots, slot_count);
  3387. alloc_size = sizeof(*dest) * slot_count;
  3388. /* we generally have at most 6 or so space infos, one for each raid
  3389. * level. So, a whole page should be more than enough for everyone
  3390. */
  3391. if (alloc_size > PAGE_CACHE_SIZE)
  3392. return -ENOMEM;
  3393. space_args.total_spaces = 0;
  3394. dest = kmalloc(alloc_size, GFP_NOFS);
  3395. if (!dest)
  3396. return -ENOMEM;
  3397. dest_orig = dest;
  3398. /* now we have a buffer to copy into */
  3399. for (i = 0; i < num_types; i++) {
  3400. struct btrfs_space_info *tmp;
  3401. if (!slot_count)
  3402. break;
  3403. info = NULL;
  3404. rcu_read_lock();
  3405. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3406. list) {
  3407. if (tmp->flags == types[i]) {
  3408. info = tmp;
  3409. break;
  3410. }
  3411. }
  3412. rcu_read_unlock();
  3413. if (!info)
  3414. continue;
  3415. down_read(&info->groups_sem);
  3416. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3417. if (!list_empty(&info->block_groups[c])) {
  3418. btrfs_get_block_group_info(
  3419. &info->block_groups[c], &space);
  3420. memcpy(dest, &space, sizeof(space));
  3421. dest++;
  3422. space_args.total_spaces++;
  3423. slot_count--;
  3424. }
  3425. if (!slot_count)
  3426. break;
  3427. }
  3428. up_read(&info->groups_sem);
  3429. }
  3430. /*
  3431. * Add global block reserve
  3432. */
  3433. if (slot_count) {
  3434. struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
  3435. spin_lock(&block_rsv->lock);
  3436. space.total_bytes = block_rsv->size;
  3437. space.used_bytes = block_rsv->size - block_rsv->reserved;
  3438. spin_unlock(&block_rsv->lock);
  3439. space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
  3440. memcpy(dest, &space, sizeof(space));
  3441. space_args.total_spaces++;
  3442. }
  3443. user_dest = (struct btrfs_ioctl_space_info __user *)
  3444. (arg + sizeof(struct btrfs_ioctl_space_args));
  3445. if (copy_to_user(user_dest, dest_orig, alloc_size))
  3446. ret = -EFAULT;
  3447. kfree(dest_orig);
  3448. out:
  3449. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  3450. ret = -EFAULT;
  3451. return ret;
  3452. }
  3453. /*
  3454. * there are many ways the trans_start and trans_end ioctls can lead
  3455. * to deadlocks. They should only be used by applications that
  3456. * basically own the machine, and have a very in depth understanding
  3457. * of all the possible deadlocks and enospc problems.
  3458. */
  3459. long btrfs_ioctl_trans_end(struct file *file)
  3460. {
  3461. struct inode *inode = file_inode(file);
  3462. struct btrfs_root *root = BTRFS_I(inode)->root;
  3463. struct btrfs_trans_handle *trans;
  3464. trans = file->private_data;
  3465. if (!trans)
  3466. return -EINVAL;
  3467. file->private_data = NULL;
  3468. btrfs_end_transaction(trans, root);
  3469. atomic_dec(&root->fs_info->open_ioctl_trans);
  3470. mnt_drop_write_file(file);
  3471. return 0;
  3472. }
  3473. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  3474. void __user *argp)
  3475. {
  3476. struct btrfs_trans_handle *trans;
  3477. u64 transid;
  3478. int ret;
  3479. trans = btrfs_attach_transaction_barrier(root);
  3480. if (IS_ERR(trans)) {
  3481. if (PTR_ERR(trans) != -ENOENT)
  3482. return PTR_ERR(trans);
  3483. /* No running transaction, don't bother */
  3484. transid = root->fs_info->last_trans_committed;
  3485. goto out;
  3486. }
  3487. transid = trans->transid;
  3488. ret = btrfs_commit_transaction_async(trans, root, 0);
  3489. if (ret) {
  3490. btrfs_end_transaction(trans, root);
  3491. return ret;
  3492. }
  3493. out:
  3494. if (argp)
  3495. if (copy_to_user(argp, &transid, sizeof(transid)))
  3496. return -EFAULT;
  3497. return 0;
  3498. }
  3499. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  3500. void __user *argp)
  3501. {
  3502. u64 transid;
  3503. if (argp) {
  3504. if (copy_from_user(&transid, argp, sizeof(transid)))
  3505. return -EFAULT;
  3506. } else {
  3507. transid = 0; /* current trans */
  3508. }
  3509. return btrfs_wait_for_commit(root, transid);
  3510. }
  3511. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  3512. {
  3513. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3514. struct btrfs_ioctl_scrub_args *sa;
  3515. int ret;
  3516. if (!capable(CAP_SYS_ADMIN))
  3517. return -EPERM;
  3518. sa = memdup_user(arg, sizeof(*sa));
  3519. if (IS_ERR(sa))
  3520. return PTR_ERR(sa);
  3521. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  3522. ret = mnt_want_write_file(file);
  3523. if (ret)
  3524. goto out;
  3525. }
  3526. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  3527. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  3528. 0);
  3529. if (copy_to_user(arg, sa, sizeof(*sa)))
  3530. ret = -EFAULT;
  3531. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  3532. mnt_drop_write_file(file);
  3533. out:
  3534. kfree(sa);
  3535. return ret;
  3536. }
  3537. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  3538. {
  3539. if (!capable(CAP_SYS_ADMIN))
  3540. return -EPERM;
  3541. return btrfs_scrub_cancel(root->fs_info);
  3542. }
  3543. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  3544. void __user *arg)
  3545. {
  3546. struct btrfs_ioctl_scrub_args *sa;
  3547. int ret;
  3548. if (!capable(CAP_SYS_ADMIN))
  3549. return -EPERM;
  3550. sa = memdup_user(arg, sizeof(*sa));
  3551. if (IS_ERR(sa))
  3552. return PTR_ERR(sa);
  3553. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  3554. if (copy_to_user(arg, sa, sizeof(*sa)))
  3555. ret = -EFAULT;
  3556. kfree(sa);
  3557. return ret;
  3558. }
  3559. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  3560. void __user *arg)
  3561. {
  3562. struct btrfs_ioctl_get_dev_stats *sa;
  3563. int ret;
  3564. sa = memdup_user(arg, sizeof(*sa));
  3565. if (IS_ERR(sa))
  3566. return PTR_ERR(sa);
  3567. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  3568. kfree(sa);
  3569. return -EPERM;
  3570. }
  3571. ret = btrfs_get_dev_stats(root, sa);
  3572. if (copy_to_user(arg, sa, sizeof(*sa)))
  3573. ret = -EFAULT;
  3574. kfree(sa);
  3575. return ret;
  3576. }
  3577. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  3578. {
  3579. struct btrfs_ioctl_dev_replace_args *p;
  3580. int ret;
  3581. if (!capable(CAP_SYS_ADMIN))
  3582. return -EPERM;
  3583. p = memdup_user(arg, sizeof(*p));
  3584. if (IS_ERR(p))
  3585. return PTR_ERR(p);
  3586. switch (p->cmd) {
  3587. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  3588. if (root->fs_info->sb->s_flags & MS_RDONLY) {
  3589. ret = -EROFS;
  3590. goto out;
  3591. }
  3592. if (atomic_xchg(
  3593. &root->fs_info->mutually_exclusive_operation_running,
  3594. 1)) {
  3595. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3596. } else {
  3597. ret = btrfs_dev_replace_start(root, p);
  3598. atomic_set(
  3599. &root->fs_info->mutually_exclusive_operation_running,
  3600. 0);
  3601. }
  3602. break;
  3603. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  3604. btrfs_dev_replace_status(root->fs_info, p);
  3605. ret = 0;
  3606. break;
  3607. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  3608. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  3609. break;
  3610. default:
  3611. ret = -EINVAL;
  3612. break;
  3613. }
  3614. if (copy_to_user(arg, p, sizeof(*p)))
  3615. ret = -EFAULT;
  3616. out:
  3617. kfree(p);
  3618. return ret;
  3619. }
  3620. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  3621. {
  3622. int ret = 0;
  3623. int i;
  3624. u64 rel_ptr;
  3625. int size;
  3626. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  3627. struct inode_fs_paths *ipath = NULL;
  3628. struct btrfs_path *path;
  3629. if (!capable(CAP_DAC_READ_SEARCH))
  3630. return -EPERM;
  3631. path = btrfs_alloc_path();
  3632. if (!path) {
  3633. ret = -ENOMEM;
  3634. goto out;
  3635. }
  3636. ipa = memdup_user(arg, sizeof(*ipa));
  3637. if (IS_ERR(ipa)) {
  3638. ret = PTR_ERR(ipa);
  3639. ipa = NULL;
  3640. goto out;
  3641. }
  3642. size = min_t(u32, ipa->size, 4096);
  3643. ipath = init_ipath(size, root, path);
  3644. if (IS_ERR(ipath)) {
  3645. ret = PTR_ERR(ipath);
  3646. ipath = NULL;
  3647. goto out;
  3648. }
  3649. ret = paths_from_inode(ipa->inum, ipath);
  3650. if (ret < 0)
  3651. goto out;
  3652. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  3653. rel_ptr = ipath->fspath->val[i] -
  3654. (u64)(unsigned long)ipath->fspath->val;
  3655. ipath->fspath->val[i] = rel_ptr;
  3656. }
  3657. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  3658. (void *)(unsigned long)ipath->fspath, size);
  3659. if (ret) {
  3660. ret = -EFAULT;
  3661. goto out;
  3662. }
  3663. out:
  3664. btrfs_free_path(path);
  3665. free_ipath(ipath);
  3666. kfree(ipa);
  3667. return ret;
  3668. }
  3669. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  3670. {
  3671. struct btrfs_data_container *inodes = ctx;
  3672. const size_t c = 3 * sizeof(u64);
  3673. if (inodes->bytes_left >= c) {
  3674. inodes->bytes_left -= c;
  3675. inodes->val[inodes->elem_cnt] = inum;
  3676. inodes->val[inodes->elem_cnt + 1] = offset;
  3677. inodes->val[inodes->elem_cnt + 2] = root;
  3678. inodes->elem_cnt += 3;
  3679. } else {
  3680. inodes->bytes_missing += c - inodes->bytes_left;
  3681. inodes->bytes_left = 0;
  3682. inodes->elem_missed += 3;
  3683. }
  3684. return 0;
  3685. }
  3686. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  3687. void __user *arg)
  3688. {
  3689. int ret = 0;
  3690. int size;
  3691. struct btrfs_ioctl_logical_ino_args *loi;
  3692. struct btrfs_data_container *inodes = NULL;
  3693. struct btrfs_path *path = NULL;
  3694. if (!capable(CAP_SYS_ADMIN))
  3695. return -EPERM;
  3696. loi = memdup_user(arg, sizeof(*loi));
  3697. if (IS_ERR(loi)) {
  3698. ret = PTR_ERR(loi);
  3699. loi = NULL;
  3700. goto out;
  3701. }
  3702. path = btrfs_alloc_path();
  3703. if (!path) {
  3704. ret = -ENOMEM;
  3705. goto out;
  3706. }
  3707. size = min_t(u32, loi->size, 64 * 1024);
  3708. inodes = init_data_container(size);
  3709. if (IS_ERR(inodes)) {
  3710. ret = PTR_ERR(inodes);
  3711. inodes = NULL;
  3712. goto out;
  3713. }
  3714. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  3715. build_ino_list, inodes);
  3716. if (ret == -EINVAL)
  3717. ret = -ENOENT;
  3718. if (ret < 0)
  3719. goto out;
  3720. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  3721. (void *)(unsigned long)inodes, size);
  3722. if (ret)
  3723. ret = -EFAULT;
  3724. out:
  3725. btrfs_free_path(path);
  3726. vfree(inodes);
  3727. kfree(loi);
  3728. return ret;
  3729. }
  3730. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  3731. struct btrfs_ioctl_balance_args *bargs)
  3732. {
  3733. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3734. bargs->flags = bctl->flags;
  3735. if (atomic_read(&fs_info->balance_running))
  3736. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  3737. if (atomic_read(&fs_info->balance_pause_req))
  3738. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  3739. if (atomic_read(&fs_info->balance_cancel_req))
  3740. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  3741. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  3742. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  3743. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  3744. if (lock) {
  3745. spin_lock(&fs_info->balance_lock);
  3746. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3747. spin_unlock(&fs_info->balance_lock);
  3748. } else {
  3749. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3750. }
  3751. }
  3752. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  3753. {
  3754. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3755. struct btrfs_fs_info *fs_info = root->fs_info;
  3756. struct btrfs_ioctl_balance_args *bargs;
  3757. struct btrfs_balance_control *bctl;
  3758. bool need_unlock; /* for mut. excl. ops lock */
  3759. int ret;
  3760. if (!capable(CAP_SYS_ADMIN))
  3761. return -EPERM;
  3762. ret = mnt_want_write_file(file);
  3763. if (ret)
  3764. return ret;
  3765. again:
  3766. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  3767. mutex_lock(&fs_info->volume_mutex);
  3768. mutex_lock(&fs_info->balance_mutex);
  3769. need_unlock = true;
  3770. goto locked;
  3771. }
  3772. /*
  3773. * mut. excl. ops lock is locked. Three possibilites:
  3774. * (1) some other op is running
  3775. * (2) balance is running
  3776. * (3) balance is paused -- special case (think resume)
  3777. */
  3778. mutex_lock(&fs_info->balance_mutex);
  3779. if (fs_info->balance_ctl) {
  3780. /* this is either (2) or (3) */
  3781. if (!atomic_read(&fs_info->balance_running)) {
  3782. mutex_unlock(&fs_info->balance_mutex);
  3783. if (!mutex_trylock(&fs_info->volume_mutex))
  3784. goto again;
  3785. mutex_lock(&fs_info->balance_mutex);
  3786. if (fs_info->balance_ctl &&
  3787. !atomic_read(&fs_info->balance_running)) {
  3788. /* this is (3) */
  3789. need_unlock = false;
  3790. goto locked;
  3791. }
  3792. mutex_unlock(&fs_info->balance_mutex);
  3793. mutex_unlock(&fs_info->volume_mutex);
  3794. goto again;
  3795. } else {
  3796. /* this is (2) */
  3797. mutex_unlock(&fs_info->balance_mutex);
  3798. ret = -EINPROGRESS;
  3799. goto out;
  3800. }
  3801. } else {
  3802. /* this is (1) */
  3803. mutex_unlock(&fs_info->balance_mutex);
  3804. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3805. goto out;
  3806. }
  3807. locked:
  3808. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3809. if (arg) {
  3810. bargs = memdup_user(arg, sizeof(*bargs));
  3811. if (IS_ERR(bargs)) {
  3812. ret = PTR_ERR(bargs);
  3813. goto out_unlock;
  3814. }
  3815. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3816. if (!fs_info->balance_ctl) {
  3817. ret = -ENOTCONN;
  3818. goto out_bargs;
  3819. }
  3820. bctl = fs_info->balance_ctl;
  3821. spin_lock(&fs_info->balance_lock);
  3822. bctl->flags |= BTRFS_BALANCE_RESUME;
  3823. spin_unlock(&fs_info->balance_lock);
  3824. goto do_balance;
  3825. }
  3826. } else {
  3827. bargs = NULL;
  3828. }
  3829. if (fs_info->balance_ctl) {
  3830. ret = -EINPROGRESS;
  3831. goto out_bargs;
  3832. }
  3833. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3834. if (!bctl) {
  3835. ret = -ENOMEM;
  3836. goto out_bargs;
  3837. }
  3838. bctl->fs_info = fs_info;
  3839. if (arg) {
  3840. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3841. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3842. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3843. bctl->flags = bargs->flags;
  3844. } else {
  3845. /* balance everything - no filters */
  3846. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3847. }
  3848. do_balance:
  3849. /*
  3850. * Ownership of bctl and mutually_exclusive_operation_running
  3851. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3852. * or, if restriper was paused all the way until unmount, in
  3853. * free_fs_info. mutually_exclusive_operation_running is
  3854. * cleared in __cancel_balance.
  3855. */
  3856. need_unlock = false;
  3857. ret = btrfs_balance(bctl, bargs);
  3858. if (arg) {
  3859. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3860. ret = -EFAULT;
  3861. }
  3862. out_bargs:
  3863. kfree(bargs);
  3864. out_unlock:
  3865. mutex_unlock(&fs_info->balance_mutex);
  3866. mutex_unlock(&fs_info->volume_mutex);
  3867. if (need_unlock)
  3868. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3869. out:
  3870. mnt_drop_write_file(file);
  3871. return ret;
  3872. }
  3873. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3874. {
  3875. if (!capable(CAP_SYS_ADMIN))
  3876. return -EPERM;
  3877. switch (cmd) {
  3878. case BTRFS_BALANCE_CTL_PAUSE:
  3879. return btrfs_pause_balance(root->fs_info);
  3880. case BTRFS_BALANCE_CTL_CANCEL:
  3881. return btrfs_cancel_balance(root->fs_info);
  3882. }
  3883. return -EINVAL;
  3884. }
  3885. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3886. void __user *arg)
  3887. {
  3888. struct btrfs_fs_info *fs_info = root->fs_info;
  3889. struct btrfs_ioctl_balance_args *bargs;
  3890. int ret = 0;
  3891. if (!capable(CAP_SYS_ADMIN))
  3892. return -EPERM;
  3893. mutex_lock(&fs_info->balance_mutex);
  3894. if (!fs_info->balance_ctl) {
  3895. ret = -ENOTCONN;
  3896. goto out;
  3897. }
  3898. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3899. if (!bargs) {
  3900. ret = -ENOMEM;
  3901. goto out;
  3902. }
  3903. update_ioctl_balance_args(fs_info, 1, bargs);
  3904. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3905. ret = -EFAULT;
  3906. kfree(bargs);
  3907. out:
  3908. mutex_unlock(&fs_info->balance_mutex);
  3909. return ret;
  3910. }
  3911. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3912. {
  3913. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3914. struct btrfs_ioctl_quota_ctl_args *sa;
  3915. struct btrfs_trans_handle *trans = NULL;
  3916. int ret;
  3917. int err;
  3918. if (!capable(CAP_SYS_ADMIN))
  3919. return -EPERM;
  3920. ret = mnt_want_write_file(file);
  3921. if (ret)
  3922. return ret;
  3923. sa = memdup_user(arg, sizeof(*sa));
  3924. if (IS_ERR(sa)) {
  3925. ret = PTR_ERR(sa);
  3926. goto drop_write;
  3927. }
  3928. down_write(&root->fs_info->subvol_sem);
  3929. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  3930. if (IS_ERR(trans)) {
  3931. ret = PTR_ERR(trans);
  3932. goto out;
  3933. }
  3934. switch (sa->cmd) {
  3935. case BTRFS_QUOTA_CTL_ENABLE:
  3936. ret = btrfs_quota_enable(trans, root->fs_info);
  3937. break;
  3938. case BTRFS_QUOTA_CTL_DISABLE:
  3939. ret = btrfs_quota_disable(trans, root->fs_info);
  3940. break;
  3941. default:
  3942. ret = -EINVAL;
  3943. break;
  3944. }
  3945. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  3946. if (err && !ret)
  3947. ret = err;
  3948. out:
  3949. kfree(sa);
  3950. up_write(&root->fs_info->subvol_sem);
  3951. drop_write:
  3952. mnt_drop_write_file(file);
  3953. return ret;
  3954. }
  3955. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3956. {
  3957. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3958. struct btrfs_ioctl_qgroup_assign_args *sa;
  3959. struct btrfs_trans_handle *trans;
  3960. int ret;
  3961. int err;
  3962. if (!capable(CAP_SYS_ADMIN))
  3963. return -EPERM;
  3964. ret = mnt_want_write_file(file);
  3965. if (ret)
  3966. return ret;
  3967. sa = memdup_user(arg, sizeof(*sa));
  3968. if (IS_ERR(sa)) {
  3969. ret = PTR_ERR(sa);
  3970. goto drop_write;
  3971. }
  3972. trans = btrfs_join_transaction(root);
  3973. if (IS_ERR(trans)) {
  3974. ret = PTR_ERR(trans);
  3975. goto out;
  3976. }
  3977. /* FIXME: check if the IDs really exist */
  3978. if (sa->assign) {
  3979. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3980. sa->src, sa->dst);
  3981. } else {
  3982. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  3983. sa->src, sa->dst);
  3984. }
  3985. err = btrfs_end_transaction(trans, root);
  3986. if (err && !ret)
  3987. ret = err;
  3988. out:
  3989. kfree(sa);
  3990. drop_write:
  3991. mnt_drop_write_file(file);
  3992. return ret;
  3993. }
  3994. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3995. {
  3996. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3997. struct btrfs_ioctl_qgroup_create_args *sa;
  3998. struct btrfs_trans_handle *trans;
  3999. int ret;
  4000. int err;
  4001. if (!capable(CAP_SYS_ADMIN))
  4002. return -EPERM;
  4003. ret = mnt_want_write_file(file);
  4004. if (ret)
  4005. return ret;
  4006. sa = memdup_user(arg, sizeof(*sa));
  4007. if (IS_ERR(sa)) {
  4008. ret = PTR_ERR(sa);
  4009. goto drop_write;
  4010. }
  4011. if (!sa->qgroupid) {
  4012. ret = -EINVAL;
  4013. goto out;
  4014. }
  4015. trans = btrfs_join_transaction(root);
  4016. if (IS_ERR(trans)) {
  4017. ret = PTR_ERR(trans);
  4018. goto out;
  4019. }
  4020. /* FIXME: check if the IDs really exist */
  4021. if (sa->create) {
  4022. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  4023. NULL);
  4024. } else {
  4025. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  4026. }
  4027. err = btrfs_end_transaction(trans, root);
  4028. if (err && !ret)
  4029. ret = err;
  4030. out:
  4031. kfree(sa);
  4032. drop_write:
  4033. mnt_drop_write_file(file);
  4034. return ret;
  4035. }
  4036. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  4037. {
  4038. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4039. struct btrfs_ioctl_qgroup_limit_args *sa;
  4040. struct btrfs_trans_handle *trans;
  4041. int ret;
  4042. int err;
  4043. u64 qgroupid;
  4044. if (!capable(CAP_SYS_ADMIN))
  4045. return -EPERM;
  4046. ret = mnt_want_write_file(file);
  4047. if (ret)
  4048. return ret;
  4049. sa = memdup_user(arg, sizeof(*sa));
  4050. if (IS_ERR(sa)) {
  4051. ret = PTR_ERR(sa);
  4052. goto drop_write;
  4053. }
  4054. trans = btrfs_join_transaction(root);
  4055. if (IS_ERR(trans)) {
  4056. ret = PTR_ERR(trans);
  4057. goto out;
  4058. }
  4059. qgroupid = sa->qgroupid;
  4060. if (!qgroupid) {
  4061. /* take the current subvol as qgroup */
  4062. qgroupid = root->root_key.objectid;
  4063. }
  4064. /* FIXME: check if the IDs really exist */
  4065. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  4066. err = btrfs_end_transaction(trans, root);
  4067. if (err && !ret)
  4068. ret = err;
  4069. out:
  4070. kfree(sa);
  4071. drop_write:
  4072. mnt_drop_write_file(file);
  4073. return ret;
  4074. }
  4075. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  4076. {
  4077. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4078. struct btrfs_ioctl_quota_rescan_args *qsa;
  4079. int ret;
  4080. if (!capable(CAP_SYS_ADMIN))
  4081. return -EPERM;
  4082. ret = mnt_want_write_file(file);
  4083. if (ret)
  4084. return ret;
  4085. qsa = memdup_user(arg, sizeof(*qsa));
  4086. if (IS_ERR(qsa)) {
  4087. ret = PTR_ERR(qsa);
  4088. goto drop_write;
  4089. }
  4090. if (qsa->flags) {
  4091. ret = -EINVAL;
  4092. goto out;
  4093. }
  4094. ret = btrfs_qgroup_rescan(root->fs_info);
  4095. out:
  4096. kfree(qsa);
  4097. drop_write:
  4098. mnt_drop_write_file(file);
  4099. return ret;
  4100. }
  4101. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  4102. {
  4103. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4104. struct btrfs_ioctl_quota_rescan_args *qsa;
  4105. int ret = 0;
  4106. if (!capable(CAP_SYS_ADMIN))
  4107. return -EPERM;
  4108. qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
  4109. if (!qsa)
  4110. return -ENOMEM;
  4111. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  4112. qsa->flags = 1;
  4113. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  4114. }
  4115. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  4116. ret = -EFAULT;
  4117. kfree(qsa);
  4118. return ret;
  4119. }
  4120. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  4121. {
  4122. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4123. if (!capable(CAP_SYS_ADMIN))
  4124. return -EPERM;
  4125. return btrfs_qgroup_wait_for_completion(root->fs_info);
  4126. }
  4127. static long _btrfs_ioctl_set_received_subvol(struct file *file,
  4128. struct btrfs_ioctl_received_subvol_args *sa)
  4129. {
  4130. struct inode *inode = file_inode(file);
  4131. struct btrfs_root *root = BTRFS_I(inode)->root;
  4132. struct btrfs_root_item *root_item = &root->root_item;
  4133. struct btrfs_trans_handle *trans;
  4134. struct timespec ct = CURRENT_TIME;
  4135. int ret = 0;
  4136. int received_uuid_changed;
  4137. if (!inode_owner_or_capable(inode))
  4138. return -EPERM;
  4139. ret = mnt_want_write_file(file);
  4140. if (ret < 0)
  4141. return ret;
  4142. down_write(&root->fs_info->subvol_sem);
  4143. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  4144. ret = -EINVAL;
  4145. goto out;
  4146. }
  4147. if (btrfs_root_readonly(root)) {
  4148. ret = -EROFS;
  4149. goto out;
  4150. }
  4151. /*
  4152. * 1 - root item
  4153. * 2 - uuid items (received uuid + subvol uuid)
  4154. */
  4155. trans = btrfs_start_transaction(root, 3);
  4156. if (IS_ERR(trans)) {
  4157. ret = PTR_ERR(trans);
  4158. trans = NULL;
  4159. goto out;
  4160. }
  4161. sa->rtransid = trans->transid;
  4162. sa->rtime.sec = ct.tv_sec;
  4163. sa->rtime.nsec = ct.tv_nsec;
  4164. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  4165. BTRFS_UUID_SIZE);
  4166. if (received_uuid_changed &&
  4167. !btrfs_is_empty_uuid(root_item->received_uuid))
  4168. btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  4169. root_item->received_uuid,
  4170. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4171. root->root_key.objectid);
  4172. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  4173. btrfs_set_root_stransid(root_item, sa->stransid);
  4174. btrfs_set_root_rtransid(root_item, sa->rtransid);
  4175. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  4176. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  4177. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  4178. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  4179. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4180. &root->root_key, &root->root_item);
  4181. if (ret < 0) {
  4182. btrfs_end_transaction(trans, root);
  4183. goto out;
  4184. }
  4185. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  4186. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  4187. sa->uuid,
  4188. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4189. root->root_key.objectid);
  4190. if (ret < 0 && ret != -EEXIST) {
  4191. btrfs_abort_transaction(trans, root, ret);
  4192. goto out;
  4193. }
  4194. }
  4195. ret = btrfs_commit_transaction(trans, root);
  4196. if (ret < 0) {
  4197. btrfs_abort_transaction(trans, root, ret);
  4198. goto out;
  4199. }
  4200. out:
  4201. up_write(&root->fs_info->subvol_sem);
  4202. mnt_drop_write_file(file);
  4203. return ret;
  4204. }
  4205. #ifdef CONFIG_64BIT
  4206. static long btrfs_ioctl_set_received_subvol_32(struct file *file,
  4207. void __user *arg)
  4208. {
  4209. struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
  4210. struct btrfs_ioctl_received_subvol_args *args64 = NULL;
  4211. int ret = 0;
  4212. args32 = memdup_user(arg, sizeof(*args32));
  4213. if (IS_ERR(args32)) {
  4214. ret = PTR_ERR(args32);
  4215. args32 = NULL;
  4216. goto out;
  4217. }
  4218. args64 = kmalloc(sizeof(*args64), GFP_NOFS);
  4219. if (!args64) {
  4220. ret = -ENOMEM;
  4221. goto out;
  4222. }
  4223. memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
  4224. args64->stransid = args32->stransid;
  4225. args64->rtransid = args32->rtransid;
  4226. args64->stime.sec = args32->stime.sec;
  4227. args64->stime.nsec = args32->stime.nsec;
  4228. args64->rtime.sec = args32->rtime.sec;
  4229. args64->rtime.nsec = args32->rtime.nsec;
  4230. args64->flags = args32->flags;
  4231. ret = _btrfs_ioctl_set_received_subvol(file, args64);
  4232. if (ret)
  4233. goto out;
  4234. memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
  4235. args32->stransid = args64->stransid;
  4236. args32->rtransid = args64->rtransid;
  4237. args32->stime.sec = args64->stime.sec;
  4238. args32->stime.nsec = args64->stime.nsec;
  4239. args32->rtime.sec = args64->rtime.sec;
  4240. args32->rtime.nsec = args64->rtime.nsec;
  4241. args32->flags = args64->flags;
  4242. ret = copy_to_user(arg, args32, sizeof(*args32));
  4243. if (ret)
  4244. ret = -EFAULT;
  4245. out:
  4246. kfree(args32);
  4247. kfree(args64);
  4248. return ret;
  4249. }
  4250. #endif
  4251. static long btrfs_ioctl_set_received_subvol(struct file *file,
  4252. void __user *arg)
  4253. {
  4254. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  4255. int ret = 0;
  4256. sa = memdup_user(arg, sizeof(*sa));
  4257. if (IS_ERR(sa)) {
  4258. ret = PTR_ERR(sa);
  4259. sa = NULL;
  4260. goto out;
  4261. }
  4262. ret = _btrfs_ioctl_set_received_subvol(file, sa);
  4263. if (ret)
  4264. goto out;
  4265. ret = copy_to_user(arg, sa, sizeof(*sa));
  4266. if (ret)
  4267. ret = -EFAULT;
  4268. out:
  4269. kfree(sa);
  4270. return ret;
  4271. }
  4272. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  4273. {
  4274. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4275. size_t len;
  4276. int ret;
  4277. char label[BTRFS_LABEL_SIZE];
  4278. spin_lock(&root->fs_info->super_lock);
  4279. memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  4280. spin_unlock(&root->fs_info->super_lock);
  4281. len = strnlen(label, BTRFS_LABEL_SIZE);
  4282. if (len == BTRFS_LABEL_SIZE) {
  4283. btrfs_warn(root->fs_info,
  4284. "label is too long, return the first %zu bytes", --len);
  4285. }
  4286. ret = copy_to_user(arg, label, len);
  4287. return ret ? -EFAULT : 0;
  4288. }
  4289. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  4290. {
  4291. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4292. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4293. struct btrfs_trans_handle *trans;
  4294. char label[BTRFS_LABEL_SIZE];
  4295. int ret;
  4296. if (!capable(CAP_SYS_ADMIN))
  4297. return -EPERM;
  4298. if (copy_from_user(label, arg, sizeof(label)))
  4299. return -EFAULT;
  4300. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  4301. btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
  4302. BTRFS_LABEL_SIZE - 1);
  4303. return -EINVAL;
  4304. }
  4305. ret = mnt_want_write_file(file);
  4306. if (ret)
  4307. return ret;
  4308. trans = btrfs_start_transaction(root, 0);
  4309. if (IS_ERR(trans)) {
  4310. ret = PTR_ERR(trans);
  4311. goto out_unlock;
  4312. }
  4313. spin_lock(&root->fs_info->super_lock);
  4314. strcpy(super_block->label, label);
  4315. spin_unlock(&root->fs_info->super_lock);
  4316. ret = btrfs_commit_transaction(trans, root);
  4317. out_unlock:
  4318. mnt_drop_write_file(file);
  4319. return ret;
  4320. }
  4321. #define INIT_FEATURE_FLAGS(suffix) \
  4322. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  4323. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  4324. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  4325. static int btrfs_ioctl_get_supported_features(struct file *file,
  4326. void __user *arg)
  4327. {
  4328. static struct btrfs_ioctl_feature_flags features[3] = {
  4329. INIT_FEATURE_FLAGS(SUPP),
  4330. INIT_FEATURE_FLAGS(SAFE_SET),
  4331. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  4332. };
  4333. if (copy_to_user(arg, &features, sizeof(features)))
  4334. return -EFAULT;
  4335. return 0;
  4336. }
  4337. static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
  4338. {
  4339. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4340. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4341. struct btrfs_ioctl_feature_flags features;
  4342. features.compat_flags = btrfs_super_compat_flags(super_block);
  4343. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  4344. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  4345. if (copy_to_user(arg, &features, sizeof(features)))
  4346. return -EFAULT;
  4347. return 0;
  4348. }
  4349. static int check_feature_bits(struct btrfs_root *root,
  4350. enum btrfs_feature_set set,
  4351. u64 change_mask, u64 flags, u64 supported_flags,
  4352. u64 safe_set, u64 safe_clear)
  4353. {
  4354. const char *type = btrfs_feature_set_names[set];
  4355. char *names;
  4356. u64 disallowed, unsupported;
  4357. u64 set_mask = flags & change_mask;
  4358. u64 clear_mask = ~flags & change_mask;
  4359. unsupported = set_mask & ~supported_flags;
  4360. if (unsupported) {
  4361. names = btrfs_printable_features(set, unsupported);
  4362. if (names) {
  4363. btrfs_warn(root->fs_info,
  4364. "this kernel does not support the %s feature bit%s",
  4365. names, strchr(names, ',') ? "s" : "");
  4366. kfree(names);
  4367. } else
  4368. btrfs_warn(root->fs_info,
  4369. "this kernel does not support %s bits 0x%llx",
  4370. type, unsupported);
  4371. return -EOPNOTSUPP;
  4372. }
  4373. disallowed = set_mask & ~safe_set;
  4374. if (disallowed) {
  4375. names = btrfs_printable_features(set, disallowed);
  4376. if (names) {
  4377. btrfs_warn(root->fs_info,
  4378. "can't set the %s feature bit%s while mounted",
  4379. names, strchr(names, ',') ? "s" : "");
  4380. kfree(names);
  4381. } else
  4382. btrfs_warn(root->fs_info,
  4383. "can't set %s bits 0x%llx while mounted",
  4384. type, disallowed);
  4385. return -EPERM;
  4386. }
  4387. disallowed = clear_mask & ~safe_clear;
  4388. if (disallowed) {
  4389. names = btrfs_printable_features(set, disallowed);
  4390. if (names) {
  4391. btrfs_warn(root->fs_info,
  4392. "can't clear the %s feature bit%s while mounted",
  4393. names, strchr(names, ',') ? "s" : "");
  4394. kfree(names);
  4395. } else
  4396. btrfs_warn(root->fs_info,
  4397. "can't clear %s bits 0x%llx while mounted",
  4398. type, disallowed);
  4399. return -EPERM;
  4400. }
  4401. return 0;
  4402. }
  4403. #define check_feature(root, change_mask, flags, mask_base) \
  4404. check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
  4405. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  4406. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  4407. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  4408. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  4409. {
  4410. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4411. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4412. struct btrfs_ioctl_feature_flags flags[2];
  4413. struct btrfs_trans_handle *trans;
  4414. u64 newflags;
  4415. int ret;
  4416. if (!capable(CAP_SYS_ADMIN))
  4417. return -EPERM;
  4418. if (copy_from_user(flags, arg, sizeof(flags)))
  4419. return -EFAULT;
  4420. /* Nothing to do */
  4421. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  4422. !flags[0].incompat_flags)
  4423. return 0;
  4424. ret = check_feature(root, flags[0].compat_flags,
  4425. flags[1].compat_flags, COMPAT);
  4426. if (ret)
  4427. return ret;
  4428. ret = check_feature(root, flags[0].compat_ro_flags,
  4429. flags[1].compat_ro_flags, COMPAT_RO);
  4430. if (ret)
  4431. return ret;
  4432. ret = check_feature(root, flags[0].incompat_flags,
  4433. flags[1].incompat_flags, INCOMPAT);
  4434. if (ret)
  4435. return ret;
  4436. trans = btrfs_start_transaction(root, 0);
  4437. if (IS_ERR(trans))
  4438. return PTR_ERR(trans);
  4439. spin_lock(&root->fs_info->super_lock);
  4440. newflags = btrfs_super_compat_flags(super_block);
  4441. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  4442. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  4443. btrfs_set_super_compat_flags(super_block, newflags);
  4444. newflags = btrfs_super_compat_ro_flags(super_block);
  4445. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  4446. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  4447. btrfs_set_super_compat_ro_flags(super_block, newflags);
  4448. newflags = btrfs_super_incompat_flags(super_block);
  4449. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  4450. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  4451. btrfs_set_super_incompat_flags(super_block, newflags);
  4452. spin_unlock(&root->fs_info->super_lock);
  4453. return btrfs_commit_transaction(trans, root);
  4454. }
  4455. long btrfs_ioctl(struct file *file, unsigned int
  4456. cmd, unsigned long arg)
  4457. {
  4458. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4459. void __user *argp = (void __user *)arg;
  4460. switch (cmd) {
  4461. case FS_IOC_GETFLAGS:
  4462. return btrfs_ioctl_getflags(file, argp);
  4463. case FS_IOC_SETFLAGS:
  4464. return btrfs_ioctl_setflags(file, argp);
  4465. case FS_IOC_GETVERSION:
  4466. return btrfs_ioctl_getversion(file, argp);
  4467. case FITRIM:
  4468. return btrfs_ioctl_fitrim(file, argp);
  4469. case BTRFS_IOC_SNAP_CREATE:
  4470. return btrfs_ioctl_snap_create(file, argp, 0);
  4471. case BTRFS_IOC_SNAP_CREATE_V2:
  4472. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  4473. case BTRFS_IOC_SUBVOL_CREATE:
  4474. return btrfs_ioctl_snap_create(file, argp, 1);
  4475. case BTRFS_IOC_SUBVOL_CREATE_V2:
  4476. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  4477. case BTRFS_IOC_SNAP_DESTROY:
  4478. return btrfs_ioctl_snap_destroy(file, argp);
  4479. case BTRFS_IOC_SUBVOL_GETFLAGS:
  4480. return btrfs_ioctl_subvol_getflags(file, argp);
  4481. case BTRFS_IOC_SUBVOL_SETFLAGS:
  4482. return btrfs_ioctl_subvol_setflags(file, argp);
  4483. case BTRFS_IOC_DEFAULT_SUBVOL:
  4484. return btrfs_ioctl_default_subvol(file, argp);
  4485. case BTRFS_IOC_DEFRAG:
  4486. return btrfs_ioctl_defrag(file, NULL);
  4487. case BTRFS_IOC_DEFRAG_RANGE:
  4488. return btrfs_ioctl_defrag(file, argp);
  4489. case BTRFS_IOC_RESIZE:
  4490. return btrfs_ioctl_resize(file, argp);
  4491. case BTRFS_IOC_ADD_DEV:
  4492. return btrfs_ioctl_add_dev(root, argp);
  4493. case BTRFS_IOC_RM_DEV:
  4494. return btrfs_ioctl_rm_dev(file, argp);
  4495. case BTRFS_IOC_FS_INFO:
  4496. return btrfs_ioctl_fs_info(root, argp);
  4497. case BTRFS_IOC_DEV_INFO:
  4498. return btrfs_ioctl_dev_info(root, argp);
  4499. case BTRFS_IOC_BALANCE:
  4500. return btrfs_ioctl_balance(file, NULL);
  4501. case BTRFS_IOC_CLONE:
  4502. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  4503. case BTRFS_IOC_CLONE_RANGE:
  4504. return btrfs_ioctl_clone_range(file, argp);
  4505. case BTRFS_IOC_TRANS_START:
  4506. return btrfs_ioctl_trans_start(file);
  4507. case BTRFS_IOC_TRANS_END:
  4508. return btrfs_ioctl_trans_end(file);
  4509. case BTRFS_IOC_TREE_SEARCH:
  4510. return btrfs_ioctl_tree_search(file, argp);
  4511. case BTRFS_IOC_TREE_SEARCH_V2:
  4512. return btrfs_ioctl_tree_search_v2(file, argp);
  4513. case BTRFS_IOC_INO_LOOKUP:
  4514. return btrfs_ioctl_ino_lookup(file, argp);
  4515. case BTRFS_IOC_INO_PATHS:
  4516. return btrfs_ioctl_ino_to_path(root, argp);
  4517. case BTRFS_IOC_LOGICAL_INO:
  4518. return btrfs_ioctl_logical_to_ino(root, argp);
  4519. case BTRFS_IOC_SPACE_INFO:
  4520. return btrfs_ioctl_space_info(root, argp);
  4521. case BTRFS_IOC_SYNC: {
  4522. int ret;
  4523. ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
  4524. if (ret)
  4525. return ret;
  4526. ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
  4527. return ret;
  4528. }
  4529. case BTRFS_IOC_START_SYNC:
  4530. return btrfs_ioctl_start_sync(root, argp);
  4531. case BTRFS_IOC_WAIT_SYNC:
  4532. return btrfs_ioctl_wait_sync(root, argp);
  4533. case BTRFS_IOC_SCRUB:
  4534. return btrfs_ioctl_scrub(file, argp);
  4535. case BTRFS_IOC_SCRUB_CANCEL:
  4536. return btrfs_ioctl_scrub_cancel(root, argp);
  4537. case BTRFS_IOC_SCRUB_PROGRESS:
  4538. return btrfs_ioctl_scrub_progress(root, argp);
  4539. case BTRFS_IOC_BALANCE_V2:
  4540. return btrfs_ioctl_balance(file, argp);
  4541. case BTRFS_IOC_BALANCE_CTL:
  4542. return btrfs_ioctl_balance_ctl(root, arg);
  4543. case BTRFS_IOC_BALANCE_PROGRESS:
  4544. return btrfs_ioctl_balance_progress(root, argp);
  4545. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  4546. return btrfs_ioctl_set_received_subvol(file, argp);
  4547. #ifdef CONFIG_64BIT
  4548. case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
  4549. return btrfs_ioctl_set_received_subvol_32(file, argp);
  4550. #endif
  4551. case BTRFS_IOC_SEND:
  4552. return btrfs_ioctl_send(file, argp);
  4553. case BTRFS_IOC_GET_DEV_STATS:
  4554. return btrfs_ioctl_get_dev_stats(root, argp);
  4555. case BTRFS_IOC_QUOTA_CTL:
  4556. return btrfs_ioctl_quota_ctl(file, argp);
  4557. case BTRFS_IOC_QGROUP_ASSIGN:
  4558. return btrfs_ioctl_qgroup_assign(file, argp);
  4559. case BTRFS_IOC_QGROUP_CREATE:
  4560. return btrfs_ioctl_qgroup_create(file, argp);
  4561. case BTRFS_IOC_QGROUP_LIMIT:
  4562. return btrfs_ioctl_qgroup_limit(file, argp);
  4563. case BTRFS_IOC_QUOTA_RESCAN:
  4564. return btrfs_ioctl_quota_rescan(file, argp);
  4565. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  4566. return btrfs_ioctl_quota_rescan_status(file, argp);
  4567. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  4568. return btrfs_ioctl_quota_rescan_wait(file, argp);
  4569. case BTRFS_IOC_DEV_REPLACE:
  4570. return btrfs_ioctl_dev_replace(root, argp);
  4571. case BTRFS_IOC_GET_FSLABEL:
  4572. return btrfs_ioctl_get_fslabel(file, argp);
  4573. case BTRFS_IOC_SET_FSLABEL:
  4574. return btrfs_ioctl_set_fslabel(file, argp);
  4575. case BTRFS_IOC_FILE_EXTENT_SAME:
  4576. return btrfs_ioctl_file_extent_same(file, argp);
  4577. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  4578. return btrfs_ioctl_get_supported_features(file, argp);
  4579. case BTRFS_IOC_GET_FEATURES:
  4580. return btrfs_ioctl_get_features(file, argp);
  4581. case BTRFS_IOC_SET_FEATURES:
  4582. return btrfs_ioctl_set_features(file, argp);
  4583. }
  4584. return -ENOTTY;
  4585. }