inode.c 140 KB

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