inode.c 136 KB

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