inode.c 150 KB

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