inode.c 139 KB

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