inode.c 133 KB

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