inode.c 89 KB

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