inode.c 134 KB

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