inode.c 160 KB

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