ioctl.c 133 KB

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