inode.c 169 KB

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