inode.c 142 KB

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