inode.c 151 KB

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