inode.c 135 KB

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