inode.c 135 KB

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