extents.c 75 KB

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