inode.c 170 KB

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