inode.c 153 KB

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