extents.c 65 KB

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