inode.c 138 KB

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