inode.c 153 KB

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