inode.c 139 KB

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