inode.c 151 KB

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