inode.c 140 KB

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