inode.c 143 KB

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