inode.c 173 KB

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