inode.c 168 KB

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