inode.c 153 KB

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