inode.c 141 KB

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