inode.c 180 KB

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