inode.c 97 KB

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