inode.c 150 KB

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