ioctl.c 135 KB

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