inode.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454
  1. /*
  2. * linux/fs/ext3/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. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@redhat.com), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext3_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/module.h>
  25. #include <linux/fs.h>
  26. #include <linux/time.h>
  27. #include <linux/ext3_jbd.h>
  28. #include <linux/jbd.h>
  29. #include <linux/highuid.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/quotaops.h>
  32. #include <linux/string.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/writeback.h>
  35. #include <linux/mpage.h>
  36. #include <linux/uio.h>
  37. #include <linux/bio.h>
  38. #include <linux/fiemap.h>
  39. #include <linux/namei.h>
  40. #include "xattr.h"
  41. #include "acl.h"
  42. static int ext3_writepage_trans_blocks(struct inode *inode);
  43. /*
  44. * Test whether an inode is a fast symlink.
  45. */
  46. static int ext3_inode_is_fast_symlink(struct inode *inode)
  47. {
  48. int ea_blocks = EXT3_I(inode)->i_file_acl ?
  49. (inode->i_sb->s_blocksize >> 9) : 0;
  50. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  51. }
  52. /*
  53. * The ext3 forget function must perform a revoke if we are freeing data
  54. * which has been journaled. Metadata (eg. indirect blocks) must be
  55. * revoked in all cases.
  56. *
  57. * "bh" may be NULL: a metadata block may have been freed from memory
  58. * but there may still be a record of it in the journal, and that record
  59. * still needs to be revoked.
  60. */
  61. int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
  62. struct buffer_head *bh, ext3_fsblk_t blocknr)
  63. {
  64. int err;
  65. might_sleep();
  66. BUFFER_TRACE(bh, "enter");
  67. jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
  68. "data mode %lx\n",
  69. bh, is_metadata, inode->i_mode,
  70. test_opt(inode->i_sb, DATA_FLAGS));
  71. /* Never use the revoke function if we are doing full data
  72. * journaling: there is no need to, and a V1 superblock won't
  73. * support it. Otherwise, only skip the revoke on un-journaled
  74. * data blocks. */
  75. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
  76. (!is_metadata && !ext3_should_journal_data(inode))) {
  77. if (bh) {
  78. BUFFER_TRACE(bh, "call journal_forget");
  79. return ext3_journal_forget(handle, bh);
  80. }
  81. return 0;
  82. }
  83. /*
  84. * data!=journal && (is_metadata || should_journal_data(inode))
  85. */
  86. BUFFER_TRACE(bh, "call ext3_journal_revoke");
  87. err = ext3_journal_revoke(handle, blocknr, bh);
  88. if (err)
  89. ext3_abort(inode->i_sb, __func__,
  90. "error %d when attempting revoke", err);
  91. BUFFER_TRACE(bh, "exit");
  92. return err;
  93. }
  94. /*
  95. * Work out how many blocks we need to proceed with the next chunk of a
  96. * truncate transaction.
  97. */
  98. static unsigned long blocks_for_truncate(struct inode *inode)
  99. {
  100. unsigned long needed;
  101. needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
  102. /* Give ourselves just enough room to cope with inodes in which
  103. * i_blocks is corrupt: we've seen disk corruptions in the past
  104. * which resulted in random data in an inode which looked enough
  105. * like a regular file for ext3 to try to delete it. Things
  106. * will go a bit crazy if that happens, but at least we should
  107. * try not to panic the whole kernel. */
  108. if (needed < 2)
  109. needed = 2;
  110. /* But we need to bound the transaction so we don't overflow the
  111. * journal. */
  112. if (needed > EXT3_MAX_TRANS_DATA)
  113. needed = EXT3_MAX_TRANS_DATA;
  114. return EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
  115. }
  116. /*
  117. * Truncate transactions can be complex and absolutely huge. So we need to
  118. * be able to restart the transaction at a conventient checkpoint to make
  119. * sure we don't overflow the journal.
  120. *
  121. * start_transaction gets us a new handle for a truncate transaction,
  122. * and extend_transaction tries to extend the existing one a bit. If
  123. * extend fails, we need to propagate the failure up and restart the
  124. * transaction in the top-level truncate loop. --sct
  125. */
  126. static handle_t *start_transaction(struct inode *inode)
  127. {
  128. handle_t *result;
  129. result = ext3_journal_start(inode, blocks_for_truncate(inode));
  130. if (!IS_ERR(result))
  131. return result;
  132. ext3_std_error(inode->i_sb, PTR_ERR(result));
  133. return result;
  134. }
  135. /*
  136. * Try to extend this transaction for the purposes of truncation.
  137. *
  138. * Returns 0 if we managed to create more room. If we can't create more
  139. * room, and the transaction must be restarted we return 1.
  140. */
  141. static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
  142. {
  143. if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
  144. return 0;
  145. if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
  146. return 0;
  147. return 1;
  148. }
  149. /*
  150. * Restart the transaction associated with *handle. This does a commit,
  151. * so before we call here everything must be consistently dirtied against
  152. * this transaction.
  153. */
  154. static int truncate_restart_transaction(handle_t *handle, struct inode *inode)
  155. {
  156. int ret;
  157. jbd_debug(2, "restarting handle %p\n", handle);
  158. /*
  159. * Drop truncate_mutex to avoid deadlock with ext3_get_blocks_handle
  160. * At this moment, get_block can be called only for blocks inside
  161. * i_size since page cache has been already dropped and writes are
  162. * blocked by i_mutex. So we can safely drop the truncate_mutex.
  163. */
  164. mutex_unlock(&EXT3_I(inode)->truncate_mutex);
  165. ret = ext3_journal_restart(handle, blocks_for_truncate(inode));
  166. mutex_lock(&EXT3_I(inode)->truncate_mutex);
  167. return ret;
  168. }
  169. /*
  170. * Called at the last iput() if i_nlink is zero.
  171. */
  172. void ext3_delete_inode (struct inode * inode)
  173. {
  174. handle_t *handle;
  175. if (!is_bad_inode(inode))
  176. dquot_initialize(inode);
  177. truncate_inode_pages(&inode->i_data, 0);
  178. if (is_bad_inode(inode))
  179. goto no_delete;
  180. handle = start_transaction(inode);
  181. if (IS_ERR(handle)) {
  182. /*
  183. * If we're going to skip the normal cleanup, we still need to
  184. * make sure that the in-core orphan linked list is properly
  185. * cleaned up.
  186. */
  187. ext3_orphan_del(NULL, inode);
  188. goto no_delete;
  189. }
  190. if (IS_SYNC(inode))
  191. handle->h_sync = 1;
  192. inode->i_size = 0;
  193. if (inode->i_blocks)
  194. ext3_truncate(inode);
  195. /*
  196. * Kill off the orphan record which ext3_truncate created.
  197. * AKPM: I think this can be inside the above `if'.
  198. * Note that ext3_orphan_del() has to be able to cope with the
  199. * deletion of a non-existent orphan - this is because we don't
  200. * know if ext3_truncate() actually created an orphan record.
  201. * (Well, we could do this if we need to, but heck - it works)
  202. */
  203. ext3_orphan_del(handle, inode);
  204. EXT3_I(inode)->i_dtime = get_seconds();
  205. /*
  206. * One subtle ordering requirement: if anything has gone wrong
  207. * (transaction abort, IO errors, whatever), then we can still
  208. * do these next steps (the fs will already have been marked as
  209. * having errors), but we can't free the inode if the mark_dirty
  210. * fails.
  211. */
  212. if (ext3_mark_inode_dirty(handle, inode))
  213. /* If that failed, just do the required in-core inode clear. */
  214. clear_inode(inode);
  215. else
  216. ext3_free_inode(handle, inode);
  217. ext3_journal_stop(handle);
  218. return;
  219. no_delete:
  220. clear_inode(inode); /* We must guarantee clearing of inode... */
  221. }
  222. typedef struct {
  223. __le32 *p;
  224. __le32 key;
  225. struct buffer_head *bh;
  226. } Indirect;
  227. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  228. {
  229. p->key = *(p->p = v);
  230. p->bh = bh;
  231. }
  232. static int verify_chain(Indirect *from, Indirect *to)
  233. {
  234. while (from <= to && from->key == *from->p)
  235. from++;
  236. return (from > to);
  237. }
  238. /**
  239. * ext3_block_to_path - parse the block number into array of offsets
  240. * @inode: inode in question (we are only interested in its superblock)
  241. * @i_block: block number to be parsed
  242. * @offsets: array to store the offsets in
  243. * @boundary: set this non-zero if the referred-to block is likely to be
  244. * followed (on disk) by an indirect block.
  245. *
  246. * To store the locations of file's data ext3 uses a data structure common
  247. * for UNIX filesystems - tree of pointers anchored in the inode, with
  248. * data blocks at leaves and indirect blocks in intermediate nodes.
  249. * This function translates the block number into path in that tree -
  250. * return value is the path length and @offsets[n] is the offset of
  251. * pointer to (n+1)th node in the nth one. If @block is out of range
  252. * (negative or too large) warning is printed and zero returned.
  253. *
  254. * Note: function doesn't find node addresses, so no IO is needed. All
  255. * we need to know is the capacity of indirect blocks (taken from the
  256. * inode->i_sb).
  257. */
  258. /*
  259. * Portability note: the last comparison (check that we fit into triple
  260. * indirect block) is spelled differently, because otherwise on an
  261. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  262. * if our filesystem had 8Kb blocks. We might use long long, but that would
  263. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  264. * i_block would have to be negative in the very beginning, so we would not
  265. * get there at all.
  266. */
  267. static int ext3_block_to_path(struct inode *inode,
  268. long i_block, int offsets[4], int *boundary)
  269. {
  270. int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
  271. int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
  272. const long direct_blocks = EXT3_NDIR_BLOCKS,
  273. indirect_blocks = ptrs,
  274. double_blocks = (1 << (ptrs_bits * 2));
  275. int n = 0;
  276. int final = 0;
  277. if (i_block < 0) {
  278. ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
  279. } else if (i_block < direct_blocks) {
  280. offsets[n++] = i_block;
  281. final = direct_blocks;
  282. } else if ( (i_block -= direct_blocks) < indirect_blocks) {
  283. offsets[n++] = EXT3_IND_BLOCK;
  284. offsets[n++] = i_block;
  285. final = ptrs;
  286. } else if ((i_block -= indirect_blocks) < double_blocks) {
  287. offsets[n++] = EXT3_DIND_BLOCK;
  288. offsets[n++] = i_block >> ptrs_bits;
  289. offsets[n++] = i_block & (ptrs - 1);
  290. final = ptrs;
  291. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  292. offsets[n++] = EXT3_TIND_BLOCK;
  293. offsets[n++] = i_block >> (ptrs_bits * 2);
  294. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  295. offsets[n++] = i_block & (ptrs - 1);
  296. final = ptrs;
  297. } else {
  298. ext3_warning(inode->i_sb, "ext3_block_to_path", "block > big");
  299. }
  300. if (boundary)
  301. *boundary = final - 1 - (i_block & (ptrs - 1));
  302. return n;
  303. }
  304. /**
  305. * ext3_get_branch - read the chain of indirect blocks leading to data
  306. * @inode: inode in question
  307. * @depth: depth of the chain (1 - direct pointer, etc.)
  308. * @offsets: offsets of pointers in inode/indirect blocks
  309. * @chain: place to store the result
  310. * @err: here we store the error value
  311. *
  312. * Function fills the array of triples <key, p, bh> and returns %NULL
  313. * if everything went OK or the pointer to the last filled triple
  314. * (incomplete one) otherwise. Upon the return chain[i].key contains
  315. * the number of (i+1)-th block in the chain (as it is stored in memory,
  316. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  317. * number (it points into struct inode for i==0 and into the bh->b_data
  318. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  319. * block for i>0 and NULL for i==0. In other words, it holds the block
  320. * numbers of the chain, addresses they were taken from (and where we can
  321. * verify that chain did not change) and buffer_heads hosting these
  322. * numbers.
  323. *
  324. * Function stops when it stumbles upon zero pointer (absent block)
  325. * (pointer to last triple returned, *@err == 0)
  326. * or when it gets an IO error reading an indirect block
  327. * (ditto, *@err == -EIO)
  328. * or when it notices that chain had been changed while it was reading
  329. * (ditto, *@err == -EAGAIN)
  330. * or when it reads all @depth-1 indirect blocks successfully and finds
  331. * the whole chain, all way to the data (returns %NULL, *err == 0).
  332. */
  333. static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
  334. Indirect chain[4], int *err)
  335. {
  336. struct super_block *sb = inode->i_sb;
  337. Indirect *p = chain;
  338. struct buffer_head *bh;
  339. *err = 0;
  340. /* i_data is not going away, no lock needed */
  341. add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
  342. if (!p->key)
  343. goto no_block;
  344. while (--depth) {
  345. bh = sb_bread(sb, le32_to_cpu(p->key));
  346. if (!bh)
  347. goto failure;
  348. /* Reader: pointers */
  349. if (!verify_chain(chain, p))
  350. goto changed;
  351. add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
  352. /* Reader: end */
  353. if (!p->key)
  354. goto no_block;
  355. }
  356. return NULL;
  357. changed:
  358. brelse(bh);
  359. *err = -EAGAIN;
  360. goto no_block;
  361. failure:
  362. *err = -EIO;
  363. no_block:
  364. return p;
  365. }
  366. /**
  367. * ext3_find_near - find a place for allocation with sufficient locality
  368. * @inode: owner
  369. * @ind: descriptor of indirect block.
  370. *
  371. * This function returns the preferred place for block allocation.
  372. * It is used when heuristic for sequential allocation fails.
  373. * Rules are:
  374. * + if there is a block to the left of our position - allocate near it.
  375. * + if pointer will live in indirect block - allocate near that block.
  376. * + if pointer will live in inode - allocate in the same
  377. * cylinder group.
  378. *
  379. * In the latter case we colour the starting block by the callers PID to
  380. * prevent it from clashing with concurrent allocations for a different inode
  381. * in the same block group. The PID is used here so that functionally related
  382. * files will be close-by on-disk.
  383. *
  384. * Caller must make sure that @ind is valid and will stay that way.
  385. */
  386. static ext3_fsblk_t ext3_find_near(struct inode *inode, Indirect *ind)
  387. {
  388. struct ext3_inode_info *ei = EXT3_I(inode);
  389. __le32 *start = ind->bh ? (__le32*) ind->bh->b_data : ei->i_data;
  390. __le32 *p;
  391. ext3_fsblk_t bg_start;
  392. ext3_grpblk_t colour;
  393. /* Try to find previous block */
  394. for (p = ind->p - 1; p >= start; p--) {
  395. if (*p)
  396. return le32_to_cpu(*p);
  397. }
  398. /* No such thing, so let's try location of indirect block */
  399. if (ind->bh)
  400. return ind->bh->b_blocknr;
  401. /*
  402. * It is going to be referred to from the inode itself? OK, just put it
  403. * into the same cylinder group then.
  404. */
  405. bg_start = ext3_group_first_block_no(inode->i_sb, ei->i_block_group);
  406. colour = (current->pid % 16) *
  407. (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  408. return bg_start + colour;
  409. }
  410. /**
  411. * ext3_find_goal - find a preferred place for allocation.
  412. * @inode: owner
  413. * @block: block we want
  414. * @partial: pointer to the last triple within a chain
  415. *
  416. * Normally this function find the preferred place for block allocation,
  417. * returns it.
  418. */
  419. static ext3_fsblk_t ext3_find_goal(struct inode *inode, long block,
  420. Indirect *partial)
  421. {
  422. struct ext3_block_alloc_info *block_i;
  423. block_i = EXT3_I(inode)->i_block_alloc_info;
  424. /*
  425. * try the heuristic for sequential allocation,
  426. * failing that at least try to get decent locality.
  427. */
  428. if (block_i && (block == block_i->last_alloc_logical_block + 1)
  429. && (block_i->last_alloc_physical_block != 0)) {
  430. return block_i->last_alloc_physical_block + 1;
  431. }
  432. return ext3_find_near(inode, partial);
  433. }
  434. /**
  435. * ext3_blks_to_allocate: Look up the block map and count the number
  436. * of direct blocks need to be allocated for the given branch.
  437. *
  438. * @branch: chain of indirect blocks
  439. * @k: number of blocks need for indirect blocks
  440. * @blks: number of data blocks to be mapped.
  441. * @blocks_to_boundary: the offset in the indirect block
  442. *
  443. * return the total number of blocks to be allocate, including the
  444. * direct and indirect blocks.
  445. */
  446. static int ext3_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
  447. int blocks_to_boundary)
  448. {
  449. unsigned long count = 0;
  450. /*
  451. * Simple case, [t,d]Indirect block(s) has not allocated yet
  452. * then it's clear blocks on that path have not allocated
  453. */
  454. if (k > 0) {
  455. /* right now we don't handle cross boundary allocation */
  456. if (blks < blocks_to_boundary + 1)
  457. count += blks;
  458. else
  459. count += blocks_to_boundary + 1;
  460. return count;
  461. }
  462. count++;
  463. while (count < blks && count <= blocks_to_boundary &&
  464. le32_to_cpu(*(branch[0].p + count)) == 0) {
  465. count++;
  466. }
  467. return count;
  468. }
  469. /**
  470. * ext3_alloc_blocks: multiple allocate blocks needed for a branch
  471. * @indirect_blks: the number of blocks need to allocate for indirect
  472. * blocks
  473. *
  474. * @new_blocks: on return it will store the new block numbers for
  475. * the indirect blocks(if needed) and the first direct block,
  476. * @blks: on return it will store the total number of allocated
  477. * direct blocks
  478. */
  479. static int ext3_alloc_blocks(handle_t *handle, struct inode *inode,
  480. ext3_fsblk_t goal, int indirect_blks, int blks,
  481. ext3_fsblk_t new_blocks[4], int *err)
  482. {
  483. int target, i;
  484. unsigned long count = 0;
  485. int index = 0;
  486. ext3_fsblk_t current_block = 0;
  487. int ret = 0;
  488. /*
  489. * Here we try to allocate the requested multiple blocks at once,
  490. * on a best-effort basis.
  491. * To build a branch, we should allocate blocks for
  492. * the indirect blocks(if not allocated yet), and at least
  493. * the first direct block of this branch. That's the
  494. * minimum number of blocks need to allocate(required)
  495. */
  496. target = blks + indirect_blks;
  497. while (1) {
  498. count = target;
  499. /* allocating blocks for indirect blocks and direct blocks */
  500. current_block = ext3_new_blocks(handle,inode,goal,&count,err);
  501. if (*err)
  502. goto failed_out;
  503. target -= count;
  504. /* allocate blocks for indirect blocks */
  505. while (index < indirect_blks && count) {
  506. new_blocks[index++] = current_block++;
  507. count--;
  508. }
  509. if (count > 0)
  510. break;
  511. }
  512. /* save the new block number for the first direct block */
  513. new_blocks[index] = current_block;
  514. /* total number of blocks allocated for direct blocks */
  515. ret = count;
  516. *err = 0;
  517. return ret;
  518. failed_out:
  519. for (i = 0; i <index; i++)
  520. ext3_free_blocks(handle, inode, new_blocks[i], 1);
  521. return ret;
  522. }
  523. /**
  524. * ext3_alloc_branch - allocate and set up a chain of blocks.
  525. * @inode: owner
  526. * @indirect_blks: number of allocated indirect blocks
  527. * @blks: number of allocated direct blocks
  528. * @offsets: offsets (in the blocks) to store the pointers to next.
  529. * @branch: place to store the chain in.
  530. *
  531. * This function allocates blocks, zeroes out all but the last one,
  532. * links them into chain and (if we are synchronous) writes them to disk.
  533. * In other words, it prepares a branch that can be spliced onto the
  534. * inode. It stores the information about that chain in the branch[], in
  535. * the same format as ext3_get_branch() would do. We are calling it after
  536. * we had read the existing part of chain and partial points to the last
  537. * triple of that (one with zero ->key). Upon the exit we have the same
  538. * picture as after the successful ext3_get_block(), except that in one
  539. * place chain is disconnected - *branch->p is still zero (we did not
  540. * set the last link), but branch->key contains the number that should
  541. * be placed into *branch->p to fill that gap.
  542. *
  543. * If allocation fails we free all blocks we've allocated (and forget
  544. * their buffer_heads) and return the error value the from failed
  545. * ext3_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  546. * as described above and return 0.
  547. */
  548. static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
  549. int indirect_blks, int *blks, ext3_fsblk_t goal,
  550. int *offsets, Indirect *branch)
  551. {
  552. int blocksize = inode->i_sb->s_blocksize;
  553. int i, n = 0;
  554. int err = 0;
  555. struct buffer_head *bh;
  556. int num;
  557. ext3_fsblk_t new_blocks[4];
  558. ext3_fsblk_t current_block;
  559. num = ext3_alloc_blocks(handle, inode, goal, indirect_blks,
  560. *blks, new_blocks, &err);
  561. if (err)
  562. return err;
  563. branch[0].key = cpu_to_le32(new_blocks[0]);
  564. /*
  565. * metadata blocks and data blocks are allocated.
  566. */
  567. for (n = 1; n <= indirect_blks; n++) {
  568. /*
  569. * Get buffer_head for parent block, zero it out
  570. * and set the pointer to new one, then send
  571. * parent to disk.
  572. */
  573. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  574. branch[n].bh = bh;
  575. lock_buffer(bh);
  576. BUFFER_TRACE(bh, "call get_create_access");
  577. err = ext3_journal_get_create_access(handle, bh);
  578. if (err) {
  579. unlock_buffer(bh);
  580. brelse(bh);
  581. goto failed;
  582. }
  583. memset(bh->b_data, 0, blocksize);
  584. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  585. branch[n].key = cpu_to_le32(new_blocks[n]);
  586. *branch[n].p = branch[n].key;
  587. if ( n == indirect_blks) {
  588. current_block = new_blocks[n];
  589. /*
  590. * End of chain, update the last new metablock of
  591. * the chain to point to the new allocated
  592. * data blocks numbers
  593. */
  594. for (i=1; i < num; i++)
  595. *(branch[n].p + i) = cpu_to_le32(++current_block);
  596. }
  597. BUFFER_TRACE(bh, "marking uptodate");
  598. set_buffer_uptodate(bh);
  599. unlock_buffer(bh);
  600. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  601. err = ext3_journal_dirty_metadata(handle, bh);
  602. if (err)
  603. goto failed;
  604. }
  605. *blks = num;
  606. return err;
  607. failed:
  608. /* Allocation failed, free what we already allocated */
  609. for (i = 1; i <= n ; i++) {
  610. BUFFER_TRACE(branch[i].bh, "call journal_forget");
  611. ext3_journal_forget(handle, branch[i].bh);
  612. }
  613. for (i = 0; i <indirect_blks; i++)
  614. ext3_free_blocks(handle, inode, new_blocks[i], 1);
  615. ext3_free_blocks(handle, inode, new_blocks[i], num);
  616. return err;
  617. }
  618. /**
  619. * ext3_splice_branch - splice the allocated branch onto inode.
  620. * @inode: owner
  621. * @block: (logical) number of block we are adding
  622. * @chain: chain of indirect blocks (with a missing link - see
  623. * ext3_alloc_branch)
  624. * @where: location of missing link
  625. * @num: number of indirect blocks we are adding
  626. * @blks: number of direct blocks we are adding
  627. *
  628. * This function fills the missing link and does all housekeeping needed in
  629. * inode (->i_blocks, etc.). In case of success we end up with the full
  630. * chain to new block and return 0.
  631. */
  632. static int ext3_splice_branch(handle_t *handle, struct inode *inode,
  633. long block, Indirect *where, int num, int blks)
  634. {
  635. int i;
  636. int err = 0;
  637. struct ext3_block_alloc_info *block_i;
  638. ext3_fsblk_t current_block;
  639. struct ext3_inode_info *ei = EXT3_I(inode);
  640. block_i = ei->i_block_alloc_info;
  641. /*
  642. * If we're splicing into a [td]indirect block (as opposed to the
  643. * inode) then we need to get write access to the [td]indirect block
  644. * before the splice.
  645. */
  646. if (where->bh) {
  647. BUFFER_TRACE(where->bh, "get_write_access");
  648. err = ext3_journal_get_write_access(handle, where->bh);
  649. if (err)
  650. goto err_out;
  651. }
  652. /* That's it */
  653. *where->p = where->key;
  654. /*
  655. * Update the host buffer_head or inode to point to more just allocated
  656. * direct blocks blocks
  657. */
  658. if (num == 0 && blks > 1) {
  659. current_block = le32_to_cpu(where->key) + 1;
  660. for (i = 1; i < blks; i++)
  661. *(where->p + i ) = cpu_to_le32(current_block++);
  662. }
  663. /*
  664. * update the most recently allocated logical & physical block
  665. * in i_block_alloc_info, to assist find the proper goal block for next
  666. * allocation
  667. */
  668. if (block_i) {
  669. block_i->last_alloc_logical_block = block + blks - 1;
  670. block_i->last_alloc_physical_block =
  671. le32_to_cpu(where[num].key) + blks - 1;
  672. }
  673. /* We are done with atomic stuff, now do the rest of housekeeping */
  674. inode->i_ctime = CURRENT_TIME_SEC;
  675. ext3_mark_inode_dirty(handle, inode);
  676. /* ext3_mark_inode_dirty already updated i_sync_tid */
  677. atomic_set(&ei->i_datasync_tid, handle->h_transaction->t_tid);
  678. /* had we spliced it onto indirect block? */
  679. if (where->bh) {
  680. /*
  681. * If we spliced it onto an indirect block, we haven't
  682. * altered the inode. Note however that if it is being spliced
  683. * onto an indirect block at the very end of the file (the
  684. * file is growing) then we *will* alter the inode to reflect
  685. * the new i_size. But that is not done here - it is done in
  686. * generic_commit_write->__mark_inode_dirty->ext3_dirty_inode.
  687. */
  688. jbd_debug(5, "splicing indirect only\n");
  689. BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
  690. err = ext3_journal_dirty_metadata(handle, where->bh);
  691. if (err)
  692. goto err_out;
  693. } else {
  694. /*
  695. * OK, we spliced it into the inode itself on a direct block.
  696. * Inode was dirtied above.
  697. */
  698. jbd_debug(5, "splicing direct\n");
  699. }
  700. return err;
  701. err_out:
  702. for (i = 1; i <= num; i++) {
  703. BUFFER_TRACE(where[i].bh, "call journal_forget");
  704. ext3_journal_forget(handle, where[i].bh);
  705. ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
  706. }
  707. ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
  708. return err;
  709. }
  710. /*
  711. * Allocation strategy is simple: if we have to allocate something, we will
  712. * have to go the whole way to leaf. So let's do it before attaching anything
  713. * to tree, set linkage between the newborn blocks, write them if sync is
  714. * required, recheck the path, free and repeat if check fails, otherwise
  715. * set the last missing link (that will protect us from any truncate-generated
  716. * removals - all blocks on the path are immune now) and possibly force the
  717. * write on the parent block.
  718. * That has a nice additional property: no special recovery from the failed
  719. * allocations is needed - we simply release blocks and do not touch anything
  720. * reachable from inode.
  721. *
  722. * `handle' can be NULL if create == 0.
  723. *
  724. * The BKL may not be held on entry here. Be sure to take it early.
  725. * return > 0, # of blocks mapped or allocated.
  726. * return = 0, if plain lookup failed.
  727. * return < 0, error case.
  728. */
  729. int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
  730. sector_t iblock, unsigned long maxblocks,
  731. struct buffer_head *bh_result,
  732. int create)
  733. {
  734. int err = -EIO;
  735. int offsets[4];
  736. Indirect chain[4];
  737. Indirect *partial;
  738. ext3_fsblk_t goal;
  739. int indirect_blks;
  740. int blocks_to_boundary = 0;
  741. int depth;
  742. struct ext3_inode_info *ei = EXT3_I(inode);
  743. int count = 0;
  744. ext3_fsblk_t first_block = 0;
  745. J_ASSERT(handle != NULL || create == 0);
  746. depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  747. if (depth == 0)
  748. goto out;
  749. partial = ext3_get_branch(inode, depth, offsets, chain, &err);
  750. /* Simplest case - block found, no allocation needed */
  751. if (!partial) {
  752. first_block = le32_to_cpu(chain[depth - 1].key);
  753. clear_buffer_new(bh_result);
  754. count++;
  755. /*map more blocks*/
  756. while (count < maxblocks && count <= blocks_to_boundary) {
  757. ext3_fsblk_t blk;
  758. if (!verify_chain(chain, chain + depth - 1)) {
  759. /*
  760. * Indirect block might be removed by
  761. * truncate while we were reading it.
  762. * Handling of that case: forget what we've
  763. * got now. Flag the err as EAGAIN, so it
  764. * will reread.
  765. */
  766. err = -EAGAIN;
  767. count = 0;
  768. break;
  769. }
  770. blk = le32_to_cpu(*(chain[depth-1].p + count));
  771. if (blk == first_block + count)
  772. count++;
  773. else
  774. break;
  775. }
  776. if (err != -EAGAIN)
  777. goto got_it;
  778. }
  779. /* Next simple case - plain lookup or failed read of indirect block */
  780. if (!create || err == -EIO)
  781. goto cleanup;
  782. mutex_lock(&ei->truncate_mutex);
  783. /*
  784. * If the indirect block is missing while we are reading
  785. * the chain(ext3_get_branch() returns -EAGAIN err), or
  786. * if the chain has been changed after we grab the semaphore,
  787. * (either because another process truncated this branch, or
  788. * another get_block allocated this branch) re-grab the chain to see if
  789. * the request block has been allocated or not.
  790. *
  791. * Since we already block the truncate/other get_block
  792. * at this point, we will have the current copy of the chain when we
  793. * splice the branch into the tree.
  794. */
  795. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  796. while (partial > chain) {
  797. brelse(partial->bh);
  798. partial--;
  799. }
  800. partial = ext3_get_branch(inode, depth, offsets, chain, &err);
  801. if (!partial) {
  802. count++;
  803. mutex_unlock(&ei->truncate_mutex);
  804. if (err)
  805. goto cleanup;
  806. clear_buffer_new(bh_result);
  807. goto got_it;
  808. }
  809. }
  810. /*
  811. * Okay, we need to do block allocation. Lazily initialize the block
  812. * allocation info here if necessary
  813. */
  814. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  815. ext3_init_block_alloc_info(inode);
  816. goal = ext3_find_goal(inode, iblock, partial);
  817. /* the number of blocks need to allocate for [d,t]indirect blocks */
  818. indirect_blks = (chain + depth) - partial - 1;
  819. /*
  820. * Next look up the indirect map to count the totoal number of
  821. * direct blocks to allocate for this branch.
  822. */
  823. count = ext3_blks_to_allocate(partial, indirect_blks,
  824. maxblocks, blocks_to_boundary);
  825. /*
  826. * Block out ext3_truncate while we alter the tree
  827. */
  828. err = ext3_alloc_branch(handle, inode, indirect_blks, &count, goal,
  829. offsets + (partial - chain), partial);
  830. /*
  831. * The ext3_splice_branch call will free and forget any buffers
  832. * on the new chain if there is a failure, but that risks using
  833. * up transaction credits, especially for bitmaps where the
  834. * credits cannot be returned. Can we handle this somehow? We
  835. * may need to return -EAGAIN upwards in the worst case. --sct
  836. */
  837. if (!err)
  838. err = ext3_splice_branch(handle, inode, iblock,
  839. partial, indirect_blks, count);
  840. mutex_unlock(&ei->truncate_mutex);
  841. if (err)
  842. goto cleanup;
  843. set_buffer_new(bh_result);
  844. got_it:
  845. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  846. if (count > blocks_to_boundary)
  847. set_buffer_boundary(bh_result);
  848. err = count;
  849. /* Clean up and exit */
  850. partial = chain + depth - 1; /* the whole chain */
  851. cleanup:
  852. while (partial > chain) {
  853. BUFFER_TRACE(partial->bh, "call brelse");
  854. brelse(partial->bh);
  855. partial--;
  856. }
  857. BUFFER_TRACE(bh_result, "returned");
  858. out:
  859. return err;
  860. }
  861. /* Maximum number of blocks we map for direct IO at once. */
  862. #define DIO_MAX_BLOCKS 4096
  863. /*
  864. * Number of credits we need for writing DIO_MAX_BLOCKS:
  865. * We need sb + group descriptor + bitmap + inode -> 4
  866. * For B blocks with A block pointers per block we need:
  867. * 1 (triple ind.) + (B/A/A + 2) (doubly ind.) + (B/A + 2) (indirect).
  868. * If we plug in 4096 for B and 256 for A (for 1KB block size), we get 25.
  869. */
  870. #define DIO_CREDITS 25
  871. static int ext3_get_block(struct inode *inode, sector_t iblock,
  872. struct buffer_head *bh_result, int create)
  873. {
  874. handle_t *handle = ext3_journal_current_handle();
  875. int ret = 0, started = 0;
  876. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  877. if (create && !handle) { /* Direct IO write... */
  878. if (max_blocks > DIO_MAX_BLOCKS)
  879. max_blocks = DIO_MAX_BLOCKS;
  880. handle = ext3_journal_start(inode, DIO_CREDITS +
  881. EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
  882. if (IS_ERR(handle)) {
  883. ret = PTR_ERR(handle);
  884. goto out;
  885. }
  886. started = 1;
  887. }
  888. ret = ext3_get_blocks_handle(handle, inode, iblock,
  889. max_blocks, bh_result, create);
  890. if (ret > 0) {
  891. bh_result->b_size = (ret << inode->i_blkbits);
  892. ret = 0;
  893. }
  894. if (started)
  895. ext3_journal_stop(handle);
  896. out:
  897. return ret;
  898. }
  899. int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  900. u64 start, u64 len)
  901. {
  902. return generic_block_fiemap(inode, fieinfo, start, len,
  903. ext3_get_block);
  904. }
  905. /*
  906. * `handle' can be NULL if create is zero
  907. */
  908. struct buffer_head *ext3_getblk(handle_t *handle, struct inode *inode,
  909. long block, int create, int *errp)
  910. {
  911. struct buffer_head dummy;
  912. int fatal = 0, err;
  913. J_ASSERT(handle != NULL || create == 0);
  914. dummy.b_state = 0;
  915. dummy.b_blocknr = -1000;
  916. buffer_trace_init(&dummy.b_history);
  917. err = ext3_get_blocks_handle(handle, inode, block, 1,
  918. &dummy, create);
  919. /*
  920. * ext3_get_blocks_handle() returns number of blocks
  921. * mapped. 0 in case of a HOLE.
  922. */
  923. if (err > 0) {
  924. if (err > 1)
  925. WARN_ON(1);
  926. err = 0;
  927. }
  928. *errp = err;
  929. if (!err && buffer_mapped(&dummy)) {
  930. struct buffer_head *bh;
  931. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  932. if (!bh) {
  933. *errp = -EIO;
  934. goto err;
  935. }
  936. if (buffer_new(&dummy)) {
  937. J_ASSERT(create != 0);
  938. J_ASSERT(handle != NULL);
  939. /*
  940. * Now that we do not always journal data, we should
  941. * keep in mind whether this should always journal the
  942. * new buffer as metadata. For now, regular file
  943. * writes use ext3_get_block instead, so it's not a
  944. * problem.
  945. */
  946. lock_buffer(bh);
  947. BUFFER_TRACE(bh, "call get_create_access");
  948. fatal = ext3_journal_get_create_access(handle, bh);
  949. if (!fatal && !buffer_uptodate(bh)) {
  950. memset(bh->b_data,0,inode->i_sb->s_blocksize);
  951. set_buffer_uptodate(bh);
  952. }
  953. unlock_buffer(bh);
  954. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  955. err = ext3_journal_dirty_metadata(handle, bh);
  956. if (!fatal)
  957. fatal = err;
  958. } else {
  959. BUFFER_TRACE(bh, "not a new buffer");
  960. }
  961. if (fatal) {
  962. *errp = fatal;
  963. brelse(bh);
  964. bh = NULL;
  965. }
  966. return bh;
  967. }
  968. err:
  969. return NULL;
  970. }
  971. struct buffer_head *ext3_bread(handle_t *handle, struct inode *inode,
  972. int block, int create, int *err)
  973. {
  974. struct buffer_head * bh;
  975. bh = ext3_getblk(handle, inode, block, create, err);
  976. if (!bh)
  977. return bh;
  978. if (buffer_uptodate(bh))
  979. return bh;
  980. ll_rw_block(READ_META, 1, &bh);
  981. wait_on_buffer(bh);
  982. if (buffer_uptodate(bh))
  983. return bh;
  984. put_bh(bh);
  985. *err = -EIO;
  986. return NULL;
  987. }
  988. static int walk_page_buffers( handle_t *handle,
  989. struct buffer_head *head,
  990. unsigned from,
  991. unsigned to,
  992. int *partial,
  993. int (*fn)( handle_t *handle,
  994. struct buffer_head *bh))
  995. {
  996. struct buffer_head *bh;
  997. unsigned block_start, block_end;
  998. unsigned blocksize = head->b_size;
  999. int err, ret = 0;
  1000. struct buffer_head *next;
  1001. for ( bh = head, block_start = 0;
  1002. ret == 0 && (bh != head || !block_start);
  1003. block_start = block_end, bh = next)
  1004. {
  1005. next = bh->b_this_page;
  1006. block_end = block_start + blocksize;
  1007. if (block_end <= from || block_start >= to) {
  1008. if (partial && !buffer_uptodate(bh))
  1009. *partial = 1;
  1010. continue;
  1011. }
  1012. err = (*fn)(handle, bh);
  1013. if (!ret)
  1014. ret = err;
  1015. }
  1016. return ret;
  1017. }
  1018. /*
  1019. * To preserve ordering, it is essential that the hole instantiation and
  1020. * the data write be encapsulated in a single transaction. We cannot
  1021. * close off a transaction and start a new one between the ext3_get_block()
  1022. * and the commit_write(). So doing the journal_start at the start of
  1023. * prepare_write() is the right place.
  1024. *
  1025. * Also, this function can nest inside ext3_writepage() ->
  1026. * block_write_full_page(). In that case, we *know* that ext3_writepage()
  1027. * has generated enough buffer credits to do the whole page. So we won't
  1028. * block on the journal in that case, which is good, because the caller may
  1029. * be PF_MEMALLOC.
  1030. *
  1031. * By accident, ext3 can be reentered when a transaction is open via
  1032. * quota file writes. If we were to commit the transaction while thus
  1033. * reentered, there can be a deadlock - we would be holding a quota
  1034. * lock, and the commit would never complete if another thread had a
  1035. * transaction open and was blocking on the quota lock - a ranking
  1036. * violation.
  1037. *
  1038. * So what we do is to rely on the fact that journal_stop/journal_start
  1039. * will _not_ run commit under these circumstances because handle->h_ref
  1040. * is elevated. We'll still have enough credits for the tiny quotafile
  1041. * write.
  1042. */
  1043. static int do_journal_get_write_access(handle_t *handle,
  1044. struct buffer_head *bh)
  1045. {
  1046. if (!buffer_mapped(bh) || buffer_freed(bh))
  1047. return 0;
  1048. return ext3_journal_get_write_access(handle, bh);
  1049. }
  1050. /*
  1051. * Truncate blocks that were not used by write. We have to truncate the
  1052. * pagecache as well so that corresponding buffers get properly unmapped.
  1053. */
  1054. static void ext3_truncate_failed_write(struct inode *inode)
  1055. {
  1056. truncate_inode_pages(inode->i_mapping, inode->i_size);
  1057. ext3_truncate(inode);
  1058. }
  1059. static int ext3_write_begin(struct file *file, struct address_space *mapping,
  1060. loff_t pos, unsigned len, unsigned flags,
  1061. struct page **pagep, void **fsdata)
  1062. {
  1063. struct inode *inode = mapping->host;
  1064. int ret;
  1065. handle_t *handle;
  1066. int retries = 0;
  1067. struct page *page;
  1068. pgoff_t index;
  1069. unsigned from, to;
  1070. /* Reserve one block more for addition to orphan list in case
  1071. * we allocate blocks but write fails for some reason */
  1072. int needed_blocks = ext3_writepage_trans_blocks(inode) + 1;
  1073. index = pos >> PAGE_CACHE_SHIFT;
  1074. from = pos & (PAGE_CACHE_SIZE - 1);
  1075. to = from + len;
  1076. retry:
  1077. page = grab_cache_page_write_begin(mapping, index, flags);
  1078. if (!page)
  1079. return -ENOMEM;
  1080. *pagep = page;
  1081. handle = ext3_journal_start(inode, needed_blocks);
  1082. if (IS_ERR(handle)) {
  1083. unlock_page(page);
  1084. page_cache_release(page);
  1085. ret = PTR_ERR(handle);
  1086. goto out;
  1087. }
  1088. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1089. ext3_get_block);
  1090. if (ret)
  1091. goto write_begin_failed;
  1092. if (ext3_should_journal_data(inode)) {
  1093. ret = walk_page_buffers(handle, page_buffers(page),
  1094. from, to, NULL, do_journal_get_write_access);
  1095. }
  1096. write_begin_failed:
  1097. if (ret) {
  1098. /*
  1099. * block_write_begin may have instantiated a few blocks
  1100. * outside i_size. Trim these off again. Don't need
  1101. * i_size_read because we hold i_mutex.
  1102. *
  1103. * Add inode to orphan list in case we crash before truncate
  1104. * finishes. Do this only if ext3_can_truncate() agrees so
  1105. * that orphan processing code is happy.
  1106. */
  1107. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1108. ext3_orphan_add(handle, inode);
  1109. ext3_journal_stop(handle);
  1110. unlock_page(page);
  1111. page_cache_release(page);
  1112. if (pos + len > inode->i_size)
  1113. ext3_truncate_failed_write(inode);
  1114. }
  1115. if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  1116. goto retry;
  1117. out:
  1118. return ret;
  1119. }
  1120. int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
  1121. {
  1122. int err = journal_dirty_data(handle, bh);
  1123. if (err)
  1124. ext3_journal_abort_handle(__func__, __func__,
  1125. bh, handle, err);
  1126. return err;
  1127. }
  1128. /* For ordered writepage and write_end functions */
  1129. static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
  1130. {
  1131. /*
  1132. * Write could have mapped the buffer but it didn't copy the data in
  1133. * yet. So avoid filing such buffer into a transaction.
  1134. */
  1135. if (buffer_mapped(bh) && buffer_uptodate(bh))
  1136. return ext3_journal_dirty_data(handle, bh);
  1137. return 0;
  1138. }
  1139. /* For write_end() in data=journal mode */
  1140. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  1141. {
  1142. if (!buffer_mapped(bh) || buffer_freed(bh))
  1143. return 0;
  1144. set_buffer_uptodate(bh);
  1145. return ext3_journal_dirty_metadata(handle, bh);
  1146. }
  1147. /*
  1148. * This is nasty and subtle: ext3_write_begin() could have allocated blocks
  1149. * for the whole page but later we failed to copy the data in. Update inode
  1150. * size according to what we managed to copy. The rest is going to be
  1151. * truncated in write_end function.
  1152. */
  1153. static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
  1154. {
  1155. /* What matters to us is i_disksize. We don't write i_size anywhere */
  1156. if (pos + copied > inode->i_size)
  1157. i_size_write(inode, pos + copied);
  1158. if (pos + copied > EXT3_I(inode)->i_disksize) {
  1159. EXT3_I(inode)->i_disksize = pos + copied;
  1160. mark_inode_dirty(inode);
  1161. }
  1162. }
  1163. /*
  1164. * We need to pick up the new inode size which generic_commit_write gave us
  1165. * `file' can be NULL - eg, when called from page_symlink().
  1166. *
  1167. * ext3 never places buffers on inode->i_mapping->private_list. metadata
  1168. * buffers are managed internally.
  1169. */
  1170. static int ext3_ordered_write_end(struct file *file,
  1171. struct address_space *mapping,
  1172. loff_t pos, unsigned len, unsigned copied,
  1173. struct page *page, void *fsdata)
  1174. {
  1175. handle_t *handle = ext3_journal_current_handle();
  1176. struct inode *inode = file->f_mapping->host;
  1177. unsigned from, to;
  1178. int ret = 0, ret2;
  1179. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1180. from = pos & (PAGE_CACHE_SIZE - 1);
  1181. to = from + copied;
  1182. ret = walk_page_buffers(handle, page_buffers(page),
  1183. from, to, NULL, journal_dirty_data_fn);
  1184. if (ret == 0)
  1185. update_file_sizes(inode, pos, copied);
  1186. /*
  1187. * There may be allocated blocks outside of i_size because
  1188. * we failed to copy some data. Prepare for truncate.
  1189. */
  1190. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1191. ext3_orphan_add(handle, inode);
  1192. ret2 = ext3_journal_stop(handle);
  1193. if (!ret)
  1194. ret = ret2;
  1195. unlock_page(page);
  1196. page_cache_release(page);
  1197. if (pos + len > inode->i_size)
  1198. ext3_truncate_failed_write(inode);
  1199. return ret ? ret : copied;
  1200. }
  1201. static int ext3_writeback_write_end(struct file *file,
  1202. struct address_space *mapping,
  1203. loff_t pos, unsigned len, unsigned copied,
  1204. struct page *page, void *fsdata)
  1205. {
  1206. handle_t *handle = ext3_journal_current_handle();
  1207. struct inode *inode = file->f_mapping->host;
  1208. int ret;
  1209. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1210. update_file_sizes(inode, pos, copied);
  1211. /*
  1212. * There may be allocated blocks outside of i_size because
  1213. * we failed to copy some data. Prepare for truncate.
  1214. */
  1215. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1216. ext3_orphan_add(handle, inode);
  1217. ret = ext3_journal_stop(handle);
  1218. unlock_page(page);
  1219. page_cache_release(page);
  1220. if (pos + len > inode->i_size)
  1221. ext3_truncate_failed_write(inode);
  1222. return ret ? ret : copied;
  1223. }
  1224. static int ext3_journalled_write_end(struct file *file,
  1225. struct address_space *mapping,
  1226. loff_t pos, unsigned len, unsigned copied,
  1227. struct page *page, void *fsdata)
  1228. {
  1229. handle_t *handle = ext3_journal_current_handle();
  1230. struct inode *inode = mapping->host;
  1231. int ret = 0, ret2;
  1232. int partial = 0;
  1233. unsigned from, to;
  1234. from = pos & (PAGE_CACHE_SIZE - 1);
  1235. to = from + len;
  1236. if (copied < len) {
  1237. if (!PageUptodate(page))
  1238. copied = 0;
  1239. page_zero_new_buffers(page, from + copied, to);
  1240. to = from + copied;
  1241. }
  1242. ret = walk_page_buffers(handle, page_buffers(page), from,
  1243. to, &partial, write_end_fn);
  1244. if (!partial)
  1245. SetPageUptodate(page);
  1246. if (pos + copied > inode->i_size)
  1247. i_size_write(inode, pos + copied);
  1248. /*
  1249. * There may be allocated blocks outside of i_size because
  1250. * we failed to copy some data. Prepare for truncate.
  1251. */
  1252. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1253. ext3_orphan_add(handle, inode);
  1254. ext3_set_inode_state(inode, EXT3_STATE_JDATA);
  1255. if (inode->i_size > EXT3_I(inode)->i_disksize) {
  1256. EXT3_I(inode)->i_disksize = inode->i_size;
  1257. ret2 = ext3_mark_inode_dirty(handle, inode);
  1258. if (!ret)
  1259. ret = ret2;
  1260. }
  1261. ret2 = ext3_journal_stop(handle);
  1262. if (!ret)
  1263. ret = ret2;
  1264. unlock_page(page);
  1265. page_cache_release(page);
  1266. if (pos + len > inode->i_size)
  1267. ext3_truncate_failed_write(inode);
  1268. return ret ? ret : copied;
  1269. }
  1270. /*
  1271. * bmap() is special. It gets used by applications such as lilo and by
  1272. * the swapper to find the on-disk block of a specific piece of data.
  1273. *
  1274. * Naturally, this is dangerous if the block concerned is still in the
  1275. * journal. If somebody makes a swapfile on an ext3 data-journaling
  1276. * filesystem and enables swap, then they may get a nasty shock when the
  1277. * data getting swapped to that swapfile suddenly gets overwritten by
  1278. * the original zero's written out previously to the journal and
  1279. * awaiting writeback in the kernel's buffer cache.
  1280. *
  1281. * So, if we see any bmap calls here on a modified, data-journaled file,
  1282. * take extra steps to flush any blocks which might be in the cache.
  1283. */
  1284. static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
  1285. {
  1286. struct inode *inode = mapping->host;
  1287. journal_t *journal;
  1288. int err;
  1289. if (ext3_test_inode_state(inode, EXT3_STATE_JDATA)) {
  1290. /*
  1291. * This is a REALLY heavyweight approach, but the use of
  1292. * bmap on dirty files is expected to be extremely rare:
  1293. * only if we run lilo or swapon on a freshly made file
  1294. * do we expect this to happen.
  1295. *
  1296. * (bmap requires CAP_SYS_RAWIO so this does not
  1297. * represent an unprivileged user DOS attack --- we'd be
  1298. * in trouble if mortal users could trigger this path at
  1299. * will.)
  1300. *
  1301. * NB. EXT3_STATE_JDATA is not set on files other than
  1302. * regular files. If somebody wants to bmap a directory
  1303. * or symlink and gets confused because the buffer
  1304. * hasn't yet been flushed to disk, they deserve
  1305. * everything they get.
  1306. */
  1307. ext3_clear_inode_state(inode, EXT3_STATE_JDATA);
  1308. journal = EXT3_JOURNAL(inode);
  1309. journal_lock_updates(journal);
  1310. err = journal_flush(journal);
  1311. journal_unlock_updates(journal);
  1312. if (err)
  1313. return 0;
  1314. }
  1315. return generic_block_bmap(mapping,block,ext3_get_block);
  1316. }
  1317. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1318. {
  1319. get_bh(bh);
  1320. return 0;
  1321. }
  1322. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1323. {
  1324. put_bh(bh);
  1325. return 0;
  1326. }
  1327. static int buffer_unmapped(handle_t *handle, struct buffer_head *bh)
  1328. {
  1329. return !buffer_mapped(bh);
  1330. }
  1331. /*
  1332. * Note that we always start a transaction even if we're not journalling
  1333. * data. This is to preserve ordering: any hole instantiation within
  1334. * __block_write_full_page -> ext3_get_block() should be journalled
  1335. * along with the data so we don't crash and then get metadata which
  1336. * refers to old data.
  1337. *
  1338. * In all journalling modes block_write_full_page() will start the I/O.
  1339. *
  1340. * Problem:
  1341. *
  1342. * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  1343. * ext3_writepage()
  1344. *
  1345. * Similar for:
  1346. *
  1347. * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
  1348. *
  1349. * Same applies to ext3_get_block(). We will deadlock on various things like
  1350. * lock_journal and i_truncate_mutex.
  1351. *
  1352. * Setting PF_MEMALLOC here doesn't work - too many internal memory
  1353. * allocations fail.
  1354. *
  1355. * 16May01: If we're reentered then journal_current_handle() will be
  1356. * non-zero. We simply *return*.
  1357. *
  1358. * 1 July 2001: @@@ FIXME:
  1359. * In journalled data mode, a data buffer may be metadata against the
  1360. * current transaction. But the same file is part of a shared mapping
  1361. * and someone does a writepage() on it.
  1362. *
  1363. * We will move the buffer onto the async_data list, but *after* it has
  1364. * been dirtied. So there's a small window where we have dirty data on
  1365. * BJ_Metadata.
  1366. *
  1367. * Note that this only applies to the last partial page in the file. The
  1368. * bit which block_write_full_page() uses prepare/commit for. (That's
  1369. * broken code anyway: it's wrong for msync()).
  1370. *
  1371. * It's a rare case: affects the final partial page, for journalled data
  1372. * where the file is subject to bith write() and writepage() in the same
  1373. * transction. To fix it we'll need a custom block_write_full_page().
  1374. * We'll probably need that anyway for journalling writepage() output.
  1375. *
  1376. * We don't honour synchronous mounts for writepage(). That would be
  1377. * disastrous. Any write() or metadata operation will sync the fs for
  1378. * us.
  1379. *
  1380. * AKPM2: if all the page's buffers are mapped to disk and !data=journal,
  1381. * we don't need to open a transaction here.
  1382. */
  1383. static int ext3_ordered_writepage(struct page *page,
  1384. struct writeback_control *wbc)
  1385. {
  1386. struct inode *inode = page->mapping->host;
  1387. struct buffer_head *page_bufs;
  1388. handle_t *handle = NULL;
  1389. int ret = 0;
  1390. int err;
  1391. J_ASSERT(PageLocked(page));
  1392. WARN_ON_ONCE(IS_RDONLY(inode));
  1393. /*
  1394. * We give up here if we're reentered, because it might be for a
  1395. * different filesystem.
  1396. */
  1397. if (ext3_journal_current_handle())
  1398. goto out_fail;
  1399. if (!page_has_buffers(page)) {
  1400. create_empty_buffers(page, inode->i_sb->s_blocksize,
  1401. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1402. page_bufs = page_buffers(page);
  1403. } else {
  1404. page_bufs = page_buffers(page);
  1405. if (!walk_page_buffers(NULL, page_bufs, 0, PAGE_CACHE_SIZE,
  1406. NULL, buffer_unmapped)) {
  1407. /* Provide NULL get_block() to catch bugs if buffers
  1408. * weren't really mapped */
  1409. return block_write_full_page(page, NULL, wbc);
  1410. }
  1411. }
  1412. handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
  1413. if (IS_ERR(handle)) {
  1414. ret = PTR_ERR(handle);
  1415. goto out_fail;
  1416. }
  1417. walk_page_buffers(handle, page_bufs, 0,
  1418. PAGE_CACHE_SIZE, NULL, bget_one);
  1419. ret = block_write_full_page(page, ext3_get_block, wbc);
  1420. /*
  1421. * The page can become unlocked at any point now, and
  1422. * truncate can then come in and change things. So we
  1423. * can't touch *page from now on. But *page_bufs is
  1424. * safe due to elevated refcount.
  1425. */
  1426. /*
  1427. * And attach them to the current transaction. But only if
  1428. * block_write_full_page() succeeded. Otherwise they are unmapped,
  1429. * and generally junk.
  1430. */
  1431. if (ret == 0) {
  1432. err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
  1433. NULL, journal_dirty_data_fn);
  1434. if (!ret)
  1435. ret = err;
  1436. }
  1437. walk_page_buffers(handle, page_bufs, 0,
  1438. PAGE_CACHE_SIZE, NULL, bput_one);
  1439. err = ext3_journal_stop(handle);
  1440. if (!ret)
  1441. ret = err;
  1442. return ret;
  1443. out_fail:
  1444. redirty_page_for_writepage(wbc, page);
  1445. unlock_page(page);
  1446. return ret;
  1447. }
  1448. static int ext3_writeback_writepage(struct page *page,
  1449. struct writeback_control *wbc)
  1450. {
  1451. struct inode *inode = page->mapping->host;
  1452. handle_t *handle = NULL;
  1453. int ret = 0;
  1454. int err;
  1455. J_ASSERT(PageLocked(page));
  1456. WARN_ON_ONCE(IS_RDONLY(inode));
  1457. if (ext3_journal_current_handle())
  1458. goto out_fail;
  1459. if (page_has_buffers(page)) {
  1460. if (!walk_page_buffers(NULL, page_buffers(page), 0,
  1461. PAGE_CACHE_SIZE, NULL, buffer_unmapped)) {
  1462. /* Provide NULL get_block() to catch bugs if buffers
  1463. * weren't really mapped */
  1464. return block_write_full_page(page, NULL, wbc);
  1465. }
  1466. }
  1467. handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
  1468. if (IS_ERR(handle)) {
  1469. ret = PTR_ERR(handle);
  1470. goto out_fail;
  1471. }
  1472. if (test_opt(inode->i_sb, NOBH) && ext3_should_writeback_data(inode))
  1473. ret = nobh_writepage(page, ext3_get_block, wbc);
  1474. else
  1475. ret = block_write_full_page(page, ext3_get_block, wbc);
  1476. err = ext3_journal_stop(handle);
  1477. if (!ret)
  1478. ret = err;
  1479. return ret;
  1480. out_fail:
  1481. redirty_page_for_writepage(wbc, page);
  1482. unlock_page(page);
  1483. return ret;
  1484. }
  1485. static int ext3_journalled_writepage(struct page *page,
  1486. struct writeback_control *wbc)
  1487. {
  1488. struct inode *inode = page->mapping->host;
  1489. handle_t *handle = NULL;
  1490. int ret = 0;
  1491. int err;
  1492. J_ASSERT(PageLocked(page));
  1493. WARN_ON_ONCE(IS_RDONLY(inode));
  1494. if (ext3_journal_current_handle())
  1495. goto no_write;
  1496. handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
  1497. if (IS_ERR(handle)) {
  1498. ret = PTR_ERR(handle);
  1499. goto no_write;
  1500. }
  1501. if (!page_has_buffers(page) || PageChecked(page)) {
  1502. /*
  1503. * It's mmapped pagecache. Add buffers and journal it. There
  1504. * doesn't seem much point in redirtying the page here.
  1505. */
  1506. ClearPageChecked(page);
  1507. ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
  1508. ext3_get_block);
  1509. if (ret != 0) {
  1510. ext3_journal_stop(handle);
  1511. goto out_unlock;
  1512. }
  1513. ret = walk_page_buffers(handle, page_buffers(page), 0,
  1514. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
  1515. err = walk_page_buffers(handle, page_buffers(page), 0,
  1516. PAGE_CACHE_SIZE, NULL, write_end_fn);
  1517. if (ret == 0)
  1518. ret = err;
  1519. ext3_set_inode_state(inode, EXT3_STATE_JDATA);
  1520. unlock_page(page);
  1521. } else {
  1522. /*
  1523. * It may be a page full of checkpoint-mode buffers. We don't
  1524. * really know unless we go poke around in the buffer_heads.
  1525. * But block_write_full_page will do the right thing.
  1526. */
  1527. ret = block_write_full_page(page, ext3_get_block, wbc);
  1528. }
  1529. err = ext3_journal_stop(handle);
  1530. if (!ret)
  1531. ret = err;
  1532. out:
  1533. return ret;
  1534. no_write:
  1535. redirty_page_for_writepage(wbc, page);
  1536. out_unlock:
  1537. unlock_page(page);
  1538. goto out;
  1539. }
  1540. static int ext3_readpage(struct file *file, struct page *page)
  1541. {
  1542. return mpage_readpage(page, ext3_get_block);
  1543. }
  1544. static int
  1545. ext3_readpages(struct file *file, struct address_space *mapping,
  1546. struct list_head *pages, unsigned nr_pages)
  1547. {
  1548. return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
  1549. }
  1550. static void ext3_invalidatepage(struct page *page, unsigned long offset)
  1551. {
  1552. journal_t *journal = EXT3_JOURNAL(page->mapping->host);
  1553. /*
  1554. * If it's a full truncate we just forget about the pending dirtying
  1555. */
  1556. if (offset == 0)
  1557. ClearPageChecked(page);
  1558. journal_invalidatepage(journal, page, offset);
  1559. }
  1560. static int ext3_releasepage(struct page *page, gfp_t wait)
  1561. {
  1562. journal_t *journal = EXT3_JOURNAL(page->mapping->host);
  1563. WARN_ON(PageChecked(page));
  1564. if (!page_has_buffers(page))
  1565. return 0;
  1566. return journal_try_to_free_buffers(journal, page, wait);
  1567. }
  1568. /*
  1569. * If the O_DIRECT write will extend the file then add this inode to the
  1570. * orphan list. So recovery will truncate it back to the original size
  1571. * if the machine crashes during the write.
  1572. *
  1573. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  1574. * crashes then stale disk data _may_ be exposed inside the file. But current
  1575. * VFS code falls back into buffered path in that case so we are safe.
  1576. */
  1577. static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
  1578. const struct iovec *iov, loff_t offset,
  1579. unsigned long nr_segs)
  1580. {
  1581. struct file *file = iocb->ki_filp;
  1582. struct inode *inode = file->f_mapping->host;
  1583. struct ext3_inode_info *ei = EXT3_I(inode);
  1584. handle_t *handle;
  1585. ssize_t ret;
  1586. int orphan = 0;
  1587. size_t count = iov_length(iov, nr_segs);
  1588. int retries = 0;
  1589. if (rw == WRITE) {
  1590. loff_t final_size = offset + count;
  1591. if (final_size > inode->i_size) {
  1592. /* Credits for sb + inode write */
  1593. handle = ext3_journal_start(inode, 2);
  1594. if (IS_ERR(handle)) {
  1595. ret = PTR_ERR(handle);
  1596. goto out;
  1597. }
  1598. ret = ext3_orphan_add(handle, inode);
  1599. if (ret) {
  1600. ext3_journal_stop(handle);
  1601. goto out;
  1602. }
  1603. orphan = 1;
  1604. ei->i_disksize = inode->i_size;
  1605. ext3_journal_stop(handle);
  1606. }
  1607. }
  1608. retry:
  1609. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  1610. offset, nr_segs,
  1611. ext3_get_block, NULL);
  1612. if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  1613. goto retry;
  1614. if (orphan) {
  1615. int err;
  1616. /* Credits for sb + inode write */
  1617. handle = ext3_journal_start(inode, 2);
  1618. if (IS_ERR(handle)) {
  1619. /* This is really bad luck. We've written the data
  1620. * but cannot extend i_size. Truncate allocated blocks
  1621. * and pretend the write failed... */
  1622. ext3_truncate(inode);
  1623. ret = PTR_ERR(handle);
  1624. goto out;
  1625. }
  1626. if (inode->i_nlink)
  1627. ext3_orphan_del(handle, inode);
  1628. if (ret > 0) {
  1629. loff_t end = offset + ret;
  1630. if (end > inode->i_size) {
  1631. ei->i_disksize = end;
  1632. i_size_write(inode, end);
  1633. /*
  1634. * We're going to return a positive `ret'
  1635. * here due to non-zero-length I/O, so there's
  1636. * no way of reporting error returns from
  1637. * ext3_mark_inode_dirty() to userspace. So
  1638. * ignore it.
  1639. */
  1640. ext3_mark_inode_dirty(handle, inode);
  1641. }
  1642. }
  1643. err = ext3_journal_stop(handle);
  1644. if (ret == 0)
  1645. ret = err;
  1646. }
  1647. out:
  1648. return ret;
  1649. }
  1650. /*
  1651. * Pages can be marked dirty completely asynchronously from ext3's journalling
  1652. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  1653. * much here because ->set_page_dirty is called under VFS locks. The page is
  1654. * not necessarily locked.
  1655. *
  1656. * We cannot just dirty the page and leave attached buffers clean, because the
  1657. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  1658. * or jbddirty because all the journalling code will explode.
  1659. *
  1660. * So what we do is to mark the page "pending dirty" and next time writepage
  1661. * is called, propagate that into the buffers appropriately.
  1662. */
  1663. static int ext3_journalled_set_page_dirty(struct page *page)
  1664. {
  1665. SetPageChecked(page);
  1666. return __set_page_dirty_nobuffers(page);
  1667. }
  1668. static const struct address_space_operations ext3_ordered_aops = {
  1669. .readpage = ext3_readpage,
  1670. .readpages = ext3_readpages,
  1671. .writepage = ext3_ordered_writepage,
  1672. .sync_page = block_sync_page,
  1673. .write_begin = ext3_write_begin,
  1674. .write_end = ext3_ordered_write_end,
  1675. .bmap = ext3_bmap,
  1676. .invalidatepage = ext3_invalidatepage,
  1677. .releasepage = ext3_releasepage,
  1678. .direct_IO = ext3_direct_IO,
  1679. .migratepage = buffer_migrate_page,
  1680. .is_partially_uptodate = block_is_partially_uptodate,
  1681. .error_remove_page = generic_error_remove_page,
  1682. };
  1683. static const struct address_space_operations ext3_writeback_aops = {
  1684. .readpage = ext3_readpage,
  1685. .readpages = ext3_readpages,
  1686. .writepage = ext3_writeback_writepage,
  1687. .sync_page = block_sync_page,
  1688. .write_begin = ext3_write_begin,
  1689. .write_end = ext3_writeback_write_end,
  1690. .bmap = ext3_bmap,
  1691. .invalidatepage = ext3_invalidatepage,
  1692. .releasepage = ext3_releasepage,
  1693. .direct_IO = ext3_direct_IO,
  1694. .migratepage = buffer_migrate_page,
  1695. .is_partially_uptodate = block_is_partially_uptodate,
  1696. .error_remove_page = generic_error_remove_page,
  1697. };
  1698. static const struct address_space_operations ext3_journalled_aops = {
  1699. .readpage = ext3_readpage,
  1700. .readpages = ext3_readpages,
  1701. .writepage = ext3_journalled_writepage,
  1702. .sync_page = block_sync_page,
  1703. .write_begin = ext3_write_begin,
  1704. .write_end = ext3_journalled_write_end,
  1705. .set_page_dirty = ext3_journalled_set_page_dirty,
  1706. .bmap = ext3_bmap,
  1707. .invalidatepage = ext3_invalidatepage,
  1708. .releasepage = ext3_releasepage,
  1709. .is_partially_uptodate = block_is_partially_uptodate,
  1710. .error_remove_page = generic_error_remove_page,
  1711. };
  1712. void ext3_set_aops(struct inode *inode)
  1713. {
  1714. if (ext3_should_order_data(inode))
  1715. inode->i_mapping->a_ops = &ext3_ordered_aops;
  1716. else if (ext3_should_writeback_data(inode))
  1717. inode->i_mapping->a_ops = &ext3_writeback_aops;
  1718. else
  1719. inode->i_mapping->a_ops = &ext3_journalled_aops;
  1720. }
  1721. /*
  1722. * ext3_block_truncate_page() zeroes out a mapping from file offset `from'
  1723. * up to the end of the block which corresponds to `from'.
  1724. * This required during truncate. We need to physically zero the tail end
  1725. * of that block so it doesn't yield old data if the file is later grown.
  1726. */
  1727. static int ext3_block_truncate_page(handle_t *handle, struct page *page,
  1728. struct address_space *mapping, loff_t from)
  1729. {
  1730. ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  1731. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  1732. unsigned blocksize, iblock, length, pos;
  1733. struct inode *inode = mapping->host;
  1734. struct buffer_head *bh;
  1735. int err = 0;
  1736. blocksize = inode->i_sb->s_blocksize;
  1737. length = blocksize - (offset & (blocksize - 1));
  1738. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1739. /*
  1740. * For "nobh" option, we can only work if we don't need to
  1741. * read-in the page - otherwise we create buffers to do the IO.
  1742. */
  1743. if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
  1744. ext3_should_writeback_data(inode) && PageUptodate(page)) {
  1745. zero_user(page, offset, length);
  1746. set_page_dirty(page);
  1747. goto unlock;
  1748. }
  1749. if (!page_has_buffers(page))
  1750. create_empty_buffers(page, blocksize, 0);
  1751. /* Find the buffer that contains "offset" */
  1752. bh = page_buffers(page);
  1753. pos = blocksize;
  1754. while (offset >= pos) {
  1755. bh = bh->b_this_page;
  1756. iblock++;
  1757. pos += blocksize;
  1758. }
  1759. err = 0;
  1760. if (buffer_freed(bh)) {
  1761. BUFFER_TRACE(bh, "freed: skip");
  1762. goto unlock;
  1763. }
  1764. if (!buffer_mapped(bh)) {
  1765. BUFFER_TRACE(bh, "unmapped");
  1766. ext3_get_block(inode, iblock, bh, 0);
  1767. /* unmapped? It's a hole - nothing to do */
  1768. if (!buffer_mapped(bh)) {
  1769. BUFFER_TRACE(bh, "still unmapped");
  1770. goto unlock;
  1771. }
  1772. }
  1773. /* Ok, it's mapped. Make sure it's up-to-date */
  1774. if (PageUptodate(page))
  1775. set_buffer_uptodate(bh);
  1776. if (!buffer_uptodate(bh)) {
  1777. err = -EIO;
  1778. ll_rw_block(READ, 1, &bh);
  1779. wait_on_buffer(bh);
  1780. /* Uhhuh. Read error. Complain and punt. */
  1781. if (!buffer_uptodate(bh))
  1782. goto unlock;
  1783. }
  1784. if (ext3_should_journal_data(inode)) {
  1785. BUFFER_TRACE(bh, "get write access");
  1786. err = ext3_journal_get_write_access(handle, bh);
  1787. if (err)
  1788. goto unlock;
  1789. }
  1790. zero_user(page, offset, length);
  1791. BUFFER_TRACE(bh, "zeroed end of block");
  1792. err = 0;
  1793. if (ext3_should_journal_data(inode)) {
  1794. err = ext3_journal_dirty_metadata(handle, bh);
  1795. } else {
  1796. if (ext3_should_order_data(inode))
  1797. err = ext3_journal_dirty_data(handle, bh);
  1798. mark_buffer_dirty(bh);
  1799. }
  1800. unlock:
  1801. unlock_page(page);
  1802. page_cache_release(page);
  1803. return err;
  1804. }
  1805. /*
  1806. * Probably it should be a library function... search for first non-zero word
  1807. * or memcmp with zero_page, whatever is better for particular architecture.
  1808. * Linus?
  1809. */
  1810. static inline int all_zeroes(__le32 *p, __le32 *q)
  1811. {
  1812. while (p < q)
  1813. if (*p++)
  1814. return 0;
  1815. return 1;
  1816. }
  1817. /**
  1818. * ext3_find_shared - find the indirect blocks for partial truncation.
  1819. * @inode: inode in question
  1820. * @depth: depth of the affected branch
  1821. * @offsets: offsets of pointers in that branch (see ext3_block_to_path)
  1822. * @chain: place to store the pointers to partial indirect blocks
  1823. * @top: place to the (detached) top of branch
  1824. *
  1825. * This is a helper function used by ext3_truncate().
  1826. *
  1827. * When we do truncate() we may have to clean the ends of several
  1828. * indirect blocks but leave the blocks themselves alive. Block is
  1829. * partially truncated if some data below the new i_size is refered
  1830. * from it (and it is on the path to the first completely truncated
  1831. * data block, indeed). We have to free the top of that path along
  1832. * with everything to the right of the path. Since no allocation
  1833. * past the truncation point is possible until ext3_truncate()
  1834. * finishes, we may safely do the latter, but top of branch may
  1835. * require special attention - pageout below the truncation point
  1836. * might try to populate it.
  1837. *
  1838. * We atomically detach the top of branch from the tree, store the
  1839. * block number of its root in *@top, pointers to buffer_heads of
  1840. * partially truncated blocks - in @chain[].bh and pointers to
  1841. * their last elements that should not be removed - in
  1842. * @chain[].p. Return value is the pointer to last filled element
  1843. * of @chain.
  1844. *
  1845. * The work left to caller to do the actual freeing of subtrees:
  1846. * a) free the subtree starting from *@top
  1847. * b) free the subtrees whose roots are stored in
  1848. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  1849. * c) free the subtrees growing from the inode past the @chain[0].
  1850. * (no partially truncated stuff there). */
  1851. static Indirect *ext3_find_shared(struct inode *inode, int depth,
  1852. int offsets[4], Indirect chain[4], __le32 *top)
  1853. {
  1854. Indirect *partial, *p;
  1855. int k, err;
  1856. *top = 0;
  1857. /* Make k index the deepest non-null offset + 1 */
  1858. for (k = depth; k > 1 && !offsets[k-1]; k--)
  1859. ;
  1860. partial = ext3_get_branch(inode, k, offsets, chain, &err);
  1861. /* Writer: pointers */
  1862. if (!partial)
  1863. partial = chain + k-1;
  1864. /*
  1865. * If the branch acquired continuation since we've looked at it -
  1866. * fine, it should all survive and (new) top doesn't belong to us.
  1867. */
  1868. if (!partial->key && *partial->p)
  1869. /* Writer: end */
  1870. goto no_top;
  1871. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  1872. ;
  1873. /*
  1874. * OK, we've found the last block that must survive. The rest of our
  1875. * branch should be detached before unlocking. However, if that rest
  1876. * of branch is all ours and does not grow immediately from the inode
  1877. * it's easier to cheat and just decrement partial->p.
  1878. */
  1879. if (p == chain + k - 1 && p > chain) {
  1880. p->p--;
  1881. } else {
  1882. *top = *p->p;
  1883. /* Nope, don't do this in ext3. Must leave the tree intact */
  1884. #if 0
  1885. *p->p = 0;
  1886. #endif
  1887. }
  1888. /* Writer: end */
  1889. while(partial > p) {
  1890. brelse(partial->bh);
  1891. partial--;
  1892. }
  1893. no_top:
  1894. return partial;
  1895. }
  1896. /*
  1897. * Zero a number of block pointers in either an inode or an indirect block.
  1898. * If we restart the transaction we must again get write access to the
  1899. * indirect block for further modification.
  1900. *
  1901. * We release `count' blocks on disk, but (last - first) may be greater
  1902. * than `count' because there can be holes in there.
  1903. */
  1904. static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
  1905. struct buffer_head *bh, ext3_fsblk_t block_to_free,
  1906. unsigned long count, __le32 *first, __le32 *last)
  1907. {
  1908. __le32 *p;
  1909. if (try_to_extend_transaction(handle, inode)) {
  1910. if (bh) {
  1911. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  1912. ext3_journal_dirty_metadata(handle, bh);
  1913. }
  1914. ext3_mark_inode_dirty(handle, inode);
  1915. truncate_restart_transaction(handle, inode);
  1916. if (bh) {
  1917. BUFFER_TRACE(bh, "retaking write access");
  1918. ext3_journal_get_write_access(handle, bh);
  1919. }
  1920. }
  1921. /*
  1922. * Any buffers which are on the journal will be in memory. We find
  1923. * them on the hash table so journal_revoke() will run journal_forget()
  1924. * on them. We've already detached each block from the file, so
  1925. * bforget() in journal_forget() should be safe.
  1926. *
  1927. * AKPM: turn on bforget in journal_forget()!!!
  1928. */
  1929. for (p = first; p < last; p++) {
  1930. u32 nr = le32_to_cpu(*p);
  1931. if (nr) {
  1932. struct buffer_head *bh;
  1933. *p = 0;
  1934. bh = sb_find_get_block(inode->i_sb, nr);
  1935. ext3_forget(handle, 0, inode, bh, nr);
  1936. }
  1937. }
  1938. ext3_free_blocks(handle, inode, block_to_free, count);
  1939. }
  1940. /**
  1941. * ext3_free_data - free a list of data blocks
  1942. * @handle: handle for this transaction
  1943. * @inode: inode we are dealing with
  1944. * @this_bh: indirect buffer_head which contains *@first and *@last
  1945. * @first: array of block numbers
  1946. * @last: points immediately past the end of array
  1947. *
  1948. * We are freeing all blocks refered from that array (numbers are stored as
  1949. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  1950. *
  1951. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  1952. * blocks are contiguous then releasing them at one time will only affect one
  1953. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  1954. * actually use a lot of journal space.
  1955. *
  1956. * @this_bh will be %NULL if @first and @last point into the inode's direct
  1957. * block pointers.
  1958. */
  1959. static void ext3_free_data(handle_t *handle, struct inode *inode,
  1960. struct buffer_head *this_bh,
  1961. __le32 *first, __le32 *last)
  1962. {
  1963. ext3_fsblk_t block_to_free = 0; /* Starting block # of a run */
  1964. unsigned long count = 0; /* Number of blocks in the run */
  1965. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  1966. corresponding to
  1967. block_to_free */
  1968. ext3_fsblk_t nr; /* Current block # */
  1969. __le32 *p; /* Pointer into inode/ind
  1970. for current block */
  1971. int err;
  1972. if (this_bh) { /* For indirect block */
  1973. BUFFER_TRACE(this_bh, "get_write_access");
  1974. err = ext3_journal_get_write_access(handle, this_bh);
  1975. /* Important: if we can't update the indirect pointers
  1976. * to the blocks, we can't free them. */
  1977. if (err)
  1978. return;
  1979. }
  1980. for (p = first; p < last; p++) {
  1981. nr = le32_to_cpu(*p);
  1982. if (nr) {
  1983. /* accumulate blocks to free if they're contiguous */
  1984. if (count == 0) {
  1985. block_to_free = nr;
  1986. block_to_free_p = p;
  1987. count = 1;
  1988. } else if (nr == block_to_free + count) {
  1989. count++;
  1990. } else {
  1991. ext3_clear_blocks(handle, inode, this_bh,
  1992. block_to_free,
  1993. count, block_to_free_p, p);
  1994. block_to_free = nr;
  1995. block_to_free_p = p;
  1996. count = 1;
  1997. }
  1998. }
  1999. }
  2000. if (count > 0)
  2001. ext3_clear_blocks(handle, inode, this_bh, block_to_free,
  2002. count, block_to_free_p, p);
  2003. if (this_bh) {
  2004. BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
  2005. /*
  2006. * The buffer head should have an attached journal head at this
  2007. * point. However, if the data is corrupted and an indirect
  2008. * block pointed to itself, it would have been detached when
  2009. * the block was cleared. Check for this instead of OOPSing.
  2010. */
  2011. if (bh2jh(this_bh))
  2012. ext3_journal_dirty_metadata(handle, this_bh);
  2013. else
  2014. ext3_error(inode->i_sb, "ext3_free_data",
  2015. "circular indirect block detected, "
  2016. "inode=%lu, block=%llu",
  2017. inode->i_ino,
  2018. (unsigned long long)this_bh->b_blocknr);
  2019. }
  2020. }
  2021. /**
  2022. * ext3_free_branches - free an array of branches
  2023. * @handle: JBD handle for this transaction
  2024. * @inode: inode we are dealing with
  2025. * @parent_bh: the buffer_head which contains *@first and *@last
  2026. * @first: array of block numbers
  2027. * @last: pointer immediately past the end of array
  2028. * @depth: depth of the branches to free
  2029. *
  2030. * We are freeing all blocks refered from these branches (numbers are
  2031. * stored as little-endian 32-bit) and updating @inode->i_blocks
  2032. * appropriately.
  2033. */
  2034. static void ext3_free_branches(handle_t *handle, struct inode *inode,
  2035. struct buffer_head *parent_bh,
  2036. __le32 *first, __le32 *last, int depth)
  2037. {
  2038. ext3_fsblk_t nr;
  2039. __le32 *p;
  2040. if (is_handle_aborted(handle))
  2041. return;
  2042. if (depth--) {
  2043. struct buffer_head *bh;
  2044. int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
  2045. p = last;
  2046. while (--p >= first) {
  2047. nr = le32_to_cpu(*p);
  2048. if (!nr)
  2049. continue; /* A hole */
  2050. /* Go read the buffer for the next level down */
  2051. bh = sb_bread(inode->i_sb, nr);
  2052. /*
  2053. * A read failure? Report error and clear slot
  2054. * (should be rare).
  2055. */
  2056. if (!bh) {
  2057. ext3_error(inode->i_sb, "ext3_free_branches",
  2058. "Read failure, inode=%lu, block="E3FSBLK,
  2059. inode->i_ino, nr);
  2060. continue;
  2061. }
  2062. /* This zaps the entire block. Bottom up. */
  2063. BUFFER_TRACE(bh, "free child branches");
  2064. ext3_free_branches(handle, inode, bh,
  2065. (__le32*)bh->b_data,
  2066. (__le32*)bh->b_data + addr_per_block,
  2067. depth);
  2068. /*
  2069. * We've probably journalled the indirect block several
  2070. * times during the truncate. But it's no longer
  2071. * needed and we now drop it from the transaction via
  2072. * journal_revoke().
  2073. *
  2074. * That's easy if it's exclusively part of this
  2075. * transaction. But if it's part of the committing
  2076. * transaction then journal_forget() will simply
  2077. * brelse() it. That means that if the underlying
  2078. * block is reallocated in ext3_get_block(),
  2079. * unmap_underlying_metadata() will find this block
  2080. * and will try to get rid of it. damn, damn.
  2081. *
  2082. * If this block has already been committed to the
  2083. * journal, a revoke record will be written. And
  2084. * revoke records must be emitted *before* clearing
  2085. * this block's bit in the bitmaps.
  2086. */
  2087. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  2088. /*
  2089. * Everything below this this pointer has been
  2090. * released. Now let this top-of-subtree go.
  2091. *
  2092. * We want the freeing of this indirect block to be
  2093. * atomic in the journal with the updating of the
  2094. * bitmap block which owns it. So make some room in
  2095. * the journal.
  2096. *
  2097. * We zero the parent pointer *after* freeing its
  2098. * pointee in the bitmaps, so if extend_transaction()
  2099. * for some reason fails to put the bitmap changes and
  2100. * the release into the same transaction, recovery
  2101. * will merely complain about releasing a free block,
  2102. * rather than leaking blocks.
  2103. */
  2104. if (is_handle_aborted(handle))
  2105. return;
  2106. if (try_to_extend_transaction(handle, inode)) {
  2107. ext3_mark_inode_dirty(handle, inode);
  2108. truncate_restart_transaction(handle, inode);
  2109. }
  2110. ext3_free_blocks(handle, inode, nr, 1);
  2111. if (parent_bh) {
  2112. /*
  2113. * The block which we have just freed is
  2114. * pointed to by an indirect block: journal it
  2115. */
  2116. BUFFER_TRACE(parent_bh, "get_write_access");
  2117. if (!ext3_journal_get_write_access(handle,
  2118. parent_bh)){
  2119. *p = 0;
  2120. BUFFER_TRACE(parent_bh,
  2121. "call ext3_journal_dirty_metadata");
  2122. ext3_journal_dirty_metadata(handle,
  2123. parent_bh);
  2124. }
  2125. }
  2126. }
  2127. } else {
  2128. /* We have reached the bottom of the tree. */
  2129. BUFFER_TRACE(parent_bh, "free data blocks");
  2130. ext3_free_data(handle, inode, parent_bh, first, last);
  2131. }
  2132. }
  2133. int ext3_can_truncate(struct inode *inode)
  2134. {
  2135. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2136. return 0;
  2137. if (S_ISREG(inode->i_mode))
  2138. return 1;
  2139. if (S_ISDIR(inode->i_mode))
  2140. return 1;
  2141. if (S_ISLNK(inode->i_mode))
  2142. return !ext3_inode_is_fast_symlink(inode);
  2143. return 0;
  2144. }
  2145. /*
  2146. * ext3_truncate()
  2147. *
  2148. * We block out ext3_get_block() block instantiations across the entire
  2149. * transaction, and VFS/VM ensures that ext3_truncate() cannot run
  2150. * simultaneously on behalf of the same inode.
  2151. *
  2152. * As we work through the truncate and commmit bits of it to the journal there
  2153. * is one core, guiding principle: the file's tree must always be consistent on
  2154. * disk. We must be able to restart the truncate after a crash.
  2155. *
  2156. * The file's tree may be transiently inconsistent in memory (although it
  2157. * probably isn't), but whenever we close off and commit a journal transaction,
  2158. * the contents of (the filesystem + the journal) must be consistent and
  2159. * restartable. It's pretty simple, really: bottom up, right to left (although
  2160. * left-to-right works OK too).
  2161. *
  2162. * Note that at recovery time, journal replay occurs *before* the restart of
  2163. * truncate against the orphan inode list.
  2164. *
  2165. * The committed inode has the new, desired i_size (which is the same as
  2166. * i_disksize in this case). After a crash, ext3_orphan_cleanup() will see
  2167. * that this inode's truncate did not complete and it will again call
  2168. * ext3_truncate() to have another go. So there will be instantiated blocks
  2169. * to the right of the truncation point in a crashed ext3 filesystem. But
  2170. * that's fine - as long as they are linked from the inode, the post-crash
  2171. * ext3_truncate() run will find them and release them.
  2172. */
  2173. void ext3_truncate(struct inode *inode)
  2174. {
  2175. handle_t *handle;
  2176. struct ext3_inode_info *ei = EXT3_I(inode);
  2177. __le32 *i_data = ei->i_data;
  2178. int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
  2179. struct address_space *mapping = inode->i_mapping;
  2180. int offsets[4];
  2181. Indirect chain[4];
  2182. Indirect *partial;
  2183. __le32 nr = 0;
  2184. int n;
  2185. long last_block;
  2186. unsigned blocksize = inode->i_sb->s_blocksize;
  2187. struct page *page;
  2188. if (!ext3_can_truncate(inode))
  2189. goto out_notrans;
  2190. if (inode->i_size == 0 && ext3_should_writeback_data(inode))
  2191. ext3_set_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE);
  2192. /*
  2193. * We have to lock the EOF page here, because lock_page() nests
  2194. * outside journal_start().
  2195. */
  2196. if ((inode->i_size & (blocksize - 1)) == 0) {
  2197. /* Block boundary? Nothing to do */
  2198. page = NULL;
  2199. } else {
  2200. page = grab_cache_page(mapping,
  2201. inode->i_size >> PAGE_CACHE_SHIFT);
  2202. if (!page)
  2203. goto out_notrans;
  2204. }
  2205. handle = start_transaction(inode);
  2206. if (IS_ERR(handle)) {
  2207. if (page) {
  2208. clear_highpage(page);
  2209. flush_dcache_page(page);
  2210. unlock_page(page);
  2211. page_cache_release(page);
  2212. }
  2213. goto out_notrans;
  2214. }
  2215. last_block = (inode->i_size + blocksize-1)
  2216. >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
  2217. if (page)
  2218. ext3_block_truncate_page(handle, page, mapping, inode->i_size);
  2219. n = ext3_block_to_path(inode, last_block, offsets, NULL);
  2220. if (n == 0)
  2221. goto out_stop; /* error */
  2222. /*
  2223. * OK. This truncate is going to happen. We add the inode to the
  2224. * orphan list, so that if this truncate spans multiple transactions,
  2225. * and we crash, we will resume the truncate when the filesystem
  2226. * recovers. It also marks the inode dirty, to catch the new size.
  2227. *
  2228. * Implication: the file must always be in a sane, consistent
  2229. * truncatable state while each transaction commits.
  2230. */
  2231. if (ext3_orphan_add(handle, inode))
  2232. goto out_stop;
  2233. /*
  2234. * The orphan list entry will now protect us from any crash which
  2235. * occurs before the truncate completes, so it is now safe to propagate
  2236. * the new, shorter inode size (held for now in i_size) into the
  2237. * on-disk inode. We do this via i_disksize, which is the value which
  2238. * ext3 *really* writes onto the disk inode.
  2239. */
  2240. ei->i_disksize = inode->i_size;
  2241. /*
  2242. * From here we block out all ext3_get_block() callers who want to
  2243. * modify the block allocation tree.
  2244. */
  2245. mutex_lock(&ei->truncate_mutex);
  2246. if (n == 1) { /* direct blocks */
  2247. ext3_free_data(handle, inode, NULL, i_data+offsets[0],
  2248. i_data + EXT3_NDIR_BLOCKS);
  2249. goto do_indirects;
  2250. }
  2251. partial = ext3_find_shared(inode, n, offsets, chain, &nr);
  2252. /* Kill the top of shared branch (not detached) */
  2253. if (nr) {
  2254. if (partial == chain) {
  2255. /* Shared branch grows from the inode */
  2256. ext3_free_branches(handle, inode, NULL,
  2257. &nr, &nr+1, (chain+n-1) - partial);
  2258. *partial->p = 0;
  2259. /*
  2260. * We mark the inode dirty prior to restart,
  2261. * and prior to stop. No need for it here.
  2262. */
  2263. } else {
  2264. /* Shared branch grows from an indirect block */
  2265. BUFFER_TRACE(partial->bh, "get_write_access");
  2266. ext3_free_branches(handle, inode, partial->bh,
  2267. partial->p,
  2268. partial->p+1, (chain+n-1) - partial);
  2269. }
  2270. }
  2271. /* Clear the ends of indirect blocks on the shared branch */
  2272. while (partial > chain) {
  2273. ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
  2274. (__le32*)partial->bh->b_data+addr_per_block,
  2275. (chain+n-1) - partial);
  2276. BUFFER_TRACE(partial->bh, "call brelse");
  2277. brelse (partial->bh);
  2278. partial--;
  2279. }
  2280. do_indirects:
  2281. /* Kill the remaining (whole) subtrees */
  2282. switch (offsets[0]) {
  2283. default:
  2284. nr = i_data[EXT3_IND_BLOCK];
  2285. if (nr) {
  2286. ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  2287. i_data[EXT3_IND_BLOCK] = 0;
  2288. }
  2289. case EXT3_IND_BLOCK:
  2290. nr = i_data[EXT3_DIND_BLOCK];
  2291. if (nr) {
  2292. ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  2293. i_data[EXT3_DIND_BLOCK] = 0;
  2294. }
  2295. case EXT3_DIND_BLOCK:
  2296. nr = i_data[EXT3_TIND_BLOCK];
  2297. if (nr) {
  2298. ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  2299. i_data[EXT3_TIND_BLOCK] = 0;
  2300. }
  2301. case EXT3_TIND_BLOCK:
  2302. ;
  2303. }
  2304. ext3_discard_reservation(inode);
  2305. mutex_unlock(&ei->truncate_mutex);
  2306. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  2307. ext3_mark_inode_dirty(handle, inode);
  2308. /*
  2309. * In a multi-transaction truncate, we only make the final transaction
  2310. * synchronous
  2311. */
  2312. if (IS_SYNC(inode))
  2313. handle->h_sync = 1;
  2314. out_stop:
  2315. /*
  2316. * If this was a simple ftruncate(), and the file will remain alive
  2317. * then we need to clear up the orphan record which we created above.
  2318. * However, if this was a real unlink then we were called by
  2319. * ext3_delete_inode(), and we allow that function to clean up the
  2320. * orphan info for us.
  2321. */
  2322. if (inode->i_nlink)
  2323. ext3_orphan_del(handle, inode);
  2324. ext3_journal_stop(handle);
  2325. return;
  2326. out_notrans:
  2327. /*
  2328. * Delete the inode from orphan list so that it doesn't stay there
  2329. * forever and trigger assertion on umount.
  2330. */
  2331. if (inode->i_nlink)
  2332. ext3_orphan_del(NULL, inode);
  2333. }
  2334. static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
  2335. unsigned long ino, struct ext3_iloc *iloc)
  2336. {
  2337. unsigned long block_group;
  2338. unsigned long offset;
  2339. ext3_fsblk_t block;
  2340. struct ext3_group_desc *gdp;
  2341. if (!ext3_valid_inum(sb, ino)) {
  2342. /*
  2343. * This error is already checked for in namei.c unless we are
  2344. * looking at an NFS filehandle, in which case no error
  2345. * report is needed
  2346. */
  2347. return 0;
  2348. }
  2349. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  2350. gdp = ext3_get_group_desc(sb, block_group, NULL);
  2351. if (!gdp)
  2352. return 0;
  2353. /*
  2354. * Figure out the offset within the block group inode table
  2355. */
  2356. offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
  2357. EXT3_INODE_SIZE(sb);
  2358. block = le32_to_cpu(gdp->bg_inode_table) +
  2359. (offset >> EXT3_BLOCK_SIZE_BITS(sb));
  2360. iloc->block_group = block_group;
  2361. iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
  2362. return block;
  2363. }
  2364. /*
  2365. * ext3_get_inode_loc returns with an extra refcount against the inode's
  2366. * underlying buffer_head on success. If 'in_mem' is true, we have all
  2367. * data in memory that is needed to recreate the on-disk version of this
  2368. * inode.
  2369. */
  2370. static int __ext3_get_inode_loc(struct inode *inode,
  2371. struct ext3_iloc *iloc, int in_mem)
  2372. {
  2373. ext3_fsblk_t block;
  2374. struct buffer_head *bh;
  2375. block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
  2376. if (!block)
  2377. return -EIO;
  2378. bh = sb_getblk(inode->i_sb, block);
  2379. if (!bh) {
  2380. ext3_error (inode->i_sb, "ext3_get_inode_loc",
  2381. "unable to read inode block - "
  2382. "inode=%lu, block="E3FSBLK,
  2383. inode->i_ino, block);
  2384. return -EIO;
  2385. }
  2386. if (!buffer_uptodate(bh)) {
  2387. lock_buffer(bh);
  2388. /*
  2389. * If the buffer has the write error flag, we have failed
  2390. * to write out another inode in the same block. In this
  2391. * case, we don't have to read the block because we may
  2392. * read the old inode data successfully.
  2393. */
  2394. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  2395. set_buffer_uptodate(bh);
  2396. if (buffer_uptodate(bh)) {
  2397. /* someone brought it uptodate while we waited */
  2398. unlock_buffer(bh);
  2399. goto has_buffer;
  2400. }
  2401. /*
  2402. * If we have all information of the inode in memory and this
  2403. * is the only valid inode in the block, we need not read the
  2404. * block.
  2405. */
  2406. if (in_mem) {
  2407. struct buffer_head *bitmap_bh;
  2408. struct ext3_group_desc *desc;
  2409. int inodes_per_buffer;
  2410. int inode_offset, i;
  2411. int block_group;
  2412. int start;
  2413. block_group = (inode->i_ino - 1) /
  2414. EXT3_INODES_PER_GROUP(inode->i_sb);
  2415. inodes_per_buffer = bh->b_size /
  2416. EXT3_INODE_SIZE(inode->i_sb);
  2417. inode_offset = ((inode->i_ino - 1) %
  2418. EXT3_INODES_PER_GROUP(inode->i_sb));
  2419. start = inode_offset & ~(inodes_per_buffer - 1);
  2420. /* Is the inode bitmap in cache? */
  2421. desc = ext3_get_group_desc(inode->i_sb,
  2422. block_group, NULL);
  2423. if (!desc)
  2424. goto make_io;
  2425. bitmap_bh = sb_getblk(inode->i_sb,
  2426. le32_to_cpu(desc->bg_inode_bitmap));
  2427. if (!bitmap_bh)
  2428. goto make_io;
  2429. /*
  2430. * If the inode bitmap isn't in cache then the
  2431. * optimisation may end up performing two reads instead
  2432. * of one, so skip it.
  2433. */
  2434. if (!buffer_uptodate(bitmap_bh)) {
  2435. brelse(bitmap_bh);
  2436. goto make_io;
  2437. }
  2438. for (i = start; i < start + inodes_per_buffer; i++) {
  2439. if (i == inode_offset)
  2440. continue;
  2441. if (ext3_test_bit(i, bitmap_bh->b_data))
  2442. break;
  2443. }
  2444. brelse(bitmap_bh);
  2445. if (i == start + inodes_per_buffer) {
  2446. /* all other inodes are free, so skip I/O */
  2447. memset(bh->b_data, 0, bh->b_size);
  2448. set_buffer_uptodate(bh);
  2449. unlock_buffer(bh);
  2450. goto has_buffer;
  2451. }
  2452. }
  2453. make_io:
  2454. /*
  2455. * There are other valid inodes in the buffer, this inode
  2456. * has in-inode xattrs, or we don't have this inode in memory.
  2457. * Read the block from disk.
  2458. */
  2459. get_bh(bh);
  2460. bh->b_end_io = end_buffer_read_sync;
  2461. submit_bh(READ_META, bh);
  2462. wait_on_buffer(bh);
  2463. if (!buffer_uptodate(bh)) {
  2464. ext3_error(inode->i_sb, "ext3_get_inode_loc",
  2465. "unable to read inode block - "
  2466. "inode=%lu, block="E3FSBLK,
  2467. inode->i_ino, block);
  2468. brelse(bh);
  2469. return -EIO;
  2470. }
  2471. }
  2472. has_buffer:
  2473. iloc->bh = bh;
  2474. return 0;
  2475. }
  2476. int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
  2477. {
  2478. /* We have all inode data except xattrs in memory here. */
  2479. return __ext3_get_inode_loc(inode, iloc,
  2480. !ext3_test_inode_state(inode, EXT3_STATE_XATTR));
  2481. }
  2482. void ext3_set_inode_flags(struct inode *inode)
  2483. {
  2484. unsigned int flags = EXT3_I(inode)->i_flags;
  2485. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  2486. if (flags & EXT3_SYNC_FL)
  2487. inode->i_flags |= S_SYNC;
  2488. if (flags & EXT3_APPEND_FL)
  2489. inode->i_flags |= S_APPEND;
  2490. if (flags & EXT3_IMMUTABLE_FL)
  2491. inode->i_flags |= S_IMMUTABLE;
  2492. if (flags & EXT3_NOATIME_FL)
  2493. inode->i_flags |= S_NOATIME;
  2494. if (flags & EXT3_DIRSYNC_FL)
  2495. inode->i_flags |= S_DIRSYNC;
  2496. }
  2497. /* Propagate flags from i_flags to EXT3_I(inode)->i_flags */
  2498. void ext3_get_inode_flags(struct ext3_inode_info *ei)
  2499. {
  2500. unsigned int flags = ei->vfs_inode.i_flags;
  2501. ei->i_flags &= ~(EXT3_SYNC_FL|EXT3_APPEND_FL|
  2502. EXT3_IMMUTABLE_FL|EXT3_NOATIME_FL|EXT3_DIRSYNC_FL);
  2503. if (flags & S_SYNC)
  2504. ei->i_flags |= EXT3_SYNC_FL;
  2505. if (flags & S_APPEND)
  2506. ei->i_flags |= EXT3_APPEND_FL;
  2507. if (flags & S_IMMUTABLE)
  2508. ei->i_flags |= EXT3_IMMUTABLE_FL;
  2509. if (flags & S_NOATIME)
  2510. ei->i_flags |= EXT3_NOATIME_FL;
  2511. if (flags & S_DIRSYNC)
  2512. ei->i_flags |= EXT3_DIRSYNC_FL;
  2513. }
  2514. struct inode *ext3_iget(struct super_block *sb, unsigned long ino)
  2515. {
  2516. struct ext3_iloc iloc;
  2517. struct ext3_inode *raw_inode;
  2518. struct ext3_inode_info *ei;
  2519. struct buffer_head *bh;
  2520. struct inode *inode;
  2521. journal_t *journal = EXT3_SB(sb)->s_journal;
  2522. transaction_t *transaction;
  2523. long ret;
  2524. int block;
  2525. inode = iget_locked(sb, ino);
  2526. if (!inode)
  2527. return ERR_PTR(-ENOMEM);
  2528. if (!(inode->i_state & I_NEW))
  2529. return inode;
  2530. ei = EXT3_I(inode);
  2531. ei->i_block_alloc_info = NULL;
  2532. ret = __ext3_get_inode_loc(inode, &iloc, 0);
  2533. if (ret < 0)
  2534. goto bad_inode;
  2535. bh = iloc.bh;
  2536. raw_inode = ext3_raw_inode(&iloc);
  2537. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  2538. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  2539. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  2540. if(!(test_opt (inode->i_sb, NO_UID32))) {
  2541. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  2542. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  2543. }
  2544. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  2545. inode->i_size = le32_to_cpu(raw_inode->i_size);
  2546. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  2547. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  2548. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  2549. inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  2550. ei->i_state_flags = 0;
  2551. ei->i_dir_start_lookup = 0;
  2552. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  2553. /* We now have enough fields to check if the inode was active or not.
  2554. * This is needed because nfsd might try to access dead inodes
  2555. * the test is that same one that e2fsck uses
  2556. * NeilBrown 1999oct15
  2557. */
  2558. if (inode->i_nlink == 0) {
  2559. if (inode->i_mode == 0 ||
  2560. !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
  2561. /* this inode is deleted */
  2562. brelse (bh);
  2563. ret = -ESTALE;
  2564. goto bad_inode;
  2565. }
  2566. /* The only unlinked inodes we let through here have
  2567. * valid i_mode and are being read by the orphan
  2568. * recovery code: that's fine, we're about to complete
  2569. * the process of deleting those. */
  2570. }
  2571. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  2572. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  2573. #ifdef EXT3_FRAGMENTS
  2574. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  2575. ei->i_frag_no = raw_inode->i_frag;
  2576. ei->i_frag_size = raw_inode->i_fsize;
  2577. #endif
  2578. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  2579. if (!S_ISREG(inode->i_mode)) {
  2580. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  2581. } else {
  2582. inode->i_size |=
  2583. ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  2584. }
  2585. ei->i_disksize = inode->i_size;
  2586. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  2587. ei->i_block_group = iloc.block_group;
  2588. /*
  2589. * NOTE! The in-memory inode i_data array is in little-endian order
  2590. * even on big-endian machines: we do NOT byteswap the block numbers!
  2591. */
  2592. for (block = 0; block < EXT3_N_BLOCKS; block++)
  2593. ei->i_data[block] = raw_inode->i_block[block];
  2594. INIT_LIST_HEAD(&ei->i_orphan);
  2595. /*
  2596. * Set transaction id's of transactions that have to be committed
  2597. * to finish f[data]sync. We set them to currently running transaction
  2598. * as we cannot be sure that the inode or some of its metadata isn't
  2599. * part of the transaction - the inode could have been reclaimed and
  2600. * now it is reread from disk.
  2601. */
  2602. if (journal) {
  2603. tid_t tid;
  2604. spin_lock(&journal->j_state_lock);
  2605. if (journal->j_running_transaction)
  2606. transaction = journal->j_running_transaction;
  2607. else
  2608. transaction = journal->j_committing_transaction;
  2609. if (transaction)
  2610. tid = transaction->t_tid;
  2611. else
  2612. tid = journal->j_commit_sequence;
  2613. spin_unlock(&journal->j_state_lock);
  2614. atomic_set(&ei->i_sync_tid, tid);
  2615. atomic_set(&ei->i_datasync_tid, tid);
  2616. }
  2617. if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
  2618. EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
  2619. /*
  2620. * When mke2fs creates big inodes it does not zero out
  2621. * the unused bytes above EXT3_GOOD_OLD_INODE_SIZE,
  2622. * so ignore those first few inodes.
  2623. */
  2624. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  2625. if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  2626. EXT3_INODE_SIZE(inode->i_sb)) {
  2627. brelse (bh);
  2628. ret = -EIO;
  2629. goto bad_inode;
  2630. }
  2631. if (ei->i_extra_isize == 0) {
  2632. /* The extra space is currently unused. Use it. */
  2633. ei->i_extra_isize = sizeof(struct ext3_inode) -
  2634. EXT3_GOOD_OLD_INODE_SIZE;
  2635. } else {
  2636. __le32 *magic = (void *)raw_inode +
  2637. EXT3_GOOD_OLD_INODE_SIZE +
  2638. ei->i_extra_isize;
  2639. if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
  2640. ext3_set_inode_state(inode, EXT3_STATE_XATTR);
  2641. }
  2642. } else
  2643. ei->i_extra_isize = 0;
  2644. if (S_ISREG(inode->i_mode)) {
  2645. inode->i_op = &ext3_file_inode_operations;
  2646. inode->i_fop = &ext3_file_operations;
  2647. ext3_set_aops(inode);
  2648. } else if (S_ISDIR(inode->i_mode)) {
  2649. inode->i_op = &ext3_dir_inode_operations;
  2650. inode->i_fop = &ext3_dir_operations;
  2651. } else if (S_ISLNK(inode->i_mode)) {
  2652. if (ext3_inode_is_fast_symlink(inode)) {
  2653. inode->i_op = &ext3_fast_symlink_inode_operations;
  2654. nd_terminate_link(ei->i_data, inode->i_size,
  2655. sizeof(ei->i_data) - 1);
  2656. } else {
  2657. inode->i_op = &ext3_symlink_inode_operations;
  2658. ext3_set_aops(inode);
  2659. }
  2660. } else {
  2661. inode->i_op = &ext3_special_inode_operations;
  2662. if (raw_inode->i_block[0])
  2663. init_special_inode(inode, inode->i_mode,
  2664. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  2665. else
  2666. init_special_inode(inode, inode->i_mode,
  2667. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  2668. }
  2669. brelse (iloc.bh);
  2670. ext3_set_inode_flags(inode);
  2671. unlock_new_inode(inode);
  2672. return inode;
  2673. bad_inode:
  2674. iget_failed(inode);
  2675. return ERR_PTR(ret);
  2676. }
  2677. /*
  2678. * Post the struct inode info into an on-disk inode location in the
  2679. * buffer-cache. This gobbles the caller's reference to the
  2680. * buffer_head in the inode location struct.
  2681. *
  2682. * The caller must have write access to iloc->bh.
  2683. */
  2684. static int ext3_do_update_inode(handle_t *handle,
  2685. struct inode *inode,
  2686. struct ext3_iloc *iloc)
  2687. {
  2688. struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
  2689. struct ext3_inode_info *ei = EXT3_I(inode);
  2690. struct buffer_head *bh = iloc->bh;
  2691. int err = 0, rc, block;
  2692. again:
  2693. /* we can't allow multiple procs in here at once, its a bit racey */
  2694. lock_buffer(bh);
  2695. /* For fields not not tracking in the in-memory inode,
  2696. * initialise them to zero for new inodes. */
  2697. if (ext3_test_inode_state(inode, EXT3_STATE_NEW))
  2698. memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
  2699. ext3_get_inode_flags(ei);
  2700. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  2701. if(!(test_opt(inode->i_sb, NO_UID32))) {
  2702. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
  2703. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
  2704. /*
  2705. * Fix up interoperability with old kernels. Otherwise, old inodes get
  2706. * re-used with the upper 16 bits of the uid/gid intact
  2707. */
  2708. if(!ei->i_dtime) {
  2709. raw_inode->i_uid_high =
  2710. cpu_to_le16(high_16_bits(inode->i_uid));
  2711. raw_inode->i_gid_high =
  2712. cpu_to_le16(high_16_bits(inode->i_gid));
  2713. } else {
  2714. raw_inode->i_uid_high = 0;
  2715. raw_inode->i_gid_high = 0;
  2716. }
  2717. } else {
  2718. raw_inode->i_uid_low =
  2719. cpu_to_le16(fs_high2lowuid(inode->i_uid));
  2720. raw_inode->i_gid_low =
  2721. cpu_to_le16(fs_high2lowgid(inode->i_gid));
  2722. raw_inode->i_uid_high = 0;
  2723. raw_inode->i_gid_high = 0;
  2724. }
  2725. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  2726. raw_inode->i_size = cpu_to_le32(ei->i_disksize);
  2727. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  2728. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  2729. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  2730. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  2731. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  2732. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  2733. #ifdef EXT3_FRAGMENTS
  2734. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  2735. raw_inode->i_frag = ei->i_frag_no;
  2736. raw_inode->i_fsize = ei->i_frag_size;
  2737. #endif
  2738. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  2739. if (!S_ISREG(inode->i_mode)) {
  2740. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  2741. } else {
  2742. raw_inode->i_size_high =
  2743. cpu_to_le32(ei->i_disksize >> 32);
  2744. if (ei->i_disksize > 0x7fffffffULL) {
  2745. struct super_block *sb = inode->i_sb;
  2746. if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
  2747. EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
  2748. EXT3_SB(sb)->s_es->s_rev_level ==
  2749. cpu_to_le32(EXT3_GOOD_OLD_REV)) {
  2750. /* If this is the first large file
  2751. * created, add a flag to the superblock.
  2752. */
  2753. unlock_buffer(bh);
  2754. err = ext3_journal_get_write_access(handle,
  2755. EXT3_SB(sb)->s_sbh);
  2756. if (err)
  2757. goto out_brelse;
  2758. ext3_update_dynamic_rev(sb);
  2759. EXT3_SET_RO_COMPAT_FEATURE(sb,
  2760. EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
  2761. handle->h_sync = 1;
  2762. err = ext3_journal_dirty_metadata(handle,
  2763. EXT3_SB(sb)->s_sbh);
  2764. /* get our lock and start over */
  2765. goto again;
  2766. }
  2767. }
  2768. }
  2769. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  2770. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  2771. if (old_valid_dev(inode->i_rdev)) {
  2772. raw_inode->i_block[0] =
  2773. cpu_to_le32(old_encode_dev(inode->i_rdev));
  2774. raw_inode->i_block[1] = 0;
  2775. } else {
  2776. raw_inode->i_block[0] = 0;
  2777. raw_inode->i_block[1] =
  2778. cpu_to_le32(new_encode_dev(inode->i_rdev));
  2779. raw_inode->i_block[2] = 0;
  2780. }
  2781. } else for (block = 0; block < EXT3_N_BLOCKS; block++)
  2782. raw_inode->i_block[block] = ei->i_data[block];
  2783. if (ei->i_extra_isize)
  2784. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  2785. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  2786. unlock_buffer(bh);
  2787. rc = ext3_journal_dirty_metadata(handle, bh);
  2788. if (!err)
  2789. err = rc;
  2790. ext3_clear_inode_state(inode, EXT3_STATE_NEW);
  2791. atomic_set(&ei->i_sync_tid, handle->h_transaction->t_tid);
  2792. out_brelse:
  2793. brelse (bh);
  2794. ext3_std_error(inode->i_sb, err);
  2795. return err;
  2796. }
  2797. /*
  2798. * ext3_write_inode()
  2799. *
  2800. * We are called from a few places:
  2801. *
  2802. * - Within generic_file_write() for O_SYNC files.
  2803. * Here, there will be no transaction running. We wait for any running
  2804. * trasnaction to commit.
  2805. *
  2806. * - Within sys_sync(), kupdate and such.
  2807. * We wait on commit, if tol to.
  2808. *
  2809. * - Within prune_icache() (PF_MEMALLOC == true)
  2810. * Here we simply return. We can't afford to block kswapd on the
  2811. * journal commit.
  2812. *
  2813. * In all cases it is actually safe for us to return without doing anything,
  2814. * because the inode has been copied into a raw inode buffer in
  2815. * ext3_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  2816. * knfsd.
  2817. *
  2818. * Note that we are absolutely dependent upon all inode dirtiers doing the
  2819. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  2820. * which we are interested.
  2821. *
  2822. * It would be a bug for them to not do this. The code:
  2823. *
  2824. * mark_inode_dirty(inode)
  2825. * stuff();
  2826. * inode->i_size = expr;
  2827. *
  2828. * is in error because a kswapd-driven write_inode() could occur while
  2829. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  2830. * will no longer be on the superblock's dirty inode list.
  2831. */
  2832. int ext3_write_inode(struct inode *inode, struct writeback_control *wbc)
  2833. {
  2834. if (current->flags & PF_MEMALLOC)
  2835. return 0;
  2836. if (ext3_journal_current_handle()) {
  2837. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  2838. dump_stack();
  2839. return -EIO;
  2840. }
  2841. if (wbc->sync_mode != WB_SYNC_ALL)
  2842. return 0;
  2843. return ext3_force_commit(inode->i_sb);
  2844. }
  2845. /*
  2846. * ext3_setattr()
  2847. *
  2848. * Called from notify_change.
  2849. *
  2850. * We want to trap VFS attempts to truncate the file as soon as
  2851. * possible. In particular, we want to make sure that when the VFS
  2852. * shrinks i_size, we put the inode on the orphan list and modify
  2853. * i_disksize immediately, so that during the subsequent flushing of
  2854. * dirty pages and freeing of disk blocks, we can guarantee that any
  2855. * commit will leave the blocks being flushed in an unused state on
  2856. * disk. (On recovery, the inode will get truncated and the blocks will
  2857. * be freed, so we have a strong guarantee that no future commit will
  2858. * leave these blocks visible to the user.)
  2859. *
  2860. * Called with inode->sem down.
  2861. */
  2862. int ext3_setattr(struct dentry *dentry, struct iattr *attr)
  2863. {
  2864. struct inode *inode = dentry->d_inode;
  2865. int error, rc = 0;
  2866. const unsigned int ia_valid = attr->ia_valid;
  2867. error = inode_change_ok(inode, attr);
  2868. if (error)
  2869. return error;
  2870. if (is_quota_modification(inode, attr))
  2871. dquot_initialize(inode);
  2872. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  2873. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  2874. handle_t *handle;
  2875. /* (user+group)*(old+new) structure, inode write (sb,
  2876. * inode block, ? - but truncate inode update has it) */
  2877. handle = ext3_journal_start(inode, EXT3_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
  2878. EXT3_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)+3);
  2879. if (IS_ERR(handle)) {
  2880. error = PTR_ERR(handle);
  2881. goto err_out;
  2882. }
  2883. error = dquot_transfer(inode, attr);
  2884. if (error) {
  2885. ext3_journal_stop(handle);
  2886. return error;
  2887. }
  2888. /* Update corresponding info in inode so that everything is in
  2889. * one transaction */
  2890. if (attr->ia_valid & ATTR_UID)
  2891. inode->i_uid = attr->ia_uid;
  2892. if (attr->ia_valid & ATTR_GID)
  2893. inode->i_gid = attr->ia_gid;
  2894. error = ext3_mark_inode_dirty(handle, inode);
  2895. ext3_journal_stop(handle);
  2896. }
  2897. if (S_ISREG(inode->i_mode) &&
  2898. attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
  2899. handle_t *handle;
  2900. handle = ext3_journal_start(inode, 3);
  2901. if (IS_ERR(handle)) {
  2902. error = PTR_ERR(handle);
  2903. goto err_out;
  2904. }
  2905. error = ext3_orphan_add(handle, inode);
  2906. EXT3_I(inode)->i_disksize = attr->ia_size;
  2907. rc = ext3_mark_inode_dirty(handle, inode);
  2908. if (!error)
  2909. error = rc;
  2910. ext3_journal_stop(handle);
  2911. }
  2912. rc = inode_setattr(inode, attr);
  2913. if (!rc && (ia_valid & ATTR_MODE))
  2914. rc = ext3_acl_chmod(inode);
  2915. err_out:
  2916. ext3_std_error(inode->i_sb, error);
  2917. if (!error)
  2918. error = rc;
  2919. return error;
  2920. }
  2921. /*
  2922. * How many blocks doth make a writepage()?
  2923. *
  2924. * With N blocks per page, it may be:
  2925. * N data blocks
  2926. * 2 indirect block
  2927. * 2 dindirect
  2928. * 1 tindirect
  2929. * N+5 bitmap blocks (from the above)
  2930. * N+5 group descriptor summary blocks
  2931. * 1 inode block
  2932. * 1 superblock.
  2933. * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quote files
  2934. *
  2935. * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
  2936. *
  2937. * With ordered or writeback data it's the same, less the N data blocks.
  2938. *
  2939. * If the inode's direct blocks can hold an integral number of pages then a
  2940. * page cannot straddle two indirect blocks, and we can only touch one indirect
  2941. * and dindirect block, and the "5" above becomes "3".
  2942. *
  2943. * This still overestimates under most circumstances. If we were to pass the
  2944. * start and end offsets in here as well we could do block_to_path() on each
  2945. * block and work out the exact number of indirects which are touched. Pah.
  2946. */
  2947. static int ext3_writepage_trans_blocks(struct inode *inode)
  2948. {
  2949. int bpp = ext3_journal_blocks_per_page(inode);
  2950. int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
  2951. int ret;
  2952. if (ext3_should_journal_data(inode))
  2953. ret = 3 * (bpp + indirects) + 2;
  2954. else
  2955. ret = 2 * (bpp + indirects) + 2;
  2956. #ifdef CONFIG_QUOTA
  2957. /* We know that structure was already allocated during dquot_initialize so
  2958. * we will be updating only the data blocks + inodes */
  2959. ret += EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  2960. #endif
  2961. return ret;
  2962. }
  2963. /*
  2964. * The caller must have previously called ext3_reserve_inode_write().
  2965. * Give this, we know that the caller already has write access to iloc->bh.
  2966. */
  2967. int ext3_mark_iloc_dirty(handle_t *handle,
  2968. struct inode *inode, struct ext3_iloc *iloc)
  2969. {
  2970. int err = 0;
  2971. /* the do_update_inode consumes one bh->b_count */
  2972. get_bh(iloc->bh);
  2973. /* ext3_do_update_inode() does journal_dirty_metadata */
  2974. err = ext3_do_update_inode(handle, inode, iloc);
  2975. put_bh(iloc->bh);
  2976. return err;
  2977. }
  2978. /*
  2979. * On success, We end up with an outstanding reference count against
  2980. * iloc->bh. This _must_ be cleaned up later.
  2981. */
  2982. int
  2983. ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
  2984. struct ext3_iloc *iloc)
  2985. {
  2986. int err = 0;
  2987. if (handle) {
  2988. err = ext3_get_inode_loc(inode, iloc);
  2989. if (!err) {
  2990. BUFFER_TRACE(iloc->bh, "get_write_access");
  2991. err = ext3_journal_get_write_access(handle, iloc->bh);
  2992. if (err) {
  2993. brelse(iloc->bh);
  2994. iloc->bh = NULL;
  2995. }
  2996. }
  2997. }
  2998. ext3_std_error(inode->i_sb, err);
  2999. return err;
  3000. }
  3001. /*
  3002. * What we do here is to mark the in-core inode as clean with respect to inode
  3003. * dirtiness (it may still be data-dirty).
  3004. * This means that the in-core inode may be reaped by prune_icache
  3005. * without having to perform any I/O. This is a very good thing,
  3006. * because *any* task may call prune_icache - even ones which
  3007. * have a transaction open against a different journal.
  3008. *
  3009. * Is this cheating? Not really. Sure, we haven't written the
  3010. * inode out, but prune_icache isn't a user-visible syncing function.
  3011. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  3012. * we start and wait on commits.
  3013. *
  3014. * Is this efficient/effective? Well, we're being nice to the system
  3015. * by cleaning up our inodes proactively so they can be reaped
  3016. * without I/O. But we are potentially leaving up to five seconds'
  3017. * worth of inodes floating about which prune_icache wants us to
  3018. * write out. One way to fix that would be to get prune_icache()
  3019. * to do a write_super() to free up some memory. It has the desired
  3020. * effect.
  3021. */
  3022. int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
  3023. {
  3024. struct ext3_iloc iloc;
  3025. int err;
  3026. might_sleep();
  3027. err = ext3_reserve_inode_write(handle, inode, &iloc);
  3028. if (!err)
  3029. err = ext3_mark_iloc_dirty(handle, inode, &iloc);
  3030. return err;
  3031. }
  3032. /*
  3033. * ext3_dirty_inode() is called from __mark_inode_dirty()
  3034. *
  3035. * We're really interested in the case where a file is being extended.
  3036. * i_size has been changed by generic_commit_write() and we thus need
  3037. * to include the updated inode in the current transaction.
  3038. *
  3039. * Also, dquot_alloc_space() will always dirty the inode when blocks
  3040. * are allocated to the file.
  3041. *
  3042. * If the inode is marked synchronous, we don't honour that here - doing
  3043. * so would cause a commit on atime updates, which we don't bother doing.
  3044. * We handle synchronous inodes at the highest possible level.
  3045. */
  3046. void ext3_dirty_inode(struct inode *inode)
  3047. {
  3048. handle_t *current_handle = ext3_journal_current_handle();
  3049. handle_t *handle;
  3050. handle = ext3_journal_start(inode, 2);
  3051. if (IS_ERR(handle))
  3052. goto out;
  3053. if (current_handle &&
  3054. current_handle->h_transaction != handle->h_transaction) {
  3055. /* This task has a transaction open against a different fs */
  3056. printk(KERN_EMERG "%s: transactions do not match!\n",
  3057. __func__);
  3058. } else {
  3059. jbd_debug(5, "marking dirty. outer handle=%p\n",
  3060. current_handle);
  3061. ext3_mark_inode_dirty(handle, inode);
  3062. }
  3063. ext3_journal_stop(handle);
  3064. out:
  3065. return;
  3066. }
  3067. #if 0
  3068. /*
  3069. * Bind an inode's backing buffer_head into this transaction, to prevent
  3070. * it from being flushed to disk early. Unlike
  3071. * ext3_reserve_inode_write, this leaves behind no bh reference and
  3072. * returns no iloc structure, so the caller needs to repeat the iloc
  3073. * lookup to mark the inode dirty later.
  3074. */
  3075. static int ext3_pin_inode(handle_t *handle, struct inode *inode)
  3076. {
  3077. struct ext3_iloc iloc;
  3078. int err = 0;
  3079. if (handle) {
  3080. err = ext3_get_inode_loc(inode, &iloc);
  3081. if (!err) {
  3082. BUFFER_TRACE(iloc.bh, "get_write_access");
  3083. err = journal_get_write_access(handle, iloc.bh);
  3084. if (!err)
  3085. err = ext3_journal_dirty_metadata(handle,
  3086. iloc.bh);
  3087. brelse(iloc.bh);
  3088. }
  3089. }
  3090. ext3_std_error(inode->i_sb, err);
  3091. return err;
  3092. }
  3093. #endif
  3094. int ext3_change_inode_journal_flag(struct inode *inode, int val)
  3095. {
  3096. journal_t *journal;
  3097. handle_t *handle;
  3098. int err;
  3099. /*
  3100. * We have to be very careful here: changing a data block's
  3101. * journaling status dynamically is dangerous. If we write a
  3102. * data block to the journal, change the status and then delete
  3103. * that block, we risk forgetting to revoke the old log record
  3104. * from the journal and so a subsequent replay can corrupt data.
  3105. * So, first we make sure that the journal is empty and that
  3106. * nobody is changing anything.
  3107. */
  3108. journal = EXT3_JOURNAL(inode);
  3109. if (is_journal_aborted(journal))
  3110. return -EROFS;
  3111. journal_lock_updates(journal);
  3112. journal_flush(journal);
  3113. /*
  3114. * OK, there are no updates running now, and all cached data is
  3115. * synced to disk. We are now in a completely consistent state
  3116. * which doesn't have anything in the journal, and we know that
  3117. * no filesystem updates are running, so it is safe to modify
  3118. * the inode's in-core data-journaling state flag now.
  3119. */
  3120. if (val)
  3121. EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
  3122. else
  3123. EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
  3124. ext3_set_aops(inode);
  3125. journal_unlock_updates(journal);
  3126. /* Finally we can mark the inode as dirty. */
  3127. handle = ext3_journal_start(inode, 1);
  3128. if (IS_ERR(handle))
  3129. return PTR_ERR(handle);
  3130. err = ext3_mark_inode_dirty(handle, inode);
  3131. handle->h_sync = 1;
  3132. ext3_journal_stop(handle);
  3133. ext3_std_error(inode->i_sb, err);
  3134. return err;
  3135. }