inode.c 97 KB

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