inode.c 134 KB

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