inode.c 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312
  1. /*
  2. * linux/fs/ext4/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@redhat.com), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/module.h>
  25. #include <linux/fs.h>
  26. #include <linux/time.h>
  27. #include <linux/jbd2.h>
  28. #include <linux/highuid.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/quotaops.h>
  31. #include <linux/string.h>
  32. #include <linux/buffer_head.h>
  33. #include <linux/writeback.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/mpage.h>
  36. #include <linux/namei.h>
  37. #include <linux/uio.h>
  38. #include <linux/bio.h>
  39. #include "ext4_jbd2.h"
  40. #include "xattr.h"
  41. #include "acl.h"
  42. #include "ext4_extents.h"
  43. #include <trace/events/ext4.h>
  44. #define MPAGE_DA_EXTENT_TAIL 0x01
  45. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  46. loff_t new_size)
  47. {
  48. return jbd2_journal_begin_ordered_truncate(
  49. EXT4_SB(inode->i_sb)->s_journal,
  50. &EXT4_I(inode)->jinode,
  51. new_size);
  52. }
  53. static void ext4_invalidatepage(struct page *page, unsigned long offset);
  54. /*
  55. * Test whether an inode is a fast symlink.
  56. */
  57. static int ext4_inode_is_fast_symlink(struct inode *inode)
  58. {
  59. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  60. (inode->i_sb->s_blocksize >> 9) : 0;
  61. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  62. }
  63. /*
  64. * The ext4 forget function must perform a revoke if we are freeing data
  65. * which has been journaled. Metadata (eg. indirect blocks) must be
  66. * revoked in all cases.
  67. *
  68. * "bh" may be NULL: a metadata block may have been freed from memory
  69. * but there may still be a record of it in the journal, and that record
  70. * still needs to be revoked.
  71. *
  72. * If the handle isn't valid we're not journaling, but we still need to
  73. * call into ext4_journal_revoke() to put the buffer head.
  74. */
  75. int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
  76. struct buffer_head *bh, ext4_fsblk_t blocknr)
  77. {
  78. int err;
  79. might_sleep();
  80. BUFFER_TRACE(bh, "enter");
  81. jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
  82. "data mode %x\n",
  83. bh, is_metadata, inode->i_mode,
  84. test_opt(inode->i_sb, DATA_FLAGS));
  85. /* Never use the revoke function if we are doing full data
  86. * journaling: there is no need to, and a V1 superblock won't
  87. * support it. Otherwise, only skip the revoke on un-journaled
  88. * data blocks. */
  89. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
  90. (!is_metadata && !ext4_should_journal_data(inode))) {
  91. if (bh) {
  92. BUFFER_TRACE(bh, "call jbd2_journal_forget");
  93. return ext4_journal_forget(handle, bh);
  94. }
  95. return 0;
  96. }
  97. /*
  98. * data!=journal && (is_metadata || should_journal_data(inode))
  99. */
  100. BUFFER_TRACE(bh, "call ext4_journal_revoke");
  101. err = ext4_journal_revoke(handle, blocknr, bh);
  102. if (err)
  103. ext4_abort(inode->i_sb, __func__,
  104. "error %d when attempting revoke", err);
  105. BUFFER_TRACE(bh, "exit");
  106. return err;
  107. }
  108. /*
  109. * Work out how many blocks we need to proceed with the next chunk of a
  110. * truncate transaction.
  111. */
  112. static unsigned long blocks_for_truncate(struct inode *inode)
  113. {
  114. ext4_lblk_t needed;
  115. needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
  116. /* Give ourselves just enough room to cope with inodes in which
  117. * i_blocks is corrupt: we've seen disk corruptions in the past
  118. * which resulted in random data in an inode which looked enough
  119. * like a regular file for ext4 to try to delete it. Things
  120. * will go a bit crazy if that happens, but at least we should
  121. * try not to panic the whole kernel. */
  122. if (needed < 2)
  123. needed = 2;
  124. /* But we need to bound the transaction so we don't overflow the
  125. * journal. */
  126. if (needed > EXT4_MAX_TRANS_DATA)
  127. needed = EXT4_MAX_TRANS_DATA;
  128. return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
  129. }
  130. /*
  131. * Truncate transactions can be complex and absolutely huge. So we need to
  132. * be able to restart the transaction at a conventient checkpoint to make
  133. * sure we don't overflow the journal.
  134. *
  135. * start_transaction gets us a new handle for a truncate transaction,
  136. * and extend_transaction tries to extend the existing one a bit. If
  137. * extend fails, we need to propagate the failure up and restart the
  138. * transaction in the top-level truncate loop. --sct
  139. */
  140. static handle_t *start_transaction(struct inode *inode)
  141. {
  142. handle_t *result;
  143. result = ext4_journal_start(inode, blocks_for_truncate(inode));
  144. if (!IS_ERR(result))
  145. return result;
  146. ext4_std_error(inode->i_sb, PTR_ERR(result));
  147. return result;
  148. }
  149. /*
  150. * Try to extend this transaction for the purposes of truncation.
  151. *
  152. * Returns 0 if we managed to create more room. If we can't create more
  153. * room, and the transaction must be restarted we return 1.
  154. */
  155. static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
  156. {
  157. if (!ext4_handle_valid(handle))
  158. return 0;
  159. if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
  160. return 0;
  161. if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
  162. return 0;
  163. return 1;
  164. }
  165. /*
  166. * Restart the transaction associated with *handle. This does a commit,
  167. * so before we call here everything must be consistently dirtied against
  168. * this transaction.
  169. */
  170. static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
  171. {
  172. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  173. jbd_debug(2, "restarting handle %p\n", handle);
  174. return ext4_journal_restart(handle, blocks_for_truncate(inode));
  175. }
  176. /*
  177. * Called at the last iput() if i_nlink is zero.
  178. */
  179. void ext4_delete_inode(struct inode *inode)
  180. {
  181. handle_t *handle;
  182. int err;
  183. if (ext4_should_order_data(inode))
  184. ext4_begin_ordered_truncate(inode, 0);
  185. truncate_inode_pages(&inode->i_data, 0);
  186. if (is_bad_inode(inode))
  187. goto no_delete;
  188. handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
  189. if (IS_ERR(handle)) {
  190. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  191. /*
  192. * If we're going to skip the normal cleanup, we still need to
  193. * make sure that the in-core orphan linked list is properly
  194. * cleaned up.
  195. */
  196. ext4_orphan_del(NULL, inode);
  197. goto no_delete;
  198. }
  199. if (IS_SYNC(inode))
  200. ext4_handle_sync(handle);
  201. inode->i_size = 0;
  202. err = ext4_mark_inode_dirty(handle, inode);
  203. if (err) {
  204. ext4_warning(inode->i_sb, __func__,
  205. "couldn't mark inode dirty (err %d)", err);
  206. goto stop_handle;
  207. }
  208. if (inode->i_blocks)
  209. ext4_truncate(inode);
  210. /*
  211. * ext4_ext_truncate() doesn't reserve any slop when it
  212. * restarts journal transactions; therefore there may not be
  213. * enough credits left in the handle to remove the inode from
  214. * the orphan list and set the dtime field.
  215. */
  216. if (!ext4_handle_has_enough_credits(handle, 3)) {
  217. err = ext4_journal_extend(handle, 3);
  218. if (err > 0)
  219. err = ext4_journal_restart(handle, 3);
  220. if (err != 0) {
  221. ext4_warning(inode->i_sb, __func__,
  222. "couldn't extend journal (err %d)", err);
  223. stop_handle:
  224. ext4_journal_stop(handle);
  225. goto no_delete;
  226. }
  227. }
  228. /*
  229. * Kill off the orphan record which ext4_truncate created.
  230. * AKPM: I think this can be inside the above `if'.
  231. * Note that ext4_orphan_del() has to be able to cope with the
  232. * deletion of a non-existent orphan - this is because we don't
  233. * know if ext4_truncate() actually created an orphan record.
  234. * (Well, we could do this if we need to, but heck - it works)
  235. */
  236. ext4_orphan_del(handle, inode);
  237. EXT4_I(inode)->i_dtime = get_seconds();
  238. /*
  239. * One subtle ordering requirement: if anything has gone wrong
  240. * (transaction abort, IO errors, whatever), then we can still
  241. * do these next steps (the fs will already have been marked as
  242. * having errors), but we can't free the inode if the mark_dirty
  243. * fails.
  244. */
  245. if (ext4_mark_inode_dirty(handle, inode))
  246. /* If that failed, just do the required in-core inode clear. */
  247. clear_inode(inode);
  248. else
  249. ext4_free_inode(handle, inode);
  250. ext4_journal_stop(handle);
  251. return;
  252. no_delete:
  253. clear_inode(inode); /* We must guarantee clearing of inode... */
  254. }
  255. typedef struct {
  256. __le32 *p;
  257. __le32 key;
  258. struct buffer_head *bh;
  259. } Indirect;
  260. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  261. {
  262. p->key = *(p->p = v);
  263. p->bh = bh;
  264. }
  265. /**
  266. * ext4_block_to_path - parse the block number into array of offsets
  267. * @inode: inode in question (we are only interested in its superblock)
  268. * @i_block: block number to be parsed
  269. * @offsets: array to store the offsets in
  270. * @boundary: set this non-zero if the referred-to block is likely to be
  271. * followed (on disk) by an indirect block.
  272. *
  273. * To store the locations of file's data ext4 uses a data structure common
  274. * for UNIX filesystems - tree of pointers anchored in the inode, with
  275. * data blocks at leaves and indirect blocks in intermediate nodes.
  276. * This function translates the block number into path in that tree -
  277. * return value is the path length and @offsets[n] is the offset of
  278. * pointer to (n+1)th node in the nth one. If @block is out of range
  279. * (negative or too large) warning is printed and zero returned.
  280. *
  281. * Note: function doesn't find node addresses, so no IO is needed. All
  282. * we need to know is the capacity of indirect blocks (taken from the
  283. * inode->i_sb).
  284. */
  285. /*
  286. * Portability note: the last comparison (check that we fit into triple
  287. * indirect block) is spelled differently, because otherwise on an
  288. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  289. * if our filesystem had 8Kb blocks. We might use long long, but that would
  290. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  291. * i_block would have to be negative in the very beginning, so we would not
  292. * get there at all.
  293. */
  294. static int ext4_block_to_path(struct inode *inode,
  295. ext4_lblk_t i_block,
  296. ext4_lblk_t offsets[4], int *boundary)
  297. {
  298. int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  299. int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
  300. const long direct_blocks = EXT4_NDIR_BLOCKS,
  301. indirect_blocks = ptrs,
  302. double_blocks = (1 << (ptrs_bits * 2));
  303. int n = 0;
  304. int final = 0;
  305. if (i_block < 0) {
  306. ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
  307. } else if (i_block < direct_blocks) {
  308. offsets[n++] = i_block;
  309. final = direct_blocks;
  310. } else if ((i_block -= direct_blocks) < indirect_blocks) {
  311. offsets[n++] = EXT4_IND_BLOCK;
  312. offsets[n++] = i_block;
  313. final = ptrs;
  314. } else if ((i_block -= indirect_blocks) < double_blocks) {
  315. offsets[n++] = EXT4_DIND_BLOCK;
  316. offsets[n++] = i_block >> ptrs_bits;
  317. offsets[n++] = i_block & (ptrs - 1);
  318. final = ptrs;
  319. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  320. offsets[n++] = EXT4_TIND_BLOCK;
  321. offsets[n++] = i_block >> (ptrs_bits * 2);
  322. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  323. offsets[n++] = i_block & (ptrs - 1);
  324. final = ptrs;
  325. } else {
  326. ext4_warning(inode->i_sb, "ext4_block_to_path",
  327. "block %lu > max in inode %lu",
  328. i_block + direct_blocks +
  329. indirect_blocks + double_blocks, inode->i_ino);
  330. }
  331. if (boundary)
  332. *boundary = final - 1 - (i_block & (ptrs - 1));
  333. return n;
  334. }
  335. static int __ext4_check_blockref(const char *function, struct inode *inode,
  336. __le32 *p, unsigned int max)
  337. {
  338. __le32 *bref = p;
  339. unsigned int blk;
  340. while (bref < p+max) {
  341. blk = le32_to_cpu(*bref++);
  342. if (blk &&
  343. unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
  344. blk, 1))) {
  345. ext4_error(inode->i_sb, function,
  346. "invalid block reference %u "
  347. "in inode #%lu", blk, inode->i_ino);
  348. return -EIO;
  349. }
  350. }
  351. return 0;
  352. }
  353. #define ext4_check_indirect_blockref(inode, bh) \
  354. __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
  355. EXT4_ADDR_PER_BLOCK((inode)->i_sb))
  356. #define ext4_check_inode_blockref(inode) \
  357. __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
  358. EXT4_NDIR_BLOCKS)
  359. /**
  360. * ext4_get_branch - read the chain of indirect blocks leading to data
  361. * @inode: inode in question
  362. * @depth: depth of the chain (1 - direct pointer, etc.)
  363. * @offsets: offsets of pointers in inode/indirect blocks
  364. * @chain: place to store the result
  365. * @err: here we store the error value
  366. *
  367. * Function fills the array of triples <key, p, bh> and returns %NULL
  368. * if everything went OK or the pointer to the last filled triple
  369. * (incomplete one) otherwise. Upon the return chain[i].key contains
  370. * the number of (i+1)-th block in the chain (as it is stored in memory,
  371. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  372. * number (it points into struct inode for i==0 and into the bh->b_data
  373. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  374. * block for i>0 and NULL for i==0. In other words, it holds the block
  375. * numbers of the chain, addresses they were taken from (and where we can
  376. * verify that chain did not change) and buffer_heads hosting these
  377. * numbers.
  378. *
  379. * Function stops when it stumbles upon zero pointer (absent block)
  380. * (pointer to last triple returned, *@err == 0)
  381. * or when it gets an IO error reading an indirect block
  382. * (ditto, *@err == -EIO)
  383. * or when it reads all @depth-1 indirect blocks successfully and finds
  384. * the whole chain, all way to the data (returns %NULL, *err == 0).
  385. *
  386. * Need to be called with
  387. * down_read(&EXT4_I(inode)->i_data_sem)
  388. */
  389. static Indirect *ext4_get_branch(struct inode *inode, int depth,
  390. ext4_lblk_t *offsets,
  391. Indirect chain[4], int *err)
  392. {
  393. struct super_block *sb = inode->i_sb;
  394. Indirect *p = chain;
  395. struct buffer_head *bh;
  396. *err = 0;
  397. /* i_data is not going away, no lock needed */
  398. add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
  399. if (!p->key)
  400. goto no_block;
  401. while (--depth) {
  402. bh = sb_getblk(sb, le32_to_cpu(p->key));
  403. if (unlikely(!bh))
  404. goto failure;
  405. if (!bh_uptodate_or_lock(bh)) {
  406. if (bh_submit_read(bh) < 0) {
  407. put_bh(bh);
  408. goto failure;
  409. }
  410. /* validate block references */
  411. if (ext4_check_indirect_blockref(inode, bh)) {
  412. put_bh(bh);
  413. goto failure;
  414. }
  415. }
  416. add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
  417. /* Reader: end */
  418. if (!p->key)
  419. goto no_block;
  420. }
  421. return NULL;
  422. failure:
  423. *err = -EIO;
  424. no_block:
  425. return p;
  426. }
  427. /**
  428. * ext4_find_near - find a place for allocation with sufficient locality
  429. * @inode: owner
  430. * @ind: descriptor of indirect block.
  431. *
  432. * This function returns the preferred place for block allocation.
  433. * It is used when heuristic for sequential allocation fails.
  434. * Rules are:
  435. * + if there is a block to the left of our position - allocate near it.
  436. * + if pointer will live in indirect block - allocate near that block.
  437. * + if pointer will live in inode - allocate in the same
  438. * cylinder group.
  439. *
  440. * In the latter case we colour the starting block by the callers PID to
  441. * prevent it from clashing with concurrent allocations for a different inode
  442. * in the same block group. The PID is used here so that functionally related
  443. * files will be close-by on-disk.
  444. *
  445. * Caller must make sure that @ind is valid and will stay that way.
  446. */
  447. static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
  448. {
  449. struct ext4_inode_info *ei = EXT4_I(inode);
  450. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  451. __le32 *p;
  452. ext4_fsblk_t bg_start;
  453. ext4_fsblk_t last_block;
  454. ext4_grpblk_t colour;
  455. ext4_group_t block_group;
  456. int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
  457. /* Try to find previous block */
  458. for (p = ind->p - 1; p >= start; p--) {
  459. if (*p)
  460. return le32_to_cpu(*p);
  461. }
  462. /* No such thing, so let's try location of indirect block */
  463. if (ind->bh)
  464. return ind->bh->b_blocknr;
  465. /*
  466. * It is going to be referred to from the inode itself? OK, just put it
  467. * into the same cylinder group then.
  468. */
  469. block_group = ei->i_block_group;
  470. if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
  471. block_group &= ~(flex_size-1);
  472. if (S_ISREG(inode->i_mode))
  473. block_group++;
  474. }
  475. bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
  476. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  477. /*
  478. * If we are doing delayed allocation, we don't need take
  479. * colour into account.
  480. */
  481. if (test_opt(inode->i_sb, DELALLOC))
  482. return bg_start;
  483. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  484. colour = (current->pid % 16) *
  485. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  486. else
  487. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  488. return bg_start + colour;
  489. }
  490. /**
  491. * ext4_find_goal - find a preferred place for allocation.
  492. * @inode: owner
  493. * @block: block we want
  494. * @partial: pointer to the last triple within a chain
  495. *
  496. * Normally this function find the preferred place for block allocation,
  497. * returns it.
  498. */
  499. static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
  500. Indirect *partial)
  501. {
  502. /*
  503. * XXX need to get goal block from mballoc's data structures
  504. */
  505. return ext4_find_near(inode, partial);
  506. }
  507. /**
  508. * ext4_blks_to_allocate: Look up the block map and count the number
  509. * of direct blocks need to be allocated for the given branch.
  510. *
  511. * @branch: chain of indirect blocks
  512. * @k: number of blocks need for indirect blocks
  513. * @blks: number of data blocks to be mapped.
  514. * @blocks_to_boundary: the offset in the indirect block
  515. *
  516. * return the total number of blocks to be allocate, including the
  517. * direct and indirect blocks.
  518. */
  519. static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
  520. int blocks_to_boundary)
  521. {
  522. unsigned int count = 0;
  523. /*
  524. * Simple case, [t,d]Indirect block(s) has not allocated yet
  525. * then it's clear blocks on that path have not allocated
  526. */
  527. if (k > 0) {
  528. /* right now we don't handle cross boundary allocation */
  529. if (blks < blocks_to_boundary + 1)
  530. count += blks;
  531. else
  532. count += blocks_to_boundary + 1;
  533. return count;
  534. }
  535. count++;
  536. while (count < blks && count <= blocks_to_boundary &&
  537. le32_to_cpu(*(branch[0].p + count)) == 0) {
  538. count++;
  539. }
  540. return count;
  541. }
  542. /**
  543. * ext4_alloc_blocks: multiple allocate blocks needed for a branch
  544. * @indirect_blks: the number of blocks need to allocate for indirect
  545. * blocks
  546. *
  547. * @new_blocks: on return it will store the new block numbers for
  548. * the indirect blocks(if needed) and the first direct block,
  549. * @blks: on return it will store the total number of allocated
  550. * direct blocks
  551. */
  552. static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
  553. ext4_lblk_t iblock, ext4_fsblk_t goal,
  554. int indirect_blks, int blks,
  555. ext4_fsblk_t new_blocks[4], int *err)
  556. {
  557. struct ext4_allocation_request ar;
  558. int target, i;
  559. unsigned long count = 0, blk_allocated = 0;
  560. int index = 0;
  561. ext4_fsblk_t current_block = 0;
  562. int ret = 0;
  563. /*
  564. * Here we try to allocate the requested multiple blocks at once,
  565. * on a best-effort basis.
  566. * To build a branch, we should allocate blocks for
  567. * the indirect blocks(if not allocated yet), and at least
  568. * the first direct block of this branch. That's the
  569. * minimum number of blocks need to allocate(required)
  570. */
  571. /* first we try to allocate the indirect blocks */
  572. target = indirect_blks;
  573. while (target > 0) {
  574. count = target;
  575. /* allocating blocks for indirect blocks and direct blocks */
  576. current_block = ext4_new_meta_blocks(handle, inode,
  577. goal, &count, err);
  578. if (*err)
  579. goto failed_out;
  580. target -= count;
  581. /* allocate blocks for indirect blocks */
  582. while (index < indirect_blks && count) {
  583. new_blocks[index++] = current_block++;
  584. count--;
  585. }
  586. if (count > 0) {
  587. /*
  588. * save the new block number
  589. * for the first direct block
  590. */
  591. new_blocks[index] = current_block;
  592. printk(KERN_INFO "%s returned more blocks than "
  593. "requested\n", __func__);
  594. WARN_ON(1);
  595. break;
  596. }
  597. }
  598. target = blks - count ;
  599. blk_allocated = count;
  600. if (!target)
  601. goto allocated;
  602. /* Now allocate data blocks */
  603. memset(&ar, 0, sizeof(ar));
  604. ar.inode = inode;
  605. ar.goal = goal;
  606. ar.len = target;
  607. ar.logical = iblock;
  608. if (S_ISREG(inode->i_mode))
  609. /* enable in-core preallocation only for regular files */
  610. ar.flags = EXT4_MB_HINT_DATA;
  611. current_block = ext4_mb_new_blocks(handle, &ar, err);
  612. if (*err && (target == blks)) {
  613. /*
  614. * if the allocation failed and we didn't allocate
  615. * any blocks before
  616. */
  617. goto failed_out;
  618. }
  619. if (!*err) {
  620. if (target == blks) {
  621. /*
  622. * save the new block number
  623. * for the first direct block
  624. */
  625. new_blocks[index] = current_block;
  626. }
  627. blk_allocated += ar.len;
  628. }
  629. allocated:
  630. /* total number of blocks allocated for direct blocks */
  631. ret = blk_allocated;
  632. *err = 0;
  633. return ret;
  634. failed_out:
  635. for (i = 0; i < index; i++)
  636. ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
  637. return ret;
  638. }
  639. /**
  640. * ext4_alloc_branch - allocate and set up a chain of blocks.
  641. * @inode: owner
  642. * @indirect_blks: number of allocated indirect blocks
  643. * @blks: number of allocated direct blocks
  644. * @offsets: offsets (in the blocks) to store the pointers to next.
  645. * @branch: place to store the chain in.
  646. *
  647. * This function allocates blocks, zeroes out all but the last one,
  648. * links them into chain and (if we are synchronous) writes them to disk.
  649. * In other words, it prepares a branch that can be spliced onto the
  650. * inode. It stores the information about that chain in the branch[], in
  651. * the same format as ext4_get_branch() would do. We are calling it after
  652. * we had read the existing part of chain and partial points to the last
  653. * triple of that (one with zero ->key). Upon the exit we have the same
  654. * picture as after the successful ext4_get_block(), except that in one
  655. * place chain is disconnected - *branch->p is still zero (we did not
  656. * set the last link), but branch->key contains the number that should
  657. * be placed into *branch->p to fill that gap.
  658. *
  659. * If allocation fails we free all blocks we've allocated (and forget
  660. * their buffer_heads) and return the error value the from failed
  661. * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  662. * as described above and return 0.
  663. */
  664. static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
  665. ext4_lblk_t iblock, int indirect_blks,
  666. int *blks, ext4_fsblk_t goal,
  667. ext4_lblk_t *offsets, Indirect *branch)
  668. {
  669. int blocksize = inode->i_sb->s_blocksize;
  670. int i, n = 0;
  671. int err = 0;
  672. struct buffer_head *bh;
  673. int num;
  674. ext4_fsblk_t new_blocks[4];
  675. ext4_fsblk_t current_block;
  676. num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
  677. *blks, new_blocks, &err);
  678. if (err)
  679. return err;
  680. branch[0].key = cpu_to_le32(new_blocks[0]);
  681. /*
  682. * metadata blocks and data blocks are allocated.
  683. */
  684. for (n = 1; n <= indirect_blks; n++) {
  685. /*
  686. * Get buffer_head for parent block, zero it out
  687. * and set the pointer to new one, then send
  688. * parent to disk.
  689. */
  690. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  691. branch[n].bh = bh;
  692. lock_buffer(bh);
  693. BUFFER_TRACE(bh, "call get_create_access");
  694. err = ext4_journal_get_create_access(handle, bh);
  695. if (err) {
  696. /* Don't brelse(bh) here; it's done in
  697. * ext4_journal_forget() below */
  698. unlock_buffer(bh);
  699. goto failed;
  700. }
  701. memset(bh->b_data, 0, blocksize);
  702. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  703. branch[n].key = cpu_to_le32(new_blocks[n]);
  704. *branch[n].p = branch[n].key;
  705. if (n == indirect_blks) {
  706. current_block = new_blocks[n];
  707. /*
  708. * End of chain, update the last new metablock of
  709. * the chain to point to the new allocated
  710. * data blocks numbers
  711. */
  712. for (i = 1; i < num; i++)
  713. *(branch[n].p + i) = cpu_to_le32(++current_block);
  714. }
  715. BUFFER_TRACE(bh, "marking uptodate");
  716. set_buffer_uptodate(bh);
  717. unlock_buffer(bh);
  718. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  719. err = ext4_handle_dirty_metadata(handle, inode, bh);
  720. if (err)
  721. goto failed;
  722. }
  723. *blks = num;
  724. return err;
  725. failed:
  726. /* Allocation failed, free what we already allocated */
  727. for (i = 1; i <= n ; i++) {
  728. BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
  729. ext4_journal_forget(handle, branch[i].bh);
  730. }
  731. for (i = 0; i < indirect_blks; i++)
  732. ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
  733. ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
  734. return err;
  735. }
  736. /**
  737. * ext4_splice_branch - splice the allocated branch onto inode.
  738. * @inode: owner
  739. * @block: (logical) number of block we are adding
  740. * @chain: chain of indirect blocks (with a missing link - see
  741. * ext4_alloc_branch)
  742. * @where: location of missing link
  743. * @num: number of indirect blocks we are adding
  744. * @blks: number of direct blocks we are adding
  745. *
  746. * This function fills the missing link and does all housekeeping needed in
  747. * inode (->i_blocks, etc.). In case of success we end up with the full
  748. * chain to new block and return 0.
  749. */
  750. static int ext4_splice_branch(handle_t *handle, struct inode *inode,
  751. ext4_lblk_t block, Indirect *where, int num,
  752. int blks)
  753. {
  754. int i;
  755. int err = 0;
  756. ext4_fsblk_t current_block;
  757. /*
  758. * If we're splicing into a [td]indirect block (as opposed to the
  759. * inode) then we need to get write access to the [td]indirect block
  760. * before the splice.
  761. */
  762. if (where->bh) {
  763. BUFFER_TRACE(where->bh, "get_write_access");
  764. err = ext4_journal_get_write_access(handle, where->bh);
  765. if (err)
  766. goto err_out;
  767. }
  768. /* That's it */
  769. *where->p = where->key;
  770. /*
  771. * Update the host buffer_head or inode to point to more just allocated
  772. * direct blocks blocks
  773. */
  774. if (num == 0 && blks > 1) {
  775. current_block = le32_to_cpu(where->key) + 1;
  776. for (i = 1; i < blks; i++)
  777. *(where->p + i) = cpu_to_le32(current_block++);
  778. }
  779. /* We are done with atomic stuff, now do the rest of housekeeping */
  780. /* had we spliced it onto indirect block? */
  781. if (where->bh) {
  782. /*
  783. * If we spliced it onto an indirect block, we haven't
  784. * altered the inode. Note however that if it is being spliced
  785. * onto an indirect block at the very end of the file (the
  786. * file is growing) then we *will* alter the inode to reflect
  787. * the new i_size. But that is not done here - it is done in
  788. * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
  789. */
  790. jbd_debug(5, "splicing indirect only\n");
  791. BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
  792. err = ext4_handle_dirty_metadata(handle, inode, where->bh);
  793. if (err)
  794. goto err_out;
  795. } else {
  796. /*
  797. * OK, we spliced it into the inode itself on a direct block.
  798. */
  799. ext4_mark_inode_dirty(handle, inode);
  800. jbd_debug(5, "splicing direct\n");
  801. }
  802. return err;
  803. err_out:
  804. for (i = 1; i <= num; i++) {
  805. BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
  806. ext4_journal_forget(handle, where[i].bh);
  807. ext4_free_blocks(handle, inode,
  808. le32_to_cpu(where[i-1].key), 1, 0);
  809. }
  810. ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
  811. return err;
  812. }
  813. /*
  814. * The ext4_ind_get_blocks() function handles non-extents inodes
  815. * (i.e., using the traditional indirect/double-indirect i_blocks
  816. * scheme) for ext4_get_blocks().
  817. *
  818. * Allocation strategy is simple: if we have to allocate something, we will
  819. * have to go the whole way to leaf. So let's do it before attaching anything
  820. * to tree, set linkage between the newborn blocks, write them if sync is
  821. * required, recheck the path, free and repeat if check fails, otherwise
  822. * set the last missing link (that will protect us from any truncate-generated
  823. * removals - all blocks on the path are immune now) and possibly force the
  824. * write on the parent block.
  825. * That has a nice additional property: no special recovery from the failed
  826. * allocations is needed - we simply release blocks and do not touch anything
  827. * reachable from inode.
  828. *
  829. * `handle' can be NULL if create == 0.
  830. *
  831. * return > 0, # of blocks mapped or allocated.
  832. * return = 0, if plain lookup failed.
  833. * return < 0, error case.
  834. *
  835. * The ext4_ind_get_blocks() function should be called with
  836. * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
  837. * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
  838. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
  839. * blocks.
  840. */
  841. static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
  842. ext4_lblk_t iblock, unsigned int maxblocks,
  843. struct buffer_head *bh_result,
  844. int flags)
  845. {
  846. int err = -EIO;
  847. ext4_lblk_t offsets[4];
  848. Indirect chain[4];
  849. Indirect *partial;
  850. ext4_fsblk_t goal;
  851. int indirect_blks;
  852. int blocks_to_boundary = 0;
  853. int depth;
  854. int count = 0;
  855. ext4_fsblk_t first_block = 0;
  856. J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
  857. J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
  858. depth = ext4_block_to_path(inode, iblock, offsets,
  859. &blocks_to_boundary);
  860. if (depth == 0)
  861. goto out;
  862. partial = ext4_get_branch(inode, depth, offsets, chain, &err);
  863. /* Simplest case - block found, no allocation needed */
  864. if (!partial) {
  865. first_block = le32_to_cpu(chain[depth - 1].key);
  866. clear_buffer_new(bh_result);
  867. count++;
  868. /*map more blocks*/
  869. while (count < maxblocks && count <= blocks_to_boundary) {
  870. ext4_fsblk_t blk;
  871. blk = le32_to_cpu(*(chain[depth-1].p + count));
  872. if (blk == first_block + count)
  873. count++;
  874. else
  875. break;
  876. }
  877. goto got_it;
  878. }
  879. /* Next simple case - plain lookup or failed read of indirect block */
  880. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
  881. goto cleanup;
  882. /*
  883. * Okay, we need to do block allocation.
  884. */
  885. goal = ext4_find_goal(inode, iblock, partial);
  886. /* the number of blocks need to allocate for [d,t]indirect blocks */
  887. indirect_blks = (chain + depth) - partial - 1;
  888. /*
  889. * Next look up the indirect map to count the totoal number of
  890. * direct blocks to allocate for this branch.
  891. */
  892. count = ext4_blks_to_allocate(partial, indirect_blks,
  893. maxblocks, blocks_to_boundary);
  894. /*
  895. * Block out ext4_truncate while we alter the tree
  896. */
  897. err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
  898. &count, goal,
  899. offsets + (partial - chain), partial);
  900. /*
  901. * The ext4_splice_branch call will free and forget any buffers
  902. * on the new chain if there is a failure, but that risks using
  903. * up transaction credits, especially for bitmaps where the
  904. * credits cannot be returned. Can we handle this somehow? We
  905. * may need to return -EAGAIN upwards in the worst case. --sct
  906. */
  907. if (!err)
  908. err = ext4_splice_branch(handle, inode, iblock,
  909. partial, indirect_blks, count);
  910. else
  911. goto cleanup;
  912. set_buffer_new(bh_result);
  913. got_it:
  914. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  915. if (count > blocks_to_boundary)
  916. set_buffer_boundary(bh_result);
  917. err = count;
  918. /* Clean up and exit */
  919. partial = chain + depth - 1; /* the whole chain */
  920. cleanup:
  921. while (partial > chain) {
  922. BUFFER_TRACE(partial->bh, "call brelse");
  923. brelse(partial->bh);
  924. partial--;
  925. }
  926. BUFFER_TRACE(bh_result, "returned");
  927. out:
  928. return err;
  929. }
  930. qsize_t ext4_get_reserved_space(struct inode *inode)
  931. {
  932. unsigned long long total;
  933. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  934. total = EXT4_I(inode)->i_reserved_data_blocks +
  935. EXT4_I(inode)->i_reserved_meta_blocks;
  936. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  937. return total;
  938. }
  939. /*
  940. * Calculate the number of metadata blocks need to reserve
  941. * to allocate @blocks for non extent file based file
  942. */
  943. static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
  944. {
  945. int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  946. int ind_blks, dind_blks, tind_blks;
  947. /* number of new indirect blocks needed */
  948. ind_blks = (blocks + icap - 1) / icap;
  949. dind_blks = (ind_blks + icap - 1) / icap;
  950. tind_blks = 1;
  951. return ind_blks + dind_blks + tind_blks;
  952. }
  953. /*
  954. * Calculate the number of metadata blocks need to reserve
  955. * to allocate given number of blocks
  956. */
  957. static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
  958. {
  959. if (!blocks)
  960. return 0;
  961. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
  962. return ext4_ext_calc_metadata_amount(inode, blocks);
  963. return ext4_indirect_calc_metadata_amount(inode, blocks);
  964. }
  965. static void ext4_da_update_reserve_space(struct inode *inode, int used)
  966. {
  967. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  968. int total, mdb, mdb_free;
  969. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  970. /* recalculate the number of metablocks still need to be reserved */
  971. total = EXT4_I(inode)->i_reserved_data_blocks - used;
  972. mdb = ext4_calc_metadata_amount(inode, total);
  973. /* figure out how many metablocks to release */
  974. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  975. mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
  976. if (mdb_free) {
  977. /* Account for allocated meta_blocks */
  978. mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
  979. /* update fs dirty blocks counter */
  980. percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
  981. EXT4_I(inode)->i_allocated_meta_blocks = 0;
  982. EXT4_I(inode)->i_reserved_meta_blocks = mdb;
  983. }
  984. /* update per-inode reservations */
  985. BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
  986. EXT4_I(inode)->i_reserved_data_blocks -= used;
  987. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  988. /*
  989. * free those over-booking quota for metadata blocks
  990. */
  991. if (mdb_free)
  992. vfs_dq_release_reservation_block(inode, mdb_free);
  993. /*
  994. * If we have done all the pending block allocations and if
  995. * there aren't any writers on the inode, we can discard the
  996. * inode's preallocations.
  997. */
  998. if (!total && (atomic_read(&inode->i_writecount) == 0))
  999. ext4_discard_preallocations(inode);
  1000. }
  1001. static int check_block_validity(struct inode *inode, sector_t logical,
  1002. sector_t phys, int len)
  1003. {
  1004. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
  1005. ext4_error(inode->i_sb, "check_block_validity",
  1006. "inode #%lu logical block %llu mapped to %llu "
  1007. "(size %d)", inode->i_ino,
  1008. (unsigned long long) logical,
  1009. (unsigned long long) phys, len);
  1010. WARN_ON(1);
  1011. return -EIO;
  1012. }
  1013. return 0;
  1014. }
  1015. /*
  1016. * The ext4_get_blocks() function tries to look up the requested blocks,
  1017. * and returns if the blocks are already mapped.
  1018. *
  1019. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  1020. * and store the allocated blocks in the result buffer head and mark it
  1021. * mapped.
  1022. *
  1023. * If file type is extents based, it will call ext4_ext_get_blocks(),
  1024. * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
  1025. * based files
  1026. *
  1027. * On success, it returns the number of blocks being mapped or allocate.
  1028. * if create==0 and the blocks are pre-allocated and uninitialized block,
  1029. * the result buffer head is unmapped. If the create ==1, it will make sure
  1030. * the buffer head is mapped.
  1031. *
  1032. * It returns 0 if plain look up failed (blocks have not been allocated), in
  1033. * that casem, buffer head is unmapped
  1034. *
  1035. * It returns the error in case of allocation failure.
  1036. */
  1037. int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
  1038. unsigned int max_blocks, struct buffer_head *bh,
  1039. int flags)
  1040. {
  1041. int retval;
  1042. clear_buffer_mapped(bh);
  1043. clear_buffer_unwritten(bh);
  1044. /*
  1045. * Try to see if we can get the block without requesting a new
  1046. * file system block.
  1047. */
  1048. down_read((&EXT4_I(inode)->i_data_sem));
  1049. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  1050. retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
  1051. bh, 0);
  1052. } else {
  1053. retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
  1054. bh, 0);
  1055. }
  1056. up_read((&EXT4_I(inode)->i_data_sem));
  1057. if (retval > 0 && buffer_mapped(bh)) {
  1058. int ret = check_block_validity(inode, block,
  1059. bh->b_blocknr, retval);
  1060. if (ret != 0)
  1061. return ret;
  1062. }
  1063. /* If it is only a block(s) look up */
  1064. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  1065. return retval;
  1066. /*
  1067. * Returns if the blocks have already allocated
  1068. *
  1069. * Note that if blocks have been preallocated
  1070. * ext4_ext_get_block() returns th create = 0
  1071. * with buffer head unmapped.
  1072. */
  1073. if (retval > 0 && buffer_mapped(bh))
  1074. return retval;
  1075. /*
  1076. * When we call get_blocks without the create flag, the
  1077. * BH_Unwritten flag could have gotten set if the blocks
  1078. * requested were part of a uninitialized extent. We need to
  1079. * clear this flag now that we are committed to convert all or
  1080. * part of the uninitialized extent to be an initialized
  1081. * extent. This is because we need to avoid the combination
  1082. * of BH_Unwritten and BH_Mapped flags being simultaneously
  1083. * set on the buffer_head.
  1084. */
  1085. clear_buffer_unwritten(bh);
  1086. /*
  1087. * New blocks allocate and/or writing to uninitialized extent
  1088. * will possibly result in updating i_data, so we take
  1089. * the write lock of i_data_sem, and call get_blocks()
  1090. * with create == 1 flag.
  1091. */
  1092. down_write((&EXT4_I(inode)->i_data_sem));
  1093. /*
  1094. * if the caller is from delayed allocation writeout path
  1095. * we have already reserved fs blocks for allocation
  1096. * let the underlying get_block() function know to
  1097. * avoid double accounting
  1098. */
  1099. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  1100. EXT4_I(inode)->i_delalloc_reserved_flag = 1;
  1101. /*
  1102. * We need to check for EXT4 here because migrate
  1103. * could have changed the inode type in between
  1104. */
  1105. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  1106. retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
  1107. bh, flags);
  1108. } else {
  1109. retval = ext4_ind_get_blocks(handle, inode, block,
  1110. max_blocks, bh, flags);
  1111. if (retval > 0 && buffer_new(bh)) {
  1112. /*
  1113. * We allocated new blocks which will result in
  1114. * i_data's format changing. Force the migrate
  1115. * to fail by clearing migrate flags
  1116. */
  1117. EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
  1118. ~EXT4_EXT_MIGRATE;
  1119. }
  1120. }
  1121. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  1122. EXT4_I(inode)->i_delalloc_reserved_flag = 0;
  1123. /*
  1124. * Update reserved blocks/metadata blocks after successful
  1125. * block allocation which had been deferred till now.
  1126. */
  1127. if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
  1128. ext4_da_update_reserve_space(inode, retval);
  1129. up_write((&EXT4_I(inode)->i_data_sem));
  1130. if (retval > 0 && buffer_mapped(bh)) {
  1131. int ret = check_block_validity(inode, block,
  1132. bh->b_blocknr, retval);
  1133. if (ret != 0)
  1134. return ret;
  1135. }
  1136. return retval;
  1137. }
  1138. /* Maximum number of blocks we map for direct IO at once. */
  1139. #define DIO_MAX_BLOCKS 4096
  1140. int ext4_get_block(struct inode *inode, sector_t iblock,
  1141. struct buffer_head *bh_result, int create)
  1142. {
  1143. handle_t *handle = ext4_journal_current_handle();
  1144. int ret = 0, started = 0;
  1145. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  1146. int dio_credits;
  1147. if (create && !handle) {
  1148. /* Direct IO write... */
  1149. if (max_blocks > DIO_MAX_BLOCKS)
  1150. max_blocks = DIO_MAX_BLOCKS;
  1151. dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
  1152. handle = ext4_journal_start(inode, dio_credits);
  1153. if (IS_ERR(handle)) {
  1154. ret = PTR_ERR(handle);
  1155. goto out;
  1156. }
  1157. started = 1;
  1158. }
  1159. ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
  1160. create ? EXT4_GET_BLOCKS_CREATE : 0);
  1161. if (ret > 0) {
  1162. bh_result->b_size = (ret << inode->i_blkbits);
  1163. ret = 0;
  1164. }
  1165. if (started)
  1166. ext4_journal_stop(handle);
  1167. out:
  1168. return ret;
  1169. }
  1170. /*
  1171. * `handle' can be NULL if create is zero
  1172. */
  1173. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  1174. ext4_lblk_t block, int create, int *errp)
  1175. {
  1176. struct buffer_head dummy;
  1177. int fatal = 0, err;
  1178. int flags = 0;
  1179. J_ASSERT(handle != NULL || create == 0);
  1180. dummy.b_state = 0;
  1181. dummy.b_blocknr = -1000;
  1182. buffer_trace_init(&dummy.b_history);
  1183. if (create)
  1184. flags |= EXT4_GET_BLOCKS_CREATE;
  1185. err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
  1186. /*
  1187. * ext4_get_blocks() returns number of blocks mapped. 0 in
  1188. * case of a HOLE.
  1189. */
  1190. if (err > 0) {
  1191. if (err > 1)
  1192. WARN_ON(1);
  1193. err = 0;
  1194. }
  1195. *errp = err;
  1196. if (!err && buffer_mapped(&dummy)) {
  1197. struct buffer_head *bh;
  1198. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  1199. if (!bh) {
  1200. *errp = -EIO;
  1201. goto err;
  1202. }
  1203. if (buffer_new(&dummy)) {
  1204. J_ASSERT(create != 0);
  1205. J_ASSERT(handle != NULL);
  1206. /*
  1207. * Now that we do not always journal data, we should
  1208. * keep in mind whether this should always journal the
  1209. * new buffer as metadata. For now, regular file
  1210. * writes use ext4_get_block instead, so it's not a
  1211. * problem.
  1212. */
  1213. lock_buffer(bh);
  1214. BUFFER_TRACE(bh, "call get_create_access");
  1215. fatal = ext4_journal_get_create_access(handle, bh);
  1216. if (!fatal && !buffer_uptodate(bh)) {
  1217. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1218. set_buffer_uptodate(bh);
  1219. }
  1220. unlock_buffer(bh);
  1221. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1222. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1223. if (!fatal)
  1224. fatal = err;
  1225. } else {
  1226. BUFFER_TRACE(bh, "not a new buffer");
  1227. }
  1228. if (fatal) {
  1229. *errp = fatal;
  1230. brelse(bh);
  1231. bh = NULL;
  1232. }
  1233. return bh;
  1234. }
  1235. err:
  1236. return NULL;
  1237. }
  1238. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  1239. ext4_lblk_t block, int create, int *err)
  1240. {
  1241. struct buffer_head *bh;
  1242. bh = ext4_getblk(handle, inode, block, create, err);
  1243. if (!bh)
  1244. return bh;
  1245. if (buffer_uptodate(bh))
  1246. return bh;
  1247. ll_rw_block(READ_META, 1, &bh);
  1248. wait_on_buffer(bh);
  1249. if (buffer_uptodate(bh))
  1250. return bh;
  1251. put_bh(bh);
  1252. *err = -EIO;
  1253. return NULL;
  1254. }
  1255. static int walk_page_buffers(handle_t *handle,
  1256. struct buffer_head *head,
  1257. unsigned from,
  1258. unsigned to,
  1259. int *partial,
  1260. int (*fn)(handle_t *handle,
  1261. struct buffer_head *bh))
  1262. {
  1263. struct buffer_head *bh;
  1264. unsigned block_start, block_end;
  1265. unsigned blocksize = head->b_size;
  1266. int err, ret = 0;
  1267. struct buffer_head *next;
  1268. for (bh = head, block_start = 0;
  1269. ret == 0 && (bh != head || !block_start);
  1270. block_start = block_end, bh = next) {
  1271. next = bh->b_this_page;
  1272. block_end = block_start + blocksize;
  1273. if (block_end <= from || block_start >= to) {
  1274. if (partial && !buffer_uptodate(bh))
  1275. *partial = 1;
  1276. continue;
  1277. }
  1278. err = (*fn)(handle, bh);
  1279. if (!ret)
  1280. ret = err;
  1281. }
  1282. return ret;
  1283. }
  1284. /*
  1285. * To preserve ordering, it is essential that the hole instantiation and
  1286. * the data write be encapsulated in a single transaction. We cannot
  1287. * close off a transaction and start a new one between the ext4_get_block()
  1288. * and the commit_write(). So doing the jbd2_journal_start at the start of
  1289. * prepare_write() is the right place.
  1290. *
  1291. * Also, this function can nest inside ext4_writepage() ->
  1292. * block_write_full_page(). In that case, we *know* that ext4_writepage()
  1293. * has generated enough buffer credits to do the whole page. So we won't
  1294. * block on the journal in that case, which is good, because the caller may
  1295. * be PF_MEMALLOC.
  1296. *
  1297. * By accident, ext4 can be reentered when a transaction is open via
  1298. * quota file writes. If we were to commit the transaction while thus
  1299. * reentered, there can be a deadlock - we would be holding a quota
  1300. * lock, and the commit would never complete if another thread had a
  1301. * transaction open and was blocking on the quota lock - a ranking
  1302. * violation.
  1303. *
  1304. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  1305. * will _not_ run commit under these circumstances because handle->h_ref
  1306. * is elevated. We'll still have enough credits for the tiny quotafile
  1307. * write.
  1308. */
  1309. static int do_journal_get_write_access(handle_t *handle,
  1310. struct buffer_head *bh)
  1311. {
  1312. if (!buffer_mapped(bh) || buffer_freed(bh))
  1313. return 0;
  1314. return ext4_journal_get_write_access(handle, bh);
  1315. }
  1316. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  1317. loff_t pos, unsigned len, unsigned flags,
  1318. struct page **pagep, void **fsdata)
  1319. {
  1320. struct inode *inode = mapping->host;
  1321. int ret, needed_blocks;
  1322. handle_t *handle;
  1323. int retries = 0;
  1324. struct page *page;
  1325. pgoff_t index;
  1326. unsigned from, to;
  1327. trace_ext4_write_begin(inode, pos, len, flags);
  1328. /*
  1329. * Reserve one block more for addition to orphan list in case
  1330. * we allocate blocks but write fails for some reason
  1331. */
  1332. needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
  1333. index = pos >> PAGE_CACHE_SHIFT;
  1334. from = pos & (PAGE_CACHE_SIZE - 1);
  1335. to = from + len;
  1336. retry:
  1337. handle = ext4_journal_start(inode, needed_blocks);
  1338. if (IS_ERR(handle)) {
  1339. ret = PTR_ERR(handle);
  1340. goto out;
  1341. }
  1342. /* We cannot recurse into the filesystem as the transaction is already
  1343. * started */
  1344. flags |= AOP_FLAG_NOFS;
  1345. page = grab_cache_page_write_begin(mapping, index, flags);
  1346. if (!page) {
  1347. ext4_journal_stop(handle);
  1348. ret = -ENOMEM;
  1349. goto out;
  1350. }
  1351. *pagep = page;
  1352. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1353. ext4_get_block);
  1354. if (!ret && ext4_should_journal_data(inode)) {
  1355. ret = walk_page_buffers(handle, page_buffers(page),
  1356. from, to, NULL, do_journal_get_write_access);
  1357. }
  1358. if (ret) {
  1359. unlock_page(page);
  1360. page_cache_release(page);
  1361. /*
  1362. * block_write_begin may have instantiated a few blocks
  1363. * outside i_size. Trim these off again. Don't need
  1364. * i_size_read because we hold i_mutex.
  1365. *
  1366. * Add inode to orphan list in case we crash before
  1367. * truncate finishes
  1368. */
  1369. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1370. ext4_orphan_add(handle, inode);
  1371. ext4_journal_stop(handle);
  1372. if (pos + len > inode->i_size) {
  1373. ext4_truncate(inode);
  1374. /*
  1375. * If truncate failed early the inode might
  1376. * still be on the orphan list; we need to
  1377. * make sure the inode is removed from the
  1378. * orphan list in that case.
  1379. */
  1380. if (inode->i_nlink)
  1381. ext4_orphan_del(NULL, inode);
  1382. }
  1383. }
  1384. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  1385. goto retry;
  1386. out:
  1387. return ret;
  1388. }
  1389. /* For write_end() in data=journal mode */
  1390. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  1391. {
  1392. if (!buffer_mapped(bh) || buffer_freed(bh))
  1393. return 0;
  1394. set_buffer_uptodate(bh);
  1395. return ext4_handle_dirty_metadata(handle, NULL, bh);
  1396. }
  1397. static int ext4_generic_write_end(struct file *file,
  1398. struct address_space *mapping,
  1399. loff_t pos, unsigned len, unsigned copied,
  1400. struct page *page, void *fsdata)
  1401. {
  1402. int i_size_changed = 0;
  1403. struct inode *inode = mapping->host;
  1404. handle_t *handle = ext4_journal_current_handle();
  1405. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1406. /*
  1407. * No need to use i_size_read() here, the i_size
  1408. * cannot change under us because we hold i_mutex.
  1409. *
  1410. * But it's important to update i_size while still holding page lock:
  1411. * page writeout could otherwise come in and zero beyond i_size.
  1412. */
  1413. if (pos + copied > inode->i_size) {
  1414. i_size_write(inode, pos + copied);
  1415. i_size_changed = 1;
  1416. }
  1417. if (pos + copied > EXT4_I(inode)->i_disksize) {
  1418. /* We need to mark inode dirty even if
  1419. * new_i_size is less that inode->i_size
  1420. * bu greater than i_disksize.(hint delalloc)
  1421. */
  1422. ext4_update_i_disksize(inode, (pos + copied));
  1423. i_size_changed = 1;
  1424. }
  1425. unlock_page(page);
  1426. page_cache_release(page);
  1427. /*
  1428. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1429. * makes the holding time of page lock longer. Second, it forces lock
  1430. * ordering of page lock and transaction start for journaling
  1431. * filesystems.
  1432. */
  1433. if (i_size_changed)
  1434. ext4_mark_inode_dirty(handle, inode);
  1435. return copied;
  1436. }
  1437. /*
  1438. * We need to pick up the new inode size which generic_commit_write gave us
  1439. * `file' can be NULL - eg, when called from page_symlink().
  1440. *
  1441. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  1442. * buffers are managed internally.
  1443. */
  1444. static int ext4_ordered_write_end(struct file *file,
  1445. struct address_space *mapping,
  1446. loff_t pos, unsigned len, unsigned copied,
  1447. struct page *page, void *fsdata)
  1448. {
  1449. handle_t *handle = ext4_journal_current_handle();
  1450. struct inode *inode = mapping->host;
  1451. int ret = 0, ret2;
  1452. trace_ext4_ordered_write_end(inode, pos, len, copied);
  1453. ret = ext4_jbd2_file_inode(handle, inode);
  1454. if (ret == 0) {
  1455. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  1456. page, fsdata);
  1457. copied = ret2;
  1458. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1459. /* if we have allocated more blocks and copied
  1460. * less. We will have blocks allocated outside
  1461. * inode->i_size. So truncate them
  1462. */
  1463. ext4_orphan_add(handle, inode);
  1464. if (ret2 < 0)
  1465. ret = ret2;
  1466. }
  1467. ret2 = ext4_journal_stop(handle);
  1468. if (!ret)
  1469. ret = ret2;
  1470. if (pos + len > inode->i_size) {
  1471. ext4_truncate(inode);
  1472. /*
  1473. * If truncate failed early the inode might still be
  1474. * on the orphan list; we need to make sure the inode
  1475. * is removed from the orphan list in that case.
  1476. */
  1477. if (inode->i_nlink)
  1478. ext4_orphan_del(NULL, inode);
  1479. }
  1480. return ret ? ret : copied;
  1481. }
  1482. static int ext4_writeback_write_end(struct file *file,
  1483. struct address_space *mapping,
  1484. loff_t pos, unsigned len, unsigned copied,
  1485. struct page *page, void *fsdata)
  1486. {
  1487. handle_t *handle = ext4_journal_current_handle();
  1488. struct inode *inode = mapping->host;
  1489. int ret = 0, ret2;
  1490. trace_ext4_writeback_write_end(inode, pos, len, copied);
  1491. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  1492. page, fsdata);
  1493. copied = ret2;
  1494. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1495. /* if we have allocated more blocks and copied
  1496. * less. We will have blocks allocated outside
  1497. * inode->i_size. So truncate them
  1498. */
  1499. ext4_orphan_add(handle, inode);
  1500. if (ret2 < 0)
  1501. ret = ret2;
  1502. ret2 = ext4_journal_stop(handle);
  1503. if (!ret)
  1504. ret = ret2;
  1505. if (pos + len > inode->i_size) {
  1506. ext4_truncate(inode);
  1507. /*
  1508. * If truncate failed early the inode might still be
  1509. * on the orphan list; we need to make sure the inode
  1510. * is removed from the orphan list in that case.
  1511. */
  1512. if (inode->i_nlink)
  1513. ext4_orphan_del(NULL, inode);
  1514. }
  1515. return ret ? ret : copied;
  1516. }
  1517. static int ext4_journalled_write_end(struct file *file,
  1518. struct address_space *mapping,
  1519. loff_t pos, unsigned len, unsigned copied,
  1520. struct page *page, void *fsdata)
  1521. {
  1522. handle_t *handle = ext4_journal_current_handle();
  1523. struct inode *inode = mapping->host;
  1524. int ret = 0, ret2;
  1525. int partial = 0;
  1526. unsigned from, to;
  1527. loff_t new_i_size;
  1528. trace_ext4_journalled_write_end(inode, pos, len, copied);
  1529. from = pos & (PAGE_CACHE_SIZE - 1);
  1530. to = from + len;
  1531. if (copied < len) {
  1532. if (!PageUptodate(page))
  1533. copied = 0;
  1534. page_zero_new_buffers(page, from+copied, to);
  1535. }
  1536. ret = walk_page_buffers(handle, page_buffers(page), from,
  1537. to, &partial, write_end_fn);
  1538. if (!partial)
  1539. SetPageUptodate(page);
  1540. new_i_size = pos + copied;
  1541. if (new_i_size > inode->i_size)
  1542. i_size_write(inode, pos+copied);
  1543. EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
  1544. if (new_i_size > EXT4_I(inode)->i_disksize) {
  1545. ext4_update_i_disksize(inode, new_i_size);
  1546. ret2 = ext4_mark_inode_dirty(handle, inode);
  1547. if (!ret)
  1548. ret = ret2;
  1549. }
  1550. unlock_page(page);
  1551. page_cache_release(page);
  1552. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1553. /* if we have allocated more blocks and copied
  1554. * less. We will have blocks allocated outside
  1555. * inode->i_size. So truncate them
  1556. */
  1557. ext4_orphan_add(handle, inode);
  1558. ret2 = ext4_journal_stop(handle);
  1559. if (!ret)
  1560. ret = ret2;
  1561. if (pos + len > inode->i_size) {
  1562. ext4_truncate(inode);
  1563. /*
  1564. * If truncate failed early the inode might still be
  1565. * on the orphan list; we need to make sure the inode
  1566. * is removed from the orphan list in that case.
  1567. */
  1568. if (inode->i_nlink)
  1569. ext4_orphan_del(NULL, inode);
  1570. }
  1571. return ret ? ret : copied;
  1572. }
  1573. static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
  1574. {
  1575. int retries = 0;
  1576. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1577. unsigned long md_needed, mdblocks, total = 0;
  1578. /*
  1579. * recalculate the amount of metadata blocks to reserve
  1580. * in order to allocate nrblocks
  1581. * worse case is one extent per block
  1582. */
  1583. repeat:
  1584. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1585. total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
  1586. mdblocks = ext4_calc_metadata_amount(inode, total);
  1587. BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
  1588. md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
  1589. total = md_needed + nrblocks;
  1590. /*
  1591. * Make quota reservation here to prevent quota overflow
  1592. * later. Real quota accounting is done at pages writeout
  1593. * time.
  1594. */
  1595. if (vfs_dq_reserve_block(inode, total)) {
  1596. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1597. return -EDQUOT;
  1598. }
  1599. if (ext4_claim_free_blocks(sbi, total)) {
  1600. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1601. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  1602. yield();
  1603. goto repeat;
  1604. }
  1605. vfs_dq_release_reservation_block(inode, total);
  1606. return -ENOSPC;
  1607. }
  1608. EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
  1609. EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
  1610. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1611. return 0; /* success */
  1612. }
  1613. static void ext4_da_release_space(struct inode *inode, int to_free)
  1614. {
  1615. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1616. int total, mdb, mdb_free, release;
  1617. if (!to_free)
  1618. return; /* Nothing to release, exit */
  1619. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1620. if (!EXT4_I(inode)->i_reserved_data_blocks) {
  1621. /*
  1622. * if there is no reserved blocks, but we try to free some
  1623. * then the counter is messed up somewhere.
  1624. * but since this function is called from invalidate
  1625. * page, it's harmless to return without any action
  1626. */
  1627. printk(KERN_INFO "ext4 delalloc try to release %d reserved "
  1628. "blocks for inode %lu, but there is no reserved "
  1629. "data blocks\n", to_free, inode->i_ino);
  1630. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1631. return;
  1632. }
  1633. /* recalculate the number of metablocks still need to be reserved */
  1634. total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
  1635. mdb = ext4_calc_metadata_amount(inode, total);
  1636. /* figure out how many metablocks to release */
  1637. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  1638. mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
  1639. release = to_free + mdb_free;
  1640. /* update fs dirty blocks counter for truncate case */
  1641. percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
  1642. /* update per-inode reservations */
  1643. BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
  1644. EXT4_I(inode)->i_reserved_data_blocks -= to_free;
  1645. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  1646. EXT4_I(inode)->i_reserved_meta_blocks = mdb;
  1647. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1648. vfs_dq_release_reservation_block(inode, release);
  1649. }
  1650. static void ext4_da_page_release_reservation(struct page *page,
  1651. unsigned long offset)
  1652. {
  1653. int to_release = 0;
  1654. struct buffer_head *head, *bh;
  1655. unsigned int curr_off = 0;
  1656. head = page_buffers(page);
  1657. bh = head;
  1658. do {
  1659. unsigned int next_off = curr_off + bh->b_size;
  1660. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1661. to_release++;
  1662. clear_buffer_delay(bh);
  1663. }
  1664. curr_off = next_off;
  1665. } while ((bh = bh->b_this_page) != head);
  1666. ext4_da_release_space(page->mapping->host, to_release);
  1667. }
  1668. /*
  1669. * Delayed allocation stuff
  1670. */
  1671. struct mpage_da_data {
  1672. struct inode *inode;
  1673. sector_t b_blocknr; /* start block number of extent */
  1674. size_t b_size; /* size of extent */
  1675. unsigned long b_state; /* state of the extent */
  1676. unsigned long first_page, next_page; /* extent of pages */
  1677. struct writeback_control *wbc;
  1678. int io_done;
  1679. int pages_written;
  1680. int retval;
  1681. };
  1682. /*
  1683. * mpage_da_submit_io - walks through extent of pages and try to write
  1684. * them with writepage() call back
  1685. *
  1686. * @mpd->inode: inode
  1687. * @mpd->first_page: first page of the extent
  1688. * @mpd->next_page: page after the last page of the extent
  1689. *
  1690. * By the time mpage_da_submit_io() is called we expect all blocks
  1691. * to be allocated. this may be wrong if allocation failed.
  1692. *
  1693. * As pages are already locked by write_cache_pages(), we can't use it
  1694. */
  1695. static int mpage_da_submit_io(struct mpage_da_data *mpd)
  1696. {
  1697. long pages_skipped;
  1698. struct pagevec pvec;
  1699. unsigned long index, end;
  1700. int ret = 0, err, nr_pages, i;
  1701. struct inode *inode = mpd->inode;
  1702. struct address_space *mapping = inode->i_mapping;
  1703. BUG_ON(mpd->next_page <= mpd->first_page);
  1704. /*
  1705. * We need to start from the first_page to the next_page - 1
  1706. * to make sure we also write the mapped dirty buffer_heads.
  1707. * If we look at mpd->b_blocknr we would only be looking
  1708. * at the currently mapped buffer_heads.
  1709. */
  1710. index = mpd->first_page;
  1711. end = mpd->next_page - 1;
  1712. pagevec_init(&pvec, 0);
  1713. while (index <= end) {
  1714. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1715. if (nr_pages == 0)
  1716. break;
  1717. for (i = 0; i < nr_pages; i++) {
  1718. struct page *page = pvec.pages[i];
  1719. index = page->index;
  1720. if (index > end)
  1721. break;
  1722. index++;
  1723. BUG_ON(!PageLocked(page));
  1724. BUG_ON(PageWriteback(page));
  1725. pages_skipped = mpd->wbc->pages_skipped;
  1726. err = mapping->a_ops->writepage(page, mpd->wbc);
  1727. if (!err && (pages_skipped == mpd->wbc->pages_skipped))
  1728. /*
  1729. * have successfully written the page
  1730. * without skipping the same
  1731. */
  1732. mpd->pages_written++;
  1733. /*
  1734. * In error case, we have to continue because
  1735. * remaining pages are still locked
  1736. * XXX: unlock and re-dirty them?
  1737. */
  1738. if (ret == 0)
  1739. ret = err;
  1740. }
  1741. pagevec_release(&pvec);
  1742. }
  1743. return ret;
  1744. }
  1745. /*
  1746. * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
  1747. *
  1748. * @mpd->inode - inode to walk through
  1749. * @exbh->b_blocknr - first block on a disk
  1750. * @exbh->b_size - amount of space in bytes
  1751. * @logical - first logical block to start assignment with
  1752. *
  1753. * the function goes through all passed space and put actual disk
  1754. * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
  1755. */
  1756. static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
  1757. struct buffer_head *exbh)
  1758. {
  1759. struct inode *inode = mpd->inode;
  1760. struct address_space *mapping = inode->i_mapping;
  1761. int blocks = exbh->b_size >> inode->i_blkbits;
  1762. sector_t pblock = exbh->b_blocknr, cur_logical;
  1763. struct buffer_head *head, *bh;
  1764. pgoff_t index, end;
  1765. struct pagevec pvec;
  1766. int nr_pages, i;
  1767. index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1768. end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1769. cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1770. pagevec_init(&pvec, 0);
  1771. while (index <= end) {
  1772. /* XXX: optimize tail */
  1773. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1774. if (nr_pages == 0)
  1775. break;
  1776. for (i = 0; i < nr_pages; i++) {
  1777. struct page *page = pvec.pages[i];
  1778. index = page->index;
  1779. if (index > end)
  1780. break;
  1781. index++;
  1782. BUG_ON(!PageLocked(page));
  1783. BUG_ON(PageWriteback(page));
  1784. BUG_ON(!page_has_buffers(page));
  1785. bh = page_buffers(page);
  1786. head = bh;
  1787. /* skip blocks out of the range */
  1788. do {
  1789. if (cur_logical >= logical)
  1790. break;
  1791. cur_logical++;
  1792. } while ((bh = bh->b_this_page) != head);
  1793. do {
  1794. if (cur_logical >= logical + blocks)
  1795. break;
  1796. if (buffer_delay(bh) ||
  1797. buffer_unwritten(bh)) {
  1798. BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
  1799. if (buffer_delay(bh)) {
  1800. clear_buffer_delay(bh);
  1801. bh->b_blocknr = pblock;
  1802. } else {
  1803. /*
  1804. * unwritten already should have
  1805. * blocknr assigned. Verify that
  1806. */
  1807. clear_buffer_unwritten(bh);
  1808. BUG_ON(bh->b_blocknr != pblock);
  1809. }
  1810. } else if (buffer_mapped(bh))
  1811. BUG_ON(bh->b_blocknr != pblock);
  1812. cur_logical++;
  1813. pblock++;
  1814. } while ((bh = bh->b_this_page) != head);
  1815. }
  1816. pagevec_release(&pvec);
  1817. }
  1818. }
  1819. /*
  1820. * __unmap_underlying_blocks - just a helper function to unmap
  1821. * set of blocks described by @bh
  1822. */
  1823. static inline void __unmap_underlying_blocks(struct inode *inode,
  1824. struct buffer_head *bh)
  1825. {
  1826. struct block_device *bdev = inode->i_sb->s_bdev;
  1827. int blocks, i;
  1828. blocks = bh->b_size >> inode->i_blkbits;
  1829. for (i = 0; i < blocks; i++)
  1830. unmap_underlying_metadata(bdev, bh->b_blocknr + i);
  1831. }
  1832. static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
  1833. sector_t logical, long blk_cnt)
  1834. {
  1835. int nr_pages, i;
  1836. pgoff_t index, end;
  1837. struct pagevec pvec;
  1838. struct inode *inode = mpd->inode;
  1839. struct address_space *mapping = inode->i_mapping;
  1840. index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1841. end = (logical + blk_cnt - 1) >>
  1842. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1843. while (index <= end) {
  1844. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1845. if (nr_pages == 0)
  1846. break;
  1847. for (i = 0; i < nr_pages; i++) {
  1848. struct page *page = pvec.pages[i];
  1849. index = page->index;
  1850. if (index > end)
  1851. break;
  1852. index++;
  1853. BUG_ON(!PageLocked(page));
  1854. BUG_ON(PageWriteback(page));
  1855. block_invalidatepage(page, 0);
  1856. ClearPageUptodate(page);
  1857. unlock_page(page);
  1858. }
  1859. }
  1860. return;
  1861. }
  1862. static void ext4_print_free_blocks(struct inode *inode)
  1863. {
  1864. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1865. printk(KERN_EMERG "Total free blocks count %lld\n",
  1866. ext4_count_free_blocks(inode->i_sb));
  1867. printk(KERN_EMERG "Free/Dirty block details\n");
  1868. printk(KERN_EMERG "free_blocks=%lld\n",
  1869. (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
  1870. printk(KERN_EMERG "dirty_blocks=%lld\n",
  1871. (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
  1872. printk(KERN_EMERG "Block reservation details\n");
  1873. printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
  1874. EXT4_I(inode)->i_reserved_data_blocks);
  1875. printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
  1876. EXT4_I(inode)->i_reserved_meta_blocks);
  1877. return;
  1878. }
  1879. /*
  1880. * mpage_da_map_blocks - go through given space
  1881. *
  1882. * @mpd - bh describing space
  1883. *
  1884. * The function skips space we know is already mapped to disk blocks.
  1885. *
  1886. */
  1887. static int mpage_da_map_blocks(struct mpage_da_data *mpd)
  1888. {
  1889. int err, blks, get_blocks_flags;
  1890. struct buffer_head new;
  1891. sector_t next = mpd->b_blocknr;
  1892. unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
  1893. loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
  1894. handle_t *handle = NULL;
  1895. /*
  1896. * We consider only non-mapped and non-allocated blocks
  1897. */
  1898. if ((mpd->b_state & (1 << BH_Mapped)) &&
  1899. !(mpd->b_state & (1 << BH_Delay)) &&
  1900. !(mpd->b_state & (1 << BH_Unwritten)))
  1901. return 0;
  1902. /*
  1903. * If we didn't accumulate anything to write simply return
  1904. */
  1905. if (!mpd->b_size)
  1906. return 0;
  1907. handle = ext4_journal_current_handle();
  1908. BUG_ON(!handle);
  1909. /*
  1910. * Call ext4_get_blocks() to allocate any delayed allocation
  1911. * blocks, or to convert an uninitialized extent to be
  1912. * initialized (in the case where we have written into
  1913. * one or more preallocated blocks).
  1914. *
  1915. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
  1916. * indicate that we are on the delayed allocation path. This
  1917. * affects functions in many different parts of the allocation
  1918. * call path. This flag exists primarily because we don't
  1919. * want to change *many* call functions, so ext4_get_blocks()
  1920. * will set the magic i_delalloc_reserved_flag once the
  1921. * inode's allocation semaphore is taken.
  1922. *
  1923. * If the blocks in questions were delalloc blocks, set
  1924. * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
  1925. * variables are updated after the blocks have been allocated.
  1926. */
  1927. new.b_state = 0;
  1928. get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
  1929. EXT4_GET_BLOCKS_DELALLOC_RESERVE);
  1930. if (mpd->b_state & (1 << BH_Delay))
  1931. get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
  1932. blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
  1933. &new, get_blocks_flags);
  1934. if (blks < 0) {
  1935. err = blks;
  1936. /*
  1937. * If get block returns with error we simply
  1938. * return. Later writepage will redirty the page and
  1939. * writepages will find the dirty page again
  1940. */
  1941. if (err == -EAGAIN)
  1942. return 0;
  1943. if (err == -ENOSPC &&
  1944. ext4_count_free_blocks(mpd->inode->i_sb)) {
  1945. mpd->retval = err;
  1946. return 0;
  1947. }
  1948. /*
  1949. * get block failure will cause us to loop in
  1950. * writepages, because a_ops->writepage won't be able
  1951. * to make progress. The page will be redirtied by
  1952. * writepage and writepages will again try to write
  1953. * the same.
  1954. */
  1955. printk(KERN_EMERG "%s block allocation failed for inode %lu "
  1956. "at logical offset %llu with max blocks "
  1957. "%zd with error %d\n",
  1958. __func__, mpd->inode->i_ino,
  1959. (unsigned long long)next,
  1960. mpd->b_size >> mpd->inode->i_blkbits, err);
  1961. printk(KERN_EMERG "This should not happen.!! "
  1962. "Data will be lost\n");
  1963. if (err == -ENOSPC) {
  1964. ext4_print_free_blocks(mpd->inode);
  1965. }
  1966. /* invalidate all the pages */
  1967. ext4_da_block_invalidatepages(mpd, next,
  1968. mpd->b_size >> mpd->inode->i_blkbits);
  1969. return err;
  1970. }
  1971. BUG_ON(blks == 0);
  1972. new.b_size = (blks << mpd->inode->i_blkbits);
  1973. if (buffer_new(&new))
  1974. __unmap_underlying_blocks(mpd->inode, &new);
  1975. /*
  1976. * If blocks are delayed marked, we need to
  1977. * put actual blocknr and drop delayed bit
  1978. */
  1979. if ((mpd->b_state & (1 << BH_Delay)) ||
  1980. (mpd->b_state & (1 << BH_Unwritten)))
  1981. mpage_put_bnr_to_bhs(mpd, next, &new);
  1982. if (ext4_should_order_data(mpd->inode)) {
  1983. err = ext4_jbd2_file_inode(handle, mpd->inode);
  1984. if (err)
  1985. return err;
  1986. }
  1987. /*
  1988. * Update on-disk size along with block allocation.
  1989. */
  1990. disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
  1991. if (disksize > i_size_read(mpd->inode))
  1992. disksize = i_size_read(mpd->inode);
  1993. if (disksize > EXT4_I(mpd->inode)->i_disksize) {
  1994. ext4_update_i_disksize(mpd->inode, disksize);
  1995. return ext4_mark_inode_dirty(handle, mpd->inode);
  1996. }
  1997. return 0;
  1998. }
  1999. #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
  2000. (1 << BH_Delay) | (1 << BH_Unwritten))
  2001. /*
  2002. * mpage_add_bh_to_extent - try to add one more block to extent of blocks
  2003. *
  2004. * @mpd->lbh - extent of blocks
  2005. * @logical - logical number of the block in the file
  2006. * @bh - bh of the block (used to access block's state)
  2007. *
  2008. * the function is used to collect contig. blocks in same state
  2009. */
  2010. static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
  2011. sector_t logical, size_t b_size,
  2012. unsigned long b_state)
  2013. {
  2014. sector_t next;
  2015. int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
  2016. /* check if thereserved journal credits might overflow */
  2017. if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
  2018. if (nrblocks >= EXT4_MAX_TRANS_DATA) {
  2019. /*
  2020. * With non-extent format we are limited by the journal
  2021. * credit available. Total credit needed to insert
  2022. * nrblocks contiguous blocks is dependent on the
  2023. * nrblocks. So limit nrblocks.
  2024. */
  2025. goto flush_it;
  2026. } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
  2027. EXT4_MAX_TRANS_DATA) {
  2028. /*
  2029. * Adding the new buffer_head would make it cross the
  2030. * allowed limit for which we have journal credit
  2031. * reserved. So limit the new bh->b_size
  2032. */
  2033. b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
  2034. mpd->inode->i_blkbits;
  2035. /* we will do mpage_da_submit_io in the next loop */
  2036. }
  2037. }
  2038. /*
  2039. * First block in the extent
  2040. */
  2041. if (mpd->b_size == 0) {
  2042. mpd->b_blocknr = logical;
  2043. mpd->b_size = b_size;
  2044. mpd->b_state = b_state & BH_FLAGS;
  2045. return;
  2046. }
  2047. next = mpd->b_blocknr + nrblocks;
  2048. /*
  2049. * Can we merge the block to our big extent?
  2050. */
  2051. if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
  2052. mpd->b_size += b_size;
  2053. return;
  2054. }
  2055. flush_it:
  2056. /*
  2057. * We couldn't merge the block to our extent, so we
  2058. * need to flush current extent and start new one
  2059. */
  2060. if (mpage_da_map_blocks(mpd) == 0)
  2061. mpage_da_submit_io(mpd);
  2062. mpd->io_done = 1;
  2063. return;
  2064. }
  2065. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  2066. {
  2067. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  2068. }
  2069. /*
  2070. * __mpage_da_writepage - finds extent of pages and blocks
  2071. *
  2072. * @page: page to consider
  2073. * @wbc: not used, we just follow rules
  2074. * @data: context
  2075. *
  2076. * The function finds extents of pages and scan them for all blocks.
  2077. */
  2078. static int __mpage_da_writepage(struct page *page,
  2079. struct writeback_control *wbc, void *data)
  2080. {
  2081. struct mpage_da_data *mpd = data;
  2082. struct inode *inode = mpd->inode;
  2083. struct buffer_head *bh, *head;
  2084. sector_t logical;
  2085. if (mpd->io_done) {
  2086. /*
  2087. * Rest of the page in the page_vec
  2088. * redirty then and skip then. We will
  2089. * try to to write them again after
  2090. * starting a new transaction
  2091. */
  2092. redirty_page_for_writepage(wbc, page);
  2093. unlock_page(page);
  2094. return MPAGE_DA_EXTENT_TAIL;
  2095. }
  2096. /*
  2097. * Can we merge this page to current extent?
  2098. */
  2099. if (mpd->next_page != page->index) {
  2100. /*
  2101. * Nope, we can't. So, we map non-allocated blocks
  2102. * and start IO on them using writepage()
  2103. */
  2104. if (mpd->next_page != mpd->first_page) {
  2105. if (mpage_da_map_blocks(mpd) == 0)
  2106. mpage_da_submit_io(mpd);
  2107. /*
  2108. * skip rest of the page in the page_vec
  2109. */
  2110. mpd->io_done = 1;
  2111. redirty_page_for_writepage(wbc, page);
  2112. unlock_page(page);
  2113. return MPAGE_DA_EXTENT_TAIL;
  2114. }
  2115. /*
  2116. * Start next extent of pages ...
  2117. */
  2118. mpd->first_page = page->index;
  2119. /*
  2120. * ... and blocks
  2121. */
  2122. mpd->b_size = 0;
  2123. mpd->b_state = 0;
  2124. mpd->b_blocknr = 0;
  2125. }
  2126. mpd->next_page = page->index + 1;
  2127. logical = (sector_t) page->index <<
  2128. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2129. if (!page_has_buffers(page)) {
  2130. mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
  2131. (1 << BH_Dirty) | (1 << BH_Uptodate));
  2132. if (mpd->io_done)
  2133. return MPAGE_DA_EXTENT_TAIL;
  2134. } else {
  2135. /*
  2136. * Page with regular buffer heads, just add all dirty ones
  2137. */
  2138. head = page_buffers(page);
  2139. bh = head;
  2140. do {
  2141. BUG_ON(buffer_locked(bh));
  2142. /*
  2143. * We need to try to allocate
  2144. * unmapped blocks in the same page.
  2145. * Otherwise we won't make progress
  2146. * with the page in ext4_writepage
  2147. */
  2148. if (ext4_bh_delay_or_unwritten(NULL, bh)) {
  2149. mpage_add_bh_to_extent(mpd, logical,
  2150. bh->b_size,
  2151. bh->b_state);
  2152. if (mpd->io_done)
  2153. return MPAGE_DA_EXTENT_TAIL;
  2154. } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
  2155. /*
  2156. * mapped dirty buffer. We need to update
  2157. * the b_state because we look at
  2158. * b_state in mpage_da_map_blocks. We don't
  2159. * update b_size because if we find an
  2160. * unmapped buffer_head later we need to
  2161. * use the b_state flag of that buffer_head.
  2162. */
  2163. if (mpd->b_size == 0)
  2164. mpd->b_state = bh->b_state & BH_FLAGS;
  2165. }
  2166. logical++;
  2167. } while ((bh = bh->b_this_page) != head);
  2168. }
  2169. return 0;
  2170. }
  2171. /*
  2172. * This is a special get_blocks_t callback which is used by
  2173. * ext4_da_write_begin(). It will either return mapped block or
  2174. * reserve space for a single block.
  2175. *
  2176. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  2177. * We also have b_blocknr = -1 and b_bdev initialized properly
  2178. *
  2179. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  2180. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  2181. * initialized properly.
  2182. */
  2183. static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  2184. struct buffer_head *bh_result, int create)
  2185. {
  2186. int ret = 0;
  2187. sector_t invalid_block = ~((sector_t) 0xffff);
  2188. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  2189. invalid_block = ~0;
  2190. BUG_ON(create == 0);
  2191. BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
  2192. /*
  2193. * first, we need to know whether the block is allocated already
  2194. * preallocated blocks are unmapped but should treated
  2195. * the same as allocated blocks.
  2196. */
  2197. ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
  2198. if ((ret == 0) && !buffer_delay(bh_result)) {
  2199. /* the block isn't (pre)allocated yet, let's reserve space */
  2200. /*
  2201. * XXX: __block_prepare_write() unmaps passed block,
  2202. * is it OK?
  2203. */
  2204. ret = ext4_da_reserve_space(inode, 1);
  2205. if (ret)
  2206. /* not enough space to reserve */
  2207. return ret;
  2208. map_bh(bh_result, inode->i_sb, invalid_block);
  2209. set_buffer_new(bh_result);
  2210. set_buffer_delay(bh_result);
  2211. } else if (ret > 0) {
  2212. bh_result->b_size = (ret << inode->i_blkbits);
  2213. if (buffer_unwritten(bh_result)) {
  2214. /* A delayed write to unwritten bh should
  2215. * be marked new and mapped. Mapped ensures
  2216. * that we don't do get_block multiple times
  2217. * when we write to the same offset and new
  2218. * ensures that we do proper zero out for
  2219. * partial write.
  2220. */
  2221. set_buffer_new(bh_result);
  2222. set_buffer_mapped(bh_result);
  2223. }
  2224. ret = 0;
  2225. }
  2226. return ret;
  2227. }
  2228. /*
  2229. * This function is used as a standard get_block_t calback function
  2230. * when there is no desire to allocate any blocks. It is used as a
  2231. * callback function for block_prepare_write(), nobh_writepage(), and
  2232. * block_write_full_page(). These functions should only try to map a
  2233. * single block at a time.
  2234. *
  2235. * Since this function doesn't do block allocations even if the caller
  2236. * requests it by passing in create=1, it is critically important that
  2237. * any caller checks to make sure that any buffer heads are returned
  2238. * by this function are either all already mapped or marked for
  2239. * delayed allocation before calling nobh_writepage() or
  2240. * block_write_full_page(). Otherwise, b_blocknr could be left
  2241. * unitialized, and the page write functions will be taken by
  2242. * surprise.
  2243. */
  2244. static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
  2245. struct buffer_head *bh_result, int create)
  2246. {
  2247. int ret = 0;
  2248. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  2249. BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
  2250. /*
  2251. * we don't want to do block allocation in writepage
  2252. * so call get_block_wrap with create = 0
  2253. */
  2254. ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
  2255. if (ret > 0) {
  2256. bh_result->b_size = (ret << inode->i_blkbits);
  2257. ret = 0;
  2258. }
  2259. return ret;
  2260. }
  2261. static int bget_one(handle_t *handle, struct buffer_head *bh)
  2262. {
  2263. get_bh(bh);
  2264. return 0;
  2265. }
  2266. static int bput_one(handle_t *handle, struct buffer_head *bh)
  2267. {
  2268. put_bh(bh);
  2269. return 0;
  2270. }
  2271. static int __ext4_journalled_writepage(struct page *page,
  2272. struct writeback_control *wbc,
  2273. unsigned int len)
  2274. {
  2275. struct address_space *mapping = page->mapping;
  2276. struct inode *inode = mapping->host;
  2277. struct buffer_head *page_bufs;
  2278. handle_t *handle = NULL;
  2279. int ret = 0;
  2280. int err;
  2281. page_bufs = page_buffers(page);
  2282. BUG_ON(!page_bufs);
  2283. walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
  2284. /* As soon as we unlock the page, it can go away, but we have
  2285. * references to buffers so we are safe */
  2286. unlock_page(page);
  2287. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  2288. if (IS_ERR(handle)) {
  2289. ret = PTR_ERR(handle);
  2290. goto out;
  2291. }
  2292. ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
  2293. do_journal_get_write_access);
  2294. err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
  2295. write_end_fn);
  2296. if (ret == 0)
  2297. ret = err;
  2298. err = ext4_journal_stop(handle);
  2299. if (!ret)
  2300. ret = err;
  2301. walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
  2302. EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
  2303. out:
  2304. return ret;
  2305. }
  2306. /*
  2307. * Note that we don't need to start a transaction unless we're journaling data
  2308. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  2309. * need to file the inode to the transaction's list in ordered mode because if
  2310. * we are writing back data added by write(), the inode is already there and if
  2311. * we are writing back data modified via mmap(), noone guarantees in which
  2312. * transaction the data will hit the disk. In case we are journaling data, we
  2313. * cannot start transaction directly because transaction start ranks above page
  2314. * lock so we have to do some magic.
  2315. *
  2316. * This function can get called via...
  2317. * - ext4_da_writepages after taking page lock (have journal handle)
  2318. * - journal_submit_inode_data_buffers (no journal handle)
  2319. * - shrink_page_list via pdflush (no journal handle)
  2320. * - grab_page_cache when doing write_begin (have journal handle)
  2321. *
  2322. * We don't do any block allocation in this function. If we have page with
  2323. * multiple blocks we need to write those buffer_heads that are mapped. This
  2324. * is important for mmaped based write. So if we do with blocksize 1K
  2325. * truncate(f, 1024);
  2326. * a = mmap(f, 0, 4096);
  2327. * a[0] = 'a';
  2328. * truncate(f, 4096);
  2329. * we have in the page first buffer_head mapped via page_mkwrite call back
  2330. * but other bufer_heads would be unmapped but dirty(dirty done via the
  2331. * do_wp_page). So writepage should write the first block. If we modify
  2332. * the mmap area beyond 1024 we will again get a page_fault and the
  2333. * page_mkwrite callback will do the block allocation and mark the
  2334. * buffer_heads mapped.
  2335. *
  2336. * We redirty the page if we have any buffer_heads that is either delay or
  2337. * unwritten in the page.
  2338. *
  2339. * We can get recursively called as show below.
  2340. *
  2341. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  2342. * ext4_writepage()
  2343. *
  2344. * But since we don't do any block allocation we should not deadlock.
  2345. * Page also have the dirty flag cleared so we don't get recurive page_lock.
  2346. */
  2347. static int ext4_writepage(struct page *page,
  2348. struct writeback_control *wbc)
  2349. {
  2350. int ret = 0;
  2351. loff_t size;
  2352. unsigned int len;
  2353. struct buffer_head *page_bufs;
  2354. struct inode *inode = page->mapping->host;
  2355. trace_ext4_writepage(inode, page);
  2356. size = i_size_read(inode);
  2357. if (page->index == size >> PAGE_CACHE_SHIFT)
  2358. len = size & ~PAGE_CACHE_MASK;
  2359. else
  2360. len = PAGE_CACHE_SIZE;
  2361. if (page_has_buffers(page)) {
  2362. page_bufs = page_buffers(page);
  2363. if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  2364. ext4_bh_delay_or_unwritten)) {
  2365. /*
  2366. * We don't want to do block allocation
  2367. * So redirty the page and return
  2368. * We may reach here when we do a journal commit
  2369. * via journal_submit_inode_data_buffers.
  2370. * If we don't have mapping block we just ignore
  2371. * them. We can also reach here via shrink_page_list
  2372. */
  2373. redirty_page_for_writepage(wbc, page);
  2374. unlock_page(page);
  2375. return 0;
  2376. }
  2377. } else {
  2378. /*
  2379. * The test for page_has_buffers() is subtle:
  2380. * We know the page is dirty but it lost buffers. That means
  2381. * that at some moment in time after write_begin()/write_end()
  2382. * has been called all buffers have been clean and thus they
  2383. * must have been written at least once. So they are all
  2384. * mapped and we can happily proceed with mapping them
  2385. * and writing the page.
  2386. *
  2387. * Try to initialize the buffer_heads and check whether
  2388. * all are mapped and non delay. We don't want to
  2389. * do block allocation here.
  2390. */
  2391. ret = block_prepare_write(page, 0, len,
  2392. noalloc_get_block_write);
  2393. if (!ret) {
  2394. page_bufs = page_buffers(page);
  2395. /* check whether all are mapped and non delay */
  2396. if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  2397. ext4_bh_delay_or_unwritten)) {
  2398. redirty_page_for_writepage(wbc, page);
  2399. unlock_page(page);
  2400. return 0;
  2401. }
  2402. } else {
  2403. /*
  2404. * We can't do block allocation here
  2405. * so just redity the page and unlock
  2406. * and return
  2407. */
  2408. redirty_page_for_writepage(wbc, page);
  2409. unlock_page(page);
  2410. return 0;
  2411. }
  2412. /* now mark the buffer_heads as dirty and uptodate */
  2413. block_commit_write(page, 0, len);
  2414. }
  2415. if (PageChecked(page) && ext4_should_journal_data(inode)) {
  2416. /*
  2417. * It's mmapped pagecache. Add buffers and journal it. There
  2418. * doesn't seem much point in redirtying the page here.
  2419. */
  2420. ClearPageChecked(page);
  2421. return __ext4_journalled_writepage(page, wbc, len);
  2422. }
  2423. if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
  2424. ret = nobh_writepage(page, noalloc_get_block_write, wbc);
  2425. else
  2426. ret = block_write_full_page(page, noalloc_get_block_write,
  2427. wbc);
  2428. return ret;
  2429. }
  2430. /*
  2431. * This is called via ext4_da_writepages() to
  2432. * calulate the total number of credits to reserve to fit
  2433. * a single extent allocation into a single transaction,
  2434. * ext4_da_writpeages() will loop calling this before
  2435. * the block allocation.
  2436. */
  2437. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  2438. {
  2439. int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  2440. /*
  2441. * With non-extent format the journal credit needed to
  2442. * insert nrblocks contiguous block is dependent on
  2443. * number of contiguous block. So we will limit
  2444. * number of contiguous block to a sane value
  2445. */
  2446. if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
  2447. (max_blocks > EXT4_MAX_TRANS_DATA))
  2448. max_blocks = EXT4_MAX_TRANS_DATA;
  2449. return ext4_chunk_trans_blocks(inode, max_blocks);
  2450. }
  2451. static int ext4_da_writepages(struct address_space *mapping,
  2452. struct writeback_control *wbc)
  2453. {
  2454. pgoff_t index;
  2455. int range_whole = 0;
  2456. handle_t *handle = NULL;
  2457. struct mpage_da_data mpd;
  2458. struct inode *inode = mapping->host;
  2459. int no_nrwrite_index_update;
  2460. int pages_written = 0;
  2461. long pages_skipped;
  2462. int range_cyclic, cycled = 1, io_done = 0;
  2463. int needed_blocks, ret = 0, nr_to_writebump = 0;
  2464. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2465. trace_ext4_da_writepages(inode, wbc);
  2466. /*
  2467. * No pages to write? This is mainly a kludge to avoid starting
  2468. * a transaction for special inodes like journal inode on last iput()
  2469. * because that could violate lock ordering on umount
  2470. */
  2471. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2472. return 0;
  2473. /*
  2474. * If the filesystem has aborted, it is read-only, so return
  2475. * right away instead of dumping stack traces later on that
  2476. * will obscure the real source of the problem. We test
  2477. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  2478. * the latter could be true if the filesystem is mounted
  2479. * read-only, and in that case, ext4_da_writepages should
  2480. * *never* be called, so if that ever happens, we would want
  2481. * the stack trace.
  2482. */
  2483. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
  2484. return -EROFS;
  2485. /*
  2486. * Make sure nr_to_write is >= sbi->s_mb_stream_request
  2487. * This make sure small files blocks are allocated in
  2488. * single attempt. This ensure that small files
  2489. * get less fragmented.
  2490. */
  2491. if (wbc->nr_to_write < sbi->s_mb_stream_request) {
  2492. nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
  2493. wbc->nr_to_write = sbi->s_mb_stream_request;
  2494. }
  2495. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2496. range_whole = 1;
  2497. range_cyclic = wbc->range_cyclic;
  2498. if (wbc->range_cyclic) {
  2499. index = mapping->writeback_index;
  2500. if (index)
  2501. cycled = 0;
  2502. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2503. wbc->range_end = LLONG_MAX;
  2504. wbc->range_cyclic = 0;
  2505. } else
  2506. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2507. mpd.wbc = wbc;
  2508. mpd.inode = mapping->host;
  2509. /*
  2510. * we don't want write_cache_pages to update
  2511. * nr_to_write and writeback_index
  2512. */
  2513. no_nrwrite_index_update = wbc->no_nrwrite_index_update;
  2514. wbc->no_nrwrite_index_update = 1;
  2515. pages_skipped = wbc->pages_skipped;
  2516. retry:
  2517. while (!ret && wbc->nr_to_write > 0) {
  2518. /*
  2519. * we insert one extent at a time. So we need
  2520. * credit needed for single extent allocation.
  2521. * journalled mode is currently not supported
  2522. * by delalloc
  2523. */
  2524. BUG_ON(ext4_should_journal_data(inode));
  2525. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2526. /* start a new transaction*/
  2527. handle = ext4_journal_start(inode, needed_blocks);
  2528. if (IS_ERR(handle)) {
  2529. ret = PTR_ERR(handle);
  2530. printk(KERN_CRIT "%s: jbd2_start: "
  2531. "%ld pages, ino %lu; err %d\n", __func__,
  2532. wbc->nr_to_write, inode->i_ino, ret);
  2533. dump_stack();
  2534. goto out_writepages;
  2535. }
  2536. /*
  2537. * Now call __mpage_da_writepage to find the next
  2538. * contiguous region of logical blocks that need
  2539. * blocks to be allocated by ext4. We don't actually
  2540. * submit the blocks for I/O here, even though
  2541. * write_cache_pages thinks it will, and will set the
  2542. * pages as clean for write before calling
  2543. * __mpage_da_writepage().
  2544. */
  2545. mpd.b_size = 0;
  2546. mpd.b_state = 0;
  2547. mpd.b_blocknr = 0;
  2548. mpd.first_page = 0;
  2549. mpd.next_page = 0;
  2550. mpd.io_done = 0;
  2551. mpd.pages_written = 0;
  2552. mpd.retval = 0;
  2553. ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
  2554. &mpd);
  2555. /*
  2556. * If we have a contigous extent of pages and we
  2557. * haven't done the I/O yet, map the blocks and submit
  2558. * them for I/O.
  2559. */
  2560. if (!mpd.io_done && mpd.next_page != mpd.first_page) {
  2561. if (mpage_da_map_blocks(&mpd) == 0)
  2562. mpage_da_submit_io(&mpd);
  2563. mpd.io_done = 1;
  2564. ret = MPAGE_DA_EXTENT_TAIL;
  2565. }
  2566. wbc->nr_to_write -= mpd.pages_written;
  2567. ext4_journal_stop(handle);
  2568. if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
  2569. /* commit the transaction which would
  2570. * free blocks released in the transaction
  2571. * and try again
  2572. */
  2573. jbd2_journal_force_commit_nested(sbi->s_journal);
  2574. wbc->pages_skipped = pages_skipped;
  2575. ret = 0;
  2576. } else if (ret == MPAGE_DA_EXTENT_TAIL) {
  2577. /*
  2578. * got one extent now try with
  2579. * rest of the pages
  2580. */
  2581. pages_written += mpd.pages_written;
  2582. wbc->pages_skipped = pages_skipped;
  2583. ret = 0;
  2584. io_done = 1;
  2585. } else if (wbc->nr_to_write)
  2586. /*
  2587. * There is no more writeout needed
  2588. * or we requested for a noblocking writeout
  2589. * and we found the device congested
  2590. */
  2591. break;
  2592. }
  2593. if (!io_done && !cycled) {
  2594. cycled = 1;
  2595. index = 0;
  2596. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2597. wbc->range_end = mapping->writeback_index - 1;
  2598. goto retry;
  2599. }
  2600. if (pages_skipped != wbc->pages_skipped)
  2601. printk(KERN_EMERG "This should not happen leaving %s "
  2602. "with nr_to_write = %ld ret = %d\n",
  2603. __func__, wbc->nr_to_write, ret);
  2604. /* Update index */
  2605. index += pages_written;
  2606. wbc->range_cyclic = range_cyclic;
  2607. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2608. /*
  2609. * set the writeback_index so that range_cyclic
  2610. * mode will write it back later
  2611. */
  2612. mapping->writeback_index = index;
  2613. out_writepages:
  2614. if (!no_nrwrite_index_update)
  2615. wbc->no_nrwrite_index_update = 0;
  2616. wbc->nr_to_write -= nr_to_writebump;
  2617. trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
  2618. return ret;
  2619. }
  2620. #define FALL_BACK_TO_NONDELALLOC 1
  2621. static int ext4_nonda_switch(struct super_block *sb)
  2622. {
  2623. s64 free_blocks, dirty_blocks;
  2624. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2625. /*
  2626. * switch to non delalloc mode if we are running low
  2627. * on free block. The free block accounting via percpu
  2628. * counters can get slightly wrong with percpu_counter_batch getting
  2629. * accumulated on each CPU without updating global counters
  2630. * Delalloc need an accurate free block accounting. So switch
  2631. * to non delalloc when we are near to error range.
  2632. */
  2633. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  2634. dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
  2635. if (2 * free_blocks < 3 * dirty_blocks ||
  2636. free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
  2637. /*
  2638. * free block count is less that 150% of dirty blocks
  2639. * or free blocks is less that watermark
  2640. */
  2641. return 1;
  2642. }
  2643. return 0;
  2644. }
  2645. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2646. loff_t pos, unsigned len, unsigned flags,
  2647. struct page **pagep, void **fsdata)
  2648. {
  2649. int ret, retries = 0;
  2650. struct page *page;
  2651. pgoff_t index;
  2652. unsigned from, to;
  2653. struct inode *inode = mapping->host;
  2654. handle_t *handle;
  2655. index = pos >> PAGE_CACHE_SHIFT;
  2656. from = pos & (PAGE_CACHE_SIZE - 1);
  2657. to = from + len;
  2658. if (ext4_nonda_switch(inode->i_sb)) {
  2659. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2660. return ext4_write_begin(file, mapping, pos,
  2661. len, flags, pagep, fsdata);
  2662. }
  2663. *fsdata = (void *)0;
  2664. trace_ext4_da_write_begin(inode, pos, len, flags);
  2665. retry:
  2666. /*
  2667. * With delayed allocation, we don't log the i_disksize update
  2668. * if there is delayed block allocation. But we still need
  2669. * to journalling the i_disksize update if writes to the end
  2670. * of file which has an already mapped buffer.
  2671. */
  2672. handle = ext4_journal_start(inode, 1);
  2673. if (IS_ERR(handle)) {
  2674. ret = PTR_ERR(handle);
  2675. goto out;
  2676. }
  2677. /* We cannot recurse into the filesystem as the transaction is already
  2678. * started */
  2679. flags |= AOP_FLAG_NOFS;
  2680. page = grab_cache_page_write_begin(mapping, index, flags);
  2681. if (!page) {
  2682. ext4_journal_stop(handle);
  2683. ret = -ENOMEM;
  2684. goto out;
  2685. }
  2686. *pagep = page;
  2687. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  2688. ext4_da_get_block_prep);
  2689. if (ret < 0) {
  2690. unlock_page(page);
  2691. ext4_journal_stop(handle);
  2692. page_cache_release(page);
  2693. /*
  2694. * block_write_begin may have instantiated a few blocks
  2695. * outside i_size. Trim these off again. Don't need
  2696. * i_size_read because we hold i_mutex.
  2697. */
  2698. if (pos + len > inode->i_size)
  2699. ext4_truncate(inode);
  2700. }
  2701. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2702. goto retry;
  2703. out:
  2704. return ret;
  2705. }
  2706. /*
  2707. * Check if we should update i_disksize
  2708. * when write to the end of file but not require block allocation
  2709. */
  2710. static int ext4_da_should_update_i_disksize(struct page *page,
  2711. unsigned long offset)
  2712. {
  2713. struct buffer_head *bh;
  2714. struct inode *inode = page->mapping->host;
  2715. unsigned int idx;
  2716. int i;
  2717. bh = page_buffers(page);
  2718. idx = offset >> inode->i_blkbits;
  2719. for (i = 0; i < idx; i++)
  2720. bh = bh->b_this_page;
  2721. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2722. return 0;
  2723. return 1;
  2724. }
  2725. static int ext4_da_write_end(struct file *file,
  2726. struct address_space *mapping,
  2727. loff_t pos, unsigned len, unsigned copied,
  2728. struct page *page, void *fsdata)
  2729. {
  2730. struct inode *inode = mapping->host;
  2731. int ret = 0, ret2;
  2732. handle_t *handle = ext4_journal_current_handle();
  2733. loff_t new_i_size;
  2734. unsigned long start, end;
  2735. int write_mode = (int)(unsigned long)fsdata;
  2736. if (write_mode == FALL_BACK_TO_NONDELALLOC) {
  2737. if (ext4_should_order_data(inode)) {
  2738. return ext4_ordered_write_end(file, mapping, pos,
  2739. len, copied, page, fsdata);
  2740. } else if (ext4_should_writeback_data(inode)) {
  2741. return ext4_writeback_write_end(file, mapping, pos,
  2742. len, copied, page, fsdata);
  2743. } else {
  2744. BUG();
  2745. }
  2746. }
  2747. trace_ext4_da_write_end(inode, pos, len, copied);
  2748. start = pos & (PAGE_CACHE_SIZE - 1);
  2749. end = start + copied - 1;
  2750. /*
  2751. * generic_write_end() will run mark_inode_dirty() if i_size
  2752. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2753. * into that.
  2754. */
  2755. new_i_size = pos + copied;
  2756. if (new_i_size > EXT4_I(inode)->i_disksize) {
  2757. if (ext4_da_should_update_i_disksize(page, end)) {
  2758. down_write(&EXT4_I(inode)->i_data_sem);
  2759. if (new_i_size > EXT4_I(inode)->i_disksize) {
  2760. /*
  2761. * Updating i_disksize when extending file
  2762. * without needing block allocation
  2763. */
  2764. if (ext4_should_order_data(inode))
  2765. ret = ext4_jbd2_file_inode(handle,
  2766. inode);
  2767. EXT4_I(inode)->i_disksize = new_i_size;
  2768. }
  2769. up_write(&EXT4_I(inode)->i_data_sem);
  2770. /* We need to mark inode dirty even if
  2771. * new_i_size is less that inode->i_size
  2772. * bu greater than i_disksize.(hint delalloc)
  2773. */
  2774. ext4_mark_inode_dirty(handle, inode);
  2775. }
  2776. }
  2777. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2778. page, fsdata);
  2779. copied = ret2;
  2780. if (ret2 < 0)
  2781. ret = ret2;
  2782. ret2 = ext4_journal_stop(handle);
  2783. if (!ret)
  2784. ret = ret2;
  2785. return ret ? ret : copied;
  2786. }
  2787. static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
  2788. {
  2789. /*
  2790. * Drop reserved blocks
  2791. */
  2792. BUG_ON(!PageLocked(page));
  2793. if (!page_has_buffers(page))
  2794. goto out;
  2795. ext4_da_page_release_reservation(page, offset);
  2796. out:
  2797. ext4_invalidatepage(page, offset);
  2798. return;
  2799. }
  2800. /*
  2801. * Force all delayed allocation blocks to be allocated for a given inode.
  2802. */
  2803. int ext4_alloc_da_blocks(struct inode *inode)
  2804. {
  2805. if (!EXT4_I(inode)->i_reserved_data_blocks &&
  2806. !EXT4_I(inode)->i_reserved_meta_blocks)
  2807. return 0;
  2808. /*
  2809. * We do something simple for now. The filemap_flush() will
  2810. * also start triggering a write of the data blocks, which is
  2811. * not strictly speaking necessary (and for users of
  2812. * laptop_mode, not even desirable). However, to do otherwise
  2813. * would require replicating code paths in:
  2814. *
  2815. * ext4_da_writepages() ->
  2816. * write_cache_pages() ---> (via passed in callback function)
  2817. * __mpage_da_writepage() -->
  2818. * mpage_add_bh_to_extent()
  2819. * mpage_da_map_blocks()
  2820. *
  2821. * The problem is that write_cache_pages(), located in
  2822. * mm/page-writeback.c, marks pages clean in preparation for
  2823. * doing I/O, which is not desirable if we're not planning on
  2824. * doing I/O at all.
  2825. *
  2826. * We could call write_cache_pages(), and then redirty all of
  2827. * the pages by calling redirty_page_for_writeback() but that
  2828. * would be ugly in the extreme. So instead we would need to
  2829. * replicate parts of the code in the above functions,
  2830. * simplifying them becuase we wouldn't actually intend to
  2831. * write out the pages, but rather only collect contiguous
  2832. * logical block extents, call the multi-block allocator, and
  2833. * then update the buffer heads with the block allocations.
  2834. *
  2835. * For now, though, we'll cheat by calling filemap_flush(),
  2836. * which will map the blocks, and start the I/O, but not
  2837. * actually wait for the I/O to complete.
  2838. */
  2839. return filemap_flush(inode->i_mapping);
  2840. }
  2841. /*
  2842. * bmap() is special. It gets used by applications such as lilo and by
  2843. * the swapper to find the on-disk block of a specific piece of data.
  2844. *
  2845. * Naturally, this is dangerous if the block concerned is still in the
  2846. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2847. * filesystem and enables swap, then they may get a nasty shock when the
  2848. * data getting swapped to that swapfile suddenly gets overwritten by
  2849. * the original zero's written out previously to the journal and
  2850. * awaiting writeback in the kernel's buffer cache.
  2851. *
  2852. * So, if we see any bmap calls here on a modified, data-journaled file,
  2853. * take extra steps to flush any blocks which might be in the cache.
  2854. */
  2855. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2856. {
  2857. struct inode *inode = mapping->host;
  2858. journal_t *journal;
  2859. int err;
  2860. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2861. test_opt(inode->i_sb, DELALLOC)) {
  2862. /*
  2863. * With delalloc we want to sync the file
  2864. * so that we can make sure we allocate
  2865. * blocks for file
  2866. */
  2867. filemap_write_and_wait(mapping);
  2868. }
  2869. if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
  2870. /*
  2871. * This is a REALLY heavyweight approach, but the use of
  2872. * bmap on dirty files is expected to be extremely rare:
  2873. * only if we run lilo or swapon on a freshly made file
  2874. * do we expect this to happen.
  2875. *
  2876. * (bmap requires CAP_SYS_RAWIO so this does not
  2877. * represent an unprivileged user DOS attack --- we'd be
  2878. * in trouble if mortal users could trigger this path at
  2879. * will.)
  2880. *
  2881. * NB. EXT4_STATE_JDATA is not set on files other than
  2882. * regular files. If somebody wants to bmap a directory
  2883. * or symlink and gets confused because the buffer
  2884. * hasn't yet been flushed to disk, they deserve
  2885. * everything they get.
  2886. */
  2887. EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
  2888. journal = EXT4_JOURNAL(inode);
  2889. jbd2_journal_lock_updates(journal);
  2890. err = jbd2_journal_flush(journal);
  2891. jbd2_journal_unlock_updates(journal);
  2892. if (err)
  2893. return 0;
  2894. }
  2895. return generic_block_bmap(mapping, block, ext4_get_block);
  2896. }
  2897. static int ext4_readpage(struct file *file, struct page *page)
  2898. {
  2899. return mpage_readpage(page, ext4_get_block);
  2900. }
  2901. static int
  2902. ext4_readpages(struct file *file, struct address_space *mapping,
  2903. struct list_head *pages, unsigned nr_pages)
  2904. {
  2905. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2906. }
  2907. static void ext4_invalidatepage(struct page *page, unsigned long offset)
  2908. {
  2909. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2910. /*
  2911. * If it's a full truncate we just forget about the pending dirtying
  2912. */
  2913. if (offset == 0)
  2914. ClearPageChecked(page);
  2915. if (journal)
  2916. jbd2_journal_invalidatepage(journal, page, offset);
  2917. else
  2918. block_invalidatepage(page, offset);
  2919. }
  2920. static int ext4_releasepage(struct page *page, gfp_t wait)
  2921. {
  2922. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2923. WARN_ON(PageChecked(page));
  2924. if (!page_has_buffers(page))
  2925. return 0;
  2926. if (journal)
  2927. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2928. else
  2929. return try_to_free_buffers(page);
  2930. }
  2931. /*
  2932. * If the O_DIRECT write will extend the file then add this inode to the
  2933. * orphan list. So recovery will truncate it back to the original size
  2934. * if the machine crashes during the write.
  2935. *
  2936. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  2937. * crashes then stale disk data _may_ be exposed inside the file. But current
  2938. * VFS code falls back into buffered path in that case so we are safe.
  2939. */
  2940. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  2941. const struct iovec *iov, loff_t offset,
  2942. unsigned long nr_segs)
  2943. {
  2944. struct file *file = iocb->ki_filp;
  2945. struct inode *inode = file->f_mapping->host;
  2946. struct ext4_inode_info *ei = EXT4_I(inode);
  2947. handle_t *handle;
  2948. ssize_t ret;
  2949. int orphan = 0;
  2950. size_t count = iov_length(iov, nr_segs);
  2951. if (rw == WRITE) {
  2952. loff_t final_size = offset + count;
  2953. if (final_size > inode->i_size) {
  2954. /* Credits for sb + inode write */
  2955. handle = ext4_journal_start(inode, 2);
  2956. if (IS_ERR(handle)) {
  2957. ret = PTR_ERR(handle);
  2958. goto out;
  2959. }
  2960. ret = ext4_orphan_add(handle, inode);
  2961. if (ret) {
  2962. ext4_journal_stop(handle);
  2963. goto out;
  2964. }
  2965. orphan = 1;
  2966. ei->i_disksize = inode->i_size;
  2967. ext4_journal_stop(handle);
  2968. }
  2969. }
  2970. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  2971. offset, nr_segs,
  2972. ext4_get_block, NULL);
  2973. if (orphan) {
  2974. int err;
  2975. /* Credits for sb + inode write */
  2976. handle = ext4_journal_start(inode, 2);
  2977. if (IS_ERR(handle)) {
  2978. /* This is really bad luck. We've written the data
  2979. * but cannot extend i_size. Bail out and pretend
  2980. * the write failed... */
  2981. ret = PTR_ERR(handle);
  2982. goto out;
  2983. }
  2984. if (inode->i_nlink)
  2985. ext4_orphan_del(handle, inode);
  2986. if (ret > 0) {
  2987. loff_t end = offset + ret;
  2988. if (end > inode->i_size) {
  2989. ei->i_disksize = end;
  2990. i_size_write(inode, end);
  2991. /*
  2992. * We're going to return a positive `ret'
  2993. * here due to non-zero-length I/O, so there's
  2994. * no way of reporting error returns from
  2995. * ext4_mark_inode_dirty() to userspace. So
  2996. * ignore it.
  2997. */
  2998. ext4_mark_inode_dirty(handle, inode);
  2999. }
  3000. }
  3001. err = ext4_journal_stop(handle);
  3002. if (ret == 0)
  3003. ret = err;
  3004. }
  3005. out:
  3006. return ret;
  3007. }
  3008. /*
  3009. * Pages can be marked dirty completely asynchronously from ext4's journalling
  3010. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  3011. * much here because ->set_page_dirty is called under VFS locks. The page is
  3012. * not necessarily locked.
  3013. *
  3014. * We cannot just dirty the page and leave attached buffers clean, because the
  3015. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  3016. * or jbddirty because all the journalling code will explode.
  3017. *
  3018. * So what we do is to mark the page "pending dirty" and next time writepage
  3019. * is called, propagate that into the buffers appropriately.
  3020. */
  3021. static int ext4_journalled_set_page_dirty(struct page *page)
  3022. {
  3023. SetPageChecked(page);
  3024. return __set_page_dirty_nobuffers(page);
  3025. }
  3026. static const struct address_space_operations ext4_ordered_aops = {
  3027. .readpage = ext4_readpage,
  3028. .readpages = ext4_readpages,
  3029. .writepage = ext4_writepage,
  3030. .sync_page = block_sync_page,
  3031. .write_begin = ext4_write_begin,
  3032. .write_end = ext4_ordered_write_end,
  3033. .bmap = ext4_bmap,
  3034. .invalidatepage = ext4_invalidatepage,
  3035. .releasepage = ext4_releasepage,
  3036. .direct_IO = ext4_direct_IO,
  3037. .migratepage = buffer_migrate_page,
  3038. .is_partially_uptodate = block_is_partially_uptodate,
  3039. };
  3040. static const struct address_space_operations ext4_writeback_aops = {
  3041. .readpage = ext4_readpage,
  3042. .readpages = ext4_readpages,
  3043. .writepage = ext4_writepage,
  3044. .sync_page = block_sync_page,
  3045. .write_begin = ext4_write_begin,
  3046. .write_end = ext4_writeback_write_end,
  3047. .bmap = ext4_bmap,
  3048. .invalidatepage = ext4_invalidatepage,
  3049. .releasepage = ext4_releasepage,
  3050. .direct_IO = ext4_direct_IO,
  3051. .migratepage = buffer_migrate_page,
  3052. .is_partially_uptodate = block_is_partially_uptodate,
  3053. };
  3054. static const struct address_space_operations ext4_journalled_aops = {
  3055. .readpage = ext4_readpage,
  3056. .readpages = ext4_readpages,
  3057. .writepage = ext4_writepage,
  3058. .sync_page = block_sync_page,
  3059. .write_begin = ext4_write_begin,
  3060. .write_end = ext4_journalled_write_end,
  3061. .set_page_dirty = ext4_journalled_set_page_dirty,
  3062. .bmap = ext4_bmap,
  3063. .invalidatepage = ext4_invalidatepage,
  3064. .releasepage = ext4_releasepage,
  3065. .is_partially_uptodate = block_is_partially_uptodate,
  3066. };
  3067. static const struct address_space_operations ext4_da_aops = {
  3068. .readpage = ext4_readpage,
  3069. .readpages = ext4_readpages,
  3070. .writepage = ext4_writepage,
  3071. .writepages = ext4_da_writepages,
  3072. .sync_page = block_sync_page,
  3073. .write_begin = ext4_da_write_begin,
  3074. .write_end = ext4_da_write_end,
  3075. .bmap = ext4_bmap,
  3076. .invalidatepage = ext4_da_invalidatepage,
  3077. .releasepage = ext4_releasepage,
  3078. .direct_IO = ext4_direct_IO,
  3079. .migratepage = buffer_migrate_page,
  3080. .is_partially_uptodate = block_is_partially_uptodate,
  3081. };
  3082. void ext4_set_aops(struct inode *inode)
  3083. {
  3084. if (ext4_should_order_data(inode) &&
  3085. test_opt(inode->i_sb, DELALLOC))
  3086. inode->i_mapping->a_ops = &ext4_da_aops;
  3087. else if (ext4_should_order_data(inode))
  3088. inode->i_mapping->a_ops = &ext4_ordered_aops;
  3089. else if (ext4_should_writeback_data(inode) &&
  3090. test_opt(inode->i_sb, DELALLOC))
  3091. inode->i_mapping->a_ops = &ext4_da_aops;
  3092. else if (ext4_should_writeback_data(inode))
  3093. inode->i_mapping->a_ops = &ext4_writeback_aops;
  3094. else
  3095. inode->i_mapping->a_ops = &ext4_journalled_aops;
  3096. }
  3097. /*
  3098. * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
  3099. * up to the end of the block which corresponds to `from'.
  3100. * This required during truncate. We need to physically zero the tail end
  3101. * of that block so it doesn't yield old data if the file is later grown.
  3102. */
  3103. int ext4_block_truncate_page(handle_t *handle,
  3104. struct address_space *mapping, loff_t from)
  3105. {
  3106. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  3107. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3108. unsigned blocksize, length, pos;
  3109. ext4_lblk_t iblock;
  3110. struct inode *inode = mapping->host;
  3111. struct buffer_head *bh;
  3112. struct page *page;
  3113. int err = 0;
  3114. page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
  3115. mapping_gfp_mask(mapping) & ~__GFP_FS);
  3116. if (!page)
  3117. return -EINVAL;
  3118. blocksize = inode->i_sb->s_blocksize;
  3119. length = blocksize - (offset & (blocksize - 1));
  3120. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  3121. /*
  3122. * For "nobh" option, we can only work if we don't need to
  3123. * read-in the page - otherwise we create buffers to do the IO.
  3124. */
  3125. if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
  3126. ext4_should_writeback_data(inode) && PageUptodate(page)) {
  3127. zero_user(page, offset, length);
  3128. set_page_dirty(page);
  3129. goto unlock;
  3130. }
  3131. if (!page_has_buffers(page))
  3132. create_empty_buffers(page, blocksize, 0);
  3133. /* Find the buffer that contains "offset" */
  3134. bh = page_buffers(page);
  3135. pos = blocksize;
  3136. while (offset >= pos) {
  3137. bh = bh->b_this_page;
  3138. iblock++;
  3139. pos += blocksize;
  3140. }
  3141. err = 0;
  3142. if (buffer_freed(bh)) {
  3143. BUFFER_TRACE(bh, "freed: skip");
  3144. goto unlock;
  3145. }
  3146. if (!buffer_mapped(bh)) {
  3147. BUFFER_TRACE(bh, "unmapped");
  3148. ext4_get_block(inode, iblock, bh, 0);
  3149. /* unmapped? It's a hole - nothing to do */
  3150. if (!buffer_mapped(bh)) {
  3151. BUFFER_TRACE(bh, "still unmapped");
  3152. goto unlock;
  3153. }
  3154. }
  3155. /* Ok, it's mapped. Make sure it's up-to-date */
  3156. if (PageUptodate(page))
  3157. set_buffer_uptodate(bh);
  3158. if (!buffer_uptodate(bh)) {
  3159. err = -EIO;
  3160. ll_rw_block(READ, 1, &bh);
  3161. wait_on_buffer(bh);
  3162. /* Uhhuh. Read error. Complain and punt. */
  3163. if (!buffer_uptodate(bh))
  3164. goto unlock;
  3165. }
  3166. if (ext4_should_journal_data(inode)) {
  3167. BUFFER_TRACE(bh, "get write access");
  3168. err = ext4_journal_get_write_access(handle, bh);
  3169. if (err)
  3170. goto unlock;
  3171. }
  3172. zero_user(page, offset, length);
  3173. BUFFER_TRACE(bh, "zeroed end of block");
  3174. err = 0;
  3175. if (ext4_should_journal_data(inode)) {
  3176. err = ext4_handle_dirty_metadata(handle, inode, bh);
  3177. } else {
  3178. if (ext4_should_order_data(inode))
  3179. err = ext4_jbd2_file_inode(handle, inode);
  3180. mark_buffer_dirty(bh);
  3181. }
  3182. unlock:
  3183. unlock_page(page);
  3184. page_cache_release(page);
  3185. return err;
  3186. }
  3187. /*
  3188. * Probably it should be a library function... search for first non-zero word
  3189. * or memcmp with zero_page, whatever is better for particular architecture.
  3190. * Linus?
  3191. */
  3192. static inline int all_zeroes(__le32 *p, __le32 *q)
  3193. {
  3194. while (p < q)
  3195. if (*p++)
  3196. return 0;
  3197. return 1;
  3198. }
  3199. /**
  3200. * ext4_find_shared - find the indirect blocks for partial truncation.
  3201. * @inode: inode in question
  3202. * @depth: depth of the affected branch
  3203. * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
  3204. * @chain: place to store the pointers to partial indirect blocks
  3205. * @top: place to the (detached) top of branch
  3206. *
  3207. * This is a helper function used by ext4_truncate().
  3208. *
  3209. * When we do truncate() we may have to clean the ends of several
  3210. * indirect blocks but leave the blocks themselves alive. Block is
  3211. * partially truncated if some data below the new i_size is refered
  3212. * from it (and it is on the path to the first completely truncated
  3213. * data block, indeed). We have to free the top of that path along
  3214. * with everything to the right of the path. Since no allocation
  3215. * past the truncation point is possible until ext4_truncate()
  3216. * finishes, we may safely do the latter, but top of branch may
  3217. * require special attention - pageout below the truncation point
  3218. * might try to populate it.
  3219. *
  3220. * We atomically detach the top of branch from the tree, store the
  3221. * block number of its root in *@top, pointers to buffer_heads of
  3222. * partially truncated blocks - in @chain[].bh and pointers to
  3223. * their last elements that should not be removed - in
  3224. * @chain[].p. Return value is the pointer to last filled element
  3225. * of @chain.
  3226. *
  3227. * The work left to caller to do the actual freeing of subtrees:
  3228. * a) free the subtree starting from *@top
  3229. * b) free the subtrees whose roots are stored in
  3230. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  3231. * c) free the subtrees growing from the inode past the @chain[0].
  3232. * (no partially truncated stuff there). */
  3233. static Indirect *ext4_find_shared(struct inode *inode, int depth,
  3234. ext4_lblk_t offsets[4], Indirect chain[4],
  3235. __le32 *top)
  3236. {
  3237. Indirect *partial, *p;
  3238. int k, err;
  3239. *top = 0;
  3240. /* Make k index the deepest non-null offest + 1 */
  3241. for (k = depth; k > 1 && !offsets[k-1]; k--)
  3242. ;
  3243. partial = ext4_get_branch(inode, k, offsets, chain, &err);
  3244. /* Writer: pointers */
  3245. if (!partial)
  3246. partial = chain + k-1;
  3247. /*
  3248. * If the branch acquired continuation since we've looked at it -
  3249. * fine, it should all survive and (new) top doesn't belong to us.
  3250. */
  3251. if (!partial->key && *partial->p)
  3252. /* Writer: end */
  3253. goto no_top;
  3254. for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
  3255. ;
  3256. /*
  3257. * OK, we've found the last block that must survive. The rest of our
  3258. * branch should be detached before unlocking. However, if that rest
  3259. * of branch is all ours and does not grow immediately from the inode
  3260. * it's easier to cheat and just decrement partial->p.
  3261. */
  3262. if (p == chain + k - 1 && p > chain) {
  3263. p->p--;
  3264. } else {
  3265. *top = *p->p;
  3266. /* Nope, don't do this in ext4. Must leave the tree intact */
  3267. #if 0
  3268. *p->p = 0;
  3269. #endif
  3270. }
  3271. /* Writer: end */
  3272. while (partial > p) {
  3273. brelse(partial->bh);
  3274. partial--;
  3275. }
  3276. no_top:
  3277. return partial;
  3278. }
  3279. /*
  3280. * Zero a number of block pointers in either an inode or an indirect block.
  3281. * If we restart the transaction we must again get write access to the
  3282. * indirect block for further modification.
  3283. *
  3284. * We release `count' blocks on disk, but (last - first) may be greater
  3285. * than `count' because there can be holes in there.
  3286. */
  3287. static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
  3288. struct buffer_head *bh,
  3289. ext4_fsblk_t block_to_free,
  3290. unsigned long count, __le32 *first,
  3291. __le32 *last)
  3292. {
  3293. __le32 *p;
  3294. if (try_to_extend_transaction(handle, inode)) {
  3295. if (bh) {
  3296. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  3297. ext4_handle_dirty_metadata(handle, inode, bh);
  3298. }
  3299. ext4_mark_inode_dirty(handle, inode);
  3300. ext4_journal_test_restart(handle, inode);
  3301. if (bh) {
  3302. BUFFER_TRACE(bh, "retaking write access");
  3303. ext4_journal_get_write_access(handle, bh);
  3304. }
  3305. }
  3306. /*
  3307. * Any buffers which are on the journal will be in memory. We
  3308. * find them on the hash table so jbd2_journal_revoke() will
  3309. * run jbd2_journal_forget() on them. We've already detached
  3310. * each block from the file, so bforget() in
  3311. * jbd2_journal_forget() should be safe.
  3312. *
  3313. * AKPM: turn on bforget in jbd2_journal_forget()!!!
  3314. */
  3315. for (p = first; p < last; p++) {
  3316. u32 nr = le32_to_cpu(*p);
  3317. if (nr) {
  3318. struct buffer_head *tbh;
  3319. *p = 0;
  3320. tbh = sb_find_get_block(inode->i_sb, nr);
  3321. ext4_forget(handle, 0, inode, tbh, nr);
  3322. }
  3323. }
  3324. ext4_free_blocks(handle, inode, block_to_free, count, 0);
  3325. }
  3326. /**
  3327. * ext4_free_data - free a list of data blocks
  3328. * @handle: handle for this transaction
  3329. * @inode: inode we are dealing with
  3330. * @this_bh: indirect buffer_head which contains *@first and *@last
  3331. * @first: array of block numbers
  3332. * @last: points immediately past the end of array
  3333. *
  3334. * We are freeing all blocks refered from that array (numbers are stored as
  3335. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  3336. *
  3337. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  3338. * blocks are contiguous then releasing them at one time will only affect one
  3339. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  3340. * actually use a lot of journal space.
  3341. *
  3342. * @this_bh will be %NULL if @first and @last point into the inode's direct
  3343. * block pointers.
  3344. */
  3345. static void ext4_free_data(handle_t *handle, struct inode *inode,
  3346. struct buffer_head *this_bh,
  3347. __le32 *first, __le32 *last)
  3348. {
  3349. ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
  3350. unsigned long count = 0; /* Number of blocks in the run */
  3351. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  3352. corresponding to
  3353. block_to_free */
  3354. ext4_fsblk_t nr; /* Current block # */
  3355. __le32 *p; /* Pointer into inode/ind
  3356. for current block */
  3357. int err;
  3358. if (this_bh) { /* For indirect block */
  3359. BUFFER_TRACE(this_bh, "get_write_access");
  3360. err = ext4_journal_get_write_access(handle, this_bh);
  3361. /* Important: if we can't update the indirect pointers
  3362. * to the blocks, we can't free them. */
  3363. if (err)
  3364. return;
  3365. }
  3366. for (p = first; p < last; p++) {
  3367. nr = le32_to_cpu(*p);
  3368. if (nr) {
  3369. /* accumulate blocks to free if they're contiguous */
  3370. if (count == 0) {
  3371. block_to_free = nr;
  3372. block_to_free_p = p;
  3373. count = 1;
  3374. } else if (nr == block_to_free + count) {
  3375. count++;
  3376. } else {
  3377. ext4_clear_blocks(handle, inode, this_bh,
  3378. block_to_free,
  3379. count, block_to_free_p, p);
  3380. block_to_free = nr;
  3381. block_to_free_p = p;
  3382. count = 1;
  3383. }
  3384. }
  3385. }
  3386. if (count > 0)
  3387. ext4_clear_blocks(handle, inode, this_bh, block_to_free,
  3388. count, block_to_free_p, p);
  3389. if (this_bh) {
  3390. BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
  3391. /*
  3392. * The buffer head should have an attached journal head at this
  3393. * point. However, if the data is corrupted and an indirect
  3394. * block pointed to itself, it would have been detached when
  3395. * the block was cleared. Check for this instead of OOPSing.
  3396. */
  3397. if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
  3398. ext4_handle_dirty_metadata(handle, inode, this_bh);
  3399. else
  3400. ext4_error(inode->i_sb, __func__,
  3401. "circular indirect block detected, "
  3402. "inode=%lu, block=%llu",
  3403. inode->i_ino,
  3404. (unsigned long long) this_bh->b_blocknr);
  3405. }
  3406. }
  3407. /**
  3408. * ext4_free_branches - free an array of branches
  3409. * @handle: JBD handle for this transaction
  3410. * @inode: inode we are dealing with
  3411. * @parent_bh: the buffer_head which contains *@first and *@last
  3412. * @first: array of block numbers
  3413. * @last: pointer immediately past the end of array
  3414. * @depth: depth of the branches to free
  3415. *
  3416. * We are freeing all blocks refered from these branches (numbers are
  3417. * stored as little-endian 32-bit) and updating @inode->i_blocks
  3418. * appropriately.
  3419. */
  3420. static void ext4_free_branches(handle_t *handle, struct inode *inode,
  3421. struct buffer_head *parent_bh,
  3422. __le32 *first, __le32 *last, int depth)
  3423. {
  3424. ext4_fsblk_t nr;
  3425. __le32 *p;
  3426. if (ext4_handle_is_aborted(handle))
  3427. return;
  3428. if (depth--) {
  3429. struct buffer_head *bh;
  3430. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  3431. p = last;
  3432. while (--p >= first) {
  3433. nr = le32_to_cpu(*p);
  3434. if (!nr)
  3435. continue; /* A hole */
  3436. /* Go read the buffer for the next level down */
  3437. bh = sb_bread(inode->i_sb, nr);
  3438. /*
  3439. * A read failure? Report error and clear slot
  3440. * (should be rare).
  3441. */
  3442. if (!bh) {
  3443. ext4_error(inode->i_sb, "ext4_free_branches",
  3444. "Read failure, inode=%lu, block=%llu",
  3445. inode->i_ino, nr);
  3446. continue;
  3447. }
  3448. /* This zaps the entire block. Bottom up. */
  3449. BUFFER_TRACE(bh, "free child branches");
  3450. ext4_free_branches(handle, inode, bh,
  3451. (__le32 *) bh->b_data,
  3452. (__le32 *) bh->b_data + addr_per_block,
  3453. depth);
  3454. /*
  3455. * We've probably journalled the indirect block several
  3456. * times during the truncate. But it's no longer
  3457. * needed and we now drop it from the transaction via
  3458. * jbd2_journal_revoke().
  3459. *
  3460. * That's easy if it's exclusively part of this
  3461. * transaction. But if it's part of the committing
  3462. * transaction then jbd2_journal_forget() will simply
  3463. * brelse() it. That means that if the underlying
  3464. * block is reallocated in ext4_get_block(),
  3465. * unmap_underlying_metadata() will find this block
  3466. * and will try to get rid of it. damn, damn.
  3467. *
  3468. * If this block has already been committed to the
  3469. * journal, a revoke record will be written. And
  3470. * revoke records must be emitted *before* clearing
  3471. * this block's bit in the bitmaps.
  3472. */
  3473. ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
  3474. /*
  3475. * Everything below this this pointer has been
  3476. * released. Now let this top-of-subtree go.
  3477. *
  3478. * We want the freeing of this indirect block to be
  3479. * atomic in the journal with the updating of the
  3480. * bitmap block which owns it. So make some room in
  3481. * the journal.
  3482. *
  3483. * We zero the parent pointer *after* freeing its
  3484. * pointee in the bitmaps, so if extend_transaction()
  3485. * for some reason fails to put the bitmap changes and
  3486. * the release into the same transaction, recovery
  3487. * will merely complain about releasing a free block,
  3488. * rather than leaking blocks.
  3489. */
  3490. if (ext4_handle_is_aborted(handle))
  3491. return;
  3492. if (try_to_extend_transaction(handle, inode)) {
  3493. ext4_mark_inode_dirty(handle, inode);
  3494. ext4_journal_test_restart(handle, inode);
  3495. }
  3496. ext4_free_blocks(handle, inode, nr, 1, 1);
  3497. if (parent_bh) {
  3498. /*
  3499. * The block which we have just freed is
  3500. * pointed to by an indirect block: journal it
  3501. */
  3502. BUFFER_TRACE(parent_bh, "get_write_access");
  3503. if (!ext4_journal_get_write_access(handle,
  3504. parent_bh)){
  3505. *p = 0;
  3506. BUFFER_TRACE(parent_bh,
  3507. "call ext4_handle_dirty_metadata");
  3508. ext4_handle_dirty_metadata(handle,
  3509. inode,
  3510. parent_bh);
  3511. }
  3512. }
  3513. }
  3514. } else {
  3515. /* We have reached the bottom of the tree. */
  3516. BUFFER_TRACE(parent_bh, "free data blocks");
  3517. ext4_free_data(handle, inode, parent_bh, first, last);
  3518. }
  3519. }
  3520. int ext4_can_truncate(struct inode *inode)
  3521. {
  3522. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3523. return 0;
  3524. if (S_ISREG(inode->i_mode))
  3525. return 1;
  3526. if (S_ISDIR(inode->i_mode))
  3527. return 1;
  3528. if (S_ISLNK(inode->i_mode))
  3529. return !ext4_inode_is_fast_symlink(inode);
  3530. return 0;
  3531. }
  3532. /*
  3533. * ext4_truncate()
  3534. *
  3535. * We block out ext4_get_block() block instantiations across the entire
  3536. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3537. * simultaneously on behalf of the same inode.
  3538. *
  3539. * As we work through the truncate and commmit bits of it to the journal there
  3540. * is one core, guiding principle: the file's tree must always be consistent on
  3541. * disk. We must be able to restart the truncate after a crash.
  3542. *
  3543. * The file's tree may be transiently inconsistent in memory (although it
  3544. * probably isn't), but whenever we close off and commit a journal transaction,
  3545. * the contents of (the filesystem + the journal) must be consistent and
  3546. * restartable. It's pretty simple, really: bottom up, right to left (although
  3547. * left-to-right works OK too).
  3548. *
  3549. * Note that at recovery time, journal replay occurs *before* the restart of
  3550. * truncate against the orphan inode list.
  3551. *
  3552. * The committed inode has the new, desired i_size (which is the same as
  3553. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3554. * that this inode's truncate did not complete and it will again call
  3555. * ext4_truncate() to have another go. So there will be instantiated blocks
  3556. * to the right of the truncation point in a crashed ext4 filesystem. But
  3557. * that's fine - as long as they are linked from the inode, the post-crash
  3558. * ext4_truncate() run will find them and release them.
  3559. */
  3560. void ext4_truncate(struct inode *inode)
  3561. {
  3562. handle_t *handle;
  3563. struct ext4_inode_info *ei = EXT4_I(inode);
  3564. __le32 *i_data = ei->i_data;
  3565. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  3566. struct address_space *mapping = inode->i_mapping;
  3567. ext4_lblk_t offsets[4];
  3568. Indirect chain[4];
  3569. Indirect *partial;
  3570. __le32 nr = 0;
  3571. int n;
  3572. ext4_lblk_t last_block;
  3573. unsigned blocksize = inode->i_sb->s_blocksize;
  3574. if (!ext4_can_truncate(inode))
  3575. return;
  3576. if (ei->i_disksize && inode->i_size == 0 &&
  3577. !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3578. ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
  3579. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  3580. ext4_ext_truncate(inode);
  3581. return;
  3582. }
  3583. handle = start_transaction(inode);
  3584. if (IS_ERR(handle))
  3585. return; /* AKPM: return what? */
  3586. last_block = (inode->i_size + blocksize-1)
  3587. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  3588. if (inode->i_size & (blocksize - 1))
  3589. if (ext4_block_truncate_page(handle, mapping, inode->i_size))
  3590. goto out_stop;
  3591. n = ext4_block_to_path(inode, last_block, offsets, NULL);
  3592. if (n == 0)
  3593. goto out_stop; /* error */
  3594. /*
  3595. * OK. This truncate is going to happen. We add the inode to the
  3596. * orphan list, so that if this truncate spans multiple transactions,
  3597. * and we crash, we will resume the truncate when the filesystem
  3598. * recovers. It also marks the inode dirty, to catch the new size.
  3599. *
  3600. * Implication: the file must always be in a sane, consistent
  3601. * truncatable state while each transaction commits.
  3602. */
  3603. if (ext4_orphan_add(handle, inode))
  3604. goto out_stop;
  3605. /*
  3606. * From here we block out all ext4_get_block() callers who want to
  3607. * modify the block allocation tree.
  3608. */
  3609. down_write(&ei->i_data_sem);
  3610. ext4_discard_preallocations(inode);
  3611. /*
  3612. * The orphan list entry will now protect us from any crash which
  3613. * occurs before the truncate completes, so it is now safe to propagate
  3614. * the new, shorter inode size (held for now in i_size) into the
  3615. * on-disk inode. We do this via i_disksize, which is the value which
  3616. * ext4 *really* writes onto the disk inode.
  3617. */
  3618. ei->i_disksize = inode->i_size;
  3619. if (n == 1) { /* direct blocks */
  3620. ext4_free_data(handle, inode, NULL, i_data+offsets[0],
  3621. i_data + EXT4_NDIR_BLOCKS);
  3622. goto do_indirects;
  3623. }
  3624. partial = ext4_find_shared(inode, n, offsets, chain, &nr);
  3625. /* Kill the top of shared branch (not detached) */
  3626. if (nr) {
  3627. if (partial == chain) {
  3628. /* Shared branch grows from the inode */
  3629. ext4_free_branches(handle, inode, NULL,
  3630. &nr, &nr+1, (chain+n-1) - partial);
  3631. *partial->p = 0;
  3632. /*
  3633. * We mark the inode dirty prior to restart,
  3634. * and prior to stop. No need for it here.
  3635. */
  3636. } else {
  3637. /* Shared branch grows from an indirect block */
  3638. BUFFER_TRACE(partial->bh, "get_write_access");
  3639. ext4_free_branches(handle, inode, partial->bh,
  3640. partial->p,
  3641. partial->p+1, (chain+n-1) - partial);
  3642. }
  3643. }
  3644. /* Clear the ends of indirect blocks on the shared branch */
  3645. while (partial > chain) {
  3646. ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
  3647. (__le32*)partial->bh->b_data+addr_per_block,
  3648. (chain+n-1) - partial);
  3649. BUFFER_TRACE(partial->bh, "call brelse");
  3650. brelse(partial->bh);
  3651. partial--;
  3652. }
  3653. do_indirects:
  3654. /* Kill the remaining (whole) subtrees */
  3655. switch (offsets[0]) {
  3656. default:
  3657. nr = i_data[EXT4_IND_BLOCK];
  3658. if (nr) {
  3659. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  3660. i_data[EXT4_IND_BLOCK] = 0;
  3661. }
  3662. case EXT4_IND_BLOCK:
  3663. nr = i_data[EXT4_DIND_BLOCK];
  3664. if (nr) {
  3665. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  3666. i_data[EXT4_DIND_BLOCK] = 0;
  3667. }
  3668. case EXT4_DIND_BLOCK:
  3669. nr = i_data[EXT4_TIND_BLOCK];
  3670. if (nr) {
  3671. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  3672. i_data[EXT4_TIND_BLOCK] = 0;
  3673. }
  3674. case EXT4_TIND_BLOCK:
  3675. ;
  3676. }
  3677. up_write(&ei->i_data_sem);
  3678. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3679. ext4_mark_inode_dirty(handle, inode);
  3680. /*
  3681. * In a multi-transaction truncate, we only make the final transaction
  3682. * synchronous
  3683. */
  3684. if (IS_SYNC(inode))
  3685. ext4_handle_sync(handle);
  3686. out_stop:
  3687. /*
  3688. * If this was a simple ftruncate(), and the file will remain alive
  3689. * then we need to clear up the orphan record which we created above.
  3690. * However, if this was a real unlink then we were called by
  3691. * ext4_delete_inode(), and we allow that function to clean up the
  3692. * orphan info for us.
  3693. */
  3694. if (inode->i_nlink)
  3695. ext4_orphan_del(handle, inode);
  3696. ext4_journal_stop(handle);
  3697. }
  3698. /*
  3699. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3700. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3701. * data in memory that is needed to recreate the on-disk version of this
  3702. * inode.
  3703. */
  3704. static int __ext4_get_inode_loc(struct inode *inode,
  3705. struct ext4_iloc *iloc, int in_mem)
  3706. {
  3707. struct ext4_group_desc *gdp;
  3708. struct buffer_head *bh;
  3709. struct super_block *sb = inode->i_sb;
  3710. ext4_fsblk_t block;
  3711. int inodes_per_block, inode_offset;
  3712. iloc->bh = NULL;
  3713. if (!ext4_valid_inum(sb, inode->i_ino))
  3714. return -EIO;
  3715. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3716. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  3717. if (!gdp)
  3718. return -EIO;
  3719. /*
  3720. * Figure out the offset within the block group inode table
  3721. */
  3722. inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
  3723. inode_offset = ((inode->i_ino - 1) %
  3724. EXT4_INODES_PER_GROUP(sb));
  3725. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  3726. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  3727. bh = sb_getblk(sb, block);
  3728. if (!bh) {
  3729. ext4_error(sb, "ext4_get_inode_loc", "unable to read "
  3730. "inode block - inode=%lu, block=%llu",
  3731. inode->i_ino, block);
  3732. return -EIO;
  3733. }
  3734. if (!buffer_uptodate(bh)) {
  3735. lock_buffer(bh);
  3736. /*
  3737. * If the buffer has the write error flag, we have failed
  3738. * to write out another inode in the same block. In this
  3739. * case, we don't have to read the block because we may
  3740. * read the old inode data successfully.
  3741. */
  3742. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3743. set_buffer_uptodate(bh);
  3744. if (buffer_uptodate(bh)) {
  3745. /* someone brought it uptodate while we waited */
  3746. unlock_buffer(bh);
  3747. goto has_buffer;
  3748. }
  3749. /*
  3750. * If we have all information of the inode in memory and this
  3751. * is the only valid inode in the block, we need not read the
  3752. * block.
  3753. */
  3754. if (in_mem) {
  3755. struct buffer_head *bitmap_bh;
  3756. int i, start;
  3757. start = inode_offset & ~(inodes_per_block - 1);
  3758. /* Is the inode bitmap in cache? */
  3759. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  3760. if (!bitmap_bh)
  3761. goto make_io;
  3762. /*
  3763. * If the inode bitmap isn't in cache then the
  3764. * optimisation may end up performing two reads instead
  3765. * of one, so skip it.
  3766. */
  3767. if (!buffer_uptodate(bitmap_bh)) {
  3768. brelse(bitmap_bh);
  3769. goto make_io;
  3770. }
  3771. for (i = start; i < start + inodes_per_block; i++) {
  3772. if (i == inode_offset)
  3773. continue;
  3774. if (ext4_test_bit(i, bitmap_bh->b_data))
  3775. break;
  3776. }
  3777. brelse(bitmap_bh);
  3778. if (i == start + inodes_per_block) {
  3779. /* all other inodes are free, so skip I/O */
  3780. memset(bh->b_data, 0, bh->b_size);
  3781. set_buffer_uptodate(bh);
  3782. unlock_buffer(bh);
  3783. goto has_buffer;
  3784. }
  3785. }
  3786. make_io:
  3787. /*
  3788. * If we need to do any I/O, try to pre-readahead extra
  3789. * blocks from the inode table.
  3790. */
  3791. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  3792. ext4_fsblk_t b, end, table;
  3793. unsigned num;
  3794. table = ext4_inode_table(sb, gdp);
  3795. /* s_inode_readahead_blks is always a power of 2 */
  3796. b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
  3797. if (table > b)
  3798. b = table;
  3799. end = b + EXT4_SB(sb)->s_inode_readahead_blks;
  3800. num = EXT4_INODES_PER_GROUP(sb);
  3801. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3802. EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
  3803. num -= ext4_itable_unused_count(sb, gdp);
  3804. table += num / inodes_per_block;
  3805. if (end > table)
  3806. end = table;
  3807. while (b <= end)
  3808. sb_breadahead(sb, b++);
  3809. }
  3810. /*
  3811. * There are other valid inodes in the buffer, this inode
  3812. * has in-inode xattrs, or we don't have this inode in memory.
  3813. * Read the block from disk.
  3814. */
  3815. get_bh(bh);
  3816. bh->b_end_io = end_buffer_read_sync;
  3817. submit_bh(READ_META, bh);
  3818. wait_on_buffer(bh);
  3819. if (!buffer_uptodate(bh)) {
  3820. ext4_error(sb, __func__,
  3821. "unable to read inode block - inode=%lu, "
  3822. "block=%llu", inode->i_ino, block);
  3823. brelse(bh);
  3824. return -EIO;
  3825. }
  3826. }
  3827. has_buffer:
  3828. iloc->bh = bh;
  3829. return 0;
  3830. }
  3831. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3832. {
  3833. /* We have all inode data except xattrs in memory here. */
  3834. return __ext4_get_inode_loc(inode, iloc,
  3835. !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
  3836. }
  3837. void ext4_set_inode_flags(struct inode *inode)
  3838. {
  3839. unsigned int flags = EXT4_I(inode)->i_flags;
  3840. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  3841. if (flags & EXT4_SYNC_FL)
  3842. inode->i_flags |= S_SYNC;
  3843. if (flags & EXT4_APPEND_FL)
  3844. inode->i_flags |= S_APPEND;
  3845. if (flags & EXT4_IMMUTABLE_FL)
  3846. inode->i_flags |= S_IMMUTABLE;
  3847. if (flags & EXT4_NOATIME_FL)
  3848. inode->i_flags |= S_NOATIME;
  3849. if (flags & EXT4_DIRSYNC_FL)
  3850. inode->i_flags |= S_DIRSYNC;
  3851. }
  3852. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3853. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3854. {
  3855. unsigned int flags = ei->vfs_inode.i_flags;
  3856. ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3857. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
  3858. if (flags & S_SYNC)
  3859. ei->i_flags |= EXT4_SYNC_FL;
  3860. if (flags & S_APPEND)
  3861. ei->i_flags |= EXT4_APPEND_FL;
  3862. if (flags & S_IMMUTABLE)
  3863. ei->i_flags |= EXT4_IMMUTABLE_FL;
  3864. if (flags & S_NOATIME)
  3865. ei->i_flags |= EXT4_NOATIME_FL;
  3866. if (flags & S_DIRSYNC)
  3867. ei->i_flags |= EXT4_DIRSYNC_FL;
  3868. }
  3869. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3870. struct ext4_inode_info *ei)
  3871. {
  3872. blkcnt_t i_blocks ;
  3873. struct inode *inode = &(ei->vfs_inode);
  3874. struct super_block *sb = inode->i_sb;
  3875. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3876. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  3877. /* we are using combined 48 bit field */
  3878. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3879. le32_to_cpu(raw_inode->i_blocks_lo);
  3880. if (ei->i_flags & EXT4_HUGE_FILE_FL) {
  3881. /* i_blocks represent file system block size */
  3882. return i_blocks << (inode->i_blkbits - 9);
  3883. } else {
  3884. return i_blocks;
  3885. }
  3886. } else {
  3887. return le32_to_cpu(raw_inode->i_blocks_lo);
  3888. }
  3889. }
  3890. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3891. {
  3892. struct ext4_iloc iloc;
  3893. struct ext4_inode *raw_inode;
  3894. struct ext4_inode_info *ei;
  3895. struct buffer_head *bh;
  3896. struct inode *inode;
  3897. long ret;
  3898. int block;
  3899. inode = iget_locked(sb, ino);
  3900. if (!inode)
  3901. return ERR_PTR(-ENOMEM);
  3902. if (!(inode->i_state & I_NEW))
  3903. return inode;
  3904. ei = EXT4_I(inode);
  3905. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3906. if (ret < 0)
  3907. goto bad_inode;
  3908. bh = iloc.bh;
  3909. raw_inode = ext4_raw_inode(&iloc);
  3910. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3911. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3912. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3913. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3914. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3915. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3916. }
  3917. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  3918. ei->i_state = 0;
  3919. ei->i_dir_start_lookup = 0;
  3920. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3921. /* We now have enough fields to check if the inode was active or not.
  3922. * This is needed because nfsd might try to access dead inodes
  3923. * the test is that same one that e2fsck uses
  3924. * NeilBrown 1999oct15
  3925. */
  3926. if (inode->i_nlink == 0) {
  3927. if (inode->i_mode == 0 ||
  3928. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  3929. /* this inode is deleted */
  3930. brelse(bh);
  3931. ret = -ESTALE;
  3932. goto bad_inode;
  3933. }
  3934. /* The only unlinked inodes we let through here have
  3935. * valid i_mode and are being read by the orphan
  3936. * recovery code: that's fine, we're about to complete
  3937. * the process of deleting those. */
  3938. }
  3939. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  3940. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  3941. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  3942. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
  3943. ei->i_file_acl |=
  3944. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  3945. inode->i_size = ext4_isize(raw_inode);
  3946. ei->i_disksize = inode->i_size;
  3947. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  3948. ei->i_block_group = iloc.block_group;
  3949. ei->i_last_alloc_group = ~0;
  3950. /*
  3951. * NOTE! The in-memory inode i_data array is in little-endian order
  3952. * even on big-endian machines: we do NOT byteswap the block numbers!
  3953. */
  3954. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3955. ei->i_data[block] = raw_inode->i_block[block];
  3956. INIT_LIST_HEAD(&ei->i_orphan);
  3957. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3958. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3959. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3960. EXT4_INODE_SIZE(inode->i_sb)) {
  3961. brelse(bh);
  3962. ret = -EIO;
  3963. goto bad_inode;
  3964. }
  3965. if (ei->i_extra_isize == 0) {
  3966. /* The extra space is currently unused. Use it. */
  3967. ei->i_extra_isize = sizeof(struct ext4_inode) -
  3968. EXT4_GOOD_OLD_INODE_SIZE;
  3969. } else {
  3970. __le32 *magic = (void *)raw_inode +
  3971. EXT4_GOOD_OLD_INODE_SIZE +
  3972. ei->i_extra_isize;
  3973. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
  3974. ei->i_state |= EXT4_STATE_XATTR;
  3975. }
  3976. } else
  3977. ei->i_extra_isize = 0;
  3978. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  3979. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  3980. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  3981. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  3982. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  3983. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3984. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3985. inode->i_version |=
  3986. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  3987. }
  3988. ret = 0;
  3989. if (ei->i_file_acl &&
  3990. ((ei->i_file_acl <
  3991. (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
  3992. EXT4_SB(sb)->s_gdb_count)) ||
  3993. (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
  3994. ext4_error(sb, __func__,
  3995. "bad extended attribute block %llu in inode #%lu",
  3996. ei->i_file_acl, inode->i_ino);
  3997. ret = -EIO;
  3998. goto bad_inode;
  3999. } else if (ei->i_flags & EXT4_EXTENTS_FL) {
  4000. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4001. (S_ISLNK(inode->i_mode) &&
  4002. !ext4_inode_is_fast_symlink(inode)))
  4003. /* Validate extent which is part of inode */
  4004. ret = ext4_ext_check_inode(inode);
  4005. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4006. (S_ISLNK(inode->i_mode) &&
  4007. !ext4_inode_is_fast_symlink(inode))) {
  4008. /* Validate block references which are part of inode */
  4009. ret = ext4_check_inode_blockref(inode);
  4010. }
  4011. if (ret) {
  4012. brelse(bh);
  4013. goto bad_inode;
  4014. }
  4015. if (S_ISREG(inode->i_mode)) {
  4016. inode->i_op = &ext4_file_inode_operations;
  4017. inode->i_fop = &ext4_file_operations;
  4018. ext4_set_aops(inode);
  4019. } else if (S_ISDIR(inode->i_mode)) {
  4020. inode->i_op = &ext4_dir_inode_operations;
  4021. inode->i_fop = &ext4_dir_operations;
  4022. } else if (S_ISLNK(inode->i_mode)) {
  4023. if (ext4_inode_is_fast_symlink(inode)) {
  4024. inode->i_op = &ext4_fast_symlink_inode_operations;
  4025. nd_terminate_link(ei->i_data, inode->i_size,
  4026. sizeof(ei->i_data) - 1);
  4027. } else {
  4028. inode->i_op = &ext4_symlink_inode_operations;
  4029. ext4_set_aops(inode);
  4030. }
  4031. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  4032. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  4033. inode->i_op = &ext4_special_inode_operations;
  4034. if (raw_inode->i_block[0])
  4035. init_special_inode(inode, inode->i_mode,
  4036. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  4037. else
  4038. init_special_inode(inode, inode->i_mode,
  4039. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  4040. } else {
  4041. brelse(bh);
  4042. ret = -EIO;
  4043. ext4_error(inode->i_sb, __func__,
  4044. "bogus i_mode (%o) for inode=%lu",
  4045. inode->i_mode, inode->i_ino);
  4046. goto bad_inode;
  4047. }
  4048. brelse(iloc.bh);
  4049. ext4_set_inode_flags(inode);
  4050. unlock_new_inode(inode);
  4051. return inode;
  4052. bad_inode:
  4053. iget_failed(inode);
  4054. return ERR_PTR(ret);
  4055. }
  4056. static int ext4_inode_blocks_set(handle_t *handle,
  4057. struct ext4_inode *raw_inode,
  4058. struct ext4_inode_info *ei)
  4059. {
  4060. struct inode *inode = &(ei->vfs_inode);
  4061. u64 i_blocks = inode->i_blocks;
  4062. struct super_block *sb = inode->i_sb;
  4063. if (i_blocks <= ~0U) {
  4064. /*
  4065. * i_blocks can be represnted in a 32 bit variable
  4066. * as multiple of 512 bytes
  4067. */
  4068. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4069. raw_inode->i_blocks_high = 0;
  4070. ei->i_flags &= ~EXT4_HUGE_FILE_FL;
  4071. return 0;
  4072. }
  4073. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
  4074. return -EFBIG;
  4075. if (i_blocks <= 0xffffffffffffULL) {
  4076. /*
  4077. * i_blocks can be represented in a 48 bit variable
  4078. * as multiple of 512 bytes
  4079. */
  4080. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4081. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4082. ei->i_flags &= ~EXT4_HUGE_FILE_FL;
  4083. } else {
  4084. ei->i_flags |= EXT4_HUGE_FILE_FL;
  4085. /* i_block is stored in file system block size */
  4086. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  4087. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4088. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4089. }
  4090. return 0;
  4091. }
  4092. /*
  4093. * Post the struct inode info into an on-disk inode location in the
  4094. * buffer-cache. This gobbles the caller's reference to the
  4095. * buffer_head in the inode location struct.
  4096. *
  4097. * The caller must have write access to iloc->bh.
  4098. */
  4099. static int ext4_do_update_inode(handle_t *handle,
  4100. struct inode *inode,
  4101. struct ext4_iloc *iloc)
  4102. {
  4103. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  4104. struct ext4_inode_info *ei = EXT4_I(inode);
  4105. struct buffer_head *bh = iloc->bh;
  4106. int err = 0, rc, block;
  4107. /* For fields not not tracking in the in-memory inode,
  4108. * initialise them to zero for new inodes. */
  4109. if (ei->i_state & EXT4_STATE_NEW)
  4110. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  4111. ext4_get_inode_flags(ei);
  4112. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  4113. if (!(test_opt(inode->i_sb, NO_UID32))) {
  4114. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
  4115. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
  4116. /*
  4117. * Fix up interoperability with old kernels. Otherwise, old inodes get
  4118. * re-used with the upper 16 bits of the uid/gid intact
  4119. */
  4120. if (!ei->i_dtime) {
  4121. raw_inode->i_uid_high =
  4122. cpu_to_le16(high_16_bits(inode->i_uid));
  4123. raw_inode->i_gid_high =
  4124. cpu_to_le16(high_16_bits(inode->i_gid));
  4125. } else {
  4126. raw_inode->i_uid_high = 0;
  4127. raw_inode->i_gid_high = 0;
  4128. }
  4129. } else {
  4130. raw_inode->i_uid_low =
  4131. cpu_to_le16(fs_high2lowuid(inode->i_uid));
  4132. raw_inode->i_gid_low =
  4133. cpu_to_le16(fs_high2lowgid(inode->i_gid));
  4134. raw_inode->i_uid_high = 0;
  4135. raw_inode->i_gid_high = 0;
  4136. }
  4137. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  4138. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  4139. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  4140. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  4141. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  4142. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  4143. goto out_brelse;
  4144. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  4145. /* clear the migrate flag in the raw_inode */
  4146. raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
  4147. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  4148. cpu_to_le32(EXT4_OS_HURD))
  4149. raw_inode->i_file_acl_high =
  4150. cpu_to_le16(ei->i_file_acl >> 32);
  4151. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  4152. ext4_isize_set(raw_inode, ei->i_disksize);
  4153. if (ei->i_disksize > 0x7fffffffULL) {
  4154. struct super_block *sb = inode->i_sb;
  4155. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  4156. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  4157. EXT4_SB(sb)->s_es->s_rev_level ==
  4158. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  4159. /* If this is the first large file
  4160. * created, add a flag to the superblock.
  4161. */
  4162. err = ext4_journal_get_write_access(handle,
  4163. EXT4_SB(sb)->s_sbh);
  4164. if (err)
  4165. goto out_brelse;
  4166. ext4_update_dynamic_rev(sb);
  4167. EXT4_SET_RO_COMPAT_FEATURE(sb,
  4168. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  4169. sb->s_dirt = 1;
  4170. ext4_handle_sync(handle);
  4171. err = ext4_handle_dirty_metadata(handle, inode,
  4172. EXT4_SB(sb)->s_sbh);
  4173. }
  4174. }
  4175. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  4176. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  4177. if (old_valid_dev(inode->i_rdev)) {
  4178. raw_inode->i_block[0] =
  4179. cpu_to_le32(old_encode_dev(inode->i_rdev));
  4180. raw_inode->i_block[1] = 0;
  4181. } else {
  4182. raw_inode->i_block[0] = 0;
  4183. raw_inode->i_block[1] =
  4184. cpu_to_le32(new_encode_dev(inode->i_rdev));
  4185. raw_inode->i_block[2] = 0;
  4186. }
  4187. } else
  4188. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4189. raw_inode->i_block[block] = ei->i_data[block];
  4190. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  4191. if (ei->i_extra_isize) {
  4192. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4193. raw_inode->i_version_hi =
  4194. cpu_to_le32(inode->i_version >> 32);
  4195. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  4196. }
  4197. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  4198. rc = ext4_handle_dirty_metadata(handle, inode, bh);
  4199. if (!err)
  4200. err = rc;
  4201. ei->i_state &= ~EXT4_STATE_NEW;
  4202. out_brelse:
  4203. brelse(bh);
  4204. ext4_std_error(inode->i_sb, err);
  4205. return err;
  4206. }
  4207. /*
  4208. * ext4_write_inode()
  4209. *
  4210. * We are called from a few places:
  4211. *
  4212. * - Within generic_file_write() for O_SYNC files.
  4213. * Here, there will be no transaction running. We wait for any running
  4214. * trasnaction to commit.
  4215. *
  4216. * - Within sys_sync(), kupdate and such.
  4217. * We wait on commit, if tol to.
  4218. *
  4219. * - Within prune_icache() (PF_MEMALLOC == true)
  4220. * Here we simply return. We can't afford to block kswapd on the
  4221. * journal commit.
  4222. *
  4223. * In all cases it is actually safe for us to return without doing anything,
  4224. * because the inode has been copied into a raw inode buffer in
  4225. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  4226. * knfsd.
  4227. *
  4228. * Note that we are absolutely dependent upon all inode dirtiers doing the
  4229. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  4230. * which we are interested.
  4231. *
  4232. * It would be a bug for them to not do this. The code:
  4233. *
  4234. * mark_inode_dirty(inode)
  4235. * stuff();
  4236. * inode->i_size = expr;
  4237. *
  4238. * is in error because a kswapd-driven write_inode() could occur while
  4239. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  4240. * will no longer be on the superblock's dirty inode list.
  4241. */
  4242. int ext4_write_inode(struct inode *inode, int wait)
  4243. {
  4244. if (current->flags & PF_MEMALLOC)
  4245. return 0;
  4246. if (ext4_journal_current_handle()) {
  4247. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  4248. dump_stack();
  4249. return -EIO;
  4250. }
  4251. if (!wait)
  4252. return 0;
  4253. return ext4_force_commit(inode->i_sb);
  4254. }
  4255. /*
  4256. * ext4_setattr()
  4257. *
  4258. * Called from notify_change.
  4259. *
  4260. * We want to trap VFS attempts to truncate the file as soon as
  4261. * possible. In particular, we want to make sure that when the VFS
  4262. * shrinks i_size, we put the inode on the orphan list and modify
  4263. * i_disksize immediately, so that during the subsequent flushing of
  4264. * dirty pages and freeing of disk blocks, we can guarantee that any
  4265. * commit will leave the blocks being flushed in an unused state on
  4266. * disk. (On recovery, the inode will get truncated and the blocks will
  4267. * be freed, so we have a strong guarantee that no future commit will
  4268. * leave these blocks visible to the user.)
  4269. *
  4270. * Another thing we have to assure is that if we are in ordered mode
  4271. * and inode is still attached to the committing transaction, we must
  4272. * we start writeout of all the dirty pages which are being truncated.
  4273. * This way we are sure that all the data written in the previous
  4274. * transaction are already on disk (truncate waits for pages under
  4275. * writeback).
  4276. *
  4277. * Called with inode->i_mutex down.
  4278. */
  4279. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  4280. {
  4281. struct inode *inode = dentry->d_inode;
  4282. int error, rc = 0;
  4283. const unsigned int ia_valid = attr->ia_valid;
  4284. error = inode_change_ok(inode, attr);
  4285. if (error)
  4286. return error;
  4287. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  4288. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  4289. handle_t *handle;
  4290. /* (user+group)*(old+new) structure, inode write (sb,
  4291. * inode block, ? - but truncate inode update has it) */
  4292. handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
  4293. EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
  4294. if (IS_ERR(handle)) {
  4295. error = PTR_ERR(handle);
  4296. goto err_out;
  4297. }
  4298. error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
  4299. if (error) {
  4300. ext4_journal_stop(handle);
  4301. return error;
  4302. }
  4303. /* Update corresponding info in inode so that everything is in
  4304. * one transaction */
  4305. if (attr->ia_valid & ATTR_UID)
  4306. inode->i_uid = attr->ia_uid;
  4307. if (attr->ia_valid & ATTR_GID)
  4308. inode->i_gid = attr->ia_gid;
  4309. error = ext4_mark_inode_dirty(handle, inode);
  4310. ext4_journal_stop(handle);
  4311. }
  4312. if (attr->ia_valid & ATTR_SIZE) {
  4313. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
  4314. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4315. if (attr->ia_size > sbi->s_bitmap_maxbytes) {
  4316. error = -EFBIG;
  4317. goto err_out;
  4318. }
  4319. }
  4320. }
  4321. if (S_ISREG(inode->i_mode) &&
  4322. attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
  4323. handle_t *handle;
  4324. handle = ext4_journal_start(inode, 3);
  4325. if (IS_ERR(handle)) {
  4326. error = PTR_ERR(handle);
  4327. goto err_out;
  4328. }
  4329. error = ext4_orphan_add(handle, inode);
  4330. EXT4_I(inode)->i_disksize = attr->ia_size;
  4331. rc = ext4_mark_inode_dirty(handle, inode);
  4332. if (!error)
  4333. error = rc;
  4334. ext4_journal_stop(handle);
  4335. if (ext4_should_order_data(inode)) {
  4336. error = ext4_begin_ordered_truncate(inode,
  4337. attr->ia_size);
  4338. if (error) {
  4339. /* Do as much error cleanup as possible */
  4340. handle = ext4_journal_start(inode, 3);
  4341. if (IS_ERR(handle)) {
  4342. ext4_orphan_del(NULL, inode);
  4343. goto err_out;
  4344. }
  4345. ext4_orphan_del(handle, inode);
  4346. ext4_journal_stop(handle);
  4347. goto err_out;
  4348. }
  4349. }
  4350. }
  4351. rc = inode_setattr(inode, attr);
  4352. /* If inode_setattr's call to ext4_truncate failed to get a
  4353. * transaction handle at all, we need to clean up the in-core
  4354. * orphan list manually. */
  4355. if (inode->i_nlink)
  4356. ext4_orphan_del(NULL, inode);
  4357. if (!rc && (ia_valid & ATTR_MODE))
  4358. rc = ext4_acl_chmod(inode);
  4359. err_out:
  4360. ext4_std_error(inode->i_sb, error);
  4361. if (!error)
  4362. error = rc;
  4363. return error;
  4364. }
  4365. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  4366. struct kstat *stat)
  4367. {
  4368. struct inode *inode;
  4369. unsigned long delalloc_blocks;
  4370. inode = dentry->d_inode;
  4371. generic_fillattr(inode, stat);
  4372. /*
  4373. * We can't update i_blocks if the block allocation is delayed
  4374. * otherwise in the case of system crash before the real block
  4375. * allocation is done, we will have i_blocks inconsistent with
  4376. * on-disk file blocks.
  4377. * We always keep i_blocks updated together with real
  4378. * allocation. But to not confuse with user, stat
  4379. * will return the blocks that include the delayed allocation
  4380. * blocks for this file.
  4381. */
  4382. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  4383. delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  4384. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  4385. stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
  4386. return 0;
  4387. }
  4388. static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
  4389. int chunk)
  4390. {
  4391. int indirects;
  4392. /* if nrblocks are contiguous */
  4393. if (chunk) {
  4394. /*
  4395. * With N contiguous data blocks, it need at most
  4396. * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
  4397. * 2 dindirect blocks
  4398. * 1 tindirect block
  4399. */
  4400. indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
  4401. return indirects + 3;
  4402. }
  4403. /*
  4404. * if nrblocks are not contiguous, worse case, each block touch
  4405. * a indirect block, and each indirect block touch a double indirect
  4406. * block, plus a triple indirect block
  4407. */
  4408. indirects = nrblocks * 2 + 1;
  4409. return indirects;
  4410. }
  4411. static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4412. {
  4413. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  4414. return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
  4415. return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
  4416. }
  4417. /*
  4418. * Account for index blocks, block groups bitmaps and block group
  4419. * descriptor blocks if modify datablocks and index blocks
  4420. * worse case, the indexs blocks spread over different block groups
  4421. *
  4422. * If datablocks are discontiguous, they are possible to spread over
  4423. * different block groups too. If they are contiugous, with flexbg,
  4424. * they could still across block group boundary.
  4425. *
  4426. * Also account for superblock, inode, quota and xattr blocks
  4427. */
  4428. int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4429. {
  4430. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  4431. int gdpblocks;
  4432. int idxblocks;
  4433. int ret = 0;
  4434. /*
  4435. * How many index blocks need to touch to modify nrblocks?
  4436. * The "Chunk" flag indicating whether the nrblocks is
  4437. * physically contiguous on disk
  4438. *
  4439. * For Direct IO and fallocate, they calls get_block to allocate
  4440. * one single extent at a time, so they could set the "Chunk" flag
  4441. */
  4442. idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
  4443. ret = idxblocks;
  4444. /*
  4445. * Now let's see how many group bitmaps and group descriptors need
  4446. * to account
  4447. */
  4448. groups = idxblocks;
  4449. if (chunk)
  4450. groups += 1;
  4451. else
  4452. groups += nrblocks;
  4453. gdpblocks = groups;
  4454. if (groups > ngroups)
  4455. groups = ngroups;
  4456. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  4457. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  4458. /* bitmaps and block group descriptor blocks */
  4459. ret += groups + gdpblocks;
  4460. /* Blocks for super block, inode, quota and xattr blocks */
  4461. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  4462. return ret;
  4463. }
  4464. /*
  4465. * Calulate the total number of credits to reserve to fit
  4466. * the modification of a single pages into a single transaction,
  4467. * which may include multiple chunks of block allocations.
  4468. *
  4469. * This could be called via ext4_write_begin()
  4470. *
  4471. * We need to consider the worse case, when
  4472. * one new block per extent.
  4473. */
  4474. int ext4_writepage_trans_blocks(struct inode *inode)
  4475. {
  4476. int bpp = ext4_journal_blocks_per_page(inode);
  4477. int ret;
  4478. ret = ext4_meta_trans_blocks(inode, bpp, 0);
  4479. /* Account for data blocks for journalled mode */
  4480. if (ext4_should_journal_data(inode))
  4481. ret += bpp;
  4482. return ret;
  4483. }
  4484. /*
  4485. * Calculate the journal credits for a chunk of data modification.
  4486. *
  4487. * This is called from DIO, fallocate or whoever calling
  4488. * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks.
  4489. *
  4490. * journal buffers for data blocks are not included here, as DIO
  4491. * and fallocate do no need to journal data buffers.
  4492. */
  4493. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  4494. {
  4495. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  4496. }
  4497. /*
  4498. * The caller must have previously called ext4_reserve_inode_write().
  4499. * Give this, we know that the caller already has write access to iloc->bh.
  4500. */
  4501. int ext4_mark_iloc_dirty(handle_t *handle,
  4502. struct inode *inode, struct ext4_iloc *iloc)
  4503. {
  4504. int err = 0;
  4505. if (test_opt(inode->i_sb, I_VERSION))
  4506. inode_inc_iversion(inode);
  4507. /* the do_update_inode consumes one bh->b_count */
  4508. get_bh(iloc->bh);
  4509. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4510. err = ext4_do_update_inode(handle, inode, iloc);
  4511. put_bh(iloc->bh);
  4512. return err;
  4513. }
  4514. /*
  4515. * On success, We end up with an outstanding reference count against
  4516. * iloc->bh. This _must_ be cleaned up later.
  4517. */
  4518. int
  4519. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4520. struct ext4_iloc *iloc)
  4521. {
  4522. int err;
  4523. err = ext4_get_inode_loc(inode, iloc);
  4524. if (!err) {
  4525. BUFFER_TRACE(iloc->bh, "get_write_access");
  4526. err = ext4_journal_get_write_access(handle, iloc->bh);
  4527. if (err) {
  4528. brelse(iloc->bh);
  4529. iloc->bh = NULL;
  4530. }
  4531. }
  4532. ext4_std_error(inode->i_sb, err);
  4533. return err;
  4534. }
  4535. /*
  4536. * Expand an inode by new_extra_isize bytes.
  4537. * Returns 0 on success or negative error number on failure.
  4538. */
  4539. static int ext4_expand_extra_isize(struct inode *inode,
  4540. unsigned int new_extra_isize,
  4541. struct ext4_iloc iloc,
  4542. handle_t *handle)
  4543. {
  4544. struct ext4_inode *raw_inode;
  4545. struct ext4_xattr_ibody_header *header;
  4546. struct ext4_xattr_entry *entry;
  4547. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4548. return 0;
  4549. raw_inode = ext4_raw_inode(&iloc);
  4550. header = IHDR(inode, raw_inode);
  4551. entry = IFIRST(header);
  4552. /* No extended attributes present */
  4553. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
  4554. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4555. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4556. new_extra_isize);
  4557. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4558. return 0;
  4559. }
  4560. /* try to expand with EAs present */
  4561. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4562. raw_inode, handle);
  4563. }
  4564. /*
  4565. * What we do here is to mark the in-core inode as clean with respect to inode
  4566. * dirtiness (it may still be data-dirty).
  4567. * This means that the in-core inode may be reaped by prune_icache
  4568. * without having to perform any I/O. This is a very good thing,
  4569. * because *any* task may call prune_icache - even ones which
  4570. * have a transaction open against a different journal.
  4571. *
  4572. * Is this cheating? Not really. Sure, we haven't written the
  4573. * inode out, but prune_icache isn't a user-visible syncing function.
  4574. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4575. * we start and wait on commits.
  4576. *
  4577. * Is this efficient/effective? Well, we're being nice to the system
  4578. * by cleaning up our inodes proactively so they can be reaped
  4579. * without I/O. But we are potentially leaving up to five seconds'
  4580. * worth of inodes floating about which prune_icache wants us to
  4581. * write out. One way to fix that would be to get prune_icache()
  4582. * to do a write_super() to free up some memory. It has the desired
  4583. * effect.
  4584. */
  4585. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4586. {
  4587. struct ext4_iloc iloc;
  4588. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4589. static unsigned int mnt_count;
  4590. int err, ret;
  4591. might_sleep();
  4592. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4593. if (ext4_handle_valid(handle) &&
  4594. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4595. !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
  4596. /*
  4597. * We need extra buffer credits since we may write into EA block
  4598. * with this same handle. If journal_extend fails, then it will
  4599. * only result in a minor loss of functionality for that inode.
  4600. * If this is felt to be critical, then e2fsck should be run to
  4601. * force a large enough s_min_extra_isize.
  4602. */
  4603. if ((jbd2_journal_extend(handle,
  4604. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4605. ret = ext4_expand_extra_isize(inode,
  4606. sbi->s_want_extra_isize,
  4607. iloc, handle);
  4608. if (ret) {
  4609. EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
  4610. if (mnt_count !=
  4611. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4612. ext4_warning(inode->i_sb, __func__,
  4613. "Unable to expand inode %lu. Delete"
  4614. " some EAs or run e2fsck.",
  4615. inode->i_ino);
  4616. mnt_count =
  4617. le16_to_cpu(sbi->s_es->s_mnt_count);
  4618. }
  4619. }
  4620. }
  4621. }
  4622. if (!err)
  4623. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  4624. return err;
  4625. }
  4626. /*
  4627. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4628. *
  4629. * We're really interested in the case where a file is being extended.
  4630. * i_size has been changed by generic_commit_write() and we thus need
  4631. * to include the updated inode in the current transaction.
  4632. *
  4633. * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
  4634. * are allocated to the file.
  4635. *
  4636. * If the inode is marked synchronous, we don't honour that here - doing
  4637. * so would cause a commit on atime updates, which we don't bother doing.
  4638. * We handle synchronous inodes at the highest possible level.
  4639. */
  4640. void ext4_dirty_inode(struct inode *inode)
  4641. {
  4642. handle_t *current_handle = ext4_journal_current_handle();
  4643. handle_t *handle;
  4644. if (!ext4_handle_valid(current_handle)) {
  4645. ext4_mark_inode_dirty(current_handle, inode);
  4646. return;
  4647. }
  4648. handle = ext4_journal_start(inode, 2);
  4649. if (IS_ERR(handle))
  4650. goto out;
  4651. if (current_handle &&
  4652. current_handle->h_transaction != handle->h_transaction) {
  4653. /* This task has a transaction open against a different fs */
  4654. printk(KERN_EMERG "%s: transactions do not match!\n",
  4655. __func__);
  4656. } else {
  4657. jbd_debug(5, "marking dirty. outer handle=%p\n",
  4658. current_handle);
  4659. ext4_mark_inode_dirty(handle, inode);
  4660. }
  4661. ext4_journal_stop(handle);
  4662. out:
  4663. return;
  4664. }
  4665. #if 0
  4666. /*
  4667. * Bind an inode's backing buffer_head into this transaction, to prevent
  4668. * it from being flushed to disk early. Unlike
  4669. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4670. * returns no iloc structure, so the caller needs to repeat the iloc
  4671. * lookup to mark the inode dirty later.
  4672. */
  4673. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4674. {
  4675. struct ext4_iloc iloc;
  4676. int err = 0;
  4677. if (handle) {
  4678. err = ext4_get_inode_loc(inode, &iloc);
  4679. if (!err) {
  4680. BUFFER_TRACE(iloc.bh, "get_write_access");
  4681. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4682. if (!err)
  4683. err = ext4_handle_dirty_metadata(handle,
  4684. inode,
  4685. iloc.bh);
  4686. brelse(iloc.bh);
  4687. }
  4688. }
  4689. ext4_std_error(inode->i_sb, err);
  4690. return err;
  4691. }
  4692. #endif
  4693. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4694. {
  4695. journal_t *journal;
  4696. handle_t *handle;
  4697. int err;
  4698. /*
  4699. * We have to be very careful here: changing a data block's
  4700. * journaling status dynamically is dangerous. If we write a
  4701. * data block to the journal, change the status and then delete
  4702. * that block, we risk forgetting to revoke the old log record
  4703. * from the journal and so a subsequent replay can corrupt data.
  4704. * So, first we make sure that the journal is empty and that
  4705. * nobody is changing anything.
  4706. */
  4707. journal = EXT4_JOURNAL(inode);
  4708. if (!journal)
  4709. return 0;
  4710. if (is_journal_aborted(journal))
  4711. return -EROFS;
  4712. jbd2_journal_lock_updates(journal);
  4713. jbd2_journal_flush(journal);
  4714. /*
  4715. * OK, there are no updates running now, and all cached data is
  4716. * synced to disk. We are now in a completely consistent state
  4717. * which doesn't have anything in the journal, and we know that
  4718. * no filesystem updates are running, so it is safe to modify
  4719. * the inode's in-core data-journaling state flag now.
  4720. */
  4721. if (val)
  4722. EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
  4723. else
  4724. EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
  4725. ext4_set_aops(inode);
  4726. jbd2_journal_unlock_updates(journal);
  4727. /* Finally we can mark the inode as dirty. */
  4728. handle = ext4_journal_start(inode, 1);
  4729. if (IS_ERR(handle))
  4730. return PTR_ERR(handle);
  4731. err = ext4_mark_inode_dirty(handle, inode);
  4732. ext4_handle_sync(handle);
  4733. ext4_journal_stop(handle);
  4734. ext4_std_error(inode->i_sb, err);
  4735. return err;
  4736. }
  4737. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  4738. {
  4739. return !buffer_mapped(bh);
  4740. }
  4741. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4742. {
  4743. struct page *page = vmf->page;
  4744. loff_t size;
  4745. unsigned long len;
  4746. int ret = -EINVAL;
  4747. void *fsdata;
  4748. struct file *file = vma->vm_file;
  4749. struct inode *inode = file->f_path.dentry->d_inode;
  4750. struct address_space *mapping = inode->i_mapping;
  4751. /*
  4752. * Get i_alloc_sem to stop truncates messing with the inode. We cannot
  4753. * get i_mutex because we are already holding mmap_sem.
  4754. */
  4755. down_read(&inode->i_alloc_sem);
  4756. size = i_size_read(inode);
  4757. if (page->mapping != mapping || size <= page_offset(page)
  4758. || !PageUptodate(page)) {
  4759. /* page got truncated from under us? */
  4760. goto out_unlock;
  4761. }
  4762. ret = 0;
  4763. if (PageMappedToDisk(page))
  4764. goto out_unlock;
  4765. if (page->index == size >> PAGE_CACHE_SHIFT)
  4766. len = size & ~PAGE_CACHE_MASK;
  4767. else
  4768. len = PAGE_CACHE_SIZE;
  4769. if (page_has_buffers(page)) {
  4770. /* return if we have all the buffers mapped */
  4771. if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
  4772. ext4_bh_unmapped))
  4773. goto out_unlock;
  4774. }
  4775. /*
  4776. * OK, we need to fill the hole... Do write_begin write_end
  4777. * to do block allocation/reservation.We are not holding
  4778. * inode.i__mutex here. That allow * parallel write_begin,
  4779. * write_end call. lock_page prevent this from happening
  4780. * on the same page though
  4781. */
  4782. ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
  4783. len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  4784. if (ret < 0)
  4785. goto out_unlock;
  4786. ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
  4787. len, len, page, fsdata);
  4788. if (ret < 0)
  4789. goto out_unlock;
  4790. ret = 0;
  4791. out_unlock:
  4792. if (ret)
  4793. ret = VM_FAULT_SIGBUS;
  4794. up_read(&inode->i_alloc_sem);
  4795. return ret;
  4796. }