inode.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316
  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/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/bit_spinlock.h>
  36. #include <linux/xattr.h>
  37. #include <linux/posix_acl.h>
  38. #include <linux/falloc.h>
  39. #include "compat.h"
  40. #include "ctree.h"
  41. #include "disk-io.h"
  42. #include "transaction.h"
  43. #include "btrfs_inode.h"
  44. #include "ioctl.h"
  45. #include "print-tree.h"
  46. #include "volumes.h"
  47. #include "ordered-data.h"
  48. #include "xattr.h"
  49. #include "tree-log.h"
  50. #include "compression.h"
  51. #include "locking.h"
  52. struct btrfs_iget_args {
  53. u64 ino;
  54. struct btrfs_root *root;
  55. };
  56. static struct inode_operations btrfs_dir_inode_operations;
  57. static struct inode_operations btrfs_symlink_inode_operations;
  58. static struct inode_operations btrfs_dir_ro_inode_operations;
  59. static struct inode_operations btrfs_special_inode_operations;
  60. static struct inode_operations btrfs_file_inode_operations;
  61. static struct address_space_operations btrfs_aops;
  62. static struct address_space_operations btrfs_symlink_aops;
  63. static struct file_operations btrfs_dir_file_operations;
  64. static struct extent_io_ops btrfs_extent_io_ops;
  65. static struct kmem_cache *btrfs_inode_cachep;
  66. struct kmem_cache *btrfs_trans_handle_cachep;
  67. struct kmem_cache *btrfs_transaction_cachep;
  68. struct kmem_cache *btrfs_path_cachep;
  69. #define S_SHIFT 12
  70. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  71. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  72. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  73. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  74. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  75. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  76. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  77. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  78. };
  79. static void btrfs_truncate(struct inode *inode);
  80. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  81. static noinline int cow_file_range(struct inode *inode,
  82. struct page *locked_page,
  83. u64 start, u64 end, int *page_started,
  84. unsigned long *nr_written, int unlock);
  85. static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
  86. {
  87. int err;
  88. err = btrfs_init_acl(inode, dir);
  89. if (!err)
  90. err = btrfs_xattr_security_init(inode, dir);
  91. return err;
  92. }
  93. /*
  94. * this does all the hard work for inserting an inline extent into
  95. * the btree. The caller should have done a btrfs_drop_extents so that
  96. * no overlapping inline items exist in the btree
  97. */
  98. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root, struct inode *inode,
  100. u64 start, size_t size, size_t compressed_size,
  101. struct page **compressed_pages)
  102. {
  103. struct btrfs_key key;
  104. struct btrfs_path *path;
  105. struct extent_buffer *leaf;
  106. struct page *page = NULL;
  107. char *kaddr;
  108. unsigned long ptr;
  109. struct btrfs_file_extent_item *ei;
  110. int err = 0;
  111. int ret;
  112. size_t cur_size = size;
  113. size_t datasize;
  114. unsigned long offset;
  115. int use_compress = 0;
  116. if (compressed_size && compressed_pages) {
  117. use_compress = 1;
  118. cur_size = compressed_size;
  119. }
  120. path = btrfs_alloc_path();
  121. if (!path)
  122. return -ENOMEM;
  123. path->leave_spinning = 1;
  124. btrfs_set_trans_block_group(trans, inode);
  125. key.objectid = inode->i_ino;
  126. key.offset = start;
  127. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  128. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  129. inode_add_bytes(inode, size);
  130. ret = btrfs_insert_empty_item(trans, root, path, &key,
  131. datasize);
  132. BUG_ON(ret);
  133. if (ret) {
  134. err = ret;
  135. goto fail;
  136. }
  137. leaf = path->nodes[0];
  138. ei = btrfs_item_ptr(leaf, path->slots[0],
  139. struct btrfs_file_extent_item);
  140. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  141. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  142. btrfs_set_file_extent_encryption(leaf, ei, 0);
  143. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  144. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  145. ptr = btrfs_file_extent_inline_start(ei);
  146. if (use_compress) {
  147. struct page *cpage;
  148. int i = 0;
  149. while (compressed_size > 0) {
  150. cpage = compressed_pages[i];
  151. cur_size = min_t(unsigned long, compressed_size,
  152. PAGE_CACHE_SIZE);
  153. kaddr = kmap_atomic(cpage, KM_USER0);
  154. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  155. kunmap_atomic(kaddr, KM_USER0);
  156. i++;
  157. ptr += cur_size;
  158. compressed_size -= cur_size;
  159. }
  160. btrfs_set_file_extent_compression(leaf, ei,
  161. BTRFS_COMPRESS_ZLIB);
  162. } else {
  163. page = find_get_page(inode->i_mapping,
  164. start >> PAGE_CACHE_SHIFT);
  165. btrfs_set_file_extent_compression(leaf, ei, 0);
  166. kaddr = kmap_atomic(page, KM_USER0);
  167. offset = start & (PAGE_CACHE_SIZE - 1);
  168. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  169. kunmap_atomic(kaddr, KM_USER0);
  170. page_cache_release(page);
  171. }
  172. btrfs_mark_buffer_dirty(leaf);
  173. btrfs_free_path(path);
  174. BTRFS_I(inode)->disk_i_size = inode->i_size;
  175. btrfs_update_inode(trans, root, inode);
  176. return 0;
  177. fail:
  178. btrfs_free_path(path);
  179. return err;
  180. }
  181. /*
  182. * conditionally insert an inline extent into the file. This
  183. * does the checks required to make sure the data is small enough
  184. * to fit as an inline extent.
  185. */
  186. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  187. struct btrfs_root *root,
  188. struct inode *inode, u64 start, u64 end,
  189. size_t compressed_size,
  190. struct page **compressed_pages)
  191. {
  192. u64 isize = i_size_read(inode);
  193. u64 actual_end = min(end + 1, isize);
  194. u64 inline_len = actual_end - start;
  195. u64 aligned_end = (end + root->sectorsize - 1) &
  196. ~((u64)root->sectorsize - 1);
  197. u64 hint_byte;
  198. u64 data_len = inline_len;
  199. int ret;
  200. if (compressed_size)
  201. data_len = compressed_size;
  202. if (start > 0 ||
  203. actual_end >= PAGE_CACHE_SIZE ||
  204. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  205. (!compressed_size &&
  206. (actual_end & (root->sectorsize - 1)) == 0) ||
  207. end + 1 < isize ||
  208. data_len > root->fs_info->max_inline) {
  209. return 1;
  210. }
  211. ret = btrfs_drop_extents(trans, root, inode, start,
  212. aligned_end, aligned_end, start, &hint_byte);
  213. BUG_ON(ret);
  214. if (isize > actual_end)
  215. inline_len = min_t(u64, isize, actual_end);
  216. ret = insert_inline_extent(trans, root, inode, start,
  217. inline_len, compressed_size,
  218. compressed_pages);
  219. BUG_ON(ret);
  220. btrfs_drop_extent_cache(inode, start, aligned_end, 0);
  221. return 0;
  222. }
  223. struct async_extent {
  224. u64 start;
  225. u64 ram_size;
  226. u64 compressed_size;
  227. struct page **pages;
  228. unsigned long nr_pages;
  229. struct list_head list;
  230. };
  231. struct async_cow {
  232. struct inode *inode;
  233. struct btrfs_root *root;
  234. struct page *locked_page;
  235. u64 start;
  236. u64 end;
  237. struct list_head extents;
  238. struct btrfs_work work;
  239. };
  240. static noinline int add_async_extent(struct async_cow *cow,
  241. u64 start, u64 ram_size,
  242. u64 compressed_size,
  243. struct page **pages,
  244. unsigned long nr_pages)
  245. {
  246. struct async_extent *async_extent;
  247. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  248. async_extent->start = start;
  249. async_extent->ram_size = ram_size;
  250. async_extent->compressed_size = compressed_size;
  251. async_extent->pages = pages;
  252. async_extent->nr_pages = nr_pages;
  253. list_add_tail(&async_extent->list, &cow->extents);
  254. return 0;
  255. }
  256. /*
  257. * we create compressed extents in two phases. The first
  258. * phase compresses a range of pages that have already been
  259. * locked (both pages and state bits are locked).
  260. *
  261. * This is done inside an ordered work queue, and the compression
  262. * is spread across many cpus. The actual IO submission is step
  263. * two, and the ordered work queue takes care of making sure that
  264. * happens in the same order things were put onto the queue by
  265. * writepages and friends.
  266. *
  267. * If this code finds it can't get good compression, it puts an
  268. * entry onto the work queue to write the uncompressed bytes. This
  269. * makes sure that both compressed inodes and uncompressed inodes
  270. * are written in the same order that pdflush sent them down.
  271. */
  272. static noinline int compress_file_range(struct inode *inode,
  273. struct page *locked_page,
  274. u64 start, u64 end,
  275. struct async_cow *async_cow,
  276. int *num_added)
  277. {
  278. struct btrfs_root *root = BTRFS_I(inode)->root;
  279. struct btrfs_trans_handle *trans;
  280. u64 num_bytes;
  281. u64 orig_start;
  282. u64 disk_num_bytes;
  283. u64 blocksize = root->sectorsize;
  284. u64 actual_end;
  285. u64 isize = i_size_read(inode);
  286. int ret = 0;
  287. struct page **pages = NULL;
  288. unsigned long nr_pages;
  289. unsigned long nr_pages_ret = 0;
  290. unsigned long total_compressed = 0;
  291. unsigned long total_in = 0;
  292. unsigned long max_compressed = 128 * 1024;
  293. unsigned long max_uncompressed = 128 * 1024;
  294. int i;
  295. int will_compress;
  296. orig_start = start;
  297. actual_end = min_t(u64, isize, end + 1);
  298. again:
  299. will_compress = 0;
  300. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  301. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  302. /*
  303. * we don't want to send crud past the end of i_size through
  304. * compression, that's just a waste of CPU time. So, if the
  305. * end of the file is before the start of our current
  306. * requested range of bytes, we bail out to the uncompressed
  307. * cleanup code that can deal with all of this.
  308. *
  309. * It isn't really the fastest way to fix things, but this is a
  310. * very uncommon corner.
  311. */
  312. if (actual_end <= start)
  313. goto cleanup_and_bail_uncompressed;
  314. total_compressed = actual_end - start;
  315. /* we want to make sure that amount of ram required to uncompress
  316. * an extent is reasonable, so we limit the total size in ram
  317. * of a compressed extent to 128k. This is a crucial number
  318. * because it also controls how easily we can spread reads across
  319. * cpus for decompression.
  320. *
  321. * We also want to make sure the amount of IO required to do
  322. * a random read is reasonably small, so we limit the size of
  323. * a compressed extent to 128k.
  324. */
  325. total_compressed = min(total_compressed, max_uncompressed);
  326. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  327. num_bytes = max(blocksize, num_bytes);
  328. disk_num_bytes = num_bytes;
  329. total_in = 0;
  330. ret = 0;
  331. /*
  332. * we do compression for mount -o compress and when the
  333. * inode has not been flagged as nocompress. This flag can
  334. * change at any time if we discover bad compression ratios.
  335. */
  336. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  337. btrfs_test_opt(root, COMPRESS)) {
  338. WARN_ON(pages);
  339. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  340. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  341. total_compressed, pages,
  342. nr_pages, &nr_pages_ret,
  343. &total_in,
  344. &total_compressed,
  345. max_compressed);
  346. if (!ret) {
  347. unsigned long offset = total_compressed &
  348. (PAGE_CACHE_SIZE - 1);
  349. struct page *page = pages[nr_pages_ret - 1];
  350. char *kaddr;
  351. /* zero the tail end of the last page, we might be
  352. * sending it down to disk
  353. */
  354. if (offset) {
  355. kaddr = kmap_atomic(page, KM_USER0);
  356. memset(kaddr + offset, 0,
  357. PAGE_CACHE_SIZE - offset);
  358. kunmap_atomic(kaddr, KM_USER0);
  359. }
  360. will_compress = 1;
  361. }
  362. }
  363. if (start == 0) {
  364. trans = btrfs_join_transaction(root, 1);
  365. BUG_ON(!trans);
  366. btrfs_set_trans_block_group(trans, inode);
  367. /* lets try to make an inline extent */
  368. if (ret || total_in < (actual_end - start)) {
  369. /* we didn't compress the entire range, try
  370. * to make an uncompressed inline extent.
  371. */
  372. ret = cow_file_range_inline(trans, root, inode,
  373. start, end, 0, NULL);
  374. } else {
  375. /* try making a compressed inline extent */
  376. ret = cow_file_range_inline(trans, root, inode,
  377. start, end,
  378. total_compressed, pages);
  379. }
  380. btrfs_end_transaction(trans, root);
  381. if (ret == 0) {
  382. /*
  383. * inline extent creation worked, we don't need
  384. * to create any more async work items. Unlock
  385. * and free up our temp pages.
  386. */
  387. extent_clear_unlock_delalloc(inode,
  388. &BTRFS_I(inode)->io_tree,
  389. start, end, NULL, 1, 0,
  390. 0, 1, 1, 1);
  391. ret = 0;
  392. goto free_pages_out;
  393. }
  394. }
  395. if (will_compress) {
  396. /*
  397. * we aren't doing an inline extent round the compressed size
  398. * up to a block size boundary so the allocator does sane
  399. * things
  400. */
  401. total_compressed = (total_compressed + blocksize - 1) &
  402. ~(blocksize - 1);
  403. /*
  404. * one last check to make sure the compression is really a
  405. * win, compare the page count read with the blocks on disk
  406. */
  407. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  408. ~(PAGE_CACHE_SIZE - 1);
  409. if (total_compressed >= total_in) {
  410. will_compress = 0;
  411. } else {
  412. disk_num_bytes = total_compressed;
  413. num_bytes = total_in;
  414. }
  415. }
  416. if (!will_compress && pages) {
  417. /*
  418. * the compression code ran but failed to make things smaller,
  419. * free any pages it allocated and our page pointer array
  420. */
  421. for (i = 0; i < nr_pages_ret; i++) {
  422. WARN_ON(pages[i]->mapping);
  423. page_cache_release(pages[i]);
  424. }
  425. kfree(pages);
  426. pages = NULL;
  427. total_compressed = 0;
  428. nr_pages_ret = 0;
  429. /* flag the file so we don't compress in the future */
  430. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  431. }
  432. if (will_compress) {
  433. *num_added += 1;
  434. /* the async work queues will take care of doing actual
  435. * allocation on disk for these compressed pages,
  436. * and will submit them to the elevator.
  437. */
  438. add_async_extent(async_cow, start, num_bytes,
  439. total_compressed, pages, nr_pages_ret);
  440. if (start + num_bytes < end && start + num_bytes < actual_end) {
  441. start += num_bytes;
  442. pages = NULL;
  443. cond_resched();
  444. goto again;
  445. }
  446. } else {
  447. cleanup_and_bail_uncompressed:
  448. /*
  449. * No compression, but we still need to write the pages in
  450. * the file we've been given so far. redirty the locked
  451. * page if it corresponds to our extent and set things up
  452. * for the async work queue to run cow_file_range to do
  453. * the normal delalloc dance
  454. */
  455. if (page_offset(locked_page) >= start &&
  456. page_offset(locked_page) <= end) {
  457. __set_page_dirty_nobuffers(locked_page);
  458. /* unlocked later on in the async handlers */
  459. }
  460. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  461. *num_added += 1;
  462. }
  463. out:
  464. return 0;
  465. free_pages_out:
  466. for (i = 0; i < nr_pages_ret; i++) {
  467. WARN_ON(pages[i]->mapping);
  468. page_cache_release(pages[i]);
  469. }
  470. kfree(pages);
  471. goto out;
  472. }
  473. /*
  474. * phase two of compressed writeback. This is the ordered portion
  475. * of the code, which only gets called in the order the work was
  476. * queued. We walk all the async extents created by compress_file_range
  477. * and send them down to the disk.
  478. */
  479. static noinline int submit_compressed_extents(struct inode *inode,
  480. struct async_cow *async_cow)
  481. {
  482. struct async_extent *async_extent;
  483. u64 alloc_hint = 0;
  484. struct btrfs_trans_handle *trans;
  485. struct btrfs_key ins;
  486. struct extent_map *em;
  487. struct btrfs_root *root = BTRFS_I(inode)->root;
  488. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  489. struct extent_io_tree *io_tree;
  490. int ret;
  491. if (list_empty(&async_cow->extents))
  492. return 0;
  493. trans = btrfs_join_transaction(root, 1);
  494. while (!list_empty(&async_cow->extents)) {
  495. async_extent = list_entry(async_cow->extents.next,
  496. struct async_extent, list);
  497. list_del(&async_extent->list);
  498. io_tree = &BTRFS_I(inode)->io_tree;
  499. /* did the compression code fall back to uncompressed IO? */
  500. if (!async_extent->pages) {
  501. int page_started = 0;
  502. unsigned long nr_written = 0;
  503. lock_extent(io_tree, async_extent->start,
  504. async_extent->start +
  505. async_extent->ram_size - 1, GFP_NOFS);
  506. /* allocate blocks */
  507. cow_file_range(inode, async_cow->locked_page,
  508. async_extent->start,
  509. async_extent->start +
  510. async_extent->ram_size - 1,
  511. &page_started, &nr_written, 0);
  512. /*
  513. * if page_started, cow_file_range inserted an
  514. * inline extent and took care of all the unlocking
  515. * and IO for us. Otherwise, we need to submit
  516. * all those pages down to the drive.
  517. */
  518. if (!page_started)
  519. extent_write_locked_range(io_tree,
  520. inode, async_extent->start,
  521. async_extent->start +
  522. async_extent->ram_size - 1,
  523. btrfs_get_extent,
  524. WB_SYNC_ALL);
  525. kfree(async_extent);
  526. cond_resched();
  527. continue;
  528. }
  529. lock_extent(io_tree, async_extent->start,
  530. async_extent->start + async_extent->ram_size - 1,
  531. GFP_NOFS);
  532. /*
  533. * here we're doing allocation and writeback of the
  534. * compressed pages
  535. */
  536. btrfs_drop_extent_cache(inode, async_extent->start,
  537. async_extent->start +
  538. async_extent->ram_size - 1, 0);
  539. ret = btrfs_reserve_extent(trans, root,
  540. async_extent->compressed_size,
  541. async_extent->compressed_size,
  542. 0, alloc_hint,
  543. (u64)-1, &ins, 1);
  544. BUG_ON(ret);
  545. em = alloc_extent_map(GFP_NOFS);
  546. em->start = async_extent->start;
  547. em->len = async_extent->ram_size;
  548. em->orig_start = em->start;
  549. em->block_start = ins.objectid;
  550. em->block_len = ins.offset;
  551. em->bdev = root->fs_info->fs_devices->latest_bdev;
  552. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  553. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  554. while (1) {
  555. spin_lock(&em_tree->lock);
  556. ret = add_extent_mapping(em_tree, em);
  557. spin_unlock(&em_tree->lock);
  558. if (ret != -EEXIST) {
  559. free_extent_map(em);
  560. break;
  561. }
  562. btrfs_drop_extent_cache(inode, async_extent->start,
  563. async_extent->start +
  564. async_extent->ram_size - 1, 0);
  565. }
  566. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  567. ins.objectid,
  568. async_extent->ram_size,
  569. ins.offset,
  570. BTRFS_ORDERED_COMPRESSED);
  571. BUG_ON(ret);
  572. btrfs_end_transaction(trans, root);
  573. /*
  574. * clear dirty, set writeback and unlock the pages.
  575. */
  576. extent_clear_unlock_delalloc(inode,
  577. &BTRFS_I(inode)->io_tree,
  578. async_extent->start,
  579. async_extent->start +
  580. async_extent->ram_size - 1,
  581. NULL, 1, 1, 0, 1, 1, 0);
  582. ret = btrfs_submit_compressed_write(inode,
  583. async_extent->start,
  584. async_extent->ram_size,
  585. ins.objectid,
  586. ins.offset, async_extent->pages,
  587. async_extent->nr_pages);
  588. BUG_ON(ret);
  589. trans = btrfs_join_transaction(root, 1);
  590. alloc_hint = ins.objectid + ins.offset;
  591. kfree(async_extent);
  592. cond_resched();
  593. }
  594. btrfs_end_transaction(trans, root);
  595. return 0;
  596. }
  597. /*
  598. * when extent_io.c finds a delayed allocation range in the file,
  599. * the call backs end up in this code. The basic idea is to
  600. * allocate extents on disk for the range, and create ordered data structs
  601. * in ram to track those extents.
  602. *
  603. * locked_page is the page that writepage had locked already. We use
  604. * it to make sure we don't do extra locks or unlocks.
  605. *
  606. * *page_started is set to one if we unlock locked_page and do everything
  607. * required to start IO on it. It may be clean and already done with
  608. * IO when we return.
  609. */
  610. static noinline int cow_file_range(struct inode *inode,
  611. struct page *locked_page,
  612. u64 start, u64 end, int *page_started,
  613. unsigned long *nr_written,
  614. int unlock)
  615. {
  616. struct btrfs_root *root = BTRFS_I(inode)->root;
  617. struct btrfs_trans_handle *trans;
  618. u64 alloc_hint = 0;
  619. u64 num_bytes;
  620. unsigned long ram_size;
  621. u64 disk_num_bytes;
  622. u64 cur_alloc_size;
  623. u64 blocksize = root->sectorsize;
  624. u64 actual_end;
  625. u64 isize = i_size_read(inode);
  626. struct btrfs_key ins;
  627. struct extent_map *em;
  628. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  629. int ret = 0;
  630. trans = btrfs_join_transaction(root, 1);
  631. BUG_ON(!trans);
  632. btrfs_set_trans_block_group(trans, inode);
  633. actual_end = min_t(u64, isize, end + 1);
  634. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  635. num_bytes = max(blocksize, num_bytes);
  636. disk_num_bytes = num_bytes;
  637. ret = 0;
  638. if (start == 0) {
  639. /* lets try to make an inline extent */
  640. ret = cow_file_range_inline(trans, root, inode,
  641. start, end, 0, NULL);
  642. if (ret == 0) {
  643. extent_clear_unlock_delalloc(inode,
  644. &BTRFS_I(inode)->io_tree,
  645. start, end, NULL, 1, 1,
  646. 1, 1, 1, 1);
  647. *nr_written = *nr_written +
  648. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  649. *page_started = 1;
  650. ret = 0;
  651. goto out;
  652. }
  653. }
  654. BUG_ON(disk_num_bytes >
  655. btrfs_super_total_bytes(&root->fs_info->super_copy));
  656. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  657. while (disk_num_bytes > 0) {
  658. cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
  659. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  660. root->sectorsize, 0, alloc_hint,
  661. (u64)-1, &ins, 1);
  662. BUG_ON(ret);
  663. em = alloc_extent_map(GFP_NOFS);
  664. em->start = start;
  665. em->orig_start = em->start;
  666. ram_size = ins.offset;
  667. em->len = ins.offset;
  668. em->block_start = ins.objectid;
  669. em->block_len = ins.offset;
  670. em->bdev = root->fs_info->fs_devices->latest_bdev;
  671. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  672. while (1) {
  673. spin_lock(&em_tree->lock);
  674. ret = add_extent_mapping(em_tree, em);
  675. spin_unlock(&em_tree->lock);
  676. if (ret != -EEXIST) {
  677. free_extent_map(em);
  678. break;
  679. }
  680. btrfs_drop_extent_cache(inode, start,
  681. start + ram_size - 1, 0);
  682. }
  683. cur_alloc_size = ins.offset;
  684. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  685. ram_size, cur_alloc_size, 0);
  686. BUG_ON(ret);
  687. if (root->root_key.objectid ==
  688. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  689. ret = btrfs_reloc_clone_csums(inode, start,
  690. cur_alloc_size);
  691. BUG_ON(ret);
  692. }
  693. if (disk_num_bytes < cur_alloc_size)
  694. break;
  695. /* we're not doing compressed IO, don't unlock the first
  696. * page (which the caller expects to stay locked), don't
  697. * clear any dirty bits and don't set any writeback bits
  698. */
  699. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  700. start, start + ram_size - 1,
  701. locked_page, unlock, 1,
  702. 1, 0, 0, 0);
  703. disk_num_bytes -= cur_alloc_size;
  704. num_bytes -= cur_alloc_size;
  705. alloc_hint = ins.objectid + ins.offset;
  706. start += cur_alloc_size;
  707. }
  708. out:
  709. ret = 0;
  710. btrfs_end_transaction(trans, root);
  711. return ret;
  712. }
  713. /*
  714. * work queue call back to started compression on a file and pages
  715. */
  716. static noinline void async_cow_start(struct btrfs_work *work)
  717. {
  718. struct async_cow *async_cow;
  719. int num_added = 0;
  720. async_cow = container_of(work, struct async_cow, work);
  721. compress_file_range(async_cow->inode, async_cow->locked_page,
  722. async_cow->start, async_cow->end, async_cow,
  723. &num_added);
  724. if (num_added == 0)
  725. async_cow->inode = NULL;
  726. }
  727. /*
  728. * work queue call back to submit previously compressed pages
  729. */
  730. static noinline void async_cow_submit(struct btrfs_work *work)
  731. {
  732. struct async_cow *async_cow;
  733. struct btrfs_root *root;
  734. unsigned long nr_pages;
  735. async_cow = container_of(work, struct async_cow, work);
  736. root = async_cow->root;
  737. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  738. PAGE_CACHE_SHIFT;
  739. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  740. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  741. 5 * 1042 * 1024 &&
  742. waitqueue_active(&root->fs_info->async_submit_wait))
  743. wake_up(&root->fs_info->async_submit_wait);
  744. if (async_cow->inode)
  745. submit_compressed_extents(async_cow->inode, async_cow);
  746. }
  747. static noinline void async_cow_free(struct btrfs_work *work)
  748. {
  749. struct async_cow *async_cow;
  750. async_cow = container_of(work, struct async_cow, work);
  751. kfree(async_cow);
  752. }
  753. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  754. u64 start, u64 end, int *page_started,
  755. unsigned long *nr_written)
  756. {
  757. struct async_cow *async_cow;
  758. struct btrfs_root *root = BTRFS_I(inode)->root;
  759. unsigned long nr_pages;
  760. u64 cur_end;
  761. int limit = 10 * 1024 * 1042;
  762. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
  763. EXTENT_DELALLOC, 1, 0, GFP_NOFS);
  764. while (start < end) {
  765. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  766. async_cow->inode = inode;
  767. async_cow->root = root;
  768. async_cow->locked_page = locked_page;
  769. async_cow->start = start;
  770. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  771. cur_end = end;
  772. else
  773. cur_end = min(end, start + 512 * 1024 - 1);
  774. async_cow->end = cur_end;
  775. INIT_LIST_HEAD(&async_cow->extents);
  776. async_cow->work.func = async_cow_start;
  777. async_cow->work.ordered_func = async_cow_submit;
  778. async_cow->work.ordered_free = async_cow_free;
  779. async_cow->work.flags = 0;
  780. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  781. PAGE_CACHE_SHIFT;
  782. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  783. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  784. &async_cow->work);
  785. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  786. wait_event(root->fs_info->async_submit_wait,
  787. (atomic_read(&root->fs_info->async_delalloc_pages) <
  788. limit));
  789. }
  790. while (atomic_read(&root->fs_info->async_submit_draining) &&
  791. atomic_read(&root->fs_info->async_delalloc_pages)) {
  792. wait_event(root->fs_info->async_submit_wait,
  793. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  794. 0));
  795. }
  796. *nr_written += nr_pages;
  797. start = cur_end + 1;
  798. }
  799. *page_started = 1;
  800. return 0;
  801. }
  802. static noinline int csum_exist_in_range(struct btrfs_root *root,
  803. u64 bytenr, u64 num_bytes)
  804. {
  805. int ret;
  806. struct btrfs_ordered_sum *sums;
  807. LIST_HEAD(list);
  808. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  809. bytenr + num_bytes - 1, &list);
  810. if (ret == 0 && list_empty(&list))
  811. return 0;
  812. while (!list_empty(&list)) {
  813. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  814. list_del(&sums->list);
  815. kfree(sums);
  816. }
  817. return 1;
  818. }
  819. /*
  820. * when nowcow writeback call back. This checks for snapshots or COW copies
  821. * of the extents that exist in the file, and COWs the file as required.
  822. *
  823. * If no cow copies or snapshots exist, we write directly to the existing
  824. * blocks on disk
  825. */
  826. static noinline int run_delalloc_nocow(struct inode *inode,
  827. struct page *locked_page,
  828. u64 start, u64 end, int *page_started, int force,
  829. unsigned long *nr_written)
  830. {
  831. struct btrfs_root *root = BTRFS_I(inode)->root;
  832. struct btrfs_trans_handle *trans;
  833. struct extent_buffer *leaf;
  834. struct btrfs_path *path;
  835. struct btrfs_file_extent_item *fi;
  836. struct btrfs_key found_key;
  837. u64 cow_start;
  838. u64 cur_offset;
  839. u64 extent_end;
  840. u64 extent_offset;
  841. u64 disk_bytenr;
  842. u64 num_bytes;
  843. int extent_type;
  844. int ret;
  845. int type;
  846. int nocow;
  847. int check_prev = 1;
  848. path = btrfs_alloc_path();
  849. BUG_ON(!path);
  850. trans = btrfs_join_transaction(root, 1);
  851. BUG_ON(!trans);
  852. cow_start = (u64)-1;
  853. cur_offset = start;
  854. while (1) {
  855. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  856. cur_offset, 0);
  857. BUG_ON(ret < 0);
  858. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  859. leaf = path->nodes[0];
  860. btrfs_item_key_to_cpu(leaf, &found_key,
  861. path->slots[0] - 1);
  862. if (found_key.objectid == inode->i_ino &&
  863. found_key.type == BTRFS_EXTENT_DATA_KEY)
  864. path->slots[0]--;
  865. }
  866. check_prev = 0;
  867. next_slot:
  868. leaf = path->nodes[0];
  869. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  870. ret = btrfs_next_leaf(root, path);
  871. if (ret < 0)
  872. BUG_ON(1);
  873. if (ret > 0)
  874. break;
  875. leaf = path->nodes[0];
  876. }
  877. nocow = 0;
  878. disk_bytenr = 0;
  879. num_bytes = 0;
  880. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  881. if (found_key.objectid > inode->i_ino ||
  882. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  883. found_key.offset > end)
  884. break;
  885. if (found_key.offset > cur_offset) {
  886. extent_end = found_key.offset;
  887. goto out_check;
  888. }
  889. fi = btrfs_item_ptr(leaf, path->slots[0],
  890. struct btrfs_file_extent_item);
  891. extent_type = btrfs_file_extent_type(leaf, fi);
  892. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  893. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  894. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  895. extent_offset = btrfs_file_extent_offset(leaf, fi);
  896. extent_end = found_key.offset +
  897. btrfs_file_extent_num_bytes(leaf, fi);
  898. if (extent_end <= start) {
  899. path->slots[0]++;
  900. goto next_slot;
  901. }
  902. if (disk_bytenr == 0)
  903. goto out_check;
  904. if (btrfs_file_extent_compression(leaf, fi) ||
  905. btrfs_file_extent_encryption(leaf, fi) ||
  906. btrfs_file_extent_other_encoding(leaf, fi))
  907. goto out_check;
  908. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  909. goto out_check;
  910. if (btrfs_extent_readonly(root, disk_bytenr))
  911. goto out_check;
  912. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  913. found_key.offset -
  914. extent_offset, disk_bytenr))
  915. goto out_check;
  916. disk_bytenr += extent_offset;
  917. disk_bytenr += cur_offset - found_key.offset;
  918. num_bytes = min(end + 1, extent_end) - cur_offset;
  919. /*
  920. * force cow if csum exists in the range.
  921. * this ensure that csum for a given extent are
  922. * either valid or do not exist.
  923. */
  924. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  925. goto out_check;
  926. nocow = 1;
  927. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  928. extent_end = found_key.offset +
  929. btrfs_file_extent_inline_len(leaf, fi);
  930. extent_end = ALIGN(extent_end, root->sectorsize);
  931. } else {
  932. BUG_ON(1);
  933. }
  934. out_check:
  935. if (extent_end <= start) {
  936. path->slots[0]++;
  937. goto next_slot;
  938. }
  939. if (!nocow) {
  940. if (cow_start == (u64)-1)
  941. cow_start = cur_offset;
  942. cur_offset = extent_end;
  943. if (cur_offset > end)
  944. break;
  945. path->slots[0]++;
  946. goto next_slot;
  947. }
  948. btrfs_release_path(root, path);
  949. if (cow_start != (u64)-1) {
  950. ret = cow_file_range(inode, locked_page, cow_start,
  951. found_key.offset - 1, page_started,
  952. nr_written, 1);
  953. BUG_ON(ret);
  954. cow_start = (u64)-1;
  955. }
  956. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  957. struct extent_map *em;
  958. struct extent_map_tree *em_tree;
  959. em_tree = &BTRFS_I(inode)->extent_tree;
  960. em = alloc_extent_map(GFP_NOFS);
  961. em->start = cur_offset;
  962. em->orig_start = em->start;
  963. em->len = num_bytes;
  964. em->block_len = num_bytes;
  965. em->block_start = disk_bytenr;
  966. em->bdev = root->fs_info->fs_devices->latest_bdev;
  967. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  968. while (1) {
  969. spin_lock(&em_tree->lock);
  970. ret = add_extent_mapping(em_tree, em);
  971. spin_unlock(&em_tree->lock);
  972. if (ret != -EEXIST) {
  973. free_extent_map(em);
  974. break;
  975. }
  976. btrfs_drop_extent_cache(inode, em->start,
  977. em->start + em->len - 1, 0);
  978. }
  979. type = BTRFS_ORDERED_PREALLOC;
  980. } else {
  981. type = BTRFS_ORDERED_NOCOW;
  982. }
  983. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  984. num_bytes, num_bytes, type);
  985. BUG_ON(ret);
  986. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  987. cur_offset, cur_offset + num_bytes - 1,
  988. locked_page, 1, 1, 1, 0, 0, 0);
  989. cur_offset = extent_end;
  990. if (cur_offset > end)
  991. break;
  992. }
  993. btrfs_release_path(root, path);
  994. if (cur_offset <= end && cow_start == (u64)-1)
  995. cow_start = cur_offset;
  996. if (cow_start != (u64)-1) {
  997. ret = cow_file_range(inode, locked_page, cow_start, end,
  998. page_started, nr_written, 1);
  999. BUG_ON(ret);
  1000. }
  1001. ret = btrfs_end_transaction(trans, root);
  1002. BUG_ON(ret);
  1003. btrfs_free_path(path);
  1004. return 0;
  1005. }
  1006. /*
  1007. * extent_io.c call back to do delayed allocation processing
  1008. */
  1009. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1010. u64 start, u64 end, int *page_started,
  1011. unsigned long *nr_written)
  1012. {
  1013. int ret;
  1014. struct btrfs_root *root = BTRFS_I(inode)->root;
  1015. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1016. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1017. page_started, 1, nr_written);
  1018. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1019. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1020. page_started, 0, nr_written);
  1021. else if (!btrfs_test_opt(root, COMPRESS))
  1022. ret = cow_file_range(inode, locked_page, start, end,
  1023. page_started, nr_written, 1);
  1024. else
  1025. ret = cow_file_range_async(inode, locked_page, start, end,
  1026. page_started, nr_written);
  1027. return ret;
  1028. }
  1029. /*
  1030. * extent_io.c set_bit_hook, used to track delayed allocation
  1031. * bytes in this file, and to maintain the list of inodes that
  1032. * have pending delalloc work to be done.
  1033. */
  1034. static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  1035. unsigned long old, unsigned long bits)
  1036. {
  1037. /*
  1038. * set_bit and clear bit hooks normally require _irqsave/restore
  1039. * but in this case, we are only testeing for the DELALLOC
  1040. * bit, which is only set or cleared with irqs on
  1041. */
  1042. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1043. struct btrfs_root *root = BTRFS_I(inode)->root;
  1044. btrfs_delalloc_reserve_space(root, inode, end - start + 1);
  1045. spin_lock(&root->fs_info->delalloc_lock);
  1046. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  1047. root->fs_info->delalloc_bytes += end - start + 1;
  1048. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1049. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1050. &root->fs_info->delalloc_inodes);
  1051. }
  1052. spin_unlock(&root->fs_info->delalloc_lock);
  1053. }
  1054. return 0;
  1055. }
  1056. /*
  1057. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1058. */
  1059. static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
  1060. unsigned long old, unsigned long bits)
  1061. {
  1062. /*
  1063. * set_bit and clear bit hooks normally require _irqsave/restore
  1064. * but in this case, we are only testeing for the DELALLOC
  1065. * bit, which is only set or cleared with irqs on
  1066. */
  1067. if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1068. struct btrfs_root *root = BTRFS_I(inode)->root;
  1069. spin_lock(&root->fs_info->delalloc_lock);
  1070. if (end - start + 1 > root->fs_info->delalloc_bytes) {
  1071. printk(KERN_INFO "btrfs warning: delalloc account "
  1072. "%llu %llu\n",
  1073. (unsigned long long)end - start + 1,
  1074. (unsigned long long)
  1075. root->fs_info->delalloc_bytes);
  1076. btrfs_delalloc_free_space(root, inode, (u64)-1);
  1077. root->fs_info->delalloc_bytes = 0;
  1078. BTRFS_I(inode)->delalloc_bytes = 0;
  1079. } else {
  1080. btrfs_delalloc_free_space(root, inode,
  1081. end - start + 1);
  1082. root->fs_info->delalloc_bytes -= end - start + 1;
  1083. BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
  1084. }
  1085. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1086. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1087. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1088. }
  1089. spin_unlock(&root->fs_info->delalloc_lock);
  1090. }
  1091. return 0;
  1092. }
  1093. /*
  1094. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1095. * we don't create bios that span stripes or chunks
  1096. */
  1097. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1098. size_t size, struct bio *bio,
  1099. unsigned long bio_flags)
  1100. {
  1101. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1102. struct btrfs_mapping_tree *map_tree;
  1103. u64 logical = (u64)bio->bi_sector << 9;
  1104. u64 length = 0;
  1105. u64 map_length;
  1106. int ret;
  1107. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1108. return 0;
  1109. length = bio->bi_size;
  1110. map_tree = &root->fs_info->mapping_tree;
  1111. map_length = length;
  1112. ret = btrfs_map_block(map_tree, READ, logical,
  1113. &map_length, NULL, 0);
  1114. if (map_length < length + size)
  1115. return 1;
  1116. return 0;
  1117. }
  1118. /*
  1119. * in order to insert checksums into the metadata in large chunks,
  1120. * we wait until bio submission time. All the pages in the bio are
  1121. * checksummed and sums are attached onto the ordered extent record.
  1122. *
  1123. * At IO completion time the cums attached on the ordered extent record
  1124. * are inserted into the btree
  1125. */
  1126. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1127. struct bio *bio, int mirror_num,
  1128. unsigned long bio_flags)
  1129. {
  1130. struct btrfs_root *root = BTRFS_I(inode)->root;
  1131. int ret = 0;
  1132. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1133. BUG_ON(ret);
  1134. return 0;
  1135. }
  1136. /*
  1137. * in order to insert checksums into the metadata in large chunks,
  1138. * we wait until bio submission time. All the pages in the bio are
  1139. * checksummed and sums are attached onto the ordered extent record.
  1140. *
  1141. * At IO completion time the cums attached on the ordered extent record
  1142. * are inserted into the btree
  1143. */
  1144. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1145. int mirror_num, unsigned long bio_flags)
  1146. {
  1147. struct btrfs_root *root = BTRFS_I(inode)->root;
  1148. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1149. }
  1150. /*
  1151. * extent_io.c submission hook. This does the right thing for csum calculation
  1152. * on write, or reading the csums from the tree before a read
  1153. */
  1154. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1155. int mirror_num, unsigned long bio_flags)
  1156. {
  1157. struct btrfs_root *root = BTRFS_I(inode)->root;
  1158. int ret = 0;
  1159. int skip_sum;
  1160. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1161. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1162. BUG_ON(ret);
  1163. if (!(rw & (1 << BIO_RW))) {
  1164. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1165. return btrfs_submit_compressed_read(inode, bio,
  1166. mirror_num, bio_flags);
  1167. } else if (!skip_sum)
  1168. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1169. goto mapit;
  1170. } else if (!skip_sum) {
  1171. /* csum items have already been cloned */
  1172. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1173. goto mapit;
  1174. /* we're doing a write, do the async checksumming */
  1175. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1176. inode, rw, bio, mirror_num,
  1177. bio_flags, __btrfs_submit_bio_start,
  1178. __btrfs_submit_bio_done);
  1179. }
  1180. mapit:
  1181. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1182. }
  1183. /*
  1184. * given a list of ordered sums record them in the inode. This happens
  1185. * at IO completion time based on sums calculated at bio submission time.
  1186. */
  1187. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1188. struct inode *inode, u64 file_offset,
  1189. struct list_head *list)
  1190. {
  1191. struct btrfs_ordered_sum *sum;
  1192. btrfs_set_trans_block_group(trans, inode);
  1193. list_for_each_entry(sum, list, list) {
  1194. btrfs_csum_file_blocks(trans,
  1195. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1196. }
  1197. return 0;
  1198. }
  1199. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  1200. {
  1201. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1202. WARN_ON(1);
  1203. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1204. GFP_NOFS);
  1205. }
  1206. /* see btrfs_writepage_start_hook for details on why this is required */
  1207. struct btrfs_writepage_fixup {
  1208. struct page *page;
  1209. struct btrfs_work work;
  1210. };
  1211. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1212. {
  1213. struct btrfs_writepage_fixup *fixup;
  1214. struct btrfs_ordered_extent *ordered;
  1215. struct page *page;
  1216. struct inode *inode;
  1217. u64 page_start;
  1218. u64 page_end;
  1219. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1220. page = fixup->page;
  1221. again:
  1222. lock_page(page);
  1223. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1224. ClearPageChecked(page);
  1225. goto out_page;
  1226. }
  1227. inode = page->mapping->host;
  1228. page_start = page_offset(page);
  1229. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1230. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1231. /* already ordered? We're done */
  1232. if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1233. EXTENT_ORDERED, 0)) {
  1234. goto out;
  1235. }
  1236. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1237. if (ordered) {
  1238. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  1239. page_end, GFP_NOFS);
  1240. unlock_page(page);
  1241. btrfs_start_ordered_extent(inode, ordered, 1);
  1242. goto again;
  1243. }
  1244. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1245. ClearPageChecked(page);
  1246. out:
  1247. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1248. out_page:
  1249. unlock_page(page);
  1250. page_cache_release(page);
  1251. }
  1252. /*
  1253. * There are a few paths in the higher layers of the kernel that directly
  1254. * set the page dirty bit without asking the filesystem if it is a
  1255. * good idea. This causes problems because we want to make sure COW
  1256. * properly happens and the data=ordered rules are followed.
  1257. *
  1258. * In our case any range that doesn't have the ORDERED bit set
  1259. * hasn't been properly setup for IO. We kick off an async process
  1260. * to fix it up. The async helper will wait for ordered extents, set
  1261. * the delalloc bit and make it safe to write the page.
  1262. */
  1263. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1264. {
  1265. struct inode *inode = page->mapping->host;
  1266. struct btrfs_writepage_fixup *fixup;
  1267. struct btrfs_root *root = BTRFS_I(inode)->root;
  1268. int ret;
  1269. ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  1270. EXTENT_ORDERED, 0);
  1271. if (ret)
  1272. return 0;
  1273. if (PageChecked(page))
  1274. return -EAGAIN;
  1275. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1276. if (!fixup)
  1277. return -EAGAIN;
  1278. SetPageChecked(page);
  1279. page_cache_get(page);
  1280. fixup->work.func = btrfs_writepage_fixup_worker;
  1281. fixup->page = page;
  1282. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1283. return -EAGAIN;
  1284. }
  1285. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1286. struct inode *inode, u64 file_pos,
  1287. u64 disk_bytenr, u64 disk_num_bytes,
  1288. u64 num_bytes, u64 ram_bytes,
  1289. u64 locked_end,
  1290. u8 compression, u8 encryption,
  1291. u16 other_encoding, int extent_type)
  1292. {
  1293. struct btrfs_root *root = BTRFS_I(inode)->root;
  1294. struct btrfs_file_extent_item *fi;
  1295. struct btrfs_path *path;
  1296. struct extent_buffer *leaf;
  1297. struct btrfs_key ins;
  1298. u64 hint;
  1299. int ret;
  1300. path = btrfs_alloc_path();
  1301. BUG_ON(!path);
  1302. path->leave_spinning = 1;
  1303. ret = btrfs_drop_extents(trans, root, inode, file_pos,
  1304. file_pos + num_bytes, locked_end,
  1305. file_pos, &hint);
  1306. BUG_ON(ret);
  1307. ins.objectid = inode->i_ino;
  1308. ins.offset = file_pos;
  1309. ins.type = BTRFS_EXTENT_DATA_KEY;
  1310. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1311. BUG_ON(ret);
  1312. leaf = path->nodes[0];
  1313. fi = btrfs_item_ptr(leaf, path->slots[0],
  1314. struct btrfs_file_extent_item);
  1315. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1316. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1317. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1318. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1319. btrfs_set_file_extent_offset(leaf, fi, 0);
  1320. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1321. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1322. btrfs_set_file_extent_compression(leaf, fi, compression);
  1323. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1324. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1325. btrfs_unlock_up_safe(path, 1);
  1326. btrfs_set_lock_blocking(leaf);
  1327. btrfs_mark_buffer_dirty(leaf);
  1328. inode_add_bytes(inode, num_bytes);
  1329. btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
  1330. ins.objectid = disk_bytenr;
  1331. ins.offset = disk_num_bytes;
  1332. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1333. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1334. root->root_key.objectid,
  1335. inode->i_ino, file_pos, &ins);
  1336. BUG_ON(ret);
  1337. btrfs_free_path(path);
  1338. return 0;
  1339. }
  1340. /*
  1341. * helper function for btrfs_finish_ordered_io, this
  1342. * just reads in some of the csum leaves to prime them into ram
  1343. * before we start the transaction. It limits the amount of btree
  1344. * reads required while inside the transaction.
  1345. */
  1346. static noinline void reada_csum(struct btrfs_root *root,
  1347. struct btrfs_path *path,
  1348. struct btrfs_ordered_extent *ordered_extent)
  1349. {
  1350. struct btrfs_ordered_sum *sum;
  1351. u64 bytenr;
  1352. sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
  1353. list);
  1354. bytenr = sum->sums[0].bytenr;
  1355. /*
  1356. * we don't care about the results, the point of this search is
  1357. * just to get the btree leaves into ram
  1358. */
  1359. btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
  1360. }
  1361. /* as ordered data IO finishes, this gets called so we can finish
  1362. * an ordered extent if the range of bytes in the file it covers are
  1363. * fully written.
  1364. */
  1365. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1366. {
  1367. struct btrfs_root *root = BTRFS_I(inode)->root;
  1368. struct btrfs_trans_handle *trans;
  1369. struct btrfs_ordered_extent *ordered_extent = NULL;
  1370. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1371. struct btrfs_path *path;
  1372. int compressed = 0;
  1373. int ret;
  1374. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1375. if (!ret)
  1376. return 0;
  1377. /*
  1378. * before we join the transaction, try to do some of our IO.
  1379. * This will limit the amount of IO that we have to do with
  1380. * the transaction running. We're unlikely to need to do any
  1381. * IO if the file extents are new, the disk_i_size checks
  1382. * covers the most common case.
  1383. */
  1384. if (start < BTRFS_I(inode)->disk_i_size) {
  1385. path = btrfs_alloc_path();
  1386. if (path) {
  1387. ret = btrfs_lookup_file_extent(NULL, root, path,
  1388. inode->i_ino,
  1389. start, 0);
  1390. ordered_extent = btrfs_lookup_ordered_extent(inode,
  1391. start);
  1392. if (!list_empty(&ordered_extent->list)) {
  1393. btrfs_release_path(root, path);
  1394. reada_csum(root, path, ordered_extent);
  1395. }
  1396. btrfs_free_path(path);
  1397. }
  1398. }
  1399. trans = btrfs_join_transaction(root, 1);
  1400. if (!ordered_extent)
  1401. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1402. BUG_ON(!ordered_extent);
  1403. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  1404. goto nocow;
  1405. lock_extent(io_tree, ordered_extent->file_offset,
  1406. ordered_extent->file_offset + ordered_extent->len - 1,
  1407. GFP_NOFS);
  1408. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1409. compressed = 1;
  1410. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1411. BUG_ON(compressed);
  1412. ret = btrfs_mark_extent_written(trans, root, inode,
  1413. ordered_extent->file_offset,
  1414. ordered_extent->file_offset +
  1415. ordered_extent->len);
  1416. BUG_ON(ret);
  1417. } else {
  1418. ret = insert_reserved_file_extent(trans, inode,
  1419. ordered_extent->file_offset,
  1420. ordered_extent->start,
  1421. ordered_extent->disk_len,
  1422. ordered_extent->len,
  1423. ordered_extent->len,
  1424. ordered_extent->file_offset +
  1425. ordered_extent->len,
  1426. compressed, 0, 0,
  1427. BTRFS_FILE_EXTENT_REG);
  1428. BUG_ON(ret);
  1429. }
  1430. unlock_extent(io_tree, ordered_extent->file_offset,
  1431. ordered_extent->file_offset + ordered_extent->len - 1,
  1432. GFP_NOFS);
  1433. nocow:
  1434. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1435. &ordered_extent->list);
  1436. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1437. btrfs_ordered_update_i_size(inode, ordered_extent);
  1438. btrfs_update_inode(trans, root, inode);
  1439. btrfs_remove_ordered_extent(inode, ordered_extent);
  1440. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1441. /* once for us */
  1442. btrfs_put_ordered_extent(ordered_extent);
  1443. /* once for the tree */
  1444. btrfs_put_ordered_extent(ordered_extent);
  1445. btrfs_end_transaction(trans, root);
  1446. return 0;
  1447. }
  1448. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1449. struct extent_state *state, int uptodate)
  1450. {
  1451. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1452. }
  1453. /*
  1454. * When IO fails, either with EIO or csum verification fails, we
  1455. * try other mirrors that might have a good copy of the data. This
  1456. * io_failure_record is used to record state as we go through all the
  1457. * mirrors. If another mirror has good data, the page is set up to date
  1458. * and things continue. If a good mirror can't be found, the original
  1459. * bio end_io callback is called to indicate things have failed.
  1460. */
  1461. struct io_failure_record {
  1462. struct page *page;
  1463. u64 start;
  1464. u64 len;
  1465. u64 logical;
  1466. unsigned long bio_flags;
  1467. int last_mirror;
  1468. };
  1469. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1470. struct page *page, u64 start, u64 end,
  1471. struct extent_state *state)
  1472. {
  1473. struct io_failure_record *failrec = NULL;
  1474. u64 private;
  1475. struct extent_map *em;
  1476. struct inode *inode = page->mapping->host;
  1477. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1478. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1479. struct bio *bio;
  1480. int num_copies;
  1481. int ret;
  1482. int rw;
  1483. u64 logical;
  1484. ret = get_state_private(failure_tree, start, &private);
  1485. if (ret) {
  1486. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1487. if (!failrec)
  1488. return -ENOMEM;
  1489. failrec->start = start;
  1490. failrec->len = end - start + 1;
  1491. failrec->last_mirror = 0;
  1492. failrec->bio_flags = 0;
  1493. spin_lock(&em_tree->lock);
  1494. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1495. if (em->start > start || em->start + em->len < start) {
  1496. free_extent_map(em);
  1497. em = NULL;
  1498. }
  1499. spin_unlock(&em_tree->lock);
  1500. if (!em || IS_ERR(em)) {
  1501. kfree(failrec);
  1502. return -EIO;
  1503. }
  1504. logical = start - em->start;
  1505. logical = em->block_start + logical;
  1506. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1507. logical = em->block_start;
  1508. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1509. }
  1510. failrec->logical = logical;
  1511. free_extent_map(em);
  1512. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1513. EXTENT_DIRTY, GFP_NOFS);
  1514. set_state_private(failure_tree, start,
  1515. (u64)(unsigned long)failrec);
  1516. } else {
  1517. failrec = (struct io_failure_record *)(unsigned long)private;
  1518. }
  1519. num_copies = btrfs_num_copies(
  1520. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1521. failrec->logical, failrec->len);
  1522. failrec->last_mirror++;
  1523. if (!state) {
  1524. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1525. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1526. failrec->start,
  1527. EXTENT_LOCKED);
  1528. if (state && state->start != failrec->start)
  1529. state = NULL;
  1530. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1531. }
  1532. if (!state || failrec->last_mirror > num_copies) {
  1533. set_state_private(failure_tree, failrec->start, 0);
  1534. clear_extent_bits(failure_tree, failrec->start,
  1535. failrec->start + failrec->len - 1,
  1536. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1537. kfree(failrec);
  1538. return -EIO;
  1539. }
  1540. bio = bio_alloc(GFP_NOFS, 1);
  1541. bio->bi_private = state;
  1542. bio->bi_end_io = failed_bio->bi_end_io;
  1543. bio->bi_sector = failrec->logical >> 9;
  1544. bio->bi_bdev = failed_bio->bi_bdev;
  1545. bio->bi_size = 0;
  1546. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1547. if (failed_bio->bi_rw & (1 << BIO_RW))
  1548. rw = WRITE;
  1549. else
  1550. rw = READ;
  1551. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1552. failrec->last_mirror,
  1553. failrec->bio_flags);
  1554. return 0;
  1555. }
  1556. /*
  1557. * each time an IO finishes, we do a fast check in the IO failure tree
  1558. * to see if we need to process or clean up an io_failure_record
  1559. */
  1560. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1561. {
  1562. u64 private;
  1563. u64 private_failure;
  1564. struct io_failure_record *failure;
  1565. int ret;
  1566. private = 0;
  1567. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1568. (u64)-1, 1, EXTENT_DIRTY)) {
  1569. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1570. start, &private_failure);
  1571. if (ret == 0) {
  1572. failure = (struct io_failure_record *)(unsigned long)
  1573. private_failure;
  1574. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1575. failure->start, 0);
  1576. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1577. failure->start,
  1578. failure->start + failure->len - 1,
  1579. EXTENT_DIRTY | EXTENT_LOCKED,
  1580. GFP_NOFS);
  1581. kfree(failure);
  1582. }
  1583. }
  1584. return 0;
  1585. }
  1586. /*
  1587. * when reads are done, we need to check csums to verify the data is correct
  1588. * if there's a match, we allow the bio to finish. If not, we go through
  1589. * the io_failure_record routines to find good copies
  1590. */
  1591. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1592. struct extent_state *state)
  1593. {
  1594. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1595. struct inode *inode = page->mapping->host;
  1596. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1597. char *kaddr;
  1598. u64 private = ~(u32)0;
  1599. int ret;
  1600. struct btrfs_root *root = BTRFS_I(inode)->root;
  1601. u32 csum = ~(u32)0;
  1602. if (PageChecked(page)) {
  1603. ClearPageChecked(page);
  1604. goto good;
  1605. }
  1606. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1607. return 0;
  1608. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1609. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
  1610. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1611. GFP_NOFS);
  1612. return 0;
  1613. }
  1614. if (state && state->start == start) {
  1615. private = state->private;
  1616. ret = 0;
  1617. } else {
  1618. ret = get_state_private(io_tree, start, &private);
  1619. }
  1620. kaddr = kmap_atomic(page, KM_USER0);
  1621. if (ret)
  1622. goto zeroit;
  1623. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1624. btrfs_csum_final(csum, (char *)&csum);
  1625. if (csum != private)
  1626. goto zeroit;
  1627. kunmap_atomic(kaddr, KM_USER0);
  1628. good:
  1629. /* if the io failure tree for this inode is non-empty,
  1630. * check to see if we've recovered from a failed IO
  1631. */
  1632. btrfs_clean_io_failures(inode, start);
  1633. return 0;
  1634. zeroit:
  1635. if (printk_ratelimit()) {
  1636. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1637. "private %llu\n", page->mapping->host->i_ino,
  1638. (unsigned long long)start, csum,
  1639. (unsigned long long)private);
  1640. }
  1641. memset(kaddr + offset, 1, end - start + 1);
  1642. flush_dcache_page(page);
  1643. kunmap_atomic(kaddr, KM_USER0);
  1644. if (private == 0)
  1645. return 0;
  1646. return -EIO;
  1647. }
  1648. /*
  1649. * This creates an orphan entry for the given inode in case something goes
  1650. * wrong in the middle of an unlink/truncate.
  1651. */
  1652. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1653. {
  1654. struct btrfs_root *root = BTRFS_I(inode)->root;
  1655. int ret = 0;
  1656. spin_lock(&root->list_lock);
  1657. /* already on the orphan list, we're good */
  1658. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1659. spin_unlock(&root->list_lock);
  1660. return 0;
  1661. }
  1662. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1663. spin_unlock(&root->list_lock);
  1664. /*
  1665. * insert an orphan item to track this unlinked/truncated file
  1666. */
  1667. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1668. return ret;
  1669. }
  1670. /*
  1671. * We have done the truncate/delete so we can go ahead and remove the orphan
  1672. * item for this particular inode.
  1673. */
  1674. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1675. {
  1676. struct btrfs_root *root = BTRFS_I(inode)->root;
  1677. int ret = 0;
  1678. spin_lock(&root->list_lock);
  1679. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1680. spin_unlock(&root->list_lock);
  1681. return 0;
  1682. }
  1683. list_del_init(&BTRFS_I(inode)->i_orphan);
  1684. if (!trans) {
  1685. spin_unlock(&root->list_lock);
  1686. return 0;
  1687. }
  1688. spin_unlock(&root->list_lock);
  1689. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1690. return ret;
  1691. }
  1692. /*
  1693. * this cleans up any orphans that may be left on the list from the last use
  1694. * of this root.
  1695. */
  1696. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1697. {
  1698. struct btrfs_path *path;
  1699. struct extent_buffer *leaf;
  1700. struct btrfs_item *item;
  1701. struct btrfs_key key, found_key;
  1702. struct btrfs_trans_handle *trans;
  1703. struct inode *inode;
  1704. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1705. path = btrfs_alloc_path();
  1706. if (!path)
  1707. return;
  1708. path->reada = -1;
  1709. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1710. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1711. key.offset = (u64)-1;
  1712. while (1) {
  1713. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1714. if (ret < 0) {
  1715. printk(KERN_ERR "Error searching slot for orphan: %d"
  1716. "\n", ret);
  1717. break;
  1718. }
  1719. /*
  1720. * if ret == 0 means we found what we were searching for, which
  1721. * is weird, but possible, so only screw with path if we didnt
  1722. * find the key and see if we have stuff that matches
  1723. */
  1724. if (ret > 0) {
  1725. if (path->slots[0] == 0)
  1726. break;
  1727. path->slots[0]--;
  1728. }
  1729. /* pull out the item */
  1730. leaf = path->nodes[0];
  1731. item = btrfs_item_nr(leaf, path->slots[0]);
  1732. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1733. /* make sure the item matches what we want */
  1734. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1735. break;
  1736. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1737. break;
  1738. /* release the path since we're done with it */
  1739. btrfs_release_path(root, path);
  1740. /*
  1741. * this is where we are basically btrfs_lookup, without the
  1742. * crossing root thing. we store the inode number in the
  1743. * offset of the orphan item.
  1744. */
  1745. found_key.objectid = found_key.offset;
  1746. found_key.type = BTRFS_INODE_ITEM_KEY;
  1747. found_key.offset = 0;
  1748. inode = btrfs_iget(root->fs_info->sb, &found_key, root);
  1749. if (IS_ERR(inode))
  1750. break;
  1751. /*
  1752. * add this inode to the orphan list so btrfs_orphan_del does
  1753. * the proper thing when we hit it
  1754. */
  1755. spin_lock(&root->list_lock);
  1756. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1757. spin_unlock(&root->list_lock);
  1758. /*
  1759. * if this is a bad inode, means we actually succeeded in
  1760. * removing the inode, but not the orphan record, which means
  1761. * we need to manually delete the orphan since iput will just
  1762. * do a destroy_inode
  1763. */
  1764. if (is_bad_inode(inode)) {
  1765. trans = btrfs_start_transaction(root, 1);
  1766. btrfs_orphan_del(trans, inode);
  1767. btrfs_end_transaction(trans, root);
  1768. iput(inode);
  1769. continue;
  1770. }
  1771. /* if we have links, this was a truncate, lets do that */
  1772. if (inode->i_nlink) {
  1773. nr_truncate++;
  1774. btrfs_truncate(inode);
  1775. } else {
  1776. nr_unlink++;
  1777. }
  1778. /* this will do delete_inode and everything for us */
  1779. iput(inode);
  1780. }
  1781. if (nr_unlink)
  1782. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1783. if (nr_truncate)
  1784. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1785. btrfs_free_path(path);
  1786. }
  1787. /*
  1788. * very simple check to peek ahead in the leaf looking for xattrs. If we
  1789. * don't find any xattrs, we know there can't be any acls.
  1790. *
  1791. * slot is the slot the inode is in, objectid is the objectid of the inode
  1792. */
  1793. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  1794. int slot, u64 objectid)
  1795. {
  1796. u32 nritems = btrfs_header_nritems(leaf);
  1797. struct btrfs_key found_key;
  1798. int scanned = 0;
  1799. slot++;
  1800. while (slot < nritems) {
  1801. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1802. /* we found a different objectid, there must not be acls */
  1803. if (found_key.objectid != objectid)
  1804. return 0;
  1805. /* we found an xattr, assume we've got an acl */
  1806. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  1807. return 1;
  1808. /*
  1809. * we found a key greater than an xattr key, there can't
  1810. * be any acls later on
  1811. */
  1812. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  1813. return 0;
  1814. slot++;
  1815. scanned++;
  1816. /*
  1817. * it goes inode, inode backrefs, xattrs, extents,
  1818. * so if there are a ton of hard links to an inode there can
  1819. * be a lot of backrefs. Don't waste time searching too hard,
  1820. * this is just an optimization
  1821. */
  1822. if (scanned >= 8)
  1823. break;
  1824. }
  1825. /* we hit the end of the leaf before we found an xattr or
  1826. * something larger than an xattr. We have to assume the inode
  1827. * has acls
  1828. */
  1829. return 1;
  1830. }
  1831. /*
  1832. * read an inode from the btree into the in-memory inode
  1833. */
  1834. static void btrfs_read_locked_inode(struct inode *inode)
  1835. {
  1836. struct btrfs_path *path;
  1837. struct extent_buffer *leaf;
  1838. struct btrfs_inode_item *inode_item;
  1839. struct btrfs_timespec *tspec;
  1840. struct btrfs_root *root = BTRFS_I(inode)->root;
  1841. struct btrfs_key location;
  1842. int maybe_acls;
  1843. u64 alloc_group_block;
  1844. u32 rdev;
  1845. int ret;
  1846. path = btrfs_alloc_path();
  1847. BUG_ON(!path);
  1848. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1849. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1850. if (ret)
  1851. goto make_bad;
  1852. leaf = path->nodes[0];
  1853. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1854. struct btrfs_inode_item);
  1855. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1856. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1857. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1858. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1859. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1860. tspec = btrfs_inode_atime(inode_item);
  1861. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1862. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1863. tspec = btrfs_inode_mtime(inode_item);
  1864. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1865. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1866. tspec = btrfs_inode_ctime(inode_item);
  1867. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1868. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1869. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  1870. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  1871. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  1872. inode->i_generation = BTRFS_I(inode)->generation;
  1873. inode->i_rdev = 0;
  1874. rdev = btrfs_inode_rdev(leaf, inode_item);
  1875. BTRFS_I(inode)->index_cnt = (u64)-1;
  1876. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  1877. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  1878. /*
  1879. * try to precache a NULL acl entry for files that don't have
  1880. * any xattrs or acls
  1881. */
  1882. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  1883. if (!maybe_acls) {
  1884. BTRFS_I(inode)->i_acl = NULL;
  1885. BTRFS_I(inode)->i_default_acl = NULL;
  1886. }
  1887. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  1888. alloc_group_block, 0);
  1889. btrfs_free_path(path);
  1890. inode_item = NULL;
  1891. switch (inode->i_mode & S_IFMT) {
  1892. case S_IFREG:
  1893. inode->i_mapping->a_ops = &btrfs_aops;
  1894. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1895. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  1896. inode->i_fop = &btrfs_file_operations;
  1897. inode->i_op = &btrfs_file_inode_operations;
  1898. break;
  1899. case S_IFDIR:
  1900. inode->i_fop = &btrfs_dir_file_operations;
  1901. if (root == root->fs_info->tree_root)
  1902. inode->i_op = &btrfs_dir_ro_inode_operations;
  1903. else
  1904. inode->i_op = &btrfs_dir_inode_operations;
  1905. break;
  1906. case S_IFLNK:
  1907. inode->i_op = &btrfs_symlink_inode_operations;
  1908. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  1909. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1910. break;
  1911. default:
  1912. inode->i_op = &btrfs_special_inode_operations;
  1913. init_special_inode(inode, inode->i_mode, rdev);
  1914. break;
  1915. }
  1916. btrfs_update_iflags(inode);
  1917. return;
  1918. make_bad:
  1919. btrfs_free_path(path);
  1920. make_bad_inode(inode);
  1921. }
  1922. /*
  1923. * given a leaf and an inode, copy the inode fields into the leaf
  1924. */
  1925. static void fill_inode_item(struct btrfs_trans_handle *trans,
  1926. struct extent_buffer *leaf,
  1927. struct btrfs_inode_item *item,
  1928. struct inode *inode)
  1929. {
  1930. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  1931. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  1932. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  1933. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  1934. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  1935. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  1936. inode->i_atime.tv_sec);
  1937. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  1938. inode->i_atime.tv_nsec);
  1939. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  1940. inode->i_mtime.tv_sec);
  1941. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  1942. inode->i_mtime.tv_nsec);
  1943. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  1944. inode->i_ctime.tv_sec);
  1945. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  1946. inode->i_ctime.tv_nsec);
  1947. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  1948. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  1949. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  1950. btrfs_set_inode_transid(leaf, item, trans->transid);
  1951. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  1952. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  1953. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  1954. }
  1955. /*
  1956. * copy everything in the in-memory inode into the btree.
  1957. */
  1958. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  1959. struct btrfs_root *root, struct inode *inode)
  1960. {
  1961. struct btrfs_inode_item *inode_item;
  1962. struct btrfs_path *path;
  1963. struct extent_buffer *leaf;
  1964. int ret;
  1965. path = btrfs_alloc_path();
  1966. BUG_ON(!path);
  1967. path->leave_spinning = 1;
  1968. ret = btrfs_lookup_inode(trans, root, path,
  1969. &BTRFS_I(inode)->location, 1);
  1970. if (ret) {
  1971. if (ret > 0)
  1972. ret = -ENOENT;
  1973. goto failed;
  1974. }
  1975. btrfs_unlock_up_safe(path, 1);
  1976. leaf = path->nodes[0];
  1977. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1978. struct btrfs_inode_item);
  1979. fill_inode_item(trans, leaf, inode_item, inode);
  1980. btrfs_mark_buffer_dirty(leaf);
  1981. btrfs_set_inode_last_trans(trans, inode);
  1982. ret = 0;
  1983. failed:
  1984. btrfs_free_path(path);
  1985. return ret;
  1986. }
  1987. /*
  1988. * unlink helper that gets used here in inode.c and in the tree logging
  1989. * recovery code. It remove a link in a directory with a given name, and
  1990. * also drops the back refs in the inode to the directory
  1991. */
  1992. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  1993. struct btrfs_root *root,
  1994. struct inode *dir, struct inode *inode,
  1995. const char *name, int name_len)
  1996. {
  1997. struct btrfs_path *path;
  1998. int ret = 0;
  1999. struct extent_buffer *leaf;
  2000. struct btrfs_dir_item *di;
  2001. struct btrfs_key key;
  2002. u64 index;
  2003. path = btrfs_alloc_path();
  2004. if (!path) {
  2005. ret = -ENOMEM;
  2006. goto err;
  2007. }
  2008. path->leave_spinning = 1;
  2009. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2010. name, name_len, -1);
  2011. if (IS_ERR(di)) {
  2012. ret = PTR_ERR(di);
  2013. goto err;
  2014. }
  2015. if (!di) {
  2016. ret = -ENOENT;
  2017. goto err;
  2018. }
  2019. leaf = path->nodes[0];
  2020. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2021. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2022. if (ret)
  2023. goto err;
  2024. btrfs_release_path(root, path);
  2025. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2026. inode->i_ino,
  2027. dir->i_ino, &index);
  2028. if (ret) {
  2029. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2030. "inode %lu parent %lu\n", name_len, name,
  2031. inode->i_ino, dir->i_ino);
  2032. goto err;
  2033. }
  2034. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2035. index, name, name_len, -1);
  2036. if (IS_ERR(di)) {
  2037. ret = PTR_ERR(di);
  2038. goto err;
  2039. }
  2040. if (!di) {
  2041. ret = -ENOENT;
  2042. goto err;
  2043. }
  2044. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2045. btrfs_release_path(root, path);
  2046. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2047. inode, dir->i_ino);
  2048. BUG_ON(ret != 0 && ret != -ENOENT);
  2049. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2050. dir, index);
  2051. BUG_ON(ret);
  2052. err:
  2053. btrfs_free_path(path);
  2054. if (ret)
  2055. goto out;
  2056. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2057. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2058. btrfs_update_inode(trans, root, dir);
  2059. btrfs_drop_nlink(inode);
  2060. ret = btrfs_update_inode(trans, root, inode);
  2061. dir->i_sb->s_dirt = 1;
  2062. out:
  2063. return ret;
  2064. }
  2065. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2066. {
  2067. struct btrfs_root *root;
  2068. struct btrfs_trans_handle *trans;
  2069. struct inode *inode = dentry->d_inode;
  2070. int ret;
  2071. unsigned long nr = 0;
  2072. root = BTRFS_I(dir)->root;
  2073. trans = btrfs_start_transaction(root, 1);
  2074. btrfs_set_trans_block_group(trans, dir);
  2075. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2076. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2077. dentry->d_name.name, dentry->d_name.len);
  2078. if (inode->i_nlink == 0)
  2079. ret = btrfs_orphan_add(trans, inode);
  2080. nr = trans->blocks_used;
  2081. btrfs_end_transaction_throttle(trans, root);
  2082. btrfs_btree_balance_dirty(root, nr);
  2083. return ret;
  2084. }
  2085. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2086. {
  2087. struct inode *inode = dentry->d_inode;
  2088. int err = 0;
  2089. int ret;
  2090. struct btrfs_root *root = BTRFS_I(dir)->root;
  2091. struct btrfs_trans_handle *trans;
  2092. unsigned long nr = 0;
  2093. /*
  2094. * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
  2095. * the root of a subvolume or snapshot
  2096. */
  2097. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2098. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  2099. return -ENOTEMPTY;
  2100. }
  2101. trans = btrfs_start_transaction(root, 1);
  2102. btrfs_set_trans_block_group(trans, dir);
  2103. err = btrfs_orphan_add(trans, inode);
  2104. if (err)
  2105. goto fail_trans;
  2106. /* now the directory is empty */
  2107. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2108. dentry->d_name.name, dentry->d_name.len);
  2109. if (!err)
  2110. btrfs_i_size_write(inode, 0);
  2111. fail_trans:
  2112. nr = trans->blocks_used;
  2113. ret = btrfs_end_transaction_throttle(trans, root);
  2114. btrfs_btree_balance_dirty(root, nr);
  2115. if (ret && !err)
  2116. err = ret;
  2117. return err;
  2118. }
  2119. #if 0
  2120. /*
  2121. * when truncating bytes in a file, it is possible to avoid reading
  2122. * the leaves that contain only checksum items. This can be the
  2123. * majority of the IO required to delete a large file, but it must
  2124. * be done carefully.
  2125. *
  2126. * The keys in the level just above the leaves are checked to make sure
  2127. * the lowest key in a given leaf is a csum key, and starts at an offset
  2128. * after the new size.
  2129. *
  2130. * Then the key for the next leaf is checked to make sure it also has
  2131. * a checksum item for the same file. If it does, we know our target leaf
  2132. * contains only checksum items, and it can be safely freed without reading
  2133. * it.
  2134. *
  2135. * This is just an optimization targeted at large files. It may do
  2136. * nothing. It will return 0 unless things went badly.
  2137. */
  2138. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2139. struct btrfs_root *root,
  2140. struct btrfs_path *path,
  2141. struct inode *inode, u64 new_size)
  2142. {
  2143. struct btrfs_key key;
  2144. int ret;
  2145. int nritems;
  2146. struct btrfs_key found_key;
  2147. struct btrfs_key other_key;
  2148. struct btrfs_leaf_ref *ref;
  2149. u64 leaf_gen;
  2150. u64 leaf_start;
  2151. path->lowest_level = 1;
  2152. key.objectid = inode->i_ino;
  2153. key.type = BTRFS_CSUM_ITEM_KEY;
  2154. key.offset = new_size;
  2155. again:
  2156. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2157. if (ret < 0)
  2158. goto out;
  2159. if (path->nodes[1] == NULL) {
  2160. ret = 0;
  2161. goto out;
  2162. }
  2163. ret = 0;
  2164. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2165. nritems = btrfs_header_nritems(path->nodes[1]);
  2166. if (!nritems)
  2167. goto out;
  2168. if (path->slots[1] >= nritems)
  2169. goto next_node;
  2170. /* did we find a key greater than anything we want to delete? */
  2171. if (found_key.objectid > inode->i_ino ||
  2172. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2173. goto out;
  2174. /* we check the next key in the node to make sure the leave contains
  2175. * only checksum items. This comparison doesn't work if our
  2176. * leaf is the last one in the node
  2177. */
  2178. if (path->slots[1] + 1 >= nritems) {
  2179. next_node:
  2180. /* search forward from the last key in the node, this
  2181. * will bring us into the next node in the tree
  2182. */
  2183. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2184. /* unlikely, but we inc below, so check to be safe */
  2185. if (found_key.offset == (u64)-1)
  2186. goto out;
  2187. /* search_forward needs a path with locks held, do the
  2188. * search again for the original key. It is possible
  2189. * this will race with a balance and return a path that
  2190. * we could modify, but this drop is just an optimization
  2191. * and is allowed to miss some leaves.
  2192. */
  2193. btrfs_release_path(root, path);
  2194. found_key.offset++;
  2195. /* setup a max key for search_forward */
  2196. other_key.offset = (u64)-1;
  2197. other_key.type = key.type;
  2198. other_key.objectid = key.objectid;
  2199. path->keep_locks = 1;
  2200. ret = btrfs_search_forward(root, &found_key, &other_key,
  2201. path, 0, 0);
  2202. path->keep_locks = 0;
  2203. if (ret || found_key.objectid != key.objectid ||
  2204. found_key.type != key.type) {
  2205. ret = 0;
  2206. goto out;
  2207. }
  2208. key.offset = found_key.offset;
  2209. btrfs_release_path(root, path);
  2210. cond_resched();
  2211. goto again;
  2212. }
  2213. /* we know there's one more slot after us in the tree,
  2214. * read that key so we can verify it is also a checksum item
  2215. */
  2216. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2217. if (found_key.objectid < inode->i_ino)
  2218. goto next_key;
  2219. if (found_key.type != key.type || found_key.offset < new_size)
  2220. goto next_key;
  2221. /*
  2222. * if the key for the next leaf isn't a csum key from this objectid,
  2223. * we can't be sure there aren't good items inside this leaf.
  2224. * Bail out
  2225. */
  2226. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2227. goto out;
  2228. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2229. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2230. /*
  2231. * it is safe to delete this leaf, it contains only
  2232. * csum items from this inode at an offset >= new_size
  2233. */
  2234. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2235. BUG_ON(ret);
  2236. if (root->ref_cows && leaf_gen < trans->transid) {
  2237. ref = btrfs_alloc_leaf_ref(root, 0);
  2238. if (ref) {
  2239. ref->root_gen = root->root_key.offset;
  2240. ref->bytenr = leaf_start;
  2241. ref->owner = 0;
  2242. ref->generation = leaf_gen;
  2243. ref->nritems = 0;
  2244. btrfs_sort_leaf_ref(ref);
  2245. ret = btrfs_add_leaf_ref(root, ref, 0);
  2246. WARN_ON(ret);
  2247. btrfs_free_leaf_ref(root, ref);
  2248. } else {
  2249. WARN_ON(1);
  2250. }
  2251. }
  2252. next_key:
  2253. btrfs_release_path(root, path);
  2254. if (other_key.objectid == inode->i_ino &&
  2255. other_key.type == key.type && other_key.offset > key.offset) {
  2256. key.offset = other_key.offset;
  2257. cond_resched();
  2258. goto again;
  2259. }
  2260. ret = 0;
  2261. out:
  2262. /* fixup any changes we've made to the path */
  2263. path->lowest_level = 0;
  2264. path->keep_locks = 0;
  2265. btrfs_release_path(root, path);
  2266. return ret;
  2267. }
  2268. #endif
  2269. /*
  2270. * this can truncate away extent items, csum items and directory items.
  2271. * It starts at a high offset and removes keys until it can't find
  2272. * any higher than new_size
  2273. *
  2274. * csum items that cross the new i_size are truncated to the new size
  2275. * as well.
  2276. *
  2277. * min_type is the minimum key type to truncate down to. If set to 0, this
  2278. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2279. */
  2280. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2281. struct btrfs_root *root,
  2282. struct inode *inode,
  2283. u64 new_size, u32 min_type)
  2284. {
  2285. int ret;
  2286. struct btrfs_path *path;
  2287. struct btrfs_key key;
  2288. struct btrfs_key found_key;
  2289. u32 found_type = (u8)-1;
  2290. struct extent_buffer *leaf;
  2291. struct btrfs_file_extent_item *fi;
  2292. u64 extent_start = 0;
  2293. u64 extent_num_bytes = 0;
  2294. u64 extent_offset = 0;
  2295. u64 item_end = 0;
  2296. int found_extent;
  2297. int del_item;
  2298. int pending_del_nr = 0;
  2299. int pending_del_slot = 0;
  2300. int extent_type = -1;
  2301. int encoding;
  2302. u64 mask = root->sectorsize - 1;
  2303. if (root->ref_cows)
  2304. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2305. path = btrfs_alloc_path();
  2306. path->reada = -1;
  2307. BUG_ON(!path);
  2308. /* FIXME, add redo link to tree so we don't leak on crash */
  2309. key.objectid = inode->i_ino;
  2310. key.offset = (u64)-1;
  2311. key.type = (u8)-1;
  2312. search_again:
  2313. path->leave_spinning = 1;
  2314. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2315. if (ret < 0)
  2316. goto error;
  2317. if (ret > 0) {
  2318. /* there are no items in the tree for us to truncate, we're
  2319. * done
  2320. */
  2321. if (path->slots[0] == 0) {
  2322. ret = 0;
  2323. goto error;
  2324. }
  2325. path->slots[0]--;
  2326. }
  2327. while (1) {
  2328. fi = NULL;
  2329. leaf = path->nodes[0];
  2330. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2331. found_type = btrfs_key_type(&found_key);
  2332. encoding = 0;
  2333. if (found_key.objectid != inode->i_ino)
  2334. break;
  2335. if (found_type < min_type)
  2336. break;
  2337. item_end = found_key.offset;
  2338. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2339. fi = btrfs_item_ptr(leaf, path->slots[0],
  2340. struct btrfs_file_extent_item);
  2341. extent_type = btrfs_file_extent_type(leaf, fi);
  2342. encoding = btrfs_file_extent_compression(leaf, fi);
  2343. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2344. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2345. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2346. item_end +=
  2347. btrfs_file_extent_num_bytes(leaf, fi);
  2348. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2349. item_end += btrfs_file_extent_inline_len(leaf,
  2350. fi);
  2351. }
  2352. item_end--;
  2353. }
  2354. if (item_end < new_size) {
  2355. if (found_type == BTRFS_DIR_ITEM_KEY)
  2356. found_type = BTRFS_INODE_ITEM_KEY;
  2357. else if (found_type == BTRFS_EXTENT_ITEM_KEY)
  2358. found_type = BTRFS_EXTENT_DATA_KEY;
  2359. else if (found_type == BTRFS_EXTENT_DATA_KEY)
  2360. found_type = BTRFS_XATTR_ITEM_KEY;
  2361. else if (found_type == BTRFS_XATTR_ITEM_KEY)
  2362. found_type = BTRFS_INODE_REF_KEY;
  2363. else if (found_type)
  2364. found_type--;
  2365. else
  2366. break;
  2367. btrfs_set_key_type(&key, found_type);
  2368. goto next;
  2369. }
  2370. if (found_key.offset >= new_size)
  2371. del_item = 1;
  2372. else
  2373. del_item = 0;
  2374. found_extent = 0;
  2375. /* FIXME, shrink the extent if the ref count is only 1 */
  2376. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2377. goto delete;
  2378. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2379. u64 num_dec;
  2380. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2381. if (!del_item && !encoding) {
  2382. u64 orig_num_bytes =
  2383. btrfs_file_extent_num_bytes(leaf, fi);
  2384. extent_num_bytes = new_size -
  2385. found_key.offset + root->sectorsize - 1;
  2386. extent_num_bytes = extent_num_bytes &
  2387. ~((u64)root->sectorsize - 1);
  2388. btrfs_set_file_extent_num_bytes(leaf, fi,
  2389. extent_num_bytes);
  2390. num_dec = (orig_num_bytes -
  2391. extent_num_bytes);
  2392. if (root->ref_cows && extent_start != 0)
  2393. inode_sub_bytes(inode, num_dec);
  2394. btrfs_mark_buffer_dirty(leaf);
  2395. } else {
  2396. extent_num_bytes =
  2397. btrfs_file_extent_disk_num_bytes(leaf,
  2398. fi);
  2399. extent_offset = found_key.offset -
  2400. btrfs_file_extent_offset(leaf, fi);
  2401. /* FIXME blocksize != 4096 */
  2402. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2403. if (extent_start != 0) {
  2404. found_extent = 1;
  2405. if (root->ref_cows)
  2406. inode_sub_bytes(inode, num_dec);
  2407. }
  2408. }
  2409. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2410. /*
  2411. * we can't truncate inline items that have had
  2412. * special encodings
  2413. */
  2414. if (!del_item &&
  2415. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2416. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2417. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2418. u32 size = new_size - found_key.offset;
  2419. if (root->ref_cows) {
  2420. inode_sub_bytes(inode, item_end + 1 -
  2421. new_size);
  2422. }
  2423. size =
  2424. btrfs_file_extent_calc_inline_size(size);
  2425. ret = btrfs_truncate_item(trans, root, path,
  2426. size, 1);
  2427. BUG_ON(ret);
  2428. } else if (root->ref_cows) {
  2429. inode_sub_bytes(inode, item_end + 1 -
  2430. found_key.offset);
  2431. }
  2432. }
  2433. delete:
  2434. if (del_item) {
  2435. if (!pending_del_nr) {
  2436. /* no pending yet, add ourselves */
  2437. pending_del_slot = path->slots[0];
  2438. pending_del_nr = 1;
  2439. } else if (pending_del_nr &&
  2440. path->slots[0] + 1 == pending_del_slot) {
  2441. /* hop on the pending chunk */
  2442. pending_del_nr++;
  2443. pending_del_slot = path->slots[0];
  2444. } else {
  2445. BUG();
  2446. }
  2447. } else {
  2448. break;
  2449. }
  2450. if (found_extent && root->ref_cows) {
  2451. btrfs_set_path_blocking(path);
  2452. ret = btrfs_free_extent(trans, root, extent_start,
  2453. extent_num_bytes, 0,
  2454. btrfs_header_owner(leaf),
  2455. inode->i_ino, extent_offset);
  2456. BUG_ON(ret);
  2457. }
  2458. next:
  2459. if (path->slots[0] == 0) {
  2460. if (pending_del_nr)
  2461. goto del_pending;
  2462. btrfs_release_path(root, path);
  2463. if (found_type == BTRFS_INODE_ITEM_KEY)
  2464. break;
  2465. goto search_again;
  2466. }
  2467. path->slots[0]--;
  2468. if (pending_del_nr &&
  2469. path->slots[0] + 1 != pending_del_slot) {
  2470. struct btrfs_key debug;
  2471. del_pending:
  2472. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  2473. pending_del_slot);
  2474. ret = btrfs_del_items(trans, root, path,
  2475. pending_del_slot,
  2476. pending_del_nr);
  2477. BUG_ON(ret);
  2478. pending_del_nr = 0;
  2479. btrfs_release_path(root, path);
  2480. if (found_type == BTRFS_INODE_ITEM_KEY)
  2481. break;
  2482. goto search_again;
  2483. }
  2484. }
  2485. ret = 0;
  2486. error:
  2487. if (pending_del_nr) {
  2488. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2489. pending_del_nr);
  2490. }
  2491. btrfs_free_path(path);
  2492. inode->i_sb->s_dirt = 1;
  2493. return ret;
  2494. }
  2495. /*
  2496. * taken from block_truncate_page, but does cow as it zeros out
  2497. * any bytes left in the last page in the file.
  2498. */
  2499. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2500. {
  2501. struct inode *inode = mapping->host;
  2502. struct btrfs_root *root = BTRFS_I(inode)->root;
  2503. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2504. struct btrfs_ordered_extent *ordered;
  2505. char *kaddr;
  2506. u32 blocksize = root->sectorsize;
  2507. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2508. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2509. struct page *page;
  2510. int ret = 0;
  2511. u64 page_start;
  2512. u64 page_end;
  2513. if ((offset & (blocksize - 1)) == 0)
  2514. goto out;
  2515. ret = -ENOMEM;
  2516. again:
  2517. page = grab_cache_page(mapping, index);
  2518. if (!page)
  2519. goto out;
  2520. page_start = page_offset(page);
  2521. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2522. if (!PageUptodate(page)) {
  2523. ret = btrfs_readpage(NULL, page);
  2524. lock_page(page);
  2525. if (page->mapping != mapping) {
  2526. unlock_page(page);
  2527. page_cache_release(page);
  2528. goto again;
  2529. }
  2530. if (!PageUptodate(page)) {
  2531. ret = -EIO;
  2532. goto out_unlock;
  2533. }
  2534. }
  2535. wait_on_page_writeback(page);
  2536. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2537. set_page_extent_mapped(page);
  2538. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2539. if (ordered) {
  2540. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2541. unlock_page(page);
  2542. page_cache_release(page);
  2543. btrfs_start_ordered_extent(inode, ordered, 1);
  2544. btrfs_put_ordered_extent(ordered);
  2545. goto again;
  2546. }
  2547. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2548. ret = 0;
  2549. if (offset != PAGE_CACHE_SIZE) {
  2550. kaddr = kmap(page);
  2551. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2552. flush_dcache_page(page);
  2553. kunmap(page);
  2554. }
  2555. ClearPageChecked(page);
  2556. set_page_dirty(page);
  2557. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2558. out_unlock:
  2559. unlock_page(page);
  2560. page_cache_release(page);
  2561. out:
  2562. return ret;
  2563. }
  2564. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2565. {
  2566. struct btrfs_trans_handle *trans;
  2567. struct btrfs_root *root = BTRFS_I(inode)->root;
  2568. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2569. struct extent_map *em;
  2570. u64 mask = root->sectorsize - 1;
  2571. u64 hole_start = (inode->i_size + mask) & ~mask;
  2572. u64 block_end = (size + mask) & ~mask;
  2573. u64 last_byte;
  2574. u64 cur_offset;
  2575. u64 hole_size;
  2576. int err;
  2577. if (size <= hole_start)
  2578. return 0;
  2579. err = btrfs_check_metadata_free_space(root);
  2580. if (err)
  2581. return err;
  2582. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2583. while (1) {
  2584. struct btrfs_ordered_extent *ordered;
  2585. btrfs_wait_ordered_range(inode, hole_start,
  2586. block_end - hole_start);
  2587. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2588. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2589. if (!ordered)
  2590. break;
  2591. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2592. btrfs_put_ordered_extent(ordered);
  2593. }
  2594. trans = btrfs_start_transaction(root, 1);
  2595. btrfs_set_trans_block_group(trans, inode);
  2596. cur_offset = hole_start;
  2597. while (1) {
  2598. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2599. block_end - cur_offset, 0);
  2600. BUG_ON(IS_ERR(em) || !em);
  2601. last_byte = min(extent_map_end(em), block_end);
  2602. last_byte = (last_byte + mask) & ~mask;
  2603. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2604. u64 hint_byte = 0;
  2605. hole_size = last_byte - cur_offset;
  2606. err = btrfs_drop_extents(trans, root, inode,
  2607. cur_offset,
  2608. cur_offset + hole_size,
  2609. block_end,
  2610. cur_offset, &hint_byte);
  2611. if (err)
  2612. break;
  2613. err = btrfs_insert_file_extent(trans, root,
  2614. inode->i_ino, cur_offset, 0,
  2615. 0, hole_size, 0, hole_size,
  2616. 0, 0, 0);
  2617. btrfs_drop_extent_cache(inode, hole_start,
  2618. last_byte - 1, 0);
  2619. }
  2620. free_extent_map(em);
  2621. cur_offset = last_byte;
  2622. if (err || cur_offset >= block_end)
  2623. break;
  2624. }
  2625. btrfs_end_transaction(trans, root);
  2626. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2627. return err;
  2628. }
  2629. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2630. {
  2631. struct inode *inode = dentry->d_inode;
  2632. int err;
  2633. err = inode_change_ok(inode, attr);
  2634. if (err)
  2635. return err;
  2636. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  2637. if (attr->ia_size > inode->i_size) {
  2638. err = btrfs_cont_expand(inode, attr->ia_size);
  2639. if (err)
  2640. return err;
  2641. } else if (inode->i_size > 0 &&
  2642. attr->ia_size == 0) {
  2643. /* we're truncating a file that used to have good
  2644. * data down to zero. Make sure it gets into
  2645. * the ordered flush list so that any new writes
  2646. * get down to disk quickly.
  2647. */
  2648. BTRFS_I(inode)->ordered_data_close = 1;
  2649. }
  2650. }
  2651. err = inode_setattr(inode, attr);
  2652. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2653. err = btrfs_acl_chmod(inode);
  2654. return err;
  2655. }
  2656. void btrfs_delete_inode(struct inode *inode)
  2657. {
  2658. struct btrfs_trans_handle *trans;
  2659. struct btrfs_root *root = BTRFS_I(inode)->root;
  2660. unsigned long nr;
  2661. int ret;
  2662. truncate_inode_pages(&inode->i_data, 0);
  2663. if (is_bad_inode(inode)) {
  2664. btrfs_orphan_del(NULL, inode);
  2665. goto no_delete;
  2666. }
  2667. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2668. btrfs_i_size_write(inode, 0);
  2669. trans = btrfs_join_transaction(root, 1);
  2670. btrfs_set_trans_block_group(trans, inode);
  2671. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  2672. if (ret) {
  2673. btrfs_orphan_del(NULL, inode);
  2674. goto no_delete_lock;
  2675. }
  2676. btrfs_orphan_del(trans, inode);
  2677. nr = trans->blocks_used;
  2678. clear_inode(inode);
  2679. btrfs_end_transaction(trans, root);
  2680. btrfs_btree_balance_dirty(root, nr);
  2681. return;
  2682. no_delete_lock:
  2683. nr = trans->blocks_used;
  2684. btrfs_end_transaction(trans, root);
  2685. btrfs_btree_balance_dirty(root, nr);
  2686. no_delete:
  2687. clear_inode(inode);
  2688. }
  2689. /*
  2690. * this returns the key found in the dir entry in the location pointer.
  2691. * If no dir entries were found, location->objectid is 0.
  2692. */
  2693. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  2694. struct btrfs_key *location)
  2695. {
  2696. const char *name = dentry->d_name.name;
  2697. int namelen = dentry->d_name.len;
  2698. struct btrfs_dir_item *di;
  2699. struct btrfs_path *path;
  2700. struct btrfs_root *root = BTRFS_I(dir)->root;
  2701. int ret = 0;
  2702. path = btrfs_alloc_path();
  2703. BUG_ON(!path);
  2704. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  2705. namelen, 0);
  2706. if (IS_ERR(di))
  2707. ret = PTR_ERR(di);
  2708. if (!di || IS_ERR(di))
  2709. goto out_err;
  2710. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  2711. out:
  2712. btrfs_free_path(path);
  2713. return ret;
  2714. out_err:
  2715. location->objectid = 0;
  2716. goto out;
  2717. }
  2718. /*
  2719. * when we hit a tree root in a directory, the btrfs part of the inode
  2720. * needs to be changed to reflect the root directory of the tree root. This
  2721. * is kind of like crossing a mount point.
  2722. */
  2723. static int fixup_tree_root_location(struct btrfs_root *root,
  2724. struct btrfs_key *location,
  2725. struct btrfs_root **sub_root,
  2726. struct dentry *dentry)
  2727. {
  2728. struct btrfs_root_item *ri;
  2729. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  2730. return 0;
  2731. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  2732. return 0;
  2733. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  2734. dentry->d_name.name,
  2735. dentry->d_name.len);
  2736. if (IS_ERR(*sub_root))
  2737. return PTR_ERR(*sub_root);
  2738. ri = &(*sub_root)->root_item;
  2739. location->objectid = btrfs_root_dirid(ri);
  2740. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  2741. location->offset = 0;
  2742. return 0;
  2743. }
  2744. static void inode_tree_add(struct inode *inode)
  2745. {
  2746. struct btrfs_root *root = BTRFS_I(inode)->root;
  2747. struct btrfs_inode *entry;
  2748. struct rb_node **p = &root->inode_tree.rb_node;
  2749. struct rb_node *parent = NULL;
  2750. spin_lock(&root->inode_lock);
  2751. while (*p) {
  2752. parent = *p;
  2753. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  2754. if (inode->i_ino < entry->vfs_inode.i_ino)
  2755. p = &(*p)->rb_left;
  2756. else if (inode->i_ino > entry->vfs_inode.i_ino)
  2757. p = &(*p)->rb_right;
  2758. else {
  2759. WARN_ON(!(entry->vfs_inode.i_state &
  2760. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  2761. break;
  2762. }
  2763. }
  2764. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  2765. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  2766. spin_unlock(&root->inode_lock);
  2767. }
  2768. static void inode_tree_del(struct inode *inode)
  2769. {
  2770. struct btrfs_root *root = BTRFS_I(inode)->root;
  2771. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  2772. spin_lock(&root->inode_lock);
  2773. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  2774. spin_unlock(&root->inode_lock);
  2775. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  2776. }
  2777. }
  2778. static noinline void init_btrfs_i(struct inode *inode)
  2779. {
  2780. struct btrfs_inode *bi = BTRFS_I(inode);
  2781. bi->i_acl = BTRFS_ACL_NOT_CACHED;
  2782. bi->i_default_acl = BTRFS_ACL_NOT_CACHED;
  2783. bi->generation = 0;
  2784. bi->sequence = 0;
  2785. bi->last_trans = 0;
  2786. bi->logged_trans = 0;
  2787. bi->delalloc_bytes = 0;
  2788. bi->reserved_bytes = 0;
  2789. bi->disk_i_size = 0;
  2790. bi->flags = 0;
  2791. bi->index_cnt = (u64)-1;
  2792. bi->last_unlink_trans = 0;
  2793. bi->ordered_data_close = 0;
  2794. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  2795. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  2796. inode->i_mapping, GFP_NOFS);
  2797. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  2798. inode->i_mapping, GFP_NOFS);
  2799. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  2800. INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
  2801. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  2802. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  2803. mutex_init(&BTRFS_I(inode)->extent_mutex);
  2804. mutex_init(&BTRFS_I(inode)->log_mutex);
  2805. }
  2806. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  2807. {
  2808. struct btrfs_iget_args *args = p;
  2809. inode->i_ino = args->ino;
  2810. init_btrfs_i(inode);
  2811. BTRFS_I(inode)->root = args->root;
  2812. btrfs_set_inode_space_info(args->root, inode);
  2813. return 0;
  2814. }
  2815. static int btrfs_find_actor(struct inode *inode, void *opaque)
  2816. {
  2817. struct btrfs_iget_args *args = opaque;
  2818. return args->ino == inode->i_ino &&
  2819. args->root == BTRFS_I(inode)->root;
  2820. }
  2821. static struct inode *btrfs_iget_locked(struct super_block *s,
  2822. u64 objectid,
  2823. struct btrfs_root *root)
  2824. {
  2825. struct inode *inode;
  2826. struct btrfs_iget_args args;
  2827. args.ino = objectid;
  2828. args.root = root;
  2829. inode = iget5_locked(s, objectid, btrfs_find_actor,
  2830. btrfs_init_locked_inode,
  2831. (void *)&args);
  2832. return inode;
  2833. }
  2834. /* Get an inode object given its location and corresponding root.
  2835. * Returns in *is_new if the inode was read from disk
  2836. */
  2837. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  2838. struct btrfs_root *root)
  2839. {
  2840. struct inode *inode;
  2841. inode = btrfs_iget_locked(s, location->objectid, root);
  2842. if (!inode)
  2843. return ERR_PTR(-ENOMEM);
  2844. if (inode->i_state & I_NEW) {
  2845. BTRFS_I(inode)->root = root;
  2846. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  2847. btrfs_read_locked_inode(inode);
  2848. inode_tree_add(inode);
  2849. unlock_new_inode(inode);
  2850. }
  2851. return inode;
  2852. }
  2853. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  2854. {
  2855. struct inode *inode;
  2856. struct btrfs_inode *bi = BTRFS_I(dir);
  2857. struct btrfs_root *root = bi->root;
  2858. struct btrfs_root *sub_root = root;
  2859. struct btrfs_key location;
  2860. int ret;
  2861. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2862. return ERR_PTR(-ENAMETOOLONG);
  2863. ret = btrfs_inode_by_name(dir, dentry, &location);
  2864. if (ret < 0)
  2865. return ERR_PTR(ret);
  2866. inode = NULL;
  2867. if (location.objectid) {
  2868. ret = fixup_tree_root_location(root, &location, &sub_root,
  2869. dentry);
  2870. if (ret < 0)
  2871. return ERR_PTR(ret);
  2872. if (ret > 0)
  2873. return ERR_PTR(-ENOENT);
  2874. inode = btrfs_iget(dir->i_sb, &location, sub_root);
  2875. if (IS_ERR(inode))
  2876. return ERR_CAST(inode);
  2877. }
  2878. return inode;
  2879. }
  2880. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  2881. struct nameidata *nd)
  2882. {
  2883. struct inode *inode;
  2884. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2885. return ERR_PTR(-ENAMETOOLONG);
  2886. inode = btrfs_lookup_dentry(dir, dentry);
  2887. if (IS_ERR(inode))
  2888. return ERR_CAST(inode);
  2889. return d_splice_alias(inode, dentry);
  2890. }
  2891. static unsigned char btrfs_filetype_table[] = {
  2892. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  2893. };
  2894. static int btrfs_real_readdir(struct file *filp, void *dirent,
  2895. filldir_t filldir)
  2896. {
  2897. struct inode *inode = filp->f_dentry->d_inode;
  2898. struct btrfs_root *root = BTRFS_I(inode)->root;
  2899. struct btrfs_item *item;
  2900. struct btrfs_dir_item *di;
  2901. struct btrfs_key key;
  2902. struct btrfs_key found_key;
  2903. struct btrfs_path *path;
  2904. int ret;
  2905. u32 nritems;
  2906. struct extent_buffer *leaf;
  2907. int slot;
  2908. int advance;
  2909. unsigned char d_type;
  2910. int over = 0;
  2911. u32 di_cur;
  2912. u32 di_total;
  2913. u32 di_len;
  2914. int key_type = BTRFS_DIR_INDEX_KEY;
  2915. char tmp_name[32];
  2916. char *name_ptr;
  2917. int name_len;
  2918. /* FIXME, use a real flag for deciding about the key type */
  2919. if (root->fs_info->tree_root == root)
  2920. key_type = BTRFS_DIR_ITEM_KEY;
  2921. /* special case for "." */
  2922. if (filp->f_pos == 0) {
  2923. over = filldir(dirent, ".", 1,
  2924. 1, inode->i_ino,
  2925. DT_DIR);
  2926. if (over)
  2927. return 0;
  2928. filp->f_pos = 1;
  2929. }
  2930. /* special case for .., just use the back ref */
  2931. if (filp->f_pos == 1) {
  2932. u64 pino = parent_ino(filp->f_path.dentry);
  2933. over = filldir(dirent, "..", 2,
  2934. 2, pino, DT_DIR);
  2935. if (over)
  2936. return 0;
  2937. filp->f_pos = 2;
  2938. }
  2939. path = btrfs_alloc_path();
  2940. path->reada = 2;
  2941. btrfs_set_key_type(&key, key_type);
  2942. key.offset = filp->f_pos;
  2943. key.objectid = inode->i_ino;
  2944. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2945. if (ret < 0)
  2946. goto err;
  2947. advance = 0;
  2948. while (1) {
  2949. leaf = path->nodes[0];
  2950. nritems = btrfs_header_nritems(leaf);
  2951. slot = path->slots[0];
  2952. if (advance || slot >= nritems) {
  2953. if (slot >= nritems - 1) {
  2954. ret = btrfs_next_leaf(root, path);
  2955. if (ret)
  2956. break;
  2957. leaf = path->nodes[0];
  2958. nritems = btrfs_header_nritems(leaf);
  2959. slot = path->slots[0];
  2960. } else {
  2961. slot++;
  2962. path->slots[0]++;
  2963. }
  2964. }
  2965. advance = 1;
  2966. item = btrfs_item_nr(leaf, slot);
  2967. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2968. if (found_key.objectid != key.objectid)
  2969. break;
  2970. if (btrfs_key_type(&found_key) != key_type)
  2971. break;
  2972. if (found_key.offset < filp->f_pos)
  2973. continue;
  2974. filp->f_pos = found_key.offset;
  2975. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  2976. di_cur = 0;
  2977. di_total = btrfs_item_size(leaf, item);
  2978. while (di_cur < di_total) {
  2979. struct btrfs_key location;
  2980. name_len = btrfs_dir_name_len(leaf, di);
  2981. if (name_len <= sizeof(tmp_name)) {
  2982. name_ptr = tmp_name;
  2983. } else {
  2984. name_ptr = kmalloc(name_len, GFP_NOFS);
  2985. if (!name_ptr) {
  2986. ret = -ENOMEM;
  2987. goto err;
  2988. }
  2989. }
  2990. read_extent_buffer(leaf, name_ptr,
  2991. (unsigned long)(di + 1), name_len);
  2992. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  2993. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  2994. /* is this a reference to our own snapshot? If so
  2995. * skip it
  2996. */
  2997. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  2998. location.objectid == root->root_key.objectid) {
  2999. over = 0;
  3000. goto skip;
  3001. }
  3002. over = filldir(dirent, name_ptr, name_len,
  3003. found_key.offset, location.objectid,
  3004. d_type);
  3005. skip:
  3006. if (name_ptr != tmp_name)
  3007. kfree(name_ptr);
  3008. if (over)
  3009. goto nopos;
  3010. di_len = btrfs_dir_name_len(leaf, di) +
  3011. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3012. di_cur += di_len;
  3013. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3014. }
  3015. }
  3016. /* Reached end of directory/root. Bump pos past the last item. */
  3017. if (key_type == BTRFS_DIR_INDEX_KEY)
  3018. filp->f_pos = INT_LIMIT(off_t);
  3019. else
  3020. filp->f_pos++;
  3021. nopos:
  3022. ret = 0;
  3023. err:
  3024. btrfs_free_path(path);
  3025. return ret;
  3026. }
  3027. int btrfs_write_inode(struct inode *inode, int wait)
  3028. {
  3029. struct btrfs_root *root = BTRFS_I(inode)->root;
  3030. struct btrfs_trans_handle *trans;
  3031. int ret = 0;
  3032. if (root->fs_info->btree_inode == inode)
  3033. return 0;
  3034. if (wait) {
  3035. trans = btrfs_join_transaction(root, 1);
  3036. btrfs_set_trans_block_group(trans, inode);
  3037. ret = btrfs_commit_transaction(trans, root);
  3038. }
  3039. return ret;
  3040. }
  3041. /*
  3042. * This is somewhat expensive, updating the tree every time the
  3043. * inode changes. But, it is most likely to find the inode in cache.
  3044. * FIXME, needs more benchmarking...there are no reasons other than performance
  3045. * to keep or drop this code.
  3046. */
  3047. void btrfs_dirty_inode(struct inode *inode)
  3048. {
  3049. struct btrfs_root *root = BTRFS_I(inode)->root;
  3050. struct btrfs_trans_handle *trans;
  3051. trans = btrfs_join_transaction(root, 1);
  3052. btrfs_set_trans_block_group(trans, inode);
  3053. btrfs_update_inode(trans, root, inode);
  3054. btrfs_end_transaction(trans, root);
  3055. }
  3056. /*
  3057. * find the highest existing sequence number in a directory
  3058. * and then set the in-memory index_cnt variable to reflect
  3059. * free sequence numbers
  3060. */
  3061. static int btrfs_set_inode_index_count(struct inode *inode)
  3062. {
  3063. struct btrfs_root *root = BTRFS_I(inode)->root;
  3064. struct btrfs_key key, found_key;
  3065. struct btrfs_path *path;
  3066. struct extent_buffer *leaf;
  3067. int ret;
  3068. key.objectid = inode->i_ino;
  3069. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3070. key.offset = (u64)-1;
  3071. path = btrfs_alloc_path();
  3072. if (!path)
  3073. return -ENOMEM;
  3074. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3075. if (ret < 0)
  3076. goto out;
  3077. /* FIXME: we should be able to handle this */
  3078. if (ret == 0)
  3079. goto out;
  3080. ret = 0;
  3081. /*
  3082. * MAGIC NUMBER EXPLANATION:
  3083. * since we search a directory based on f_pos we have to start at 2
  3084. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3085. * else has to start at 2
  3086. */
  3087. if (path->slots[0] == 0) {
  3088. BTRFS_I(inode)->index_cnt = 2;
  3089. goto out;
  3090. }
  3091. path->slots[0]--;
  3092. leaf = path->nodes[0];
  3093. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3094. if (found_key.objectid != inode->i_ino ||
  3095. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3096. BTRFS_I(inode)->index_cnt = 2;
  3097. goto out;
  3098. }
  3099. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3100. out:
  3101. btrfs_free_path(path);
  3102. return ret;
  3103. }
  3104. /*
  3105. * helper to find a free sequence number in a given directory. This current
  3106. * code is very simple, later versions will do smarter things in the btree
  3107. */
  3108. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3109. {
  3110. int ret = 0;
  3111. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3112. ret = btrfs_set_inode_index_count(dir);
  3113. if (ret)
  3114. return ret;
  3115. }
  3116. *index = BTRFS_I(dir)->index_cnt;
  3117. BTRFS_I(dir)->index_cnt++;
  3118. return ret;
  3119. }
  3120. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3121. struct btrfs_root *root,
  3122. struct inode *dir,
  3123. const char *name, int name_len,
  3124. u64 ref_objectid, u64 objectid,
  3125. u64 alloc_hint, int mode, u64 *index)
  3126. {
  3127. struct inode *inode;
  3128. struct btrfs_inode_item *inode_item;
  3129. struct btrfs_key *location;
  3130. struct btrfs_path *path;
  3131. struct btrfs_inode_ref *ref;
  3132. struct btrfs_key key[2];
  3133. u32 sizes[2];
  3134. unsigned long ptr;
  3135. int ret;
  3136. int owner;
  3137. path = btrfs_alloc_path();
  3138. BUG_ON(!path);
  3139. inode = new_inode(root->fs_info->sb);
  3140. if (!inode)
  3141. return ERR_PTR(-ENOMEM);
  3142. if (dir) {
  3143. ret = btrfs_set_inode_index(dir, index);
  3144. if (ret) {
  3145. iput(inode);
  3146. return ERR_PTR(ret);
  3147. }
  3148. }
  3149. /*
  3150. * index_cnt is ignored for everything but a dir,
  3151. * btrfs_get_inode_index_count has an explanation for the magic
  3152. * number
  3153. */
  3154. init_btrfs_i(inode);
  3155. BTRFS_I(inode)->index_cnt = 2;
  3156. BTRFS_I(inode)->root = root;
  3157. BTRFS_I(inode)->generation = trans->transid;
  3158. btrfs_set_inode_space_info(root, inode);
  3159. if (mode & S_IFDIR)
  3160. owner = 0;
  3161. else
  3162. owner = 1;
  3163. BTRFS_I(inode)->block_group =
  3164. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3165. if ((mode & S_IFREG)) {
  3166. if (btrfs_test_opt(root, NODATASUM))
  3167. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3168. if (btrfs_test_opt(root, NODATACOW))
  3169. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3170. }
  3171. key[0].objectid = objectid;
  3172. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3173. key[0].offset = 0;
  3174. key[1].objectid = objectid;
  3175. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3176. key[1].offset = ref_objectid;
  3177. sizes[0] = sizeof(struct btrfs_inode_item);
  3178. sizes[1] = name_len + sizeof(*ref);
  3179. path->leave_spinning = 1;
  3180. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3181. if (ret != 0)
  3182. goto fail;
  3183. if (objectid > root->highest_inode)
  3184. root->highest_inode = objectid;
  3185. inode->i_uid = current_fsuid();
  3186. if (dir && (dir->i_mode & S_ISGID)) {
  3187. inode->i_gid = dir->i_gid;
  3188. if (S_ISDIR(mode))
  3189. mode |= S_ISGID;
  3190. } else
  3191. inode->i_gid = current_fsgid();
  3192. inode->i_mode = mode;
  3193. inode->i_ino = objectid;
  3194. inode_set_bytes(inode, 0);
  3195. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3196. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3197. struct btrfs_inode_item);
  3198. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3199. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3200. struct btrfs_inode_ref);
  3201. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3202. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3203. ptr = (unsigned long)(ref + 1);
  3204. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3205. btrfs_mark_buffer_dirty(path->nodes[0]);
  3206. btrfs_free_path(path);
  3207. location = &BTRFS_I(inode)->location;
  3208. location->objectid = objectid;
  3209. location->offset = 0;
  3210. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3211. btrfs_inherit_iflags(inode, dir);
  3212. insert_inode_hash(inode);
  3213. inode_tree_add(inode);
  3214. return inode;
  3215. fail:
  3216. if (dir)
  3217. BTRFS_I(dir)->index_cnt--;
  3218. btrfs_free_path(path);
  3219. iput(inode);
  3220. return ERR_PTR(ret);
  3221. }
  3222. static inline u8 btrfs_inode_type(struct inode *inode)
  3223. {
  3224. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3225. }
  3226. /*
  3227. * utility function to add 'inode' into 'parent_inode' with
  3228. * a give name and a given sequence number.
  3229. * if 'add_backref' is true, also insert a backref from the
  3230. * inode to the parent directory.
  3231. */
  3232. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3233. struct inode *parent_inode, struct inode *inode,
  3234. const char *name, int name_len, int add_backref, u64 index)
  3235. {
  3236. int ret;
  3237. struct btrfs_key key;
  3238. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3239. key.objectid = inode->i_ino;
  3240. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3241. key.offset = 0;
  3242. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3243. parent_inode->i_ino,
  3244. &key, btrfs_inode_type(inode),
  3245. index);
  3246. if (ret == 0) {
  3247. if (add_backref) {
  3248. ret = btrfs_insert_inode_ref(trans, root,
  3249. name, name_len,
  3250. inode->i_ino,
  3251. parent_inode->i_ino,
  3252. index);
  3253. }
  3254. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3255. name_len * 2);
  3256. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3257. ret = btrfs_update_inode(trans, root, parent_inode);
  3258. }
  3259. return ret;
  3260. }
  3261. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3262. struct dentry *dentry, struct inode *inode,
  3263. int backref, u64 index)
  3264. {
  3265. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3266. inode, dentry->d_name.name,
  3267. dentry->d_name.len, backref, index);
  3268. if (!err) {
  3269. d_instantiate(dentry, inode);
  3270. return 0;
  3271. }
  3272. if (err > 0)
  3273. err = -EEXIST;
  3274. return err;
  3275. }
  3276. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3277. int mode, dev_t rdev)
  3278. {
  3279. struct btrfs_trans_handle *trans;
  3280. struct btrfs_root *root = BTRFS_I(dir)->root;
  3281. struct inode *inode = NULL;
  3282. int err;
  3283. int drop_inode = 0;
  3284. u64 objectid;
  3285. unsigned long nr = 0;
  3286. u64 index = 0;
  3287. if (!new_valid_dev(rdev))
  3288. return -EINVAL;
  3289. err = btrfs_check_metadata_free_space(root);
  3290. if (err)
  3291. goto fail;
  3292. trans = btrfs_start_transaction(root, 1);
  3293. btrfs_set_trans_block_group(trans, dir);
  3294. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3295. if (err) {
  3296. err = -ENOSPC;
  3297. goto out_unlock;
  3298. }
  3299. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3300. dentry->d_name.len,
  3301. dentry->d_parent->d_inode->i_ino, objectid,
  3302. BTRFS_I(dir)->block_group, mode, &index);
  3303. err = PTR_ERR(inode);
  3304. if (IS_ERR(inode))
  3305. goto out_unlock;
  3306. err = btrfs_init_inode_security(inode, dir);
  3307. if (err) {
  3308. drop_inode = 1;
  3309. goto out_unlock;
  3310. }
  3311. btrfs_set_trans_block_group(trans, inode);
  3312. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3313. if (err)
  3314. drop_inode = 1;
  3315. else {
  3316. inode->i_op = &btrfs_special_inode_operations;
  3317. init_special_inode(inode, inode->i_mode, rdev);
  3318. btrfs_update_inode(trans, root, inode);
  3319. }
  3320. dir->i_sb->s_dirt = 1;
  3321. btrfs_update_inode_block_group(trans, inode);
  3322. btrfs_update_inode_block_group(trans, dir);
  3323. out_unlock:
  3324. nr = trans->blocks_used;
  3325. btrfs_end_transaction_throttle(trans, root);
  3326. fail:
  3327. if (drop_inode) {
  3328. inode_dec_link_count(inode);
  3329. iput(inode);
  3330. }
  3331. btrfs_btree_balance_dirty(root, nr);
  3332. return err;
  3333. }
  3334. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3335. int mode, struct nameidata *nd)
  3336. {
  3337. struct btrfs_trans_handle *trans;
  3338. struct btrfs_root *root = BTRFS_I(dir)->root;
  3339. struct inode *inode = NULL;
  3340. int err;
  3341. int drop_inode = 0;
  3342. unsigned long nr = 0;
  3343. u64 objectid;
  3344. u64 index = 0;
  3345. err = btrfs_check_metadata_free_space(root);
  3346. if (err)
  3347. goto fail;
  3348. trans = btrfs_start_transaction(root, 1);
  3349. btrfs_set_trans_block_group(trans, dir);
  3350. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3351. if (err) {
  3352. err = -ENOSPC;
  3353. goto out_unlock;
  3354. }
  3355. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3356. dentry->d_name.len,
  3357. dentry->d_parent->d_inode->i_ino,
  3358. objectid, BTRFS_I(dir)->block_group, mode,
  3359. &index);
  3360. err = PTR_ERR(inode);
  3361. if (IS_ERR(inode))
  3362. goto out_unlock;
  3363. err = btrfs_init_inode_security(inode, dir);
  3364. if (err) {
  3365. drop_inode = 1;
  3366. goto out_unlock;
  3367. }
  3368. btrfs_set_trans_block_group(trans, inode);
  3369. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3370. if (err)
  3371. drop_inode = 1;
  3372. else {
  3373. inode->i_mapping->a_ops = &btrfs_aops;
  3374. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3375. inode->i_fop = &btrfs_file_operations;
  3376. inode->i_op = &btrfs_file_inode_operations;
  3377. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3378. }
  3379. dir->i_sb->s_dirt = 1;
  3380. btrfs_update_inode_block_group(trans, inode);
  3381. btrfs_update_inode_block_group(trans, dir);
  3382. out_unlock:
  3383. nr = trans->blocks_used;
  3384. btrfs_end_transaction_throttle(trans, root);
  3385. fail:
  3386. if (drop_inode) {
  3387. inode_dec_link_count(inode);
  3388. iput(inode);
  3389. }
  3390. btrfs_btree_balance_dirty(root, nr);
  3391. return err;
  3392. }
  3393. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3394. struct dentry *dentry)
  3395. {
  3396. struct btrfs_trans_handle *trans;
  3397. struct btrfs_root *root = BTRFS_I(dir)->root;
  3398. struct inode *inode = old_dentry->d_inode;
  3399. u64 index;
  3400. unsigned long nr = 0;
  3401. int err;
  3402. int drop_inode = 0;
  3403. if (inode->i_nlink == 0)
  3404. return -ENOENT;
  3405. btrfs_inc_nlink(inode);
  3406. err = btrfs_check_metadata_free_space(root);
  3407. if (err)
  3408. goto fail;
  3409. err = btrfs_set_inode_index(dir, &index);
  3410. if (err)
  3411. goto fail;
  3412. trans = btrfs_start_transaction(root, 1);
  3413. btrfs_set_trans_block_group(trans, dir);
  3414. atomic_inc(&inode->i_count);
  3415. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3416. if (err)
  3417. drop_inode = 1;
  3418. dir->i_sb->s_dirt = 1;
  3419. btrfs_update_inode_block_group(trans, dir);
  3420. err = btrfs_update_inode(trans, root, inode);
  3421. if (err)
  3422. drop_inode = 1;
  3423. nr = trans->blocks_used;
  3424. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  3425. btrfs_end_transaction_throttle(trans, root);
  3426. fail:
  3427. if (drop_inode) {
  3428. inode_dec_link_count(inode);
  3429. iput(inode);
  3430. }
  3431. btrfs_btree_balance_dirty(root, nr);
  3432. return err;
  3433. }
  3434. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3435. {
  3436. struct inode *inode = NULL;
  3437. struct btrfs_trans_handle *trans;
  3438. struct btrfs_root *root = BTRFS_I(dir)->root;
  3439. int err = 0;
  3440. int drop_on_err = 0;
  3441. u64 objectid = 0;
  3442. u64 index = 0;
  3443. unsigned long nr = 1;
  3444. err = btrfs_check_metadata_free_space(root);
  3445. if (err)
  3446. goto out_unlock;
  3447. trans = btrfs_start_transaction(root, 1);
  3448. btrfs_set_trans_block_group(trans, dir);
  3449. if (IS_ERR(trans)) {
  3450. err = PTR_ERR(trans);
  3451. goto out_unlock;
  3452. }
  3453. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3454. if (err) {
  3455. err = -ENOSPC;
  3456. goto out_unlock;
  3457. }
  3458. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3459. dentry->d_name.len,
  3460. dentry->d_parent->d_inode->i_ino, objectid,
  3461. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3462. &index);
  3463. if (IS_ERR(inode)) {
  3464. err = PTR_ERR(inode);
  3465. goto out_fail;
  3466. }
  3467. drop_on_err = 1;
  3468. err = btrfs_init_inode_security(inode, dir);
  3469. if (err)
  3470. goto out_fail;
  3471. inode->i_op = &btrfs_dir_inode_operations;
  3472. inode->i_fop = &btrfs_dir_file_operations;
  3473. btrfs_set_trans_block_group(trans, inode);
  3474. btrfs_i_size_write(inode, 0);
  3475. err = btrfs_update_inode(trans, root, inode);
  3476. if (err)
  3477. goto out_fail;
  3478. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3479. inode, dentry->d_name.name,
  3480. dentry->d_name.len, 0, index);
  3481. if (err)
  3482. goto out_fail;
  3483. d_instantiate(dentry, inode);
  3484. drop_on_err = 0;
  3485. dir->i_sb->s_dirt = 1;
  3486. btrfs_update_inode_block_group(trans, inode);
  3487. btrfs_update_inode_block_group(trans, dir);
  3488. out_fail:
  3489. nr = trans->blocks_used;
  3490. btrfs_end_transaction_throttle(trans, root);
  3491. out_unlock:
  3492. if (drop_on_err)
  3493. iput(inode);
  3494. btrfs_btree_balance_dirty(root, nr);
  3495. return err;
  3496. }
  3497. /* helper for btfs_get_extent. Given an existing extent in the tree,
  3498. * and an extent that you want to insert, deal with overlap and insert
  3499. * the new extent into the tree.
  3500. */
  3501. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  3502. struct extent_map *existing,
  3503. struct extent_map *em,
  3504. u64 map_start, u64 map_len)
  3505. {
  3506. u64 start_diff;
  3507. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  3508. start_diff = map_start - em->start;
  3509. em->start = map_start;
  3510. em->len = map_len;
  3511. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  3512. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  3513. em->block_start += start_diff;
  3514. em->block_len -= start_diff;
  3515. }
  3516. return add_extent_mapping(em_tree, em);
  3517. }
  3518. static noinline int uncompress_inline(struct btrfs_path *path,
  3519. struct inode *inode, struct page *page,
  3520. size_t pg_offset, u64 extent_offset,
  3521. struct btrfs_file_extent_item *item)
  3522. {
  3523. int ret;
  3524. struct extent_buffer *leaf = path->nodes[0];
  3525. char *tmp;
  3526. size_t max_size;
  3527. unsigned long inline_size;
  3528. unsigned long ptr;
  3529. WARN_ON(pg_offset != 0);
  3530. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  3531. inline_size = btrfs_file_extent_inline_item_len(leaf,
  3532. btrfs_item_nr(leaf, path->slots[0]));
  3533. tmp = kmalloc(inline_size, GFP_NOFS);
  3534. ptr = btrfs_file_extent_inline_start(item);
  3535. read_extent_buffer(leaf, tmp, ptr, inline_size);
  3536. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  3537. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  3538. inline_size, max_size);
  3539. if (ret) {
  3540. char *kaddr = kmap_atomic(page, KM_USER0);
  3541. unsigned long copy_size = min_t(u64,
  3542. PAGE_CACHE_SIZE - pg_offset,
  3543. max_size - extent_offset);
  3544. memset(kaddr + pg_offset, 0, copy_size);
  3545. kunmap_atomic(kaddr, KM_USER0);
  3546. }
  3547. kfree(tmp);
  3548. return 0;
  3549. }
  3550. /*
  3551. * a bit scary, this does extent mapping from logical file offset to the disk.
  3552. * the ugly parts come from merging extents from the disk with the in-ram
  3553. * representation. This gets more complex because of the data=ordered code,
  3554. * where the in-ram extents might be locked pending data=ordered completion.
  3555. *
  3556. * This also copies inline extents directly into the page.
  3557. */
  3558. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  3559. size_t pg_offset, u64 start, u64 len,
  3560. int create)
  3561. {
  3562. int ret;
  3563. int err = 0;
  3564. u64 bytenr;
  3565. u64 extent_start = 0;
  3566. u64 extent_end = 0;
  3567. u64 objectid = inode->i_ino;
  3568. u32 found_type;
  3569. struct btrfs_path *path = NULL;
  3570. struct btrfs_root *root = BTRFS_I(inode)->root;
  3571. struct btrfs_file_extent_item *item;
  3572. struct extent_buffer *leaf;
  3573. struct btrfs_key found_key;
  3574. struct extent_map *em = NULL;
  3575. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3576. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3577. struct btrfs_trans_handle *trans = NULL;
  3578. int compressed;
  3579. again:
  3580. spin_lock(&em_tree->lock);
  3581. em = lookup_extent_mapping(em_tree, start, len);
  3582. if (em)
  3583. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3584. spin_unlock(&em_tree->lock);
  3585. if (em) {
  3586. if (em->start > start || em->start + em->len <= start)
  3587. free_extent_map(em);
  3588. else if (em->block_start == EXTENT_MAP_INLINE && page)
  3589. free_extent_map(em);
  3590. else
  3591. goto out;
  3592. }
  3593. em = alloc_extent_map(GFP_NOFS);
  3594. if (!em) {
  3595. err = -ENOMEM;
  3596. goto out;
  3597. }
  3598. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3599. em->start = EXTENT_MAP_HOLE;
  3600. em->orig_start = EXTENT_MAP_HOLE;
  3601. em->len = (u64)-1;
  3602. em->block_len = (u64)-1;
  3603. if (!path) {
  3604. path = btrfs_alloc_path();
  3605. BUG_ON(!path);
  3606. }
  3607. ret = btrfs_lookup_file_extent(trans, root, path,
  3608. objectid, start, trans != NULL);
  3609. if (ret < 0) {
  3610. err = ret;
  3611. goto out;
  3612. }
  3613. if (ret != 0) {
  3614. if (path->slots[0] == 0)
  3615. goto not_found;
  3616. path->slots[0]--;
  3617. }
  3618. leaf = path->nodes[0];
  3619. item = btrfs_item_ptr(leaf, path->slots[0],
  3620. struct btrfs_file_extent_item);
  3621. /* are we inside the extent that was found? */
  3622. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3623. found_type = btrfs_key_type(&found_key);
  3624. if (found_key.objectid != objectid ||
  3625. found_type != BTRFS_EXTENT_DATA_KEY) {
  3626. goto not_found;
  3627. }
  3628. found_type = btrfs_file_extent_type(leaf, item);
  3629. extent_start = found_key.offset;
  3630. compressed = btrfs_file_extent_compression(leaf, item);
  3631. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3632. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3633. extent_end = extent_start +
  3634. btrfs_file_extent_num_bytes(leaf, item);
  3635. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3636. size_t size;
  3637. size = btrfs_file_extent_inline_len(leaf, item);
  3638. extent_end = (extent_start + size + root->sectorsize - 1) &
  3639. ~((u64)root->sectorsize - 1);
  3640. }
  3641. if (start >= extent_end) {
  3642. path->slots[0]++;
  3643. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3644. ret = btrfs_next_leaf(root, path);
  3645. if (ret < 0) {
  3646. err = ret;
  3647. goto out;
  3648. }
  3649. if (ret > 0)
  3650. goto not_found;
  3651. leaf = path->nodes[0];
  3652. }
  3653. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3654. if (found_key.objectid != objectid ||
  3655. found_key.type != BTRFS_EXTENT_DATA_KEY)
  3656. goto not_found;
  3657. if (start + len <= found_key.offset)
  3658. goto not_found;
  3659. em->start = start;
  3660. em->len = found_key.offset - start;
  3661. goto not_found_em;
  3662. }
  3663. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3664. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3665. em->start = extent_start;
  3666. em->len = extent_end - extent_start;
  3667. em->orig_start = extent_start -
  3668. btrfs_file_extent_offset(leaf, item);
  3669. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  3670. if (bytenr == 0) {
  3671. em->block_start = EXTENT_MAP_HOLE;
  3672. goto insert;
  3673. }
  3674. if (compressed) {
  3675. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3676. em->block_start = bytenr;
  3677. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  3678. item);
  3679. } else {
  3680. bytenr += btrfs_file_extent_offset(leaf, item);
  3681. em->block_start = bytenr;
  3682. em->block_len = em->len;
  3683. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  3684. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  3685. }
  3686. goto insert;
  3687. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3688. unsigned long ptr;
  3689. char *map;
  3690. size_t size;
  3691. size_t extent_offset;
  3692. size_t copy_size;
  3693. em->block_start = EXTENT_MAP_INLINE;
  3694. if (!page || create) {
  3695. em->start = extent_start;
  3696. em->len = extent_end - extent_start;
  3697. goto out;
  3698. }
  3699. size = btrfs_file_extent_inline_len(leaf, item);
  3700. extent_offset = page_offset(page) + pg_offset - extent_start;
  3701. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  3702. size - extent_offset);
  3703. em->start = extent_start + extent_offset;
  3704. em->len = (copy_size + root->sectorsize - 1) &
  3705. ~((u64)root->sectorsize - 1);
  3706. em->orig_start = EXTENT_MAP_INLINE;
  3707. if (compressed)
  3708. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3709. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  3710. if (create == 0 && !PageUptodate(page)) {
  3711. if (btrfs_file_extent_compression(leaf, item) ==
  3712. BTRFS_COMPRESS_ZLIB) {
  3713. ret = uncompress_inline(path, inode, page,
  3714. pg_offset,
  3715. extent_offset, item);
  3716. BUG_ON(ret);
  3717. } else {
  3718. map = kmap(page);
  3719. read_extent_buffer(leaf, map + pg_offset, ptr,
  3720. copy_size);
  3721. kunmap(page);
  3722. }
  3723. flush_dcache_page(page);
  3724. } else if (create && PageUptodate(page)) {
  3725. if (!trans) {
  3726. kunmap(page);
  3727. free_extent_map(em);
  3728. em = NULL;
  3729. btrfs_release_path(root, path);
  3730. trans = btrfs_join_transaction(root, 1);
  3731. goto again;
  3732. }
  3733. map = kmap(page);
  3734. write_extent_buffer(leaf, map + pg_offset, ptr,
  3735. copy_size);
  3736. kunmap(page);
  3737. btrfs_mark_buffer_dirty(leaf);
  3738. }
  3739. set_extent_uptodate(io_tree, em->start,
  3740. extent_map_end(em) - 1, GFP_NOFS);
  3741. goto insert;
  3742. } else {
  3743. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  3744. WARN_ON(1);
  3745. }
  3746. not_found:
  3747. em->start = start;
  3748. em->len = len;
  3749. not_found_em:
  3750. em->block_start = EXTENT_MAP_HOLE;
  3751. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  3752. insert:
  3753. btrfs_release_path(root, path);
  3754. if (em->start > start || extent_map_end(em) <= start) {
  3755. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  3756. "[%llu %llu]\n", (unsigned long long)em->start,
  3757. (unsigned long long)em->len,
  3758. (unsigned long long)start,
  3759. (unsigned long long)len);
  3760. err = -EIO;
  3761. goto out;
  3762. }
  3763. err = 0;
  3764. spin_lock(&em_tree->lock);
  3765. ret = add_extent_mapping(em_tree, em);
  3766. /* it is possible that someone inserted the extent into the tree
  3767. * while we had the lock dropped. It is also possible that
  3768. * an overlapping map exists in the tree
  3769. */
  3770. if (ret == -EEXIST) {
  3771. struct extent_map *existing;
  3772. ret = 0;
  3773. existing = lookup_extent_mapping(em_tree, start, len);
  3774. if (existing && (existing->start > start ||
  3775. existing->start + existing->len <= start)) {
  3776. free_extent_map(existing);
  3777. existing = NULL;
  3778. }
  3779. if (!existing) {
  3780. existing = lookup_extent_mapping(em_tree, em->start,
  3781. em->len);
  3782. if (existing) {
  3783. err = merge_extent_mapping(em_tree, existing,
  3784. em, start,
  3785. root->sectorsize);
  3786. free_extent_map(existing);
  3787. if (err) {
  3788. free_extent_map(em);
  3789. em = NULL;
  3790. }
  3791. } else {
  3792. err = -EIO;
  3793. free_extent_map(em);
  3794. em = NULL;
  3795. }
  3796. } else {
  3797. free_extent_map(em);
  3798. em = existing;
  3799. err = 0;
  3800. }
  3801. }
  3802. spin_unlock(&em_tree->lock);
  3803. out:
  3804. if (path)
  3805. btrfs_free_path(path);
  3806. if (trans) {
  3807. ret = btrfs_end_transaction(trans, root);
  3808. if (!err)
  3809. err = ret;
  3810. }
  3811. if (err) {
  3812. free_extent_map(em);
  3813. return ERR_PTR(err);
  3814. }
  3815. return em;
  3816. }
  3817. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  3818. const struct iovec *iov, loff_t offset,
  3819. unsigned long nr_segs)
  3820. {
  3821. return -EINVAL;
  3822. }
  3823. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3824. __u64 start, __u64 len)
  3825. {
  3826. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  3827. }
  3828. int btrfs_readpage(struct file *file, struct page *page)
  3829. {
  3830. struct extent_io_tree *tree;
  3831. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3832. return extent_read_full_page(tree, page, btrfs_get_extent);
  3833. }
  3834. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  3835. {
  3836. struct extent_io_tree *tree;
  3837. if (current->flags & PF_MEMALLOC) {
  3838. redirty_page_for_writepage(wbc, page);
  3839. unlock_page(page);
  3840. return 0;
  3841. }
  3842. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3843. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  3844. }
  3845. int btrfs_writepages(struct address_space *mapping,
  3846. struct writeback_control *wbc)
  3847. {
  3848. struct extent_io_tree *tree;
  3849. tree = &BTRFS_I(mapping->host)->io_tree;
  3850. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  3851. }
  3852. static int
  3853. btrfs_readpages(struct file *file, struct address_space *mapping,
  3854. struct list_head *pages, unsigned nr_pages)
  3855. {
  3856. struct extent_io_tree *tree;
  3857. tree = &BTRFS_I(mapping->host)->io_tree;
  3858. return extent_readpages(tree, mapping, pages, nr_pages,
  3859. btrfs_get_extent);
  3860. }
  3861. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3862. {
  3863. struct extent_io_tree *tree;
  3864. struct extent_map_tree *map;
  3865. int ret;
  3866. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3867. map = &BTRFS_I(page->mapping->host)->extent_tree;
  3868. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  3869. if (ret == 1) {
  3870. ClearPagePrivate(page);
  3871. set_page_private(page, 0);
  3872. page_cache_release(page);
  3873. }
  3874. return ret;
  3875. }
  3876. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3877. {
  3878. if (PageWriteback(page) || PageDirty(page))
  3879. return 0;
  3880. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  3881. }
  3882. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  3883. {
  3884. struct extent_io_tree *tree;
  3885. struct btrfs_ordered_extent *ordered;
  3886. u64 page_start = page_offset(page);
  3887. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  3888. wait_on_page_writeback(page);
  3889. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3890. if (offset) {
  3891. btrfs_releasepage(page, GFP_NOFS);
  3892. return;
  3893. }
  3894. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3895. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  3896. page_offset(page));
  3897. if (ordered) {
  3898. /*
  3899. * IO on this page will never be started, so we need
  3900. * to account for any ordered extents now
  3901. */
  3902. clear_extent_bit(tree, page_start, page_end,
  3903. EXTENT_DIRTY | EXTENT_DELALLOC |
  3904. EXTENT_LOCKED, 1, 0, GFP_NOFS);
  3905. btrfs_finish_ordered_io(page->mapping->host,
  3906. page_start, page_end);
  3907. btrfs_put_ordered_extent(ordered);
  3908. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3909. }
  3910. clear_extent_bit(tree, page_start, page_end,
  3911. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3912. EXTENT_ORDERED,
  3913. 1, 1, GFP_NOFS);
  3914. __btrfs_releasepage(page, GFP_NOFS);
  3915. ClearPageChecked(page);
  3916. if (PagePrivate(page)) {
  3917. ClearPagePrivate(page);
  3918. set_page_private(page, 0);
  3919. page_cache_release(page);
  3920. }
  3921. }
  3922. /*
  3923. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  3924. * called from a page fault handler when a page is first dirtied. Hence we must
  3925. * be careful to check for EOF conditions here. We set the page up correctly
  3926. * for a written page which means we get ENOSPC checking when writing into
  3927. * holes and correct delalloc and unwritten extent mapping on filesystems that
  3928. * support these features.
  3929. *
  3930. * We are not allowed to take the i_mutex here so we have to play games to
  3931. * protect against truncate races as the page could now be beyond EOF. Because
  3932. * vmtruncate() writes the inode size before removing pages, once we have the
  3933. * page lock we can determine safely if the page is beyond EOF. If it is not
  3934. * beyond EOF, then the page is guaranteed safe against truncation until we
  3935. * unlock the page.
  3936. */
  3937. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  3938. {
  3939. struct page *page = vmf->page;
  3940. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  3941. struct btrfs_root *root = BTRFS_I(inode)->root;
  3942. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3943. struct btrfs_ordered_extent *ordered;
  3944. char *kaddr;
  3945. unsigned long zero_start;
  3946. loff_t size;
  3947. int ret;
  3948. u64 page_start;
  3949. u64 page_end;
  3950. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  3951. if (ret) {
  3952. if (ret == -ENOMEM)
  3953. ret = VM_FAULT_OOM;
  3954. else /* -ENOSPC, -EIO, etc */
  3955. ret = VM_FAULT_SIGBUS;
  3956. goto out;
  3957. }
  3958. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  3959. again:
  3960. lock_page(page);
  3961. size = i_size_read(inode);
  3962. page_start = page_offset(page);
  3963. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3964. if ((page->mapping != inode->i_mapping) ||
  3965. (page_start >= size)) {
  3966. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  3967. /* page got truncated out from underneath us */
  3968. goto out_unlock;
  3969. }
  3970. wait_on_page_writeback(page);
  3971. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3972. set_page_extent_mapped(page);
  3973. /*
  3974. * we can't set the delalloc bits if there are pending ordered
  3975. * extents. Drop our locks and wait for them to finish
  3976. */
  3977. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3978. if (ordered) {
  3979. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3980. unlock_page(page);
  3981. btrfs_start_ordered_extent(inode, ordered, 1);
  3982. btrfs_put_ordered_extent(ordered);
  3983. goto again;
  3984. }
  3985. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3986. ret = 0;
  3987. /* page is wholly or partially inside EOF */
  3988. if (page_start + PAGE_CACHE_SIZE > size)
  3989. zero_start = size & ~PAGE_CACHE_MASK;
  3990. else
  3991. zero_start = PAGE_CACHE_SIZE;
  3992. if (zero_start != PAGE_CACHE_SIZE) {
  3993. kaddr = kmap(page);
  3994. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  3995. flush_dcache_page(page);
  3996. kunmap(page);
  3997. }
  3998. ClearPageChecked(page);
  3999. set_page_dirty(page);
  4000. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  4001. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4002. out_unlock:
  4003. unlock_page(page);
  4004. out:
  4005. return ret;
  4006. }
  4007. static void btrfs_truncate(struct inode *inode)
  4008. {
  4009. struct btrfs_root *root = BTRFS_I(inode)->root;
  4010. int ret;
  4011. struct btrfs_trans_handle *trans;
  4012. unsigned long nr;
  4013. u64 mask = root->sectorsize - 1;
  4014. if (!S_ISREG(inode->i_mode))
  4015. return;
  4016. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  4017. return;
  4018. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4019. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4020. trans = btrfs_start_transaction(root, 1);
  4021. /*
  4022. * setattr is responsible for setting the ordered_data_close flag,
  4023. * but that is only tested during the last file release. That
  4024. * could happen well after the next commit, leaving a great big
  4025. * window where new writes may get lost if someone chooses to write
  4026. * to this file after truncating to zero
  4027. *
  4028. * The inode doesn't have any dirty data here, and so if we commit
  4029. * this is a noop. If someone immediately starts writing to the inode
  4030. * it is very likely we'll catch some of their writes in this
  4031. * transaction, and the commit will find this file on the ordered
  4032. * data list with good things to send down.
  4033. *
  4034. * This is a best effort solution, there is still a window where
  4035. * using truncate to replace the contents of the file will
  4036. * end up with a zero length file after a crash.
  4037. */
  4038. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4039. btrfs_add_ordered_operation(trans, root, inode);
  4040. btrfs_set_trans_block_group(trans, inode);
  4041. btrfs_i_size_write(inode, inode->i_size);
  4042. ret = btrfs_orphan_add(trans, inode);
  4043. if (ret)
  4044. goto out;
  4045. /* FIXME, add redo link to tree so we don't leak on crash */
  4046. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  4047. BTRFS_EXTENT_DATA_KEY);
  4048. btrfs_update_inode(trans, root, inode);
  4049. ret = btrfs_orphan_del(trans, inode);
  4050. BUG_ON(ret);
  4051. out:
  4052. nr = trans->blocks_used;
  4053. ret = btrfs_end_transaction_throttle(trans, root);
  4054. BUG_ON(ret);
  4055. btrfs_btree_balance_dirty(root, nr);
  4056. }
  4057. /*
  4058. * create a new subvolume directory/inode (helper for the ioctl).
  4059. */
  4060. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4061. struct btrfs_root *new_root, struct dentry *dentry,
  4062. u64 new_dirid, u64 alloc_hint)
  4063. {
  4064. struct inode *inode;
  4065. int error;
  4066. u64 index = 0;
  4067. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4068. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4069. if (IS_ERR(inode))
  4070. return PTR_ERR(inode);
  4071. inode->i_op = &btrfs_dir_inode_operations;
  4072. inode->i_fop = &btrfs_dir_file_operations;
  4073. inode->i_nlink = 1;
  4074. btrfs_i_size_write(inode, 0);
  4075. error = btrfs_update_inode(trans, new_root, inode);
  4076. if (error)
  4077. return error;
  4078. d_instantiate(dentry, inode);
  4079. return 0;
  4080. }
  4081. /* helper function for file defrag and space balancing. This
  4082. * forces readahead on a given range of bytes in an inode
  4083. */
  4084. unsigned long btrfs_force_ra(struct address_space *mapping,
  4085. struct file_ra_state *ra, struct file *file,
  4086. pgoff_t offset, pgoff_t last_index)
  4087. {
  4088. pgoff_t req_size = last_index - offset + 1;
  4089. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4090. return offset + req_size;
  4091. }
  4092. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4093. {
  4094. struct btrfs_inode *ei;
  4095. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4096. if (!ei)
  4097. return NULL;
  4098. ei->last_trans = 0;
  4099. ei->logged_trans = 0;
  4100. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4101. ei->i_acl = BTRFS_ACL_NOT_CACHED;
  4102. ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
  4103. INIT_LIST_HEAD(&ei->i_orphan);
  4104. INIT_LIST_HEAD(&ei->ordered_operations);
  4105. return &ei->vfs_inode;
  4106. }
  4107. void btrfs_destroy_inode(struct inode *inode)
  4108. {
  4109. struct btrfs_ordered_extent *ordered;
  4110. struct btrfs_root *root = BTRFS_I(inode)->root;
  4111. WARN_ON(!list_empty(&inode->i_dentry));
  4112. WARN_ON(inode->i_data.nrpages);
  4113. if (BTRFS_I(inode)->i_acl &&
  4114. BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
  4115. posix_acl_release(BTRFS_I(inode)->i_acl);
  4116. if (BTRFS_I(inode)->i_default_acl &&
  4117. BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
  4118. posix_acl_release(BTRFS_I(inode)->i_default_acl);
  4119. /*
  4120. * Make sure we're properly removed from the ordered operation
  4121. * lists.
  4122. */
  4123. smp_mb();
  4124. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4125. spin_lock(&root->fs_info->ordered_extent_lock);
  4126. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4127. spin_unlock(&root->fs_info->ordered_extent_lock);
  4128. }
  4129. spin_lock(&root->list_lock);
  4130. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4131. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  4132. " list\n", inode->i_ino);
  4133. dump_stack();
  4134. }
  4135. spin_unlock(&root->list_lock);
  4136. while (1) {
  4137. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4138. if (!ordered)
  4139. break;
  4140. else {
  4141. printk(KERN_ERR "btrfs found ordered "
  4142. "extent %llu %llu on inode cleanup\n",
  4143. (unsigned long long)ordered->file_offset,
  4144. (unsigned long long)ordered->len);
  4145. btrfs_remove_ordered_extent(inode, ordered);
  4146. btrfs_put_ordered_extent(ordered);
  4147. btrfs_put_ordered_extent(ordered);
  4148. }
  4149. }
  4150. inode_tree_del(inode);
  4151. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4152. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4153. }
  4154. static void init_once(void *foo)
  4155. {
  4156. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4157. inode_init_once(&ei->vfs_inode);
  4158. }
  4159. void btrfs_destroy_cachep(void)
  4160. {
  4161. if (btrfs_inode_cachep)
  4162. kmem_cache_destroy(btrfs_inode_cachep);
  4163. if (btrfs_trans_handle_cachep)
  4164. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4165. if (btrfs_transaction_cachep)
  4166. kmem_cache_destroy(btrfs_transaction_cachep);
  4167. if (btrfs_path_cachep)
  4168. kmem_cache_destroy(btrfs_path_cachep);
  4169. }
  4170. int btrfs_init_cachep(void)
  4171. {
  4172. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  4173. sizeof(struct btrfs_inode), 0,
  4174. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  4175. if (!btrfs_inode_cachep)
  4176. goto fail;
  4177. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  4178. sizeof(struct btrfs_trans_handle), 0,
  4179. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4180. if (!btrfs_trans_handle_cachep)
  4181. goto fail;
  4182. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  4183. sizeof(struct btrfs_transaction), 0,
  4184. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4185. if (!btrfs_transaction_cachep)
  4186. goto fail;
  4187. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  4188. sizeof(struct btrfs_path), 0,
  4189. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4190. if (!btrfs_path_cachep)
  4191. goto fail;
  4192. return 0;
  4193. fail:
  4194. btrfs_destroy_cachep();
  4195. return -ENOMEM;
  4196. }
  4197. static int btrfs_getattr(struct vfsmount *mnt,
  4198. struct dentry *dentry, struct kstat *stat)
  4199. {
  4200. struct inode *inode = dentry->d_inode;
  4201. generic_fillattr(inode, stat);
  4202. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4203. stat->blksize = PAGE_CACHE_SIZE;
  4204. stat->blocks = (inode_get_bytes(inode) +
  4205. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4206. return 0;
  4207. }
  4208. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4209. struct inode *new_dir, struct dentry *new_dentry)
  4210. {
  4211. struct btrfs_trans_handle *trans;
  4212. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4213. struct inode *new_inode = new_dentry->d_inode;
  4214. struct inode *old_inode = old_dentry->d_inode;
  4215. struct timespec ctime = CURRENT_TIME;
  4216. u64 index = 0;
  4217. int ret;
  4218. /* we're not allowed to rename between subvolumes */
  4219. if (BTRFS_I(old_inode)->root->root_key.objectid !=
  4220. BTRFS_I(new_dir)->root->root_key.objectid)
  4221. return -EXDEV;
  4222. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4223. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  4224. return -ENOTEMPTY;
  4225. }
  4226. /* to rename a snapshot or subvolume, we need to juggle the
  4227. * backrefs. This isn't coded yet
  4228. */
  4229. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4230. return -EXDEV;
  4231. ret = btrfs_check_metadata_free_space(root);
  4232. if (ret)
  4233. goto out_unlock;
  4234. /*
  4235. * we're using rename to replace one file with another.
  4236. * and the replacement file is large. Start IO on it now so
  4237. * we don't add too much work to the end of the transaction
  4238. */
  4239. if (new_inode && old_inode && S_ISREG(old_inode->i_mode) &&
  4240. new_inode->i_size &&
  4241. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  4242. filemap_flush(old_inode->i_mapping);
  4243. trans = btrfs_start_transaction(root, 1);
  4244. /*
  4245. * make sure the inode gets flushed if it is replacing
  4246. * something.
  4247. */
  4248. if (new_inode && new_inode->i_size &&
  4249. old_inode && S_ISREG(old_inode->i_mode)) {
  4250. btrfs_add_ordered_operation(trans, root, old_inode);
  4251. }
  4252. /*
  4253. * this is an ugly little race, but the rename is required to make
  4254. * sure that if we crash, the inode is either at the old name
  4255. * or the new one. pinning the log transaction lets us make sure
  4256. * we don't allow a log commit to come in after we unlink the
  4257. * name but before we add the new name back in.
  4258. */
  4259. btrfs_pin_log_trans(root);
  4260. btrfs_set_trans_block_group(trans, new_dir);
  4261. btrfs_inc_nlink(old_dentry->d_inode);
  4262. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4263. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4264. old_inode->i_ctime = ctime;
  4265. if (old_dentry->d_parent != new_dentry->d_parent)
  4266. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  4267. ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
  4268. old_dentry->d_name.name,
  4269. old_dentry->d_name.len);
  4270. if (ret)
  4271. goto out_fail;
  4272. if (new_inode) {
  4273. new_inode->i_ctime = CURRENT_TIME;
  4274. ret = btrfs_unlink_inode(trans, root, new_dir,
  4275. new_dentry->d_inode,
  4276. new_dentry->d_name.name,
  4277. new_dentry->d_name.len);
  4278. if (ret)
  4279. goto out_fail;
  4280. if (new_inode->i_nlink == 0) {
  4281. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4282. if (ret)
  4283. goto out_fail;
  4284. }
  4285. }
  4286. ret = btrfs_set_inode_index(new_dir, &index);
  4287. if (ret)
  4288. goto out_fail;
  4289. ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
  4290. old_inode, new_dentry->d_name.name,
  4291. new_dentry->d_name.len, 1, index);
  4292. if (ret)
  4293. goto out_fail;
  4294. btrfs_log_new_name(trans, old_inode, old_dir,
  4295. new_dentry->d_parent);
  4296. out_fail:
  4297. /* this btrfs_end_log_trans just allows the current
  4298. * log-sub transaction to complete
  4299. */
  4300. btrfs_end_log_trans(root);
  4301. btrfs_end_transaction_throttle(trans, root);
  4302. out_unlock:
  4303. return ret;
  4304. }
  4305. /*
  4306. * some fairly slow code that needs optimization. This walks the list
  4307. * of all the inodes with pending delalloc and forces them to disk.
  4308. */
  4309. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  4310. {
  4311. struct list_head *head = &root->fs_info->delalloc_inodes;
  4312. struct btrfs_inode *binode;
  4313. struct inode *inode;
  4314. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4315. return -EROFS;
  4316. spin_lock(&root->fs_info->delalloc_lock);
  4317. while (!list_empty(head)) {
  4318. binode = list_entry(head->next, struct btrfs_inode,
  4319. delalloc_inodes);
  4320. inode = igrab(&binode->vfs_inode);
  4321. if (!inode)
  4322. list_del_init(&binode->delalloc_inodes);
  4323. spin_unlock(&root->fs_info->delalloc_lock);
  4324. if (inode) {
  4325. filemap_flush(inode->i_mapping);
  4326. iput(inode);
  4327. }
  4328. cond_resched();
  4329. spin_lock(&root->fs_info->delalloc_lock);
  4330. }
  4331. spin_unlock(&root->fs_info->delalloc_lock);
  4332. /* the filemap_flush will queue IO into the worker threads, but
  4333. * we have to make sure the IO is actually started and that
  4334. * ordered extents get created before we return
  4335. */
  4336. atomic_inc(&root->fs_info->async_submit_draining);
  4337. while (atomic_read(&root->fs_info->nr_async_submits) ||
  4338. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4339. wait_event(root->fs_info->async_submit_wait,
  4340. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4341. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4342. }
  4343. atomic_dec(&root->fs_info->async_submit_draining);
  4344. return 0;
  4345. }
  4346. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4347. const char *symname)
  4348. {
  4349. struct btrfs_trans_handle *trans;
  4350. struct btrfs_root *root = BTRFS_I(dir)->root;
  4351. struct btrfs_path *path;
  4352. struct btrfs_key key;
  4353. struct inode *inode = NULL;
  4354. int err;
  4355. int drop_inode = 0;
  4356. u64 objectid;
  4357. u64 index = 0 ;
  4358. int name_len;
  4359. int datasize;
  4360. unsigned long ptr;
  4361. struct btrfs_file_extent_item *ei;
  4362. struct extent_buffer *leaf;
  4363. unsigned long nr = 0;
  4364. name_len = strlen(symname) + 1;
  4365. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4366. return -ENAMETOOLONG;
  4367. err = btrfs_check_metadata_free_space(root);
  4368. if (err)
  4369. goto out_fail;
  4370. trans = btrfs_start_transaction(root, 1);
  4371. btrfs_set_trans_block_group(trans, dir);
  4372. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  4373. if (err) {
  4374. err = -ENOSPC;
  4375. goto out_unlock;
  4376. }
  4377. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4378. dentry->d_name.len,
  4379. dentry->d_parent->d_inode->i_ino, objectid,
  4380. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  4381. &index);
  4382. err = PTR_ERR(inode);
  4383. if (IS_ERR(inode))
  4384. goto out_unlock;
  4385. err = btrfs_init_inode_security(inode, dir);
  4386. if (err) {
  4387. drop_inode = 1;
  4388. goto out_unlock;
  4389. }
  4390. btrfs_set_trans_block_group(trans, inode);
  4391. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4392. if (err)
  4393. drop_inode = 1;
  4394. else {
  4395. inode->i_mapping->a_ops = &btrfs_aops;
  4396. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4397. inode->i_fop = &btrfs_file_operations;
  4398. inode->i_op = &btrfs_file_inode_operations;
  4399. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4400. }
  4401. dir->i_sb->s_dirt = 1;
  4402. btrfs_update_inode_block_group(trans, inode);
  4403. btrfs_update_inode_block_group(trans, dir);
  4404. if (drop_inode)
  4405. goto out_unlock;
  4406. path = btrfs_alloc_path();
  4407. BUG_ON(!path);
  4408. key.objectid = inode->i_ino;
  4409. key.offset = 0;
  4410. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  4411. datasize = btrfs_file_extent_calc_inline_size(name_len);
  4412. err = btrfs_insert_empty_item(trans, root, path, &key,
  4413. datasize);
  4414. if (err) {
  4415. drop_inode = 1;
  4416. goto out_unlock;
  4417. }
  4418. leaf = path->nodes[0];
  4419. ei = btrfs_item_ptr(leaf, path->slots[0],
  4420. struct btrfs_file_extent_item);
  4421. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  4422. btrfs_set_file_extent_type(leaf, ei,
  4423. BTRFS_FILE_EXTENT_INLINE);
  4424. btrfs_set_file_extent_encryption(leaf, ei, 0);
  4425. btrfs_set_file_extent_compression(leaf, ei, 0);
  4426. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  4427. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  4428. ptr = btrfs_file_extent_inline_start(ei);
  4429. write_extent_buffer(leaf, symname, ptr, name_len);
  4430. btrfs_mark_buffer_dirty(leaf);
  4431. btrfs_free_path(path);
  4432. inode->i_op = &btrfs_symlink_inode_operations;
  4433. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  4434. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4435. inode_set_bytes(inode, name_len);
  4436. btrfs_i_size_write(inode, name_len - 1);
  4437. err = btrfs_update_inode(trans, root, inode);
  4438. if (err)
  4439. drop_inode = 1;
  4440. out_unlock:
  4441. nr = trans->blocks_used;
  4442. btrfs_end_transaction_throttle(trans, root);
  4443. out_fail:
  4444. if (drop_inode) {
  4445. inode_dec_link_count(inode);
  4446. iput(inode);
  4447. }
  4448. btrfs_btree_balance_dirty(root, nr);
  4449. return err;
  4450. }
  4451. static int prealloc_file_range(struct btrfs_trans_handle *trans,
  4452. struct inode *inode, u64 start, u64 end,
  4453. u64 locked_end, u64 alloc_hint, int mode)
  4454. {
  4455. struct btrfs_root *root = BTRFS_I(inode)->root;
  4456. struct btrfs_key ins;
  4457. u64 alloc_size;
  4458. u64 cur_offset = start;
  4459. u64 num_bytes = end - start;
  4460. int ret = 0;
  4461. while (num_bytes > 0) {
  4462. alloc_size = min(num_bytes, root->fs_info->max_extent);
  4463. ret = btrfs_reserve_extent(trans, root, alloc_size,
  4464. root->sectorsize, 0, alloc_hint,
  4465. (u64)-1, &ins, 1);
  4466. if (ret) {
  4467. WARN_ON(1);
  4468. goto out;
  4469. }
  4470. ret = insert_reserved_file_extent(trans, inode,
  4471. cur_offset, ins.objectid,
  4472. ins.offset, ins.offset,
  4473. ins.offset, locked_end,
  4474. 0, 0, 0,
  4475. BTRFS_FILE_EXTENT_PREALLOC);
  4476. BUG_ON(ret);
  4477. num_bytes -= ins.offset;
  4478. cur_offset += ins.offset;
  4479. alloc_hint = ins.objectid + ins.offset;
  4480. }
  4481. out:
  4482. if (cur_offset > start) {
  4483. inode->i_ctime = CURRENT_TIME;
  4484. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  4485. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4486. cur_offset > i_size_read(inode))
  4487. btrfs_i_size_write(inode, cur_offset);
  4488. ret = btrfs_update_inode(trans, root, inode);
  4489. BUG_ON(ret);
  4490. }
  4491. return ret;
  4492. }
  4493. static long btrfs_fallocate(struct inode *inode, int mode,
  4494. loff_t offset, loff_t len)
  4495. {
  4496. u64 cur_offset;
  4497. u64 last_byte;
  4498. u64 alloc_start;
  4499. u64 alloc_end;
  4500. u64 alloc_hint = 0;
  4501. u64 locked_end;
  4502. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  4503. struct extent_map *em;
  4504. struct btrfs_trans_handle *trans;
  4505. int ret;
  4506. alloc_start = offset & ~mask;
  4507. alloc_end = (offset + len + mask) & ~mask;
  4508. /*
  4509. * wait for ordered IO before we have any locks. We'll loop again
  4510. * below with the locks held.
  4511. */
  4512. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  4513. mutex_lock(&inode->i_mutex);
  4514. if (alloc_start > inode->i_size) {
  4515. ret = btrfs_cont_expand(inode, alloc_start);
  4516. if (ret)
  4517. goto out;
  4518. }
  4519. locked_end = alloc_end - 1;
  4520. while (1) {
  4521. struct btrfs_ordered_extent *ordered;
  4522. trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
  4523. if (!trans) {
  4524. ret = -EIO;
  4525. goto out;
  4526. }
  4527. /* the extent lock is ordered inside the running
  4528. * transaction
  4529. */
  4530. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  4531. GFP_NOFS);
  4532. ordered = btrfs_lookup_first_ordered_extent(inode,
  4533. alloc_end - 1);
  4534. if (ordered &&
  4535. ordered->file_offset + ordered->len > alloc_start &&
  4536. ordered->file_offset < alloc_end) {
  4537. btrfs_put_ordered_extent(ordered);
  4538. unlock_extent(&BTRFS_I(inode)->io_tree,
  4539. alloc_start, locked_end, GFP_NOFS);
  4540. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  4541. /*
  4542. * we can't wait on the range with the transaction
  4543. * running or with the extent lock held
  4544. */
  4545. btrfs_wait_ordered_range(inode, alloc_start,
  4546. alloc_end - alloc_start);
  4547. } else {
  4548. if (ordered)
  4549. btrfs_put_ordered_extent(ordered);
  4550. break;
  4551. }
  4552. }
  4553. cur_offset = alloc_start;
  4554. while (1) {
  4555. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4556. alloc_end - cur_offset, 0);
  4557. BUG_ON(IS_ERR(em) || !em);
  4558. last_byte = min(extent_map_end(em), alloc_end);
  4559. last_byte = (last_byte + mask) & ~mask;
  4560. if (em->block_start == EXTENT_MAP_HOLE) {
  4561. ret = prealloc_file_range(trans, inode, cur_offset,
  4562. last_byte, locked_end + 1,
  4563. alloc_hint, mode);
  4564. if (ret < 0) {
  4565. free_extent_map(em);
  4566. break;
  4567. }
  4568. }
  4569. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  4570. alloc_hint = em->block_start;
  4571. free_extent_map(em);
  4572. cur_offset = last_byte;
  4573. if (cur_offset >= alloc_end) {
  4574. ret = 0;
  4575. break;
  4576. }
  4577. }
  4578. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  4579. GFP_NOFS);
  4580. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  4581. out:
  4582. mutex_unlock(&inode->i_mutex);
  4583. return ret;
  4584. }
  4585. static int btrfs_set_page_dirty(struct page *page)
  4586. {
  4587. return __set_page_dirty_nobuffers(page);
  4588. }
  4589. static int btrfs_permission(struct inode *inode, int mask)
  4590. {
  4591. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  4592. return -EACCES;
  4593. return generic_permission(inode, mask, btrfs_check_acl);
  4594. }
  4595. static struct inode_operations btrfs_dir_inode_operations = {
  4596. .getattr = btrfs_getattr,
  4597. .lookup = btrfs_lookup,
  4598. .create = btrfs_create,
  4599. .unlink = btrfs_unlink,
  4600. .link = btrfs_link,
  4601. .mkdir = btrfs_mkdir,
  4602. .rmdir = btrfs_rmdir,
  4603. .rename = btrfs_rename,
  4604. .symlink = btrfs_symlink,
  4605. .setattr = btrfs_setattr,
  4606. .mknod = btrfs_mknod,
  4607. .setxattr = btrfs_setxattr,
  4608. .getxattr = btrfs_getxattr,
  4609. .listxattr = btrfs_listxattr,
  4610. .removexattr = btrfs_removexattr,
  4611. .permission = btrfs_permission,
  4612. };
  4613. static struct inode_operations btrfs_dir_ro_inode_operations = {
  4614. .lookup = btrfs_lookup,
  4615. .permission = btrfs_permission,
  4616. };
  4617. static struct file_operations btrfs_dir_file_operations = {
  4618. .llseek = generic_file_llseek,
  4619. .read = generic_read_dir,
  4620. .readdir = btrfs_real_readdir,
  4621. .unlocked_ioctl = btrfs_ioctl,
  4622. #ifdef CONFIG_COMPAT
  4623. .compat_ioctl = btrfs_ioctl,
  4624. #endif
  4625. .release = btrfs_release_file,
  4626. .fsync = btrfs_sync_file,
  4627. };
  4628. static struct extent_io_ops btrfs_extent_io_ops = {
  4629. .fill_delalloc = run_delalloc_range,
  4630. .submit_bio_hook = btrfs_submit_bio_hook,
  4631. .merge_bio_hook = btrfs_merge_bio_hook,
  4632. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  4633. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  4634. .writepage_start_hook = btrfs_writepage_start_hook,
  4635. .readpage_io_failed_hook = btrfs_io_failed_hook,
  4636. .set_bit_hook = btrfs_set_bit_hook,
  4637. .clear_bit_hook = btrfs_clear_bit_hook,
  4638. };
  4639. /*
  4640. * btrfs doesn't support the bmap operation because swapfiles
  4641. * use bmap to make a mapping of extents in the file. They assume
  4642. * these extents won't change over the life of the file and they
  4643. * use the bmap result to do IO directly to the drive.
  4644. *
  4645. * the btrfs bmap call would return logical addresses that aren't
  4646. * suitable for IO and they also will change frequently as COW
  4647. * operations happen. So, swapfile + btrfs == corruption.
  4648. *
  4649. * For now we're avoiding this by dropping bmap.
  4650. */
  4651. static struct address_space_operations btrfs_aops = {
  4652. .readpage = btrfs_readpage,
  4653. .writepage = btrfs_writepage,
  4654. .writepages = btrfs_writepages,
  4655. .readpages = btrfs_readpages,
  4656. .sync_page = block_sync_page,
  4657. .direct_IO = btrfs_direct_IO,
  4658. .invalidatepage = btrfs_invalidatepage,
  4659. .releasepage = btrfs_releasepage,
  4660. .set_page_dirty = btrfs_set_page_dirty,
  4661. };
  4662. static struct address_space_operations btrfs_symlink_aops = {
  4663. .readpage = btrfs_readpage,
  4664. .writepage = btrfs_writepage,
  4665. .invalidatepage = btrfs_invalidatepage,
  4666. .releasepage = btrfs_releasepage,
  4667. };
  4668. static struct inode_operations btrfs_file_inode_operations = {
  4669. .truncate = btrfs_truncate,
  4670. .getattr = btrfs_getattr,
  4671. .setattr = btrfs_setattr,
  4672. .setxattr = btrfs_setxattr,
  4673. .getxattr = btrfs_getxattr,
  4674. .listxattr = btrfs_listxattr,
  4675. .removexattr = btrfs_removexattr,
  4676. .permission = btrfs_permission,
  4677. .fallocate = btrfs_fallocate,
  4678. .fiemap = btrfs_fiemap,
  4679. };
  4680. static struct inode_operations btrfs_special_inode_operations = {
  4681. .getattr = btrfs_getattr,
  4682. .setattr = btrfs_setattr,
  4683. .permission = btrfs_permission,
  4684. .setxattr = btrfs_setxattr,
  4685. .getxattr = btrfs_getxattr,
  4686. .listxattr = btrfs_listxattr,
  4687. .removexattr = btrfs_removexattr,
  4688. };
  4689. static struct inode_operations btrfs_symlink_inode_operations = {
  4690. .readlink = generic_readlink,
  4691. .follow_link = page_follow_link_light,
  4692. .put_link = page_put_link,
  4693. .permission = btrfs_permission,
  4694. .setxattr = btrfs_setxattr,
  4695. .getxattr = btrfs_getxattr,
  4696. .listxattr = btrfs_listxattr,
  4697. .removexattr = btrfs_removexattr,
  4698. };