inode.c 152 KB

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