inode.c 128 KB

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