ioctl.c 137 KB

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