extents.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/time.h>
  33. #include <linux/jbd2.h>
  34. #include <linux/highuid.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/quotaops.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include <linux/falloc.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/fiemap.h>
  42. #include "ext4_jbd2.h"
  43. #include "ext4_extents.h"
  44. /*
  45. * ext_pblock:
  46. * combine low and high parts of physical block number into ext4_fsblk_t
  47. */
  48. static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
  49. {
  50. ext4_fsblk_t block;
  51. block = le32_to_cpu(ex->ee_start_lo);
  52. block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
  53. return block;
  54. }
  55. /*
  56. * idx_pblock:
  57. * combine low and high parts of a leaf physical block number into ext4_fsblk_t
  58. */
  59. ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
  60. {
  61. ext4_fsblk_t block;
  62. block = le32_to_cpu(ix->ei_leaf_lo);
  63. block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
  64. return block;
  65. }
  66. /*
  67. * ext4_ext_store_pblock:
  68. * stores a large physical block number into an extent struct,
  69. * breaking it into parts
  70. */
  71. void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
  72. {
  73. ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  74. ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  75. }
  76. /*
  77. * ext4_idx_store_pblock:
  78. * stores a large physical block number into an index struct,
  79. * breaking it into parts
  80. */
  81. static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
  82. {
  83. ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  84. ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  85. }
  86. static int ext4_ext_journal_restart(handle_t *handle, int needed)
  87. {
  88. int err;
  89. if (!ext4_handle_valid(handle))
  90. return 0;
  91. if (handle->h_buffer_credits > needed)
  92. return 0;
  93. err = ext4_journal_extend(handle, needed);
  94. if (err <= 0)
  95. return err;
  96. return ext4_journal_restart(handle, needed);
  97. }
  98. /*
  99. * could return:
  100. * - EROFS
  101. * - ENOMEM
  102. */
  103. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  104. struct ext4_ext_path *path)
  105. {
  106. if (path->p_bh) {
  107. /* path points to block */
  108. return ext4_journal_get_write_access(handle, path->p_bh);
  109. }
  110. /* path points to leaf/index in inode body */
  111. /* we use in-core data, no need to protect them */
  112. return 0;
  113. }
  114. /*
  115. * could return:
  116. * - EROFS
  117. * - ENOMEM
  118. * - EIO
  119. */
  120. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  121. struct ext4_ext_path *path)
  122. {
  123. int err;
  124. if (path->p_bh) {
  125. /* path points to block */
  126. err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
  127. } else {
  128. /* path points to leaf/index in inode body */
  129. err = ext4_mark_inode_dirty(handle, inode);
  130. }
  131. return err;
  132. }
  133. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  134. struct ext4_ext_path *path,
  135. ext4_lblk_t block)
  136. {
  137. struct ext4_inode_info *ei = EXT4_I(inode);
  138. ext4_fsblk_t bg_start;
  139. ext4_fsblk_t last_block;
  140. ext4_grpblk_t colour;
  141. ext4_group_t block_group;
  142. int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
  143. int depth;
  144. if (path) {
  145. struct ext4_extent *ex;
  146. depth = path->p_depth;
  147. /* try to predict block placement */
  148. ex = path[depth].p_ext;
  149. if (ex)
  150. return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
  151. /* it looks like index is empty;
  152. * try to find starting block from index itself */
  153. if (path[depth].p_bh)
  154. return path[depth].p_bh->b_blocknr;
  155. }
  156. /* OK. use inode's group */
  157. block_group = ei->i_block_group;
  158. if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
  159. /*
  160. * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
  161. * block groups per flexgroup, reserve the first block
  162. * group for directories and special files. Regular
  163. * files will start at the second block group. This
  164. * tends to speed up directory access and improves
  165. * fsck times.
  166. */
  167. block_group &= ~(flex_size-1);
  168. if (S_ISREG(inode->i_mode))
  169. block_group++;
  170. }
  171. bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
  172. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
  173. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  174. /*
  175. * If we are doing delayed allocation, we don't need take
  176. * colour into account.
  177. */
  178. if (test_opt(inode->i_sb, DELALLOC))
  179. return bg_start;
  180. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  181. colour = (current->pid % 16) *
  182. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  183. else
  184. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  185. return bg_start + colour + block;
  186. }
  187. /*
  188. * Allocation for a meta data block
  189. */
  190. static ext4_fsblk_t
  191. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  192. struct ext4_ext_path *path,
  193. struct ext4_extent *ex, int *err)
  194. {
  195. ext4_fsblk_t goal, newblock;
  196. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  197. newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
  198. return newblock;
  199. }
  200. static int ext4_ext_space_block(struct inode *inode)
  201. {
  202. int size;
  203. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  204. / sizeof(struct ext4_extent);
  205. #ifdef AGGRESSIVE_TEST
  206. if (size > 6)
  207. size = 6;
  208. #endif
  209. return size;
  210. }
  211. static int ext4_ext_space_block_idx(struct inode *inode)
  212. {
  213. int size;
  214. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  215. / sizeof(struct ext4_extent_idx);
  216. #ifdef AGGRESSIVE_TEST
  217. if (size > 5)
  218. size = 5;
  219. #endif
  220. return size;
  221. }
  222. static int ext4_ext_space_root(struct inode *inode)
  223. {
  224. int size;
  225. size = sizeof(EXT4_I(inode)->i_data);
  226. size -= sizeof(struct ext4_extent_header);
  227. size /= sizeof(struct ext4_extent);
  228. #ifdef AGGRESSIVE_TEST
  229. if (size > 3)
  230. size = 3;
  231. #endif
  232. return size;
  233. }
  234. static int ext4_ext_space_root_idx(struct inode *inode)
  235. {
  236. int size;
  237. size = sizeof(EXT4_I(inode)->i_data);
  238. size -= sizeof(struct ext4_extent_header);
  239. size /= sizeof(struct ext4_extent_idx);
  240. #ifdef AGGRESSIVE_TEST
  241. if (size > 4)
  242. size = 4;
  243. #endif
  244. return size;
  245. }
  246. /*
  247. * Calculate the number of metadata blocks needed
  248. * to allocate @blocks
  249. * Worse case is one block per extent
  250. */
  251. int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
  252. {
  253. int lcap, icap, rcap, leafs, idxs, num;
  254. int newextents = blocks;
  255. rcap = ext4_ext_space_root_idx(inode);
  256. lcap = ext4_ext_space_block(inode);
  257. icap = ext4_ext_space_block_idx(inode);
  258. /* number of new leaf blocks needed */
  259. num = leafs = (newextents + lcap - 1) / lcap;
  260. /*
  261. * Worse case, we need separate index block(s)
  262. * to link all new leaf blocks
  263. */
  264. idxs = (leafs + icap - 1) / icap;
  265. do {
  266. num += idxs;
  267. idxs = (idxs + icap - 1) / icap;
  268. } while (idxs > rcap);
  269. return num;
  270. }
  271. static int
  272. ext4_ext_max_entries(struct inode *inode, int depth)
  273. {
  274. int max;
  275. if (depth == ext_depth(inode)) {
  276. if (depth == 0)
  277. max = ext4_ext_space_root(inode);
  278. else
  279. max = ext4_ext_space_root_idx(inode);
  280. } else {
  281. if (depth == 0)
  282. max = ext4_ext_space_block(inode);
  283. else
  284. max = ext4_ext_space_block_idx(inode);
  285. }
  286. return max;
  287. }
  288. static int __ext4_ext_check_header(const char *function, struct inode *inode,
  289. struct ext4_extent_header *eh,
  290. int depth)
  291. {
  292. const char *error_msg;
  293. int max = 0;
  294. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  295. error_msg = "invalid magic";
  296. goto corrupted;
  297. }
  298. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  299. error_msg = "unexpected eh_depth";
  300. goto corrupted;
  301. }
  302. if (unlikely(eh->eh_max == 0)) {
  303. error_msg = "invalid eh_max";
  304. goto corrupted;
  305. }
  306. max = ext4_ext_max_entries(inode, depth);
  307. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  308. error_msg = "too large eh_max";
  309. goto corrupted;
  310. }
  311. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  312. error_msg = "invalid eh_entries";
  313. goto corrupted;
  314. }
  315. return 0;
  316. corrupted:
  317. ext4_error(inode->i_sb, function,
  318. "bad header in inode #%lu: %s - magic %x, "
  319. "entries %u, max %u(%u), depth %u(%u)",
  320. inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
  321. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  322. max, le16_to_cpu(eh->eh_depth), depth);
  323. return -EIO;
  324. }
  325. #define ext4_ext_check_header(inode, eh, depth) \
  326. __ext4_ext_check_header(__func__, inode, eh, depth)
  327. #ifdef EXT_DEBUG
  328. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  329. {
  330. int k, l = path->p_depth;
  331. ext_debug("path:");
  332. for (k = 0; k <= l; k++, path++) {
  333. if (path->p_idx) {
  334. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  335. idx_pblock(path->p_idx));
  336. } else if (path->p_ext) {
  337. ext_debug(" %d:%d:%llu ",
  338. le32_to_cpu(path->p_ext->ee_block),
  339. ext4_ext_get_actual_len(path->p_ext),
  340. ext_pblock(path->p_ext));
  341. } else
  342. ext_debug(" []");
  343. }
  344. ext_debug("\n");
  345. }
  346. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  347. {
  348. int depth = ext_depth(inode);
  349. struct ext4_extent_header *eh;
  350. struct ext4_extent *ex;
  351. int i;
  352. if (!path)
  353. return;
  354. eh = path[depth].p_hdr;
  355. ex = EXT_FIRST_EXTENT(eh);
  356. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  357. ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
  358. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  359. }
  360. ext_debug("\n");
  361. }
  362. #else
  363. #define ext4_ext_show_path(inode, path)
  364. #define ext4_ext_show_leaf(inode, path)
  365. #endif
  366. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  367. {
  368. int depth = path->p_depth;
  369. int i;
  370. for (i = 0; i <= depth; i++, path++)
  371. if (path->p_bh) {
  372. brelse(path->p_bh);
  373. path->p_bh = NULL;
  374. }
  375. }
  376. /*
  377. * ext4_ext_binsearch_idx:
  378. * binary search for the closest index of the given block
  379. * the header must be checked before calling this
  380. */
  381. static void
  382. ext4_ext_binsearch_idx(struct inode *inode,
  383. struct ext4_ext_path *path, ext4_lblk_t block)
  384. {
  385. struct ext4_extent_header *eh = path->p_hdr;
  386. struct ext4_extent_idx *r, *l, *m;
  387. ext_debug("binsearch for %u(idx): ", block);
  388. l = EXT_FIRST_INDEX(eh) + 1;
  389. r = EXT_LAST_INDEX(eh);
  390. while (l <= r) {
  391. m = l + (r - l) / 2;
  392. if (block < le32_to_cpu(m->ei_block))
  393. r = m - 1;
  394. else
  395. l = m + 1;
  396. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  397. m, le32_to_cpu(m->ei_block),
  398. r, le32_to_cpu(r->ei_block));
  399. }
  400. path->p_idx = l - 1;
  401. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  402. idx_pblock(path->p_idx));
  403. #ifdef CHECK_BINSEARCH
  404. {
  405. struct ext4_extent_idx *chix, *ix;
  406. int k;
  407. chix = ix = EXT_FIRST_INDEX(eh);
  408. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  409. if (k != 0 &&
  410. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  411. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  412. "first=0x%p\n", k,
  413. ix, EXT_FIRST_INDEX(eh));
  414. printk(KERN_DEBUG "%u <= %u\n",
  415. le32_to_cpu(ix->ei_block),
  416. le32_to_cpu(ix[-1].ei_block));
  417. }
  418. BUG_ON(k && le32_to_cpu(ix->ei_block)
  419. <= le32_to_cpu(ix[-1].ei_block));
  420. if (block < le32_to_cpu(ix->ei_block))
  421. break;
  422. chix = ix;
  423. }
  424. BUG_ON(chix != path->p_idx);
  425. }
  426. #endif
  427. }
  428. /*
  429. * ext4_ext_binsearch:
  430. * binary search for closest extent of the given block
  431. * the header must be checked before calling this
  432. */
  433. static void
  434. ext4_ext_binsearch(struct inode *inode,
  435. struct ext4_ext_path *path, ext4_lblk_t block)
  436. {
  437. struct ext4_extent_header *eh = path->p_hdr;
  438. struct ext4_extent *r, *l, *m;
  439. if (eh->eh_entries == 0) {
  440. /*
  441. * this leaf is empty:
  442. * we get such a leaf in split/add case
  443. */
  444. return;
  445. }
  446. ext_debug("binsearch for %u: ", block);
  447. l = EXT_FIRST_EXTENT(eh) + 1;
  448. r = EXT_LAST_EXTENT(eh);
  449. while (l <= r) {
  450. m = l + (r - l) / 2;
  451. if (block < le32_to_cpu(m->ee_block))
  452. r = m - 1;
  453. else
  454. l = m + 1;
  455. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  456. m, le32_to_cpu(m->ee_block),
  457. r, le32_to_cpu(r->ee_block));
  458. }
  459. path->p_ext = l - 1;
  460. ext_debug(" -> %d:%llu:%d ",
  461. le32_to_cpu(path->p_ext->ee_block),
  462. ext_pblock(path->p_ext),
  463. ext4_ext_get_actual_len(path->p_ext));
  464. #ifdef CHECK_BINSEARCH
  465. {
  466. struct ext4_extent *chex, *ex;
  467. int k;
  468. chex = ex = EXT_FIRST_EXTENT(eh);
  469. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  470. BUG_ON(k && le32_to_cpu(ex->ee_block)
  471. <= le32_to_cpu(ex[-1].ee_block));
  472. if (block < le32_to_cpu(ex->ee_block))
  473. break;
  474. chex = ex;
  475. }
  476. BUG_ON(chex != path->p_ext);
  477. }
  478. #endif
  479. }
  480. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  481. {
  482. struct ext4_extent_header *eh;
  483. eh = ext_inode_hdr(inode);
  484. eh->eh_depth = 0;
  485. eh->eh_entries = 0;
  486. eh->eh_magic = EXT4_EXT_MAGIC;
  487. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
  488. ext4_mark_inode_dirty(handle, inode);
  489. ext4_ext_invalidate_cache(inode);
  490. return 0;
  491. }
  492. struct ext4_ext_path *
  493. ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
  494. struct ext4_ext_path *path)
  495. {
  496. struct ext4_extent_header *eh;
  497. struct buffer_head *bh;
  498. short int depth, i, ppos = 0, alloc = 0;
  499. eh = ext_inode_hdr(inode);
  500. depth = ext_depth(inode);
  501. if (ext4_ext_check_header(inode, eh, depth))
  502. return ERR_PTR(-EIO);
  503. /* account possible depth increase */
  504. if (!path) {
  505. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  506. GFP_NOFS);
  507. if (!path)
  508. return ERR_PTR(-ENOMEM);
  509. alloc = 1;
  510. }
  511. path[0].p_hdr = eh;
  512. path[0].p_bh = NULL;
  513. i = depth;
  514. /* walk through the tree */
  515. while (i) {
  516. ext_debug("depth %d: num %d, max %d\n",
  517. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  518. ext4_ext_binsearch_idx(inode, path + ppos, block);
  519. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  520. path[ppos].p_depth = i;
  521. path[ppos].p_ext = NULL;
  522. bh = sb_bread(inode->i_sb, path[ppos].p_block);
  523. if (!bh)
  524. goto err;
  525. eh = ext_block_hdr(bh);
  526. ppos++;
  527. BUG_ON(ppos > depth);
  528. path[ppos].p_bh = bh;
  529. path[ppos].p_hdr = eh;
  530. i--;
  531. if (ext4_ext_check_header(inode, eh, i))
  532. goto err;
  533. }
  534. path[ppos].p_depth = i;
  535. path[ppos].p_ext = NULL;
  536. path[ppos].p_idx = NULL;
  537. /* find extent */
  538. ext4_ext_binsearch(inode, path + ppos, block);
  539. /* if not an empty leaf */
  540. if (path[ppos].p_ext)
  541. path[ppos].p_block = ext_pblock(path[ppos].p_ext);
  542. ext4_ext_show_path(inode, path);
  543. return path;
  544. err:
  545. ext4_ext_drop_refs(path);
  546. if (alloc)
  547. kfree(path);
  548. return ERR_PTR(-EIO);
  549. }
  550. /*
  551. * ext4_ext_insert_index:
  552. * insert new index [@logical;@ptr] into the block at @curp;
  553. * check where to insert: before @curp or after @curp
  554. */
  555. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  556. struct ext4_ext_path *curp,
  557. int logical, ext4_fsblk_t ptr)
  558. {
  559. struct ext4_extent_idx *ix;
  560. int len, err;
  561. err = ext4_ext_get_access(handle, inode, curp);
  562. if (err)
  563. return err;
  564. BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
  565. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  566. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  567. /* insert after */
  568. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  569. len = (len - 1) * sizeof(struct ext4_extent_idx);
  570. len = len < 0 ? 0 : len;
  571. ext_debug("insert new index %d after: %llu. "
  572. "move %d from 0x%p to 0x%p\n",
  573. logical, ptr, len,
  574. (curp->p_idx + 1), (curp->p_idx + 2));
  575. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  576. }
  577. ix = curp->p_idx + 1;
  578. } else {
  579. /* insert before */
  580. len = len * sizeof(struct ext4_extent_idx);
  581. len = len < 0 ? 0 : len;
  582. ext_debug("insert new index %d before: %llu. "
  583. "move %d from 0x%p to 0x%p\n",
  584. logical, ptr, len,
  585. curp->p_idx, (curp->p_idx + 1));
  586. memmove(curp->p_idx + 1, curp->p_idx, len);
  587. ix = curp->p_idx;
  588. }
  589. ix->ei_block = cpu_to_le32(logical);
  590. ext4_idx_store_pblock(ix, ptr);
  591. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  592. BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
  593. > le16_to_cpu(curp->p_hdr->eh_max));
  594. BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
  595. err = ext4_ext_dirty(handle, inode, curp);
  596. ext4_std_error(inode->i_sb, err);
  597. return err;
  598. }
  599. /*
  600. * ext4_ext_split:
  601. * inserts new subtree into the path, using free index entry
  602. * at depth @at:
  603. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  604. * - makes decision where to split
  605. * - moves remaining extents and index entries (right to the split point)
  606. * into the newly allocated blocks
  607. * - initializes subtree
  608. */
  609. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  610. struct ext4_ext_path *path,
  611. struct ext4_extent *newext, int at)
  612. {
  613. struct buffer_head *bh = NULL;
  614. int depth = ext_depth(inode);
  615. struct ext4_extent_header *neh;
  616. struct ext4_extent_idx *fidx;
  617. struct ext4_extent *ex;
  618. int i = at, k, m, a;
  619. ext4_fsblk_t newblock, oldblock;
  620. __le32 border;
  621. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  622. int err = 0;
  623. /* make decision: where to split? */
  624. /* FIXME: now decision is simplest: at current extent */
  625. /* if current leaf will be split, then we should use
  626. * border from split point */
  627. BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
  628. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  629. border = path[depth].p_ext[1].ee_block;
  630. ext_debug("leaf will be split."
  631. " next leaf starts at %d\n",
  632. le32_to_cpu(border));
  633. } else {
  634. border = newext->ee_block;
  635. ext_debug("leaf will be added."
  636. " next leaf starts at %d\n",
  637. le32_to_cpu(border));
  638. }
  639. /*
  640. * If error occurs, then we break processing
  641. * and mark filesystem read-only. index won't
  642. * be inserted and tree will be in consistent
  643. * state. Next mount will repair buffers too.
  644. */
  645. /*
  646. * Get array to track all allocated blocks.
  647. * We need this to handle errors and free blocks
  648. * upon them.
  649. */
  650. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  651. if (!ablocks)
  652. return -ENOMEM;
  653. /* allocate all needed blocks */
  654. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  655. for (a = 0; a < depth - at; a++) {
  656. newblock = ext4_ext_new_meta_block(handle, inode, path,
  657. newext, &err);
  658. if (newblock == 0)
  659. goto cleanup;
  660. ablocks[a] = newblock;
  661. }
  662. /* initialize new leaf */
  663. newblock = ablocks[--a];
  664. BUG_ON(newblock == 0);
  665. bh = sb_getblk(inode->i_sb, newblock);
  666. if (!bh) {
  667. err = -EIO;
  668. goto cleanup;
  669. }
  670. lock_buffer(bh);
  671. err = ext4_journal_get_create_access(handle, bh);
  672. if (err)
  673. goto cleanup;
  674. neh = ext_block_hdr(bh);
  675. neh->eh_entries = 0;
  676. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  677. neh->eh_magic = EXT4_EXT_MAGIC;
  678. neh->eh_depth = 0;
  679. ex = EXT_FIRST_EXTENT(neh);
  680. /* move remainder of path[depth] to the new leaf */
  681. BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
  682. /* start copy from next extent */
  683. /* TODO: we could do it by single memmove */
  684. m = 0;
  685. path[depth].p_ext++;
  686. while (path[depth].p_ext <=
  687. EXT_MAX_EXTENT(path[depth].p_hdr)) {
  688. ext_debug("move %d:%llu:%d in new leaf %llu\n",
  689. le32_to_cpu(path[depth].p_ext->ee_block),
  690. ext_pblock(path[depth].p_ext),
  691. ext4_ext_get_actual_len(path[depth].p_ext),
  692. newblock);
  693. /*memmove(ex++, path[depth].p_ext++,
  694. sizeof(struct ext4_extent));
  695. neh->eh_entries++;*/
  696. path[depth].p_ext++;
  697. m++;
  698. }
  699. if (m) {
  700. memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
  701. le16_add_cpu(&neh->eh_entries, m);
  702. }
  703. set_buffer_uptodate(bh);
  704. unlock_buffer(bh);
  705. err = ext4_handle_dirty_metadata(handle, inode, bh);
  706. if (err)
  707. goto cleanup;
  708. brelse(bh);
  709. bh = NULL;
  710. /* correct old leaf */
  711. if (m) {
  712. err = ext4_ext_get_access(handle, inode, path + depth);
  713. if (err)
  714. goto cleanup;
  715. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  716. err = ext4_ext_dirty(handle, inode, path + depth);
  717. if (err)
  718. goto cleanup;
  719. }
  720. /* create intermediate indexes */
  721. k = depth - at - 1;
  722. BUG_ON(k < 0);
  723. if (k)
  724. ext_debug("create %d intermediate indices\n", k);
  725. /* insert new index into current index block */
  726. /* current depth stored in i var */
  727. i = depth - 1;
  728. while (k--) {
  729. oldblock = newblock;
  730. newblock = ablocks[--a];
  731. bh = sb_getblk(inode->i_sb, newblock);
  732. if (!bh) {
  733. err = -EIO;
  734. goto cleanup;
  735. }
  736. lock_buffer(bh);
  737. err = ext4_journal_get_create_access(handle, bh);
  738. if (err)
  739. goto cleanup;
  740. neh = ext_block_hdr(bh);
  741. neh->eh_entries = cpu_to_le16(1);
  742. neh->eh_magic = EXT4_EXT_MAGIC;
  743. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  744. neh->eh_depth = cpu_to_le16(depth - i);
  745. fidx = EXT_FIRST_INDEX(neh);
  746. fidx->ei_block = border;
  747. ext4_idx_store_pblock(fidx, oldblock);
  748. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  749. i, newblock, le32_to_cpu(border), oldblock);
  750. /* copy indexes */
  751. m = 0;
  752. path[i].p_idx++;
  753. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  754. EXT_MAX_INDEX(path[i].p_hdr));
  755. BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
  756. EXT_LAST_INDEX(path[i].p_hdr));
  757. while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
  758. ext_debug("%d: move %d:%llu in new index %llu\n", i,
  759. le32_to_cpu(path[i].p_idx->ei_block),
  760. idx_pblock(path[i].p_idx),
  761. newblock);
  762. /*memmove(++fidx, path[i].p_idx++,
  763. sizeof(struct ext4_extent_idx));
  764. neh->eh_entries++;
  765. BUG_ON(neh->eh_entries > neh->eh_max);*/
  766. path[i].p_idx++;
  767. m++;
  768. }
  769. if (m) {
  770. memmove(++fidx, path[i].p_idx - m,
  771. sizeof(struct ext4_extent_idx) * m);
  772. le16_add_cpu(&neh->eh_entries, m);
  773. }
  774. set_buffer_uptodate(bh);
  775. unlock_buffer(bh);
  776. err = ext4_handle_dirty_metadata(handle, inode, bh);
  777. if (err)
  778. goto cleanup;
  779. brelse(bh);
  780. bh = NULL;
  781. /* correct old index */
  782. if (m) {
  783. err = ext4_ext_get_access(handle, inode, path + i);
  784. if (err)
  785. goto cleanup;
  786. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  787. err = ext4_ext_dirty(handle, inode, path + i);
  788. if (err)
  789. goto cleanup;
  790. }
  791. i--;
  792. }
  793. /* insert new index */
  794. err = ext4_ext_insert_index(handle, inode, path + at,
  795. le32_to_cpu(border), newblock);
  796. cleanup:
  797. if (bh) {
  798. if (buffer_locked(bh))
  799. unlock_buffer(bh);
  800. brelse(bh);
  801. }
  802. if (err) {
  803. /* free all allocated blocks in error case */
  804. for (i = 0; i < depth; i++) {
  805. if (!ablocks[i])
  806. continue;
  807. ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
  808. }
  809. }
  810. kfree(ablocks);
  811. return err;
  812. }
  813. /*
  814. * ext4_ext_grow_indepth:
  815. * implements tree growing procedure:
  816. * - allocates new block
  817. * - moves top-level data (index block or leaf) into the new block
  818. * - initializes new top-level, creating index that points to the
  819. * just created block
  820. */
  821. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  822. struct ext4_ext_path *path,
  823. struct ext4_extent *newext)
  824. {
  825. struct ext4_ext_path *curp = path;
  826. struct ext4_extent_header *neh;
  827. struct ext4_extent_idx *fidx;
  828. struct buffer_head *bh;
  829. ext4_fsblk_t newblock;
  830. int err = 0;
  831. newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
  832. if (newblock == 0)
  833. return err;
  834. bh = sb_getblk(inode->i_sb, newblock);
  835. if (!bh) {
  836. err = -EIO;
  837. ext4_std_error(inode->i_sb, err);
  838. return err;
  839. }
  840. lock_buffer(bh);
  841. err = ext4_journal_get_create_access(handle, bh);
  842. if (err) {
  843. unlock_buffer(bh);
  844. goto out;
  845. }
  846. /* move top-level index/leaf into new block */
  847. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  848. /* set size of new block */
  849. neh = ext_block_hdr(bh);
  850. /* old root could have indexes or leaves
  851. * so calculate e_max right way */
  852. if (ext_depth(inode))
  853. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  854. else
  855. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  856. neh->eh_magic = EXT4_EXT_MAGIC;
  857. set_buffer_uptodate(bh);
  858. unlock_buffer(bh);
  859. err = ext4_handle_dirty_metadata(handle, inode, bh);
  860. if (err)
  861. goto out;
  862. /* create index in new top-level index: num,max,pointer */
  863. err = ext4_ext_get_access(handle, inode, curp);
  864. if (err)
  865. goto out;
  866. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  867. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
  868. curp->p_hdr->eh_entries = cpu_to_le16(1);
  869. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  870. if (path[0].p_hdr->eh_depth)
  871. curp->p_idx->ei_block =
  872. EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
  873. else
  874. curp->p_idx->ei_block =
  875. EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  876. ext4_idx_store_pblock(curp->p_idx, newblock);
  877. neh = ext_inode_hdr(inode);
  878. fidx = EXT_FIRST_INDEX(neh);
  879. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  880. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  881. le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
  882. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  883. err = ext4_ext_dirty(handle, inode, curp);
  884. out:
  885. brelse(bh);
  886. return err;
  887. }
  888. /*
  889. * ext4_ext_create_new_leaf:
  890. * finds empty index and adds new leaf.
  891. * if no free index is found, then it requests in-depth growing.
  892. */
  893. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  894. struct ext4_ext_path *path,
  895. struct ext4_extent *newext)
  896. {
  897. struct ext4_ext_path *curp;
  898. int depth, i, err = 0;
  899. repeat:
  900. i = depth = ext_depth(inode);
  901. /* walk up to the tree and look for free index entry */
  902. curp = path + depth;
  903. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  904. i--;
  905. curp--;
  906. }
  907. /* we use already allocated block for index block,
  908. * so subsequent data blocks should be contiguous */
  909. if (EXT_HAS_FREE_INDEX(curp)) {
  910. /* if we found index with free entry, then use that
  911. * entry: create all needed subtree and add new leaf */
  912. err = ext4_ext_split(handle, inode, path, newext, i);
  913. if (err)
  914. goto out;
  915. /* refill path */
  916. ext4_ext_drop_refs(path);
  917. path = ext4_ext_find_extent(inode,
  918. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  919. path);
  920. if (IS_ERR(path))
  921. err = PTR_ERR(path);
  922. } else {
  923. /* tree is full, time to grow in depth */
  924. err = ext4_ext_grow_indepth(handle, inode, path, newext);
  925. if (err)
  926. goto out;
  927. /* refill path */
  928. ext4_ext_drop_refs(path);
  929. path = ext4_ext_find_extent(inode,
  930. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  931. path);
  932. if (IS_ERR(path)) {
  933. err = PTR_ERR(path);
  934. goto out;
  935. }
  936. /*
  937. * only first (depth 0 -> 1) produces free space;
  938. * in all other cases we have to split the grown tree
  939. */
  940. depth = ext_depth(inode);
  941. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  942. /* now we need to split */
  943. goto repeat;
  944. }
  945. }
  946. out:
  947. return err;
  948. }
  949. /*
  950. * search the closest allocated block to the left for *logical
  951. * and returns it at @logical + it's physical address at @phys
  952. * if *logical is the smallest allocated block, the function
  953. * returns 0 at @phys
  954. * return value contains 0 (success) or error code
  955. */
  956. int
  957. ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
  958. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  959. {
  960. struct ext4_extent_idx *ix;
  961. struct ext4_extent *ex;
  962. int depth, ee_len;
  963. BUG_ON(path == NULL);
  964. depth = path->p_depth;
  965. *phys = 0;
  966. if (depth == 0 && path->p_ext == NULL)
  967. return 0;
  968. /* usually extent in the path covers blocks smaller
  969. * then *logical, but it can be that extent is the
  970. * first one in the file */
  971. ex = path[depth].p_ext;
  972. ee_len = ext4_ext_get_actual_len(ex);
  973. if (*logical < le32_to_cpu(ex->ee_block)) {
  974. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  975. while (--depth >= 0) {
  976. ix = path[depth].p_idx;
  977. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  978. }
  979. return 0;
  980. }
  981. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  982. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  983. *phys = ext_pblock(ex) + ee_len - 1;
  984. return 0;
  985. }
  986. /*
  987. * search the closest allocated block to the right for *logical
  988. * and returns it at @logical + it's physical address at @phys
  989. * if *logical is the smallest allocated block, the function
  990. * returns 0 at @phys
  991. * return value contains 0 (success) or error code
  992. */
  993. int
  994. ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
  995. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  996. {
  997. struct buffer_head *bh = NULL;
  998. struct ext4_extent_header *eh;
  999. struct ext4_extent_idx *ix;
  1000. struct ext4_extent *ex;
  1001. ext4_fsblk_t block;
  1002. int depth; /* Note, NOT eh_depth; depth from top of tree */
  1003. int ee_len;
  1004. BUG_ON(path == NULL);
  1005. depth = path->p_depth;
  1006. *phys = 0;
  1007. if (depth == 0 && path->p_ext == NULL)
  1008. return 0;
  1009. /* usually extent in the path covers blocks smaller
  1010. * then *logical, but it can be that extent is the
  1011. * first one in the file */
  1012. ex = path[depth].p_ext;
  1013. ee_len = ext4_ext_get_actual_len(ex);
  1014. if (*logical < le32_to_cpu(ex->ee_block)) {
  1015. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  1016. while (--depth >= 0) {
  1017. ix = path[depth].p_idx;
  1018. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  1019. }
  1020. *logical = le32_to_cpu(ex->ee_block);
  1021. *phys = ext_pblock(ex);
  1022. return 0;
  1023. }
  1024. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  1025. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1026. /* next allocated block in this leaf */
  1027. ex++;
  1028. *logical = le32_to_cpu(ex->ee_block);
  1029. *phys = ext_pblock(ex);
  1030. return 0;
  1031. }
  1032. /* go up and search for index to the right */
  1033. while (--depth >= 0) {
  1034. ix = path[depth].p_idx;
  1035. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1036. goto got_index;
  1037. }
  1038. /* we've gone up to the root and found no index to the right */
  1039. return 0;
  1040. got_index:
  1041. /* we've found index to the right, let's
  1042. * follow it and find the closest allocated
  1043. * block to the right */
  1044. ix++;
  1045. block = idx_pblock(ix);
  1046. while (++depth < path->p_depth) {
  1047. bh = sb_bread(inode->i_sb, block);
  1048. if (bh == NULL)
  1049. return -EIO;
  1050. eh = ext_block_hdr(bh);
  1051. /* subtract from p_depth to get proper eh_depth */
  1052. if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
  1053. put_bh(bh);
  1054. return -EIO;
  1055. }
  1056. ix = EXT_FIRST_INDEX(eh);
  1057. block = idx_pblock(ix);
  1058. put_bh(bh);
  1059. }
  1060. bh = sb_bread(inode->i_sb, block);
  1061. if (bh == NULL)
  1062. return -EIO;
  1063. eh = ext_block_hdr(bh);
  1064. if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
  1065. put_bh(bh);
  1066. return -EIO;
  1067. }
  1068. ex = EXT_FIRST_EXTENT(eh);
  1069. *logical = le32_to_cpu(ex->ee_block);
  1070. *phys = ext_pblock(ex);
  1071. put_bh(bh);
  1072. return 0;
  1073. }
  1074. /*
  1075. * ext4_ext_next_allocated_block:
  1076. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  1077. * NOTE: it considers block number from index entry as
  1078. * allocated block. Thus, index entries have to be consistent
  1079. * with leaves.
  1080. */
  1081. static ext4_lblk_t
  1082. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1083. {
  1084. int depth;
  1085. BUG_ON(path == NULL);
  1086. depth = path->p_depth;
  1087. if (depth == 0 && path->p_ext == NULL)
  1088. return EXT_MAX_BLOCK;
  1089. while (depth >= 0) {
  1090. if (depth == path->p_depth) {
  1091. /* leaf */
  1092. if (path[depth].p_ext !=
  1093. EXT_LAST_EXTENT(path[depth].p_hdr))
  1094. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1095. } else {
  1096. /* index */
  1097. if (path[depth].p_idx !=
  1098. EXT_LAST_INDEX(path[depth].p_hdr))
  1099. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1100. }
  1101. depth--;
  1102. }
  1103. return EXT_MAX_BLOCK;
  1104. }
  1105. /*
  1106. * ext4_ext_next_leaf_block:
  1107. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  1108. */
  1109. static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
  1110. struct ext4_ext_path *path)
  1111. {
  1112. int depth;
  1113. BUG_ON(path == NULL);
  1114. depth = path->p_depth;
  1115. /* zero-tree has no leaf blocks at all */
  1116. if (depth == 0)
  1117. return EXT_MAX_BLOCK;
  1118. /* go to index block */
  1119. depth--;
  1120. while (depth >= 0) {
  1121. if (path[depth].p_idx !=
  1122. EXT_LAST_INDEX(path[depth].p_hdr))
  1123. return (ext4_lblk_t)
  1124. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1125. depth--;
  1126. }
  1127. return EXT_MAX_BLOCK;
  1128. }
  1129. /*
  1130. * ext4_ext_correct_indexes:
  1131. * if leaf gets modified and modified extent is first in the leaf,
  1132. * then we have to correct all indexes above.
  1133. * TODO: do we need to correct tree in all cases?
  1134. */
  1135. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1136. struct ext4_ext_path *path)
  1137. {
  1138. struct ext4_extent_header *eh;
  1139. int depth = ext_depth(inode);
  1140. struct ext4_extent *ex;
  1141. __le32 border;
  1142. int k, err = 0;
  1143. eh = path[depth].p_hdr;
  1144. ex = path[depth].p_ext;
  1145. BUG_ON(ex == NULL);
  1146. BUG_ON(eh == NULL);
  1147. if (depth == 0) {
  1148. /* there is no tree at all */
  1149. return 0;
  1150. }
  1151. if (ex != EXT_FIRST_EXTENT(eh)) {
  1152. /* we correct tree if first leaf got modified only */
  1153. return 0;
  1154. }
  1155. /*
  1156. * TODO: we need correction if border is smaller than current one
  1157. */
  1158. k = depth - 1;
  1159. border = path[depth].p_ext->ee_block;
  1160. err = ext4_ext_get_access(handle, inode, path + k);
  1161. if (err)
  1162. return err;
  1163. path[k].p_idx->ei_block = border;
  1164. err = ext4_ext_dirty(handle, inode, path + k);
  1165. if (err)
  1166. return err;
  1167. while (k--) {
  1168. /* change all left-side indexes */
  1169. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1170. break;
  1171. err = ext4_ext_get_access(handle, inode, path + k);
  1172. if (err)
  1173. break;
  1174. path[k].p_idx->ei_block = border;
  1175. err = ext4_ext_dirty(handle, inode, path + k);
  1176. if (err)
  1177. break;
  1178. }
  1179. return err;
  1180. }
  1181. static int
  1182. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1183. struct ext4_extent *ex2)
  1184. {
  1185. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1186. /*
  1187. * Make sure that either both extents are uninitialized, or
  1188. * both are _not_.
  1189. */
  1190. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  1191. return 0;
  1192. if (ext4_ext_is_uninitialized(ex1))
  1193. max_len = EXT_UNINIT_MAX_LEN;
  1194. else
  1195. max_len = EXT_INIT_MAX_LEN;
  1196. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1197. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1198. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1199. le32_to_cpu(ex2->ee_block))
  1200. return 0;
  1201. /*
  1202. * To allow future support for preallocated extents to be added
  1203. * as an RO_COMPAT feature, refuse to merge to extents if
  1204. * this can result in the top bit of ee_len being set.
  1205. */
  1206. if (ext1_ee_len + ext2_ee_len > max_len)
  1207. return 0;
  1208. #ifdef AGGRESSIVE_TEST
  1209. if (ext1_ee_len >= 4)
  1210. return 0;
  1211. #endif
  1212. if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
  1213. return 1;
  1214. return 0;
  1215. }
  1216. /*
  1217. * This function tries to merge the "ex" extent to the next extent in the tree.
  1218. * It always tries to merge towards right. If you want to merge towards
  1219. * left, pass "ex - 1" as argument instead of "ex".
  1220. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1221. * 1 if they got merged.
  1222. */
  1223. int ext4_ext_try_to_merge(struct inode *inode,
  1224. struct ext4_ext_path *path,
  1225. struct ext4_extent *ex)
  1226. {
  1227. struct ext4_extent_header *eh;
  1228. unsigned int depth, len;
  1229. int merge_done = 0;
  1230. int uninitialized = 0;
  1231. depth = ext_depth(inode);
  1232. BUG_ON(path[depth].p_hdr == NULL);
  1233. eh = path[depth].p_hdr;
  1234. while (ex < EXT_LAST_EXTENT(eh)) {
  1235. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1236. break;
  1237. /* merge with next extent! */
  1238. if (ext4_ext_is_uninitialized(ex))
  1239. uninitialized = 1;
  1240. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1241. + ext4_ext_get_actual_len(ex + 1));
  1242. if (uninitialized)
  1243. ext4_ext_mark_uninitialized(ex);
  1244. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1245. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1246. * sizeof(struct ext4_extent);
  1247. memmove(ex + 1, ex + 2, len);
  1248. }
  1249. le16_add_cpu(&eh->eh_entries, -1);
  1250. merge_done = 1;
  1251. WARN_ON(eh->eh_entries == 0);
  1252. if (!eh->eh_entries)
  1253. ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
  1254. "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
  1255. }
  1256. return merge_done;
  1257. }
  1258. /*
  1259. * check if a portion of the "newext" extent overlaps with an
  1260. * existing extent.
  1261. *
  1262. * If there is an overlap discovered, it updates the length of the newext
  1263. * such that there will be no overlap, and then returns 1.
  1264. * If there is no overlap found, it returns 0.
  1265. */
  1266. unsigned int ext4_ext_check_overlap(struct inode *inode,
  1267. struct ext4_extent *newext,
  1268. struct ext4_ext_path *path)
  1269. {
  1270. ext4_lblk_t b1, b2;
  1271. unsigned int depth, len1;
  1272. unsigned int ret = 0;
  1273. b1 = le32_to_cpu(newext->ee_block);
  1274. len1 = ext4_ext_get_actual_len(newext);
  1275. depth = ext_depth(inode);
  1276. if (!path[depth].p_ext)
  1277. goto out;
  1278. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1279. /*
  1280. * get the next allocated block if the extent in the path
  1281. * is before the requested block(s)
  1282. */
  1283. if (b2 < b1) {
  1284. b2 = ext4_ext_next_allocated_block(path);
  1285. if (b2 == EXT_MAX_BLOCK)
  1286. goto out;
  1287. }
  1288. /* check for wrap through zero on extent logical start block*/
  1289. if (b1 + len1 < b1) {
  1290. len1 = EXT_MAX_BLOCK - b1;
  1291. newext->ee_len = cpu_to_le16(len1);
  1292. ret = 1;
  1293. }
  1294. /* check for overlap */
  1295. if (b1 + len1 > b2) {
  1296. newext->ee_len = cpu_to_le16(b2 - b1);
  1297. ret = 1;
  1298. }
  1299. out:
  1300. return ret;
  1301. }
  1302. /*
  1303. * ext4_ext_insert_extent:
  1304. * tries to merge requsted extent into the existing extent or
  1305. * inserts requested extent as new one into the tree,
  1306. * creating new leaf in the no-space case.
  1307. */
  1308. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1309. struct ext4_ext_path *path,
  1310. struct ext4_extent *newext)
  1311. {
  1312. struct ext4_extent_header *eh;
  1313. struct ext4_extent *ex, *fex;
  1314. struct ext4_extent *nearex; /* nearest extent */
  1315. struct ext4_ext_path *npath = NULL;
  1316. int depth, len, err;
  1317. ext4_lblk_t next;
  1318. unsigned uninitialized = 0;
  1319. BUG_ON(ext4_ext_get_actual_len(newext) == 0);
  1320. depth = ext_depth(inode);
  1321. ex = path[depth].p_ext;
  1322. BUG_ON(path[depth].p_hdr == NULL);
  1323. /* try to insert block into found extent and return */
  1324. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1325. ext_debug("append %d block to %d:%d (from %llu)\n",
  1326. ext4_ext_get_actual_len(newext),
  1327. le32_to_cpu(ex->ee_block),
  1328. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  1329. err = ext4_ext_get_access(handle, inode, path + depth);
  1330. if (err)
  1331. return err;
  1332. /*
  1333. * ext4_can_extents_be_merged should have checked that either
  1334. * both extents are uninitialized, or both aren't. Thus we
  1335. * need to check only one of them here.
  1336. */
  1337. if (ext4_ext_is_uninitialized(ex))
  1338. uninitialized = 1;
  1339. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1340. + ext4_ext_get_actual_len(newext));
  1341. if (uninitialized)
  1342. ext4_ext_mark_uninitialized(ex);
  1343. eh = path[depth].p_hdr;
  1344. nearex = ex;
  1345. goto merge;
  1346. }
  1347. repeat:
  1348. depth = ext_depth(inode);
  1349. eh = path[depth].p_hdr;
  1350. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1351. goto has_space;
  1352. /* probably next leaf has space for us? */
  1353. fex = EXT_LAST_EXTENT(eh);
  1354. next = ext4_ext_next_leaf_block(inode, path);
  1355. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1356. && next != EXT_MAX_BLOCK) {
  1357. ext_debug("next leaf block - %d\n", next);
  1358. BUG_ON(npath != NULL);
  1359. npath = ext4_ext_find_extent(inode, next, NULL);
  1360. if (IS_ERR(npath))
  1361. return PTR_ERR(npath);
  1362. BUG_ON(npath->p_depth != path->p_depth);
  1363. eh = npath[depth].p_hdr;
  1364. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1365. ext_debug("next leaf isnt full(%d)\n",
  1366. le16_to_cpu(eh->eh_entries));
  1367. path = npath;
  1368. goto repeat;
  1369. }
  1370. ext_debug("next leaf has no free space(%d,%d)\n",
  1371. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1372. }
  1373. /*
  1374. * There is no free space in the found leaf.
  1375. * We're gonna add a new leaf in the tree.
  1376. */
  1377. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1378. if (err)
  1379. goto cleanup;
  1380. depth = ext_depth(inode);
  1381. eh = path[depth].p_hdr;
  1382. has_space:
  1383. nearex = path[depth].p_ext;
  1384. err = ext4_ext_get_access(handle, inode, path + depth);
  1385. if (err)
  1386. goto cleanup;
  1387. if (!nearex) {
  1388. /* there is no extent in this leaf, create first one */
  1389. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1390. le32_to_cpu(newext->ee_block),
  1391. ext_pblock(newext),
  1392. ext4_ext_get_actual_len(newext));
  1393. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1394. } else if (le32_to_cpu(newext->ee_block)
  1395. > le32_to_cpu(nearex->ee_block)) {
  1396. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1397. if (nearex != EXT_LAST_EXTENT(eh)) {
  1398. len = EXT_MAX_EXTENT(eh) - nearex;
  1399. len = (len - 1) * sizeof(struct ext4_extent);
  1400. len = len < 0 ? 0 : len;
  1401. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1402. "move %d from 0x%p to 0x%p\n",
  1403. le32_to_cpu(newext->ee_block),
  1404. ext_pblock(newext),
  1405. ext4_ext_get_actual_len(newext),
  1406. nearex, len, nearex + 1, nearex + 2);
  1407. memmove(nearex + 2, nearex + 1, len);
  1408. }
  1409. path[depth].p_ext = nearex + 1;
  1410. } else {
  1411. BUG_ON(newext->ee_block == nearex->ee_block);
  1412. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1413. len = len < 0 ? 0 : len;
  1414. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1415. "move %d from 0x%p to 0x%p\n",
  1416. le32_to_cpu(newext->ee_block),
  1417. ext_pblock(newext),
  1418. ext4_ext_get_actual_len(newext),
  1419. nearex, len, nearex + 1, nearex + 2);
  1420. memmove(nearex + 1, nearex, len);
  1421. path[depth].p_ext = nearex;
  1422. }
  1423. le16_add_cpu(&eh->eh_entries, 1);
  1424. nearex = path[depth].p_ext;
  1425. nearex->ee_block = newext->ee_block;
  1426. ext4_ext_store_pblock(nearex, ext_pblock(newext));
  1427. nearex->ee_len = newext->ee_len;
  1428. merge:
  1429. /* try to merge extents to the right */
  1430. ext4_ext_try_to_merge(inode, path, nearex);
  1431. /* try to merge extents to the left */
  1432. /* time to correct all indexes above */
  1433. err = ext4_ext_correct_indexes(handle, inode, path);
  1434. if (err)
  1435. goto cleanup;
  1436. err = ext4_ext_dirty(handle, inode, path + depth);
  1437. cleanup:
  1438. if (npath) {
  1439. ext4_ext_drop_refs(npath);
  1440. kfree(npath);
  1441. }
  1442. ext4_ext_invalidate_cache(inode);
  1443. return err;
  1444. }
  1445. int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
  1446. ext4_lblk_t num, ext_prepare_callback func,
  1447. void *cbdata)
  1448. {
  1449. struct ext4_ext_path *path = NULL;
  1450. struct ext4_ext_cache cbex;
  1451. struct ext4_extent *ex;
  1452. ext4_lblk_t next, start = 0, end = 0;
  1453. ext4_lblk_t last = block + num;
  1454. int depth, exists, err = 0;
  1455. BUG_ON(func == NULL);
  1456. BUG_ON(inode == NULL);
  1457. while (block < last && block != EXT_MAX_BLOCK) {
  1458. num = last - block;
  1459. /* find extent for this block */
  1460. path = ext4_ext_find_extent(inode, block, path);
  1461. if (IS_ERR(path)) {
  1462. err = PTR_ERR(path);
  1463. path = NULL;
  1464. break;
  1465. }
  1466. depth = ext_depth(inode);
  1467. BUG_ON(path[depth].p_hdr == NULL);
  1468. ex = path[depth].p_ext;
  1469. next = ext4_ext_next_allocated_block(path);
  1470. exists = 0;
  1471. if (!ex) {
  1472. /* there is no extent yet, so try to allocate
  1473. * all requested space */
  1474. start = block;
  1475. end = block + num;
  1476. } else if (le32_to_cpu(ex->ee_block) > block) {
  1477. /* need to allocate space before found extent */
  1478. start = block;
  1479. end = le32_to_cpu(ex->ee_block);
  1480. if (block + num < end)
  1481. end = block + num;
  1482. } else if (block >= le32_to_cpu(ex->ee_block)
  1483. + ext4_ext_get_actual_len(ex)) {
  1484. /* need to allocate space after found extent */
  1485. start = block;
  1486. end = block + num;
  1487. if (end >= next)
  1488. end = next;
  1489. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1490. /*
  1491. * some part of requested space is covered
  1492. * by found extent
  1493. */
  1494. start = block;
  1495. end = le32_to_cpu(ex->ee_block)
  1496. + ext4_ext_get_actual_len(ex);
  1497. if (block + num < end)
  1498. end = block + num;
  1499. exists = 1;
  1500. } else {
  1501. BUG();
  1502. }
  1503. BUG_ON(end <= start);
  1504. if (!exists) {
  1505. cbex.ec_block = start;
  1506. cbex.ec_len = end - start;
  1507. cbex.ec_start = 0;
  1508. cbex.ec_type = EXT4_EXT_CACHE_GAP;
  1509. } else {
  1510. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1511. cbex.ec_len = ext4_ext_get_actual_len(ex);
  1512. cbex.ec_start = ext_pblock(ex);
  1513. cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
  1514. }
  1515. BUG_ON(cbex.ec_len == 0);
  1516. err = func(inode, path, &cbex, ex, cbdata);
  1517. ext4_ext_drop_refs(path);
  1518. if (err < 0)
  1519. break;
  1520. if (err == EXT_REPEAT)
  1521. continue;
  1522. else if (err == EXT_BREAK) {
  1523. err = 0;
  1524. break;
  1525. }
  1526. if (ext_depth(inode) != depth) {
  1527. /* depth was changed. we have to realloc path */
  1528. kfree(path);
  1529. path = NULL;
  1530. }
  1531. block = cbex.ec_block + cbex.ec_len;
  1532. }
  1533. if (path) {
  1534. ext4_ext_drop_refs(path);
  1535. kfree(path);
  1536. }
  1537. return err;
  1538. }
  1539. static void
  1540. ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
  1541. __u32 len, ext4_fsblk_t start, int type)
  1542. {
  1543. struct ext4_ext_cache *cex;
  1544. BUG_ON(len == 0);
  1545. cex = &EXT4_I(inode)->i_cached_extent;
  1546. cex->ec_type = type;
  1547. cex->ec_block = block;
  1548. cex->ec_len = len;
  1549. cex->ec_start = start;
  1550. }
  1551. /*
  1552. * ext4_ext_put_gap_in_cache:
  1553. * calculate boundaries of the gap that the requested block fits into
  1554. * and cache this gap
  1555. */
  1556. static void
  1557. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1558. ext4_lblk_t block)
  1559. {
  1560. int depth = ext_depth(inode);
  1561. unsigned long len;
  1562. ext4_lblk_t lblock;
  1563. struct ext4_extent *ex;
  1564. ex = path[depth].p_ext;
  1565. if (ex == NULL) {
  1566. /* there is no extent yet, so gap is [0;-] */
  1567. lblock = 0;
  1568. len = EXT_MAX_BLOCK;
  1569. ext_debug("cache gap(whole file):");
  1570. } else if (block < le32_to_cpu(ex->ee_block)) {
  1571. lblock = block;
  1572. len = le32_to_cpu(ex->ee_block) - block;
  1573. ext_debug("cache gap(before): %u [%u:%u]",
  1574. block,
  1575. le32_to_cpu(ex->ee_block),
  1576. ext4_ext_get_actual_len(ex));
  1577. } else if (block >= le32_to_cpu(ex->ee_block)
  1578. + ext4_ext_get_actual_len(ex)) {
  1579. ext4_lblk_t next;
  1580. lblock = le32_to_cpu(ex->ee_block)
  1581. + ext4_ext_get_actual_len(ex);
  1582. next = ext4_ext_next_allocated_block(path);
  1583. ext_debug("cache gap(after): [%u:%u] %u",
  1584. le32_to_cpu(ex->ee_block),
  1585. ext4_ext_get_actual_len(ex),
  1586. block);
  1587. BUG_ON(next == lblock);
  1588. len = next - lblock;
  1589. } else {
  1590. lblock = len = 0;
  1591. BUG();
  1592. }
  1593. ext_debug(" -> %u:%lu\n", lblock, len);
  1594. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1595. }
  1596. static int
  1597. ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
  1598. struct ext4_extent *ex)
  1599. {
  1600. struct ext4_ext_cache *cex;
  1601. cex = &EXT4_I(inode)->i_cached_extent;
  1602. /* has cache valid data? */
  1603. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1604. return EXT4_EXT_CACHE_NO;
  1605. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1606. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1607. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1608. ex->ee_block = cpu_to_le32(cex->ec_block);
  1609. ext4_ext_store_pblock(ex, cex->ec_start);
  1610. ex->ee_len = cpu_to_le16(cex->ec_len);
  1611. ext_debug("%u cached by %u:%u:%llu\n",
  1612. block,
  1613. cex->ec_block, cex->ec_len, cex->ec_start);
  1614. return cex->ec_type;
  1615. }
  1616. /* not in cache */
  1617. return EXT4_EXT_CACHE_NO;
  1618. }
  1619. /*
  1620. * ext4_ext_rm_idx:
  1621. * removes index from the index block.
  1622. * It's used in truncate case only, thus all requests are for
  1623. * last index in the block only.
  1624. */
  1625. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1626. struct ext4_ext_path *path)
  1627. {
  1628. struct buffer_head *bh;
  1629. int err;
  1630. ext4_fsblk_t leaf;
  1631. /* free index block */
  1632. path--;
  1633. leaf = idx_pblock(path->p_idx);
  1634. BUG_ON(path->p_hdr->eh_entries == 0);
  1635. err = ext4_ext_get_access(handle, inode, path);
  1636. if (err)
  1637. return err;
  1638. le16_add_cpu(&path->p_hdr->eh_entries, -1);
  1639. err = ext4_ext_dirty(handle, inode, path);
  1640. if (err)
  1641. return err;
  1642. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1643. bh = sb_find_get_block(inode->i_sb, leaf);
  1644. ext4_forget(handle, 1, inode, bh, leaf);
  1645. ext4_free_blocks(handle, inode, leaf, 1, 1);
  1646. return err;
  1647. }
  1648. /*
  1649. * ext4_ext_calc_credits_for_single_extent:
  1650. * This routine returns max. credits that needed to insert an extent
  1651. * to the extent tree.
  1652. * When pass the actual path, the caller should calculate credits
  1653. * under i_data_sem.
  1654. */
  1655. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  1656. struct ext4_ext_path *path)
  1657. {
  1658. if (path) {
  1659. int depth = ext_depth(inode);
  1660. int ret = 0;
  1661. /* probably there is space in leaf? */
  1662. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1663. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  1664. /*
  1665. * There are some space in the leaf tree, no
  1666. * need to account for leaf block credit
  1667. *
  1668. * bitmaps and block group descriptor blocks
  1669. * and other metadat blocks still need to be
  1670. * accounted.
  1671. */
  1672. /* 1 bitmap, 1 block group descriptor */
  1673. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  1674. }
  1675. }
  1676. return ext4_chunk_trans_blocks(inode, nrblocks);
  1677. }
  1678. /*
  1679. * How many index/leaf blocks need to change/allocate to modify nrblocks?
  1680. *
  1681. * if nrblocks are fit in a single extent (chunk flag is 1), then
  1682. * in the worse case, each tree level index/leaf need to be changed
  1683. * if the tree split due to insert a new extent, then the old tree
  1684. * index/leaf need to be updated too
  1685. *
  1686. * If the nrblocks are discontiguous, they could cause
  1687. * the whole tree split more than once, but this is really rare.
  1688. */
  1689. int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  1690. {
  1691. int index;
  1692. int depth = ext_depth(inode);
  1693. if (chunk)
  1694. index = depth * 2;
  1695. else
  1696. index = depth * 3;
  1697. return index;
  1698. }
  1699. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1700. struct ext4_extent *ex,
  1701. ext4_lblk_t from, ext4_lblk_t to)
  1702. {
  1703. struct buffer_head *bh;
  1704. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1705. int i, metadata = 0;
  1706. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1707. metadata = 1;
  1708. #ifdef EXTENTS_STATS
  1709. {
  1710. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1711. spin_lock(&sbi->s_ext_stats_lock);
  1712. sbi->s_ext_blocks += ee_len;
  1713. sbi->s_ext_extents++;
  1714. if (ee_len < sbi->s_ext_min)
  1715. sbi->s_ext_min = ee_len;
  1716. if (ee_len > sbi->s_ext_max)
  1717. sbi->s_ext_max = ee_len;
  1718. if (ext_depth(inode) > sbi->s_depth_max)
  1719. sbi->s_depth_max = ext_depth(inode);
  1720. spin_unlock(&sbi->s_ext_stats_lock);
  1721. }
  1722. #endif
  1723. if (from >= le32_to_cpu(ex->ee_block)
  1724. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1725. /* tail removal */
  1726. ext4_lblk_t num;
  1727. ext4_fsblk_t start;
  1728. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1729. start = ext_pblock(ex) + ee_len - num;
  1730. ext_debug("free last %u blocks starting %llu\n", num, start);
  1731. for (i = 0; i < num; i++) {
  1732. bh = sb_find_get_block(inode->i_sb, start + i);
  1733. ext4_forget(handle, 0, inode, bh, start + i);
  1734. }
  1735. ext4_free_blocks(handle, inode, start, num, metadata);
  1736. } else if (from == le32_to_cpu(ex->ee_block)
  1737. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1738. printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
  1739. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1740. } else {
  1741. printk(KERN_INFO "strange request: removal(2) "
  1742. "%u-%u from %u:%u\n",
  1743. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1744. }
  1745. return 0;
  1746. }
  1747. static int
  1748. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1749. struct ext4_ext_path *path, ext4_lblk_t start)
  1750. {
  1751. int err = 0, correct_index = 0;
  1752. int depth = ext_depth(inode), credits;
  1753. struct ext4_extent_header *eh;
  1754. ext4_lblk_t a, b, block;
  1755. unsigned num;
  1756. ext4_lblk_t ex_ee_block;
  1757. unsigned short ex_ee_len;
  1758. unsigned uninitialized = 0;
  1759. struct ext4_extent *ex;
  1760. /* the header must be checked already in ext4_ext_remove_space() */
  1761. ext_debug("truncate since %u in leaf\n", start);
  1762. if (!path[depth].p_hdr)
  1763. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1764. eh = path[depth].p_hdr;
  1765. BUG_ON(eh == NULL);
  1766. /* find where to start removing */
  1767. ex = EXT_LAST_EXTENT(eh);
  1768. ex_ee_block = le32_to_cpu(ex->ee_block);
  1769. if (ext4_ext_is_uninitialized(ex))
  1770. uninitialized = 1;
  1771. ex_ee_len = ext4_ext_get_actual_len(ex);
  1772. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1773. ex_ee_block + ex_ee_len > start) {
  1774. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1775. path[depth].p_ext = ex;
  1776. a = ex_ee_block > start ? ex_ee_block : start;
  1777. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1778. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1779. ext_debug(" border %u:%u\n", a, b);
  1780. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1781. block = 0;
  1782. num = 0;
  1783. BUG();
  1784. } else if (a != ex_ee_block) {
  1785. /* remove tail of the extent */
  1786. block = ex_ee_block;
  1787. num = a - block;
  1788. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1789. /* remove head of the extent */
  1790. block = a;
  1791. num = b - a;
  1792. /* there is no "make a hole" API yet */
  1793. BUG();
  1794. } else {
  1795. /* remove whole extent: excellent! */
  1796. block = ex_ee_block;
  1797. num = 0;
  1798. BUG_ON(a != ex_ee_block);
  1799. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1800. }
  1801. /*
  1802. * 3 for leaf, sb, and inode plus 2 (bmap and group
  1803. * descriptor) for each block group; assume two block
  1804. * groups plus ex_ee_len/blocks_per_block_group for
  1805. * the worst case
  1806. */
  1807. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  1808. if (ex == EXT_FIRST_EXTENT(eh)) {
  1809. correct_index = 1;
  1810. credits += (ext_depth(inode)) + 1;
  1811. }
  1812. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1813. err = ext4_ext_journal_restart(handle, credits);
  1814. if (err)
  1815. goto out;
  1816. err = ext4_ext_get_access(handle, inode, path + depth);
  1817. if (err)
  1818. goto out;
  1819. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1820. if (err)
  1821. goto out;
  1822. if (num == 0) {
  1823. /* this extent is removed; mark slot entirely unused */
  1824. ext4_ext_store_pblock(ex, 0);
  1825. le16_add_cpu(&eh->eh_entries, -1);
  1826. }
  1827. ex->ee_block = cpu_to_le32(block);
  1828. ex->ee_len = cpu_to_le16(num);
  1829. /*
  1830. * Do not mark uninitialized if all the blocks in the
  1831. * extent have been removed.
  1832. */
  1833. if (uninitialized && num)
  1834. ext4_ext_mark_uninitialized(ex);
  1835. err = ext4_ext_dirty(handle, inode, path + depth);
  1836. if (err)
  1837. goto out;
  1838. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1839. ext_pblock(ex));
  1840. ex--;
  1841. ex_ee_block = le32_to_cpu(ex->ee_block);
  1842. ex_ee_len = ext4_ext_get_actual_len(ex);
  1843. }
  1844. if (correct_index && eh->eh_entries)
  1845. err = ext4_ext_correct_indexes(handle, inode, path);
  1846. /* if this leaf is free, then we should
  1847. * remove it from index block above */
  1848. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1849. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1850. out:
  1851. return err;
  1852. }
  1853. /*
  1854. * ext4_ext_more_to_rm:
  1855. * returns 1 if current index has to be freed (even partial)
  1856. */
  1857. static int
  1858. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1859. {
  1860. BUG_ON(path->p_idx == NULL);
  1861. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1862. return 0;
  1863. /*
  1864. * if truncate on deeper level happened, it wasn't partial,
  1865. * so we have to consider current index for truncation
  1866. */
  1867. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1868. return 0;
  1869. return 1;
  1870. }
  1871. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
  1872. {
  1873. struct super_block *sb = inode->i_sb;
  1874. int depth = ext_depth(inode);
  1875. struct ext4_ext_path *path;
  1876. handle_t *handle;
  1877. int i = 0, err = 0;
  1878. ext_debug("truncate since %u\n", start);
  1879. /* probably first extent we're gonna free will be last in block */
  1880. handle = ext4_journal_start(inode, depth + 1);
  1881. if (IS_ERR(handle))
  1882. return PTR_ERR(handle);
  1883. ext4_ext_invalidate_cache(inode);
  1884. /*
  1885. * We start scanning from right side, freeing all the blocks
  1886. * after i_size and walking into the tree depth-wise.
  1887. */
  1888. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
  1889. if (path == NULL) {
  1890. ext4_journal_stop(handle);
  1891. return -ENOMEM;
  1892. }
  1893. path[0].p_hdr = ext_inode_hdr(inode);
  1894. if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
  1895. err = -EIO;
  1896. goto out;
  1897. }
  1898. path[0].p_depth = depth;
  1899. while (i >= 0 && err == 0) {
  1900. if (i == depth) {
  1901. /* this is leaf block */
  1902. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1903. /* root level has p_bh == NULL, brelse() eats this */
  1904. brelse(path[i].p_bh);
  1905. path[i].p_bh = NULL;
  1906. i--;
  1907. continue;
  1908. }
  1909. /* this is index block */
  1910. if (!path[i].p_hdr) {
  1911. ext_debug("initialize header\n");
  1912. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1913. }
  1914. if (!path[i].p_idx) {
  1915. /* this level hasn't been touched yet */
  1916. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1917. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1918. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1919. path[i].p_hdr,
  1920. le16_to_cpu(path[i].p_hdr->eh_entries));
  1921. } else {
  1922. /* we were already here, see at next index */
  1923. path[i].p_idx--;
  1924. }
  1925. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1926. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1927. path[i].p_idx);
  1928. if (ext4_ext_more_to_rm(path + i)) {
  1929. struct buffer_head *bh;
  1930. /* go to the next level */
  1931. ext_debug("move to level %d (block %llu)\n",
  1932. i + 1, idx_pblock(path[i].p_idx));
  1933. memset(path + i + 1, 0, sizeof(*path));
  1934. bh = sb_bread(sb, idx_pblock(path[i].p_idx));
  1935. if (!bh) {
  1936. /* should we reset i_size? */
  1937. err = -EIO;
  1938. break;
  1939. }
  1940. if (WARN_ON(i + 1 > depth)) {
  1941. err = -EIO;
  1942. break;
  1943. }
  1944. if (ext4_ext_check_header(inode, ext_block_hdr(bh),
  1945. depth - i - 1)) {
  1946. err = -EIO;
  1947. break;
  1948. }
  1949. path[i + 1].p_bh = bh;
  1950. /* save actual number of indexes since this
  1951. * number is changed at the next iteration */
  1952. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1953. i++;
  1954. } else {
  1955. /* we finished processing this index, go up */
  1956. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1957. /* index is empty, remove it;
  1958. * handle must be already prepared by the
  1959. * truncatei_leaf() */
  1960. err = ext4_ext_rm_idx(handle, inode, path + i);
  1961. }
  1962. /* root level has p_bh == NULL, brelse() eats this */
  1963. brelse(path[i].p_bh);
  1964. path[i].p_bh = NULL;
  1965. i--;
  1966. ext_debug("return to level %d\n", i);
  1967. }
  1968. }
  1969. /* TODO: flexible tree reduction should be here */
  1970. if (path->p_hdr->eh_entries == 0) {
  1971. /*
  1972. * truncate to zero freed all the tree,
  1973. * so we need to correct eh_depth
  1974. */
  1975. err = ext4_ext_get_access(handle, inode, path);
  1976. if (err == 0) {
  1977. ext_inode_hdr(inode)->eh_depth = 0;
  1978. ext_inode_hdr(inode)->eh_max =
  1979. cpu_to_le16(ext4_ext_space_root(inode));
  1980. err = ext4_ext_dirty(handle, inode, path);
  1981. }
  1982. }
  1983. out:
  1984. ext4_ext_drop_refs(path);
  1985. kfree(path);
  1986. ext4_journal_stop(handle);
  1987. return err;
  1988. }
  1989. /*
  1990. * called at mount time
  1991. */
  1992. void ext4_ext_init(struct super_block *sb)
  1993. {
  1994. /*
  1995. * possible initialization would be here
  1996. */
  1997. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  1998. printk(KERN_INFO "EXT4-fs: file extents enabled");
  1999. #ifdef AGGRESSIVE_TEST
  2000. printk(", aggressive tests");
  2001. #endif
  2002. #ifdef CHECK_BINSEARCH
  2003. printk(", check binsearch");
  2004. #endif
  2005. #ifdef EXTENTS_STATS
  2006. printk(", stats");
  2007. #endif
  2008. printk("\n");
  2009. #ifdef EXTENTS_STATS
  2010. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  2011. EXT4_SB(sb)->s_ext_min = 1 << 30;
  2012. EXT4_SB(sb)->s_ext_max = 0;
  2013. #endif
  2014. }
  2015. }
  2016. /*
  2017. * called at umount time
  2018. */
  2019. void ext4_ext_release(struct super_block *sb)
  2020. {
  2021. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
  2022. return;
  2023. #ifdef EXTENTS_STATS
  2024. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  2025. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2026. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  2027. sbi->s_ext_blocks, sbi->s_ext_extents,
  2028. sbi->s_ext_blocks / sbi->s_ext_extents);
  2029. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  2030. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  2031. }
  2032. #endif
  2033. }
  2034. static void bi_complete(struct bio *bio, int error)
  2035. {
  2036. complete((struct completion *)bio->bi_private);
  2037. }
  2038. /* FIXME!! we need to try to merge to left or right after zero-out */
  2039. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  2040. {
  2041. int ret = -EIO;
  2042. struct bio *bio;
  2043. int blkbits, blocksize;
  2044. sector_t ee_pblock;
  2045. struct completion event;
  2046. unsigned int ee_len, len, done, offset;
  2047. blkbits = inode->i_blkbits;
  2048. blocksize = inode->i_sb->s_blocksize;
  2049. ee_len = ext4_ext_get_actual_len(ex);
  2050. ee_pblock = ext_pblock(ex);
  2051. /* convert ee_pblock to 512 byte sectors */
  2052. ee_pblock = ee_pblock << (blkbits - 9);
  2053. while (ee_len > 0) {
  2054. if (ee_len > BIO_MAX_PAGES)
  2055. len = BIO_MAX_PAGES;
  2056. else
  2057. len = ee_len;
  2058. bio = bio_alloc(GFP_NOIO, len);
  2059. if (!bio)
  2060. return -ENOMEM;
  2061. bio->bi_sector = ee_pblock;
  2062. bio->bi_bdev = inode->i_sb->s_bdev;
  2063. done = 0;
  2064. offset = 0;
  2065. while (done < len) {
  2066. ret = bio_add_page(bio, ZERO_PAGE(0),
  2067. blocksize, offset);
  2068. if (ret != blocksize) {
  2069. /*
  2070. * We can't add any more pages because of
  2071. * hardware limitations. Start a new bio.
  2072. */
  2073. break;
  2074. }
  2075. done++;
  2076. offset += blocksize;
  2077. if (offset >= PAGE_CACHE_SIZE)
  2078. offset = 0;
  2079. }
  2080. init_completion(&event);
  2081. bio->bi_private = &event;
  2082. bio->bi_end_io = bi_complete;
  2083. submit_bio(WRITE, bio);
  2084. wait_for_completion(&event);
  2085. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  2086. ret = 0;
  2087. else {
  2088. ret = -EIO;
  2089. break;
  2090. }
  2091. bio_put(bio);
  2092. ee_len -= done;
  2093. ee_pblock += done << (blkbits - 9);
  2094. }
  2095. return ret;
  2096. }
  2097. #define EXT4_EXT_ZERO_LEN 7
  2098. /*
  2099. * This function is called by ext4_ext_get_blocks() if someone tries to write
  2100. * to an uninitialized extent. It may result in splitting the uninitialized
  2101. * extent into multiple extents (upto three - one initialized and two
  2102. * uninitialized).
  2103. * There are three possibilities:
  2104. * a> There is no split required: Entire extent should be initialized
  2105. * b> Splits in two extents: Write is happening at either end of the extent
  2106. * c> Splits in three extents: Somone is writing in middle of the extent
  2107. */
  2108. static int ext4_ext_convert_to_initialized(handle_t *handle,
  2109. struct inode *inode,
  2110. struct ext4_ext_path *path,
  2111. ext4_lblk_t iblock,
  2112. unsigned int max_blocks)
  2113. {
  2114. struct ext4_extent *ex, newex, orig_ex;
  2115. struct ext4_extent *ex1 = NULL;
  2116. struct ext4_extent *ex2 = NULL;
  2117. struct ext4_extent *ex3 = NULL;
  2118. struct ext4_extent_header *eh;
  2119. ext4_lblk_t ee_block;
  2120. unsigned int allocated, ee_len, depth;
  2121. ext4_fsblk_t newblock;
  2122. int err = 0;
  2123. int ret = 0;
  2124. depth = ext_depth(inode);
  2125. eh = path[depth].p_hdr;
  2126. ex = path[depth].p_ext;
  2127. ee_block = le32_to_cpu(ex->ee_block);
  2128. ee_len = ext4_ext_get_actual_len(ex);
  2129. allocated = ee_len - (iblock - ee_block);
  2130. newblock = iblock - ee_block + ext_pblock(ex);
  2131. ex2 = ex;
  2132. orig_ex.ee_block = ex->ee_block;
  2133. orig_ex.ee_len = cpu_to_le16(ee_len);
  2134. ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
  2135. err = ext4_ext_get_access(handle, inode, path + depth);
  2136. if (err)
  2137. goto out;
  2138. /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
  2139. if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
  2140. err = ext4_ext_zeroout(inode, &orig_ex);
  2141. if (err)
  2142. goto fix_extent_len;
  2143. /* update the extent length and mark as initialized */
  2144. ex->ee_block = orig_ex.ee_block;
  2145. ex->ee_len = orig_ex.ee_len;
  2146. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2147. ext4_ext_dirty(handle, inode, path + depth);
  2148. /* zeroed the full extent */
  2149. return allocated;
  2150. }
  2151. /* ex1: ee_block to iblock - 1 : uninitialized */
  2152. if (iblock > ee_block) {
  2153. ex1 = ex;
  2154. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  2155. ext4_ext_mark_uninitialized(ex1);
  2156. ex2 = &newex;
  2157. }
  2158. /*
  2159. * for sanity, update the length of the ex2 extent before
  2160. * we insert ex3, if ex1 is NULL. This is to avoid temporary
  2161. * overlap of blocks.
  2162. */
  2163. if (!ex1 && allocated > max_blocks)
  2164. ex2->ee_len = cpu_to_le16(max_blocks);
  2165. /* ex3: to ee_block + ee_len : uninitialised */
  2166. if (allocated > max_blocks) {
  2167. unsigned int newdepth;
  2168. /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
  2169. if (allocated <= EXT4_EXT_ZERO_LEN) {
  2170. /*
  2171. * iblock == ee_block is handled by the zerouout
  2172. * at the beginning.
  2173. * Mark first half uninitialized.
  2174. * Mark second half initialized and zero out the
  2175. * initialized extent
  2176. */
  2177. ex->ee_block = orig_ex.ee_block;
  2178. ex->ee_len = cpu_to_le16(ee_len - allocated);
  2179. ext4_ext_mark_uninitialized(ex);
  2180. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2181. ext4_ext_dirty(handle, inode, path + depth);
  2182. ex3 = &newex;
  2183. ex3->ee_block = cpu_to_le32(iblock);
  2184. ext4_ext_store_pblock(ex3, newblock);
  2185. ex3->ee_len = cpu_to_le16(allocated);
  2186. err = ext4_ext_insert_extent(handle, inode, path, ex3);
  2187. if (err == -ENOSPC) {
  2188. err = ext4_ext_zeroout(inode, &orig_ex);
  2189. if (err)
  2190. goto fix_extent_len;
  2191. ex->ee_block = orig_ex.ee_block;
  2192. ex->ee_len = orig_ex.ee_len;
  2193. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2194. ext4_ext_dirty(handle, inode, path + depth);
  2195. /* blocks available from iblock */
  2196. return allocated;
  2197. } else if (err)
  2198. goto fix_extent_len;
  2199. /*
  2200. * We need to zero out the second half because
  2201. * an fallocate request can update file size and
  2202. * converting the second half to initialized extent
  2203. * implies that we can leak some junk data to user
  2204. * space.
  2205. */
  2206. err = ext4_ext_zeroout(inode, ex3);
  2207. if (err) {
  2208. /*
  2209. * We should actually mark the
  2210. * second half as uninit and return error
  2211. * Insert would have changed the extent
  2212. */
  2213. depth = ext_depth(inode);
  2214. ext4_ext_drop_refs(path);
  2215. path = ext4_ext_find_extent(inode,
  2216. iblock, path);
  2217. if (IS_ERR(path)) {
  2218. err = PTR_ERR(path);
  2219. return err;
  2220. }
  2221. /* get the second half extent details */
  2222. ex = path[depth].p_ext;
  2223. err = ext4_ext_get_access(handle, inode,
  2224. path + depth);
  2225. if (err)
  2226. return err;
  2227. ext4_ext_mark_uninitialized(ex);
  2228. ext4_ext_dirty(handle, inode, path + depth);
  2229. return err;
  2230. }
  2231. /* zeroed the second half */
  2232. return allocated;
  2233. }
  2234. ex3 = &newex;
  2235. ex3->ee_block = cpu_to_le32(iblock + max_blocks);
  2236. ext4_ext_store_pblock(ex3, newblock + max_blocks);
  2237. ex3->ee_len = cpu_to_le16(allocated - max_blocks);
  2238. ext4_ext_mark_uninitialized(ex3);
  2239. err = ext4_ext_insert_extent(handle, inode, path, ex3);
  2240. if (err == -ENOSPC) {
  2241. err = ext4_ext_zeroout(inode, &orig_ex);
  2242. if (err)
  2243. goto fix_extent_len;
  2244. /* update the extent length and mark as initialized */
  2245. ex->ee_block = orig_ex.ee_block;
  2246. ex->ee_len = orig_ex.ee_len;
  2247. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2248. ext4_ext_dirty(handle, inode, path + depth);
  2249. /* zeroed the full extent */
  2250. /* blocks available from iblock */
  2251. return allocated;
  2252. } else if (err)
  2253. goto fix_extent_len;
  2254. /*
  2255. * The depth, and hence eh & ex might change
  2256. * as part of the insert above.
  2257. */
  2258. newdepth = ext_depth(inode);
  2259. /*
  2260. * update the extent length after successful insert of the
  2261. * split extent
  2262. */
  2263. orig_ex.ee_len = cpu_to_le16(ee_len -
  2264. ext4_ext_get_actual_len(ex3));
  2265. depth = newdepth;
  2266. ext4_ext_drop_refs(path);
  2267. path = ext4_ext_find_extent(inode, iblock, path);
  2268. if (IS_ERR(path)) {
  2269. err = PTR_ERR(path);
  2270. goto out;
  2271. }
  2272. eh = path[depth].p_hdr;
  2273. ex = path[depth].p_ext;
  2274. if (ex2 != &newex)
  2275. ex2 = ex;
  2276. err = ext4_ext_get_access(handle, inode, path + depth);
  2277. if (err)
  2278. goto out;
  2279. allocated = max_blocks;
  2280. /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
  2281. * to insert a extent in the middle zerout directly
  2282. * otherwise give the extent a chance to merge to left
  2283. */
  2284. if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
  2285. iblock != ee_block) {
  2286. err = ext4_ext_zeroout(inode, &orig_ex);
  2287. if (err)
  2288. goto fix_extent_len;
  2289. /* update the extent length and mark as initialized */
  2290. ex->ee_block = orig_ex.ee_block;
  2291. ex->ee_len = orig_ex.ee_len;
  2292. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2293. ext4_ext_dirty(handle, inode, path + depth);
  2294. /* zero out the first half */
  2295. /* blocks available from iblock */
  2296. return allocated;
  2297. }
  2298. }
  2299. /*
  2300. * If there was a change of depth as part of the
  2301. * insertion of ex3 above, we need to update the length
  2302. * of the ex1 extent again here
  2303. */
  2304. if (ex1 && ex1 != ex) {
  2305. ex1 = ex;
  2306. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  2307. ext4_ext_mark_uninitialized(ex1);
  2308. ex2 = &newex;
  2309. }
  2310. /* ex2: iblock to iblock + maxblocks-1 : initialised */
  2311. ex2->ee_block = cpu_to_le32(iblock);
  2312. ext4_ext_store_pblock(ex2, newblock);
  2313. ex2->ee_len = cpu_to_le16(allocated);
  2314. if (ex2 != ex)
  2315. goto insert;
  2316. /*
  2317. * New (initialized) extent starts from the first block
  2318. * in the current extent. i.e., ex2 == ex
  2319. * We have to see if it can be merged with the extent
  2320. * on the left.
  2321. */
  2322. if (ex2 > EXT_FIRST_EXTENT(eh)) {
  2323. /*
  2324. * To merge left, pass "ex2 - 1" to try_to_merge(),
  2325. * since it merges towards right _only_.
  2326. */
  2327. ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
  2328. if (ret) {
  2329. err = ext4_ext_correct_indexes(handle, inode, path);
  2330. if (err)
  2331. goto out;
  2332. depth = ext_depth(inode);
  2333. ex2--;
  2334. }
  2335. }
  2336. /*
  2337. * Try to Merge towards right. This might be required
  2338. * only when the whole extent is being written to.
  2339. * i.e. ex2 == ex and ex3 == NULL.
  2340. */
  2341. if (!ex3) {
  2342. ret = ext4_ext_try_to_merge(inode, path, ex2);
  2343. if (ret) {
  2344. err = ext4_ext_correct_indexes(handle, inode, path);
  2345. if (err)
  2346. goto out;
  2347. }
  2348. }
  2349. /* Mark modified extent as dirty */
  2350. err = ext4_ext_dirty(handle, inode, path + depth);
  2351. goto out;
  2352. insert:
  2353. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2354. if (err == -ENOSPC) {
  2355. err = ext4_ext_zeroout(inode, &orig_ex);
  2356. if (err)
  2357. goto fix_extent_len;
  2358. /* update the extent length and mark as initialized */
  2359. ex->ee_block = orig_ex.ee_block;
  2360. ex->ee_len = orig_ex.ee_len;
  2361. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2362. ext4_ext_dirty(handle, inode, path + depth);
  2363. /* zero out the first half */
  2364. return allocated;
  2365. } else if (err)
  2366. goto fix_extent_len;
  2367. out:
  2368. return err ? err : allocated;
  2369. fix_extent_len:
  2370. ex->ee_block = orig_ex.ee_block;
  2371. ex->ee_len = orig_ex.ee_len;
  2372. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2373. ext4_ext_mark_uninitialized(ex);
  2374. ext4_ext_dirty(handle, inode, path + depth);
  2375. return err;
  2376. }
  2377. /*
  2378. * Block allocation/map/preallocation routine for extents based files
  2379. *
  2380. *
  2381. * Need to be called with
  2382. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  2383. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  2384. *
  2385. * return > 0, number of of blocks already mapped/allocated
  2386. * if create == 0 and these are pre-allocated blocks
  2387. * buffer head is unmapped
  2388. * otherwise blocks are mapped
  2389. *
  2390. * return = 0, if plain look up failed (blocks have not been allocated)
  2391. * buffer head is unmapped
  2392. *
  2393. * return < 0, error case.
  2394. */
  2395. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  2396. ext4_lblk_t iblock,
  2397. unsigned int max_blocks, struct buffer_head *bh_result,
  2398. int create, int extend_disksize)
  2399. {
  2400. struct ext4_ext_path *path = NULL;
  2401. struct ext4_extent_header *eh;
  2402. struct ext4_extent newex, *ex;
  2403. ext4_fsblk_t newblock;
  2404. int err = 0, depth, ret, cache_type;
  2405. unsigned int allocated = 0;
  2406. struct ext4_allocation_request ar;
  2407. loff_t disksize;
  2408. __clear_bit(BH_New, &bh_result->b_state);
  2409. ext_debug("blocks %u/%u requested for inode %u\n",
  2410. iblock, max_blocks, inode->i_ino);
  2411. /* check in cache */
  2412. cache_type = ext4_ext_in_cache(inode, iblock, &newex);
  2413. if (cache_type) {
  2414. if (cache_type == EXT4_EXT_CACHE_GAP) {
  2415. if (!create) {
  2416. /*
  2417. * block isn't allocated yet and
  2418. * user doesn't want to allocate it
  2419. */
  2420. goto out2;
  2421. }
  2422. /* we should allocate requested block */
  2423. } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
  2424. /* block is already allocated */
  2425. newblock = iblock
  2426. - le32_to_cpu(newex.ee_block)
  2427. + ext_pblock(&newex);
  2428. /* number of remaining blocks in the extent */
  2429. allocated = ext4_ext_get_actual_len(&newex) -
  2430. (iblock - le32_to_cpu(newex.ee_block));
  2431. goto out;
  2432. } else {
  2433. BUG();
  2434. }
  2435. }
  2436. /* find extent for this block */
  2437. path = ext4_ext_find_extent(inode, iblock, NULL);
  2438. if (IS_ERR(path)) {
  2439. err = PTR_ERR(path);
  2440. path = NULL;
  2441. goto out2;
  2442. }
  2443. depth = ext_depth(inode);
  2444. /*
  2445. * consistent leaf must not be empty;
  2446. * this situation is possible, though, _during_ tree modification;
  2447. * this is why assert can't be put in ext4_ext_find_extent()
  2448. */
  2449. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  2450. eh = path[depth].p_hdr;
  2451. ex = path[depth].p_ext;
  2452. if (ex) {
  2453. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  2454. ext4_fsblk_t ee_start = ext_pblock(ex);
  2455. unsigned short ee_len;
  2456. /*
  2457. * Uninitialized extents are treated as holes, except that
  2458. * we split out initialized portions during a write.
  2459. */
  2460. ee_len = ext4_ext_get_actual_len(ex);
  2461. /* if found extent covers block, simply return it */
  2462. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  2463. newblock = iblock - ee_block + ee_start;
  2464. /* number of remaining blocks in the extent */
  2465. allocated = ee_len - (iblock - ee_block);
  2466. ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
  2467. ee_block, ee_len, newblock);
  2468. /* Do not put uninitialized extent in the cache */
  2469. if (!ext4_ext_is_uninitialized(ex)) {
  2470. ext4_ext_put_in_cache(inode, ee_block,
  2471. ee_len, ee_start,
  2472. EXT4_EXT_CACHE_EXTENT);
  2473. goto out;
  2474. }
  2475. if (create == EXT4_CREATE_UNINITIALIZED_EXT)
  2476. goto out;
  2477. if (!create) {
  2478. /*
  2479. * We have blocks reserved already. We
  2480. * return allocated blocks so that delalloc
  2481. * won't do block reservation for us. But
  2482. * the buffer head will be unmapped so that
  2483. * a read from the block returns 0s.
  2484. */
  2485. if (allocated > max_blocks)
  2486. allocated = max_blocks;
  2487. set_buffer_unwritten(bh_result);
  2488. goto out2;
  2489. }
  2490. ret = ext4_ext_convert_to_initialized(handle, inode,
  2491. path, iblock,
  2492. max_blocks);
  2493. if (ret <= 0) {
  2494. err = ret;
  2495. goto out2;
  2496. } else
  2497. allocated = ret;
  2498. goto outnew;
  2499. }
  2500. }
  2501. /*
  2502. * requested block isn't allocated yet;
  2503. * we couldn't try to create block if create flag is zero
  2504. */
  2505. if (!create) {
  2506. /*
  2507. * put just found gap into cache to speed up
  2508. * subsequent requests
  2509. */
  2510. ext4_ext_put_gap_in_cache(inode, path, iblock);
  2511. goto out2;
  2512. }
  2513. /*
  2514. * Okay, we need to do block allocation.
  2515. */
  2516. /* find neighbour allocated blocks */
  2517. ar.lleft = iblock;
  2518. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  2519. if (err)
  2520. goto out2;
  2521. ar.lright = iblock;
  2522. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
  2523. if (err)
  2524. goto out2;
  2525. /*
  2526. * See if request is beyond maximum number of blocks we can have in
  2527. * a single extent. For an initialized extent this limit is
  2528. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  2529. * EXT_UNINIT_MAX_LEN.
  2530. */
  2531. if (max_blocks > EXT_INIT_MAX_LEN &&
  2532. create != EXT4_CREATE_UNINITIALIZED_EXT)
  2533. max_blocks = EXT_INIT_MAX_LEN;
  2534. else if (max_blocks > EXT_UNINIT_MAX_LEN &&
  2535. create == EXT4_CREATE_UNINITIALIZED_EXT)
  2536. max_blocks = EXT_UNINIT_MAX_LEN;
  2537. /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
  2538. newex.ee_block = cpu_to_le32(iblock);
  2539. newex.ee_len = cpu_to_le16(max_blocks);
  2540. err = ext4_ext_check_overlap(inode, &newex, path);
  2541. if (err)
  2542. allocated = ext4_ext_get_actual_len(&newex);
  2543. else
  2544. allocated = max_blocks;
  2545. /* allocate new block */
  2546. ar.inode = inode;
  2547. ar.goal = ext4_ext_find_goal(inode, path, iblock);
  2548. ar.logical = iblock;
  2549. ar.len = allocated;
  2550. if (S_ISREG(inode->i_mode))
  2551. ar.flags = EXT4_MB_HINT_DATA;
  2552. else
  2553. /* disable in-core preallocation for non-regular files */
  2554. ar.flags = 0;
  2555. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  2556. if (!newblock)
  2557. goto out2;
  2558. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  2559. ar.goal, newblock, allocated);
  2560. /* try to insert new extent into found leaf and return */
  2561. ext4_ext_store_pblock(&newex, newblock);
  2562. newex.ee_len = cpu_to_le16(ar.len);
  2563. if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
  2564. ext4_ext_mark_uninitialized(&newex);
  2565. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2566. if (err) {
  2567. /* free data blocks we just allocated */
  2568. /* not a good idea to call discard here directly,
  2569. * but otherwise we'd need to call it every free() */
  2570. ext4_discard_preallocations(inode);
  2571. ext4_free_blocks(handle, inode, ext_pblock(&newex),
  2572. ext4_ext_get_actual_len(&newex), 0);
  2573. goto out2;
  2574. }
  2575. /* previous routine could use block we allocated */
  2576. newblock = ext_pblock(&newex);
  2577. allocated = ext4_ext_get_actual_len(&newex);
  2578. outnew:
  2579. if (extend_disksize) {
  2580. disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
  2581. if (disksize > i_size_read(inode))
  2582. disksize = i_size_read(inode);
  2583. if (disksize > EXT4_I(inode)->i_disksize)
  2584. EXT4_I(inode)->i_disksize = disksize;
  2585. }
  2586. set_buffer_new(bh_result);
  2587. /* Cache only when it is _not_ an uninitialized extent */
  2588. if (create != EXT4_CREATE_UNINITIALIZED_EXT)
  2589. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  2590. EXT4_EXT_CACHE_EXTENT);
  2591. out:
  2592. if (allocated > max_blocks)
  2593. allocated = max_blocks;
  2594. ext4_ext_show_leaf(inode, path);
  2595. set_buffer_mapped(bh_result);
  2596. bh_result->b_bdev = inode->i_sb->s_bdev;
  2597. bh_result->b_blocknr = newblock;
  2598. out2:
  2599. if (path) {
  2600. ext4_ext_drop_refs(path);
  2601. kfree(path);
  2602. }
  2603. return err ? err : allocated;
  2604. }
  2605. void ext4_ext_truncate(struct inode *inode)
  2606. {
  2607. struct address_space *mapping = inode->i_mapping;
  2608. struct super_block *sb = inode->i_sb;
  2609. ext4_lblk_t last_block;
  2610. handle_t *handle;
  2611. int err = 0;
  2612. /*
  2613. * probably first extent we're gonna free will be last in block
  2614. */
  2615. err = ext4_writepage_trans_blocks(inode);
  2616. handle = ext4_journal_start(inode, err);
  2617. if (IS_ERR(handle))
  2618. return;
  2619. if (inode->i_size & (sb->s_blocksize - 1))
  2620. ext4_block_truncate_page(handle, mapping, inode->i_size);
  2621. if (ext4_orphan_add(handle, inode))
  2622. goto out_stop;
  2623. down_write(&EXT4_I(inode)->i_data_sem);
  2624. ext4_ext_invalidate_cache(inode);
  2625. ext4_discard_preallocations(inode);
  2626. /*
  2627. * TODO: optimization is possible here.
  2628. * Probably we need not scan at all,
  2629. * because page truncation is enough.
  2630. */
  2631. /* we have to know where to truncate from in crash case */
  2632. EXT4_I(inode)->i_disksize = inode->i_size;
  2633. ext4_mark_inode_dirty(handle, inode);
  2634. last_block = (inode->i_size + sb->s_blocksize - 1)
  2635. >> EXT4_BLOCK_SIZE_BITS(sb);
  2636. err = ext4_ext_remove_space(inode, last_block);
  2637. /* In a multi-transaction truncate, we only make the final
  2638. * transaction synchronous.
  2639. */
  2640. if (IS_SYNC(inode))
  2641. ext4_handle_sync(handle);
  2642. out_stop:
  2643. up_write(&EXT4_I(inode)->i_data_sem);
  2644. /*
  2645. * If this was a simple ftruncate() and the file will remain alive,
  2646. * then we need to clear up the orphan record which we created above.
  2647. * However, if this was a real unlink then we were called by
  2648. * ext4_delete_inode(), and we allow that function to clean up the
  2649. * orphan info for us.
  2650. */
  2651. if (inode->i_nlink)
  2652. ext4_orphan_del(handle, inode);
  2653. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  2654. ext4_mark_inode_dirty(handle, inode);
  2655. ext4_journal_stop(handle);
  2656. }
  2657. static void ext4_falloc_update_inode(struct inode *inode,
  2658. int mode, loff_t new_size, int update_ctime)
  2659. {
  2660. struct timespec now;
  2661. if (update_ctime) {
  2662. now = current_fs_time(inode->i_sb);
  2663. if (!timespec_equal(&inode->i_ctime, &now))
  2664. inode->i_ctime = now;
  2665. }
  2666. /*
  2667. * Update only when preallocation was requested beyond
  2668. * the file size.
  2669. */
  2670. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  2671. if (new_size > i_size_read(inode))
  2672. i_size_write(inode, new_size);
  2673. if (new_size > EXT4_I(inode)->i_disksize)
  2674. ext4_update_i_disksize(inode, new_size);
  2675. }
  2676. }
  2677. /*
  2678. * preallocate space for a file. This implements ext4's fallocate inode
  2679. * operation, which gets called from sys_fallocate system call.
  2680. * For block-mapped files, posix_fallocate should fall back to the method
  2681. * of writing zeroes to the required new blocks (the same behavior which is
  2682. * expected for file systems which do not support fallocate() system call).
  2683. */
  2684. long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
  2685. {
  2686. handle_t *handle;
  2687. ext4_lblk_t block;
  2688. loff_t new_size;
  2689. unsigned int max_blocks;
  2690. int ret = 0;
  2691. int ret2 = 0;
  2692. int retries = 0;
  2693. struct buffer_head map_bh;
  2694. unsigned int credits, blkbits = inode->i_blkbits;
  2695. /*
  2696. * currently supporting (pre)allocate mode for extent-based
  2697. * files _only_
  2698. */
  2699. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  2700. return -EOPNOTSUPP;
  2701. /* preallocation to directories is currently not supported */
  2702. if (S_ISDIR(inode->i_mode))
  2703. return -ENODEV;
  2704. block = offset >> blkbits;
  2705. /*
  2706. * We can't just convert len to max_blocks because
  2707. * If blocksize = 4096 offset = 3072 and len = 2048
  2708. */
  2709. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  2710. - block;
  2711. /*
  2712. * credits to insert 1 extent into extent tree
  2713. */
  2714. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  2715. mutex_lock(&inode->i_mutex);
  2716. retry:
  2717. while (ret >= 0 && ret < max_blocks) {
  2718. block = block + ret;
  2719. max_blocks = max_blocks - ret;
  2720. handle = ext4_journal_start(inode, credits);
  2721. if (IS_ERR(handle)) {
  2722. ret = PTR_ERR(handle);
  2723. break;
  2724. }
  2725. ret = ext4_get_blocks_wrap(handle, inode, block,
  2726. max_blocks, &map_bh,
  2727. EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
  2728. if (ret <= 0) {
  2729. #ifdef EXT4FS_DEBUG
  2730. WARN_ON(ret <= 0);
  2731. printk(KERN_ERR "%s: ext4_ext_get_blocks "
  2732. "returned error inode#%lu, block=%u, "
  2733. "max_blocks=%u", __func__,
  2734. inode->i_ino, block, max_blocks);
  2735. #endif
  2736. ext4_mark_inode_dirty(handle, inode);
  2737. ret2 = ext4_journal_stop(handle);
  2738. break;
  2739. }
  2740. if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
  2741. blkbits) >> blkbits))
  2742. new_size = offset + len;
  2743. else
  2744. new_size = (block + ret) << blkbits;
  2745. ext4_falloc_update_inode(inode, mode, new_size,
  2746. buffer_new(&map_bh));
  2747. ext4_mark_inode_dirty(handle, inode);
  2748. ret2 = ext4_journal_stop(handle);
  2749. if (ret2)
  2750. break;
  2751. }
  2752. if (ret == -ENOSPC &&
  2753. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  2754. ret = 0;
  2755. goto retry;
  2756. }
  2757. mutex_unlock(&inode->i_mutex);
  2758. return ret > 0 ? ret2 : ret;
  2759. }
  2760. /*
  2761. * Callback function called for each extent to gather FIEMAP information.
  2762. */
  2763. static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
  2764. struct ext4_ext_cache *newex, struct ext4_extent *ex,
  2765. void *data)
  2766. {
  2767. struct fiemap_extent_info *fieinfo = data;
  2768. unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
  2769. __u64 logical;
  2770. __u64 physical;
  2771. __u64 length;
  2772. __u32 flags = 0;
  2773. int error;
  2774. logical = (__u64)newex->ec_block << blksize_bits;
  2775. if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
  2776. pgoff_t offset;
  2777. struct page *page;
  2778. struct buffer_head *bh = NULL;
  2779. offset = logical >> PAGE_SHIFT;
  2780. page = find_get_page(inode->i_mapping, offset);
  2781. if (!page || !page_has_buffers(page))
  2782. return EXT_CONTINUE;
  2783. bh = page_buffers(page);
  2784. if (!bh)
  2785. return EXT_CONTINUE;
  2786. if (buffer_delay(bh)) {
  2787. flags |= FIEMAP_EXTENT_DELALLOC;
  2788. page_cache_release(page);
  2789. } else {
  2790. page_cache_release(page);
  2791. return EXT_CONTINUE;
  2792. }
  2793. }
  2794. physical = (__u64)newex->ec_start << blksize_bits;
  2795. length = (__u64)newex->ec_len << blksize_bits;
  2796. if (ex && ext4_ext_is_uninitialized(ex))
  2797. flags |= FIEMAP_EXTENT_UNWRITTEN;
  2798. /*
  2799. * If this extent reaches EXT_MAX_BLOCK, it must be last.
  2800. *
  2801. * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
  2802. * this also indicates no more allocated blocks.
  2803. *
  2804. * XXX this might miss a single-block extent at EXT_MAX_BLOCK
  2805. */
  2806. if (logical + length - 1 == EXT_MAX_BLOCK ||
  2807. ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK)
  2808. flags |= FIEMAP_EXTENT_LAST;
  2809. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  2810. length, flags);
  2811. if (error < 0)
  2812. return error;
  2813. if (error == 1)
  2814. return EXT_BREAK;
  2815. return EXT_CONTINUE;
  2816. }
  2817. /* fiemap flags we can handle specified here */
  2818. #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  2819. static int ext4_xattr_fiemap(struct inode *inode,
  2820. struct fiemap_extent_info *fieinfo)
  2821. {
  2822. __u64 physical = 0;
  2823. __u64 length;
  2824. __u32 flags = FIEMAP_EXTENT_LAST;
  2825. int blockbits = inode->i_sb->s_blocksize_bits;
  2826. int error = 0;
  2827. /* in-inode? */
  2828. if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
  2829. struct ext4_iloc iloc;
  2830. int offset; /* offset of xattr in inode */
  2831. error = ext4_get_inode_loc(inode, &iloc);
  2832. if (error)
  2833. return error;
  2834. physical = iloc.bh->b_blocknr << blockbits;
  2835. offset = EXT4_GOOD_OLD_INODE_SIZE +
  2836. EXT4_I(inode)->i_extra_isize;
  2837. physical += offset;
  2838. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  2839. flags |= FIEMAP_EXTENT_DATA_INLINE;
  2840. } else { /* external block */
  2841. physical = EXT4_I(inode)->i_file_acl << blockbits;
  2842. length = inode->i_sb->s_blocksize;
  2843. }
  2844. if (physical)
  2845. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  2846. length, flags);
  2847. return (error < 0 ? error : 0);
  2848. }
  2849. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  2850. __u64 start, __u64 len)
  2851. {
  2852. ext4_lblk_t start_blk;
  2853. ext4_lblk_t len_blks;
  2854. int error = 0;
  2855. /* fallback to generic here if not in extents fmt */
  2856. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  2857. return generic_block_fiemap(inode, fieinfo, start, len,
  2858. ext4_get_block);
  2859. if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
  2860. return -EBADR;
  2861. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  2862. error = ext4_xattr_fiemap(inode, fieinfo);
  2863. } else {
  2864. start_blk = start >> inode->i_sb->s_blocksize_bits;
  2865. len_blks = len >> inode->i_sb->s_blocksize_bits;
  2866. /*
  2867. * Walk the extent tree gathering extent information.
  2868. * ext4_ext_fiemap_cb will push extents back to user.
  2869. */
  2870. down_write(&EXT4_I(inode)->i_data_sem);
  2871. error = ext4_ext_walk_space(inode, start_blk, len_blks,
  2872. ext4_ext_fiemap_cb, fieinfo);
  2873. up_write(&EXT4_I(inode)->i_data_sem);
  2874. }
  2875. return error;
  2876. }