localalloc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * localalloc.c
  5. *
  6. * Node local data allocation
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/bitops.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "blockcheck.h"
  35. #include "dlmglue.h"
  36. #include "inode.h"
  37. #include "journal.h"
  38. #include "localalloc.h"
  39. #include "suballoc.h"
  40. #include "super.h"
  41. #include "sysfile.h"
  42. #include "buffer_head_io.h"
  43. #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
  44. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
  45. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  46. struct ocfs2_dinode *alloc,
  47. u32 *numbits,
  48. struct ocfs2_alloc_reservation *resv);
  49. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
  50. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  51. handle_t *handle,
  52. struct ocfs2_dinode *alloc,
  53. struct inode *main_bm_inode,
  54. struct buffer_head *main_bm_bh);
  55. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  56. struct ocfs2_alloc_context **ac,
  57. struct inode **bitmap_inode,
  58. struct buffer_head **bitmap_bh);
  59. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  60. handle_t *handle,
  61. struct ocfs2_alloc_context *ac);
  62. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  63. struct inode *local_alloc_inode);
  64. /*
  65. * ocfs2_la_default_mb() - determine a default size, in megabytes of
  66. * the local alloc.
  67. *
  68. * Generally, we'd like to pick as large a local alloc as
  69. * possible. Performance on large workloads tends to scale
  70. * proportionally to la size. In addition to that, the reservations
  71. * code functions more efficiently as it can reserve more windows for
  72. * write.
  73. *
  74. * Some things work against us when trying to choose a large local alloc:
  75. *
  76. * - We need to ensure our sizing is picked to leave enough space in
  77. * group descriptors for other allocations (such as block groups,
  78. * etc). Picking default sizes which are a multiple of 4 could help
  79. * - block groups are allocated in 2mb and 4mb chunks.
  80. *
  81. * - Likewise, we don't want to starve other nodes of bits on small
  82. * file systems. This can easily be taken care of by limiting our
  83. * default to a reasonable size (256M) on larger cluster sizes.
  84. *
  85. * - Some file systems can't support very large sizes - 4k and 8k in
  86. * particular are limited to less than 128 and 256 megabytes respectively.
  87. *
  88. * The following reference table shows group descriptor and local
  89. * alloc maximums at various cluster sizes (4k blocksize)
  90. *
  91. * csize: 4K group: 126M la: 121M
  92. * csize: 8K group: 252M la: 243M
  93. * csize: 16K group: 504M la: 486M
  94. * csize: 32K group: 1008M la: 972M
  95. * csize: 64K group: 2016M la: 1944M
  96. * csize: 128K group: 4032M la: 3888M
  97. * csize: 256K group: 8064M la: 7776M
  98. * csize: 512K group: 16128M la: 15552M
  99. * csize: 1024K group: 32256M la: 31104M
  100. */
  101. #define OCFS2_LA_MAX_DEFAULT_MB 256
  102. #define OCFS2_LA_OLD_DEFAULT 8
  103. unsigned int ocfs2_la_default_mb(struct ocfs2_super *osb)
  104. {
  105. unsigned int la_mb;
  106. unsigned int gd_mb;
  107. unsigned int megs_per_slot;
  108. struct super_block *sb = osb->sb;
  109. gd_mb = ocfs2_clusters_to_megabytes(osb->sb,
  110. 8 * ocfs2_group_bitmap_size(sb, 0, osb->s_feature_incompat));
  111. /*
  112. * This takes care of files systems with very small group
  113. * descriptors - 512 byte blocksize at cluster sizes lower
  114. * than 16K and also 1k blocksize with 4k cluster size.
  115. */
  116. if ((sb->s_blocksize == 512 && osb->s_clustersize <= 8192)
  117. || (sb->s_blocksize == 1024 && osb->s_clustersize == 4096))
  118. return OCFS2_LA_OLD_DEFAULT;
  119. /*
  120. * Leave enough room for some block groups and make the final
  121. * value we work from a multiple of 4.
  122. */
  123. gd_mb -= 16;
  124. gd_mb &= 0xFFFFFFFB;
  125. la_mb = gd_mb;
  126. /*
  127. * Keep window sizes down to a reasonable default
  128. */
  129. if (la_mb > OCFS2_LA_MAX_DEFAULT_MB) {
  130. /*
  131. * Some clustersize / blocksize combinations will have
  132. * given us a larger than OCFS2_LA_MAX_DEFAULT_MB
  133. * default size, but get poor distribution when
  134. * limited to exactly 256 megabytes.
  135. *
  136. * As an example, 16K clustersize at 4K blocksize
  137. * gives us a cluster group size of 504M. Paring the
  138. * local alloc size down to 256 however, would give us
  139. * only one window and around 200MB left in the
  140. * cluster group. Instead, find the first size below
  141. * 256 which would give us an even distribution.
  142. *
  143. * Larger cluster group sizes actually work out pretty
  144. * well when pared to 256, so we don't have to do this
  145. * for any group that fits more than two
  146. * OCFS2_LA_MAX_DEFAULT_MB windows.
  147. */
  148. if (gd_mb > (2 * OCFS2_LA_MAX_DEFAULT_MB))
  149. la_mb = 256;
  150. else {
  151. unsigned int gd_mult = gd_mb;
  152. while (gd_mult > 256)
  153. gd_mult = gd_mult >> 1;
  154. la_mb = gd_mult;
  155. }
  156. }
  157. megs_per_slot = osb->osb_clusters_at_boot / osb->max_slots;
  158. megs_per_slot = ocfs2_clusters_to_megabytes(osb->sb, megs_per_slot);
  159. /* Too many nodes, too few disk clusters. */
  160. if (megs_per_slot < la_mb)
  161. la_mb = megs_per_slot;
  162. return la_mb;
  163. }
  164. void ocfs2_la_set_sizes(struct ocfs2_super *osb, int requested_mb)
  165. {
  166. struct super_block *sb = osb->sb;
  167. unsigned int la_default_mb = ocfs2_la_default_mb(osb);
  168. unsigned int la_max_mb;
  169. la_max_mb = ocfs2_clusters_to_megabytes(sb,
  170. ocfs2_local_alloc_size(sb) * 8);
  171. mlog(0, "requested: %dM, max: %uM, default: %uM\n",
  172. requested_mb, la_max_mb, la_default_mb);
  173. if (requested_mb == -1) {
  174. /* No user request - use defaults */
  175. osb->local_alloc_default_bits =
  176. ocfs2_megabytes_to_clusters(sb, la_default_mb);
  177. } else if (requested_mb > la_max_mb) {
  178. /* Request is too big, we give the maximum available */
  179. osb->local_alloc_default_bits =
  180. ocfs2_megabytes_to_clusters(sb, la_max_mb);
  181. } else {
  182. osb->local_alloc_default_bits =
  183. ocfs2_megabytes_to_clusters(sb, requested_mb);
  184. }
  185. osb->local_alloc_bits = osb->local_alloc_default_bits;
  186. }
  187. static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
  188. {
  189. return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
  190. osb->local_alloc_state == OCFS2_LA_ENABLED);
  191. }
  192. void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
  193. unsigned int num_clusters)
  194. {
  195. spin_lock(&osb->osb_lock);
  196. if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
  197. osb->local_alloc_state == OCFS2_LA_THROTTLED)
  198. if (num_clusters >= osb->local_alloc_default_bits) {
  199. cancel_delayed_work(&osb->la_enable_wq);
  200. osb->local_alloc_state = OCFS2_LA_ENABLED;
  201. }
  202. spin_unlock(&osb->osb_lock);
  203. }
  204. void ocfs2_la_enable_worker(struct work_struct *work)
  205. {
  206. struct ocfs2_super *osb =
  207. container_of(work, struct ocfs2_super,
  208. la_enable_wq.work);
  209. spin_lock(&osb->osb_lock);
  210. osb->local_alloc_state = OCFS2_LA_ENABLED;
  211. spin_unlock(&osb->osb_lock);
  212. }
  213. /*
  214. * Tell us whether a given allocation should use the local alloc
  215. * file. Otherwise, it has to go to the main bitmap.
  216. *
  217. * This function does semi-dirty reads of local alloc size and state!
  218. * This is ok however, as the values are re-checked once under mutex.
  219. */
  220. int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
  221. {
  222. int ret = 0;
  223. int la_bits;
  224. spin_lock(&osb->osb_lock);
  225. la_bits = osb->local_alloc_bits;
  226. if (!ocfs2_la_state_enabled(osb))
  227. goto bail;
  228. /* la_bits should be at least twice the size (in clusters) of
  229. * a new block group. We want to be sure block group
  230. * allocations go through the local alloc, so allow an
  231. * allocation to take up to half the bitmap. */
  232. if (bits > (la_bits / 2))
  233. goto bail;
  234. ret = 1;
  235. bail:
  236. mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
  237. osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
  238. spin_unlock(&osb->osb_lock);
  239. return ret;
  240. }
  241. int ocfs2_load_local_alloc(struct ocfs2_super *osb)
  242. {
  243. int status = 0;
  244. struct ocfs2_dinode *alloc = NULL;
  245. struct buffer_head *alloc_bh = NULL;
  246. u32 num_used;
  247. struct inode *inode = NULL;
  248. struct ocfs2_local_alloc *la;
  249. mlog_entry_void();
  250. if (osb->local_alloc_bits == 0)
  251. goto bail;
  252. if (osb->local_alloc_bits >= osb->bitmap_cpg) {
  253. mlog(ML_NOTICE, "Requested local alloc window %d is larger "
  254. "than max possible %u. Using defaults.\n",
  255. osb->local_alloc_bits, (osb->bitmap_cpg - 1));
  256. osb->local_alloc_bits =
  257. ocfs2_megabytes_to_clusters(osb->sb,
  258. ocfs2_la_default_mb(osb));
  259. }
  260. /* read the alloc off disk */
  261. inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
  262. osb->slot_num);
  263. if (!inode) {
  264. status = -EINVAL;
  265. mlog_errno(status);
  266. goto bail;
  267. }
  268. status = ocfs2_read_inode_block_full(inode, &alloc_bh,
  269. OCFS2_BH_IGNORE_CACHE);
  270. if (status < 0) {
  271. mlog_errno(status);
  272. goto bail;
  273. }
  274. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  275. la = OCFS2_LOCAL_ALLOC(alloc);
  276. if (!(le32_to_cpu(alloc->i_flags) &
  277. (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
  278. mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
  279. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  280. status = -EINVAL;
  281. goto bail;
  282. }
  283. if ((la->la_size == 0) ||
  284. (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
  285. mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
  286. le16_to_cpu(la->la_size));
  287. status = -EINVAL;
  288. goto bail;
  289. }
  290. /* do a little verification. */
  291. num_used = ocfs2_local_alloc_count_bits(alloc);
  292. /* hopefully the local alloc has always been recovered before
  293. * we load it. */
  294. if (num_used
  295. || alloc->id1.bitmap1.i_used
  296. || alloc->id1.bitmap1.i_total
  297. || la->la_bm_off)
  298. mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
  299. "found = %u, set = %u, taken = %u, off = %u\n",
  300. num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
  301. le32_to_cpu(alloc->id1.bitmap1.i_total),
  302. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  303. osb->local_alloc_bh = alloc_bh;
  304. osb->local_alloc_state = OCFS2_LA_ENABLED;
  305. bail:
  306. if (status < 0)
  307. brelse(alloc_bh);
  308. if (inode)
  309. iput(inode);
  310. mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
  311. mlog_exit(status);
  312. return status;
  313. }
  314. /*
  315. * return any unused bits to the bitmap and write out a clean
  316. * local_alloc.
  317. *
  318. * local_alloc_bh is optional. If not passed, we will simply use the
  319. * one off osb. If you do pass it however, be warned that it *will* be
  320. * returned brelse'd and NULL'd out.*/
  321. void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
  322. {
  323. int status;
  324. handle_t *handle;
  325. struct inode *local_alloc_inode = NULL;
  326. struct buffer_head *bh = NULL;
  327. struct buffer_head *main_bm_bh = NULL;
  328. struct inode *main_bm_inode = NULL;
  329. struct ocfs2_dinode *alloc_copy = NULL;
  330. struct ocfs2_dinode *alloc = NULL;
  331. mlog_entry_void();
  332. cancel_delayed_work(&osb->la_enable_wq);
  333. flush_workqueue(ocfs2_wq);
  334. if (osb->local_alloc_state == OCFS2_LA_UNUSED)
  335. goto out;
  336. local_alloc_inode =
  337. ocfs2_get_system_file_inode(osb,
  338. LOCAL_ALLOC_SYSTEM_INODE,
  339. osb->slot_num);
  340. if (!local_alloc_inode) {
  341. status = -ENOENT;
  342. mlog_errno(status);
  343. goto out;
  344. }
  345. osb->local_alloc_state = OCFS2_LA_DISABLED;
  346. ocfs2_resmap_uninit(&osb->osb_la_resmap);
  347. main_bm_inode = ocfs2_get_system_file_inode(osb,
  348. GLOBAL_BITMAP_SYSTEM_INODE,
  349. OCFS2_INVALID_SLOT);
  350. if (!main_bm_inode) {
  351. status = -EINVAL;
  352. mlog_errno(status);
  353. goto out;
  354. }
  355. mutex_lock(&main_bm_inode->i_mutex);
  356. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  357. if (status < 0) {
  358. mlog_errno(status);
  359. goto out_mutex;
  360. }
  361. /* WINDOW_MOVE_CREDITS is a bit heavy... */
  362. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  363. if (IS_ERR(handle)) {
  364. mlog_errno(PTR_ERR(handle));
  365. handle = NULL;
  366. goto out_unlock;
  367. }
  368. bh = osb->local_alloc_bh;
  369. alloc = (struct ocfs2_dinode *) bh->b_data;
  370. alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
  371. if (!alloc_copy) {
  372. status = -ENOMEM;
  373. goto out_commit;
  374. }
  375. memcpy(alloc_copy, alloc, bh->b_size);
  376. status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
  377. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  378. if (status < 0) {
  379. mlog_errno(status);
  380. goto out_commit;
  381. }
  382. ocfs2_clear_local_alloc(alloc);
  383. ocfs2_journal_dirty(handle, bh);
  384. brelse(bh);
  385. osb->local_alloc_bh = NULL;
  386. osb->local_alloc_state = OCFS2_LA_UNUSED;
  387. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  388. main_bm_inode, main_bm_bh);
  389. if (status < 0)
  390. mlog_errno(status);
  391. out_commit:
  392. ocfs2_commit_trans(osb, handle);
  393. out_unlock:
  394. brelse(main_bm_bh);
  395. ocfs2_inode_unlock(main_bm_inode, 1);
  396. out_mutex:
  397. mutex_unlock(&main_bm_inode->i_mutex);
  398. iput(main_bm_inode);
  399. out:
  400. if (local_alloc_inode)
  401. iput(local_alloc_inode);
  402. if (alloc_copy)
  403. kfree(alloc_copy);
  404. mlog_exit_void();
  405. }
  406. /*
  407. * We want to free the bitmap bits outside of any recovery context as
  408. * we'll need a cluster lock to do so, but we must clear the local
  409. * alloc before giving up the recovered nodes journal. To solve this,
  410. * we kmalloc a copy of the local alloc before it's change for the
  411. * caller to process with ocfs2_complete_local_alloc_recovery
  412. */
  413. int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
  414. int slot_num,
  415. struct ocfs2_dinode **alloc_copy)
  416. {
  417. int status = 0;
  418. struct buffer_head *alloc_bh = NULL;
  419. struct inode *inode = NULL;
  420. struct ocfs2_dinode *alloc;
  421. mlog_entry("(slot_num = %d)\n", slot_num);
  422. *alloc_copy = NULL;
  423. inode = ocfs2_get_system_file_inode(osb,
  424. LOCAL_ALLOC_SYSTEM_INODE,
  425. slot_num);
  426. if (!inode) {
  427. status = -EINVAL;
  428. mlog_errno(status);
  429. goto bail;
  430. }
  431. mutex_lock(&inode->i_mutex);
  432. status = ocfs2_read_inode_block_full(inode, &alloc_bh,
  433. OCFS2_BH_IGNORE_CACHE);
  434. if (status < 0) {
  435. mlog_errno(status);
  436. goto bail;
  437. }
  438. *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
  439. if (!(*alloc_copy)) {
  440. status = -ENOMEM;
  441. goto bail;
  442. }
  443. memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
  444. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  445. ocfs2_clear_local_alloc(alloc);
  446. ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
  447. status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
  448. if (status < 0)
  449. mlog_errno(status);
  450. bail:
  451. if ((status < 0) && (*alloc_copy)) {
  452. kfree(*alloc_copy);
  453. *alloc_copy = NULL;
  454. }
  455. brelse(alloc_bh);
  456. if (inode) {
  457. mutex_unlock(&inode->i_mutex);
  458. iput(inode);
  459. }
  460. mlog_exit(status);
  461. return status;
  462. }
  463. /*
  464. * Step 2: By now, we've completed the journal recovery, we've stamped
  465. * a clean local alloc on disk and dropped the node out of the
  466. * recovery map. Dlm locks will no longer stall, so lets clear out the
  467. * main bitmap.
  468. */
  469. int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
  470. struct ocfs2_dinode *alloc)
  471. {
  472. int status;
  473. handle_t *handle;
  474. struct buffer_head *main_bm_bh = NULL;
  475. struct inode *main_bm_inode;
  476. mlog_entry_void();
  477. main_bm_inode = ocfs2_get_system_file_inode(osb,
  478. GLOBAL_BITMAP_SYSTEM_INODE,
  479. OCFS2_INVALID_SLOT);
  480. if (!main_bm_inode) {
  481. status = -EINVAL;
  482. mlog_errno(status);
  483. goto out;
  484. }
  485. mutex_lock(&main_bm_inode->i_mutex);
  486. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  487. if (status < 0) {
  488. mlog_errno(status);
  489. goto out_mutex;
  490. }
  491. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  492. if (IS_ERR(handle)) {
  493. status = PTR_ERR(handle);
  494. handle = NULL;
  495. mlog_errno(status);
  496. goto out_unlock;
  497. }
  498. /* we want the bitmap change to be recorded on disk asap */
  499. handle->h_sync = 1;
  500. status = ocfs2_sync_local_to_main(osb, handle, alloc,
  501. main_bm_inode, main_bm_bh);
  502. if (status < 0)
  503. mlog_errno(status);
  504. ocfs2_commit_trans(osb, handle);
  505. out_unlock:
  506. ocfs2_inode_unlock(main_bm_inode, 1);
  507. out_mutex:
  508. mutex_unlock(&main_bm_inode->i_mutex);
  509. brelse(main_bm_bh);
  510. iput(main_bm_inode);
  511. out:
  512. if (!status)
  513. ocfs2_init_steal_slots(osb);
  514. mlog_exit(status);
  515. return status;
  516. }
  517. /*
  518. * make sure we've got at least bits_wanted contiguous bits in the
  519. * local alloc. You lose them when you drop i_mutex.
  520. *
  521. * We will add ourselves to the transaction passed in, but may start
  522. * our own in order to shift windows.
  523. */
  524. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  525. u32 bits_wanted,
  526. struct ocfs2_alloc_context *ac)
  527. {
  528. int status;
  529. struct ocfs2_dinode *alloc;
  530. struct inode *local_alloc_inode;
  531. unsigned int free_bits;
  532. mlog_entry_void();
  533. BUG_ON(!ac);
  534. local_alloc_inode =
  535. ocfs2_get_system_file_inode(osb,
  536. LOCAL_ALLOC_SYSTEM_INODE,
  537. osb->slot_num);
  538. if (!local_alloc_inode) {
  539. status = -ENOENT;
  540. mlog_errno(status);
  541. goto bail;
  542. }
  543. mutex_lock(&local_alloc_inode->i_mutex);
  544. /*
  545. * We must double check state and allocator bits because
  546. * another process may have changed them while holding i_mutex.
  547. */
  548. spin_lock(&osb->osb_lock);
  549. if (!ocfs2_la_state_enabled(osb) ||
  550. (bits_wanted > osb->local_alloc_bits)) {
  551. spin_unlock(&osb->osb_lock);
  552. status = -ENOSPC;
  553. goto bail;
  554. }
  555. spin_unlock(&osb->osb_lock);
  556. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  557. #ifdef CONFIG_OCFS2_DEBUG_FS
  558. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  559. ocfs2_local_alloc_count_bits(alloc)) {
  560. ocfs2_error(osb->sb, "local alloc inode %llu says it has "
  561. "%u free bits, but a count shows %u",
  562. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  563. le32_to_cpu(alloc->id1.bitmap1.i_used),
  564. ocfs2_local_alloc_count_bits(alloc));
  565. status = -EIO;
  566. goto bail;
  567. }
  568. #endif
  569. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  570. le32_to_cpu(alloc->id1.bitmap1.i_used);
  571. if (bits_wanted > free_bits) {
  572. /* uhoh, window change time. */
  573. status =
  574. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  575. if (status < 0) {
  576. if (status != -ENOSPC)
  577. mlog_errno(status);
  578. goto bail;
  579. }
  580. /*
  581. * Under certain conditions, the window slide code
  582. * might have reduced the number of bits available or
  583. * disabled the the local alloc entirely. Re-check
  584. * here and return -ENOSPC if necessary.
  585. */
  586. status = -ENOSPC;
  587. if (!ocfs2_la_state_enabled(osb))
  588. goto bail;
  589. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  590. le32_to_cpu(alloc->id1.bitmap1.i_used);
  591. if (bits_wanted > free_bits)
  592. goto bail;
  593. }
  594. if (ac->ac_max_block)
  595. mlog(0, "Calling in_range for max block %llu\n",
  596. (unsigned long long)ac->ac_max_block);
  597. ac->ac_inode = local_alloc_inode;
  598. /* We should never use localalloc from another slot */
  599. ac->ac_alloc_slot = osb->slot_num;
  600. ac->ac_which = OCFS2_AC_USE_LOCAL;
  601. get_bh(osb->local_alloc_bh);
  602. ac->ac_bh = osb->local_alloc_bh;
  603. status = 0;
  604. bail:
  605. if (status < 0 && local_alloc_inode) {
  606. mutex_unlock(&local_alloc_inode->i_mutex);
  607. iput(local_alloc_inode);
  608. }
  609. mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
  610. status);
  611. mlog_exit(status);
  612. return status;
  613. }
  614. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  615. handle_t *handle,
  616. struct ocfs2_alloc_context *ac,
  617. u32 bits_wanted,
  618. u32 *bit_off,
  619. u32 *num_bits)
  620. {
  621. int status, start;
  622. struct inode *local_alloc_inode;
  623. void *bitmap;
  624. struct ocfs2_dinode *alloc;
  625. struct ocfs2_local_alloc *la;
  626. mlog_entry_void();
  627. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  628. local_alloc_inode = ac->ac_inode;
  629. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  630. la = OCFS2_LOCAL_ALLOC(alloc);
  631. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
  632. ac->ac_resv);
  633. if (start == -1) {
  634. /* TODO: Shouldn't we just BUG here? */
  635. status = -ENOSPC;
  636. mlog_errno(status);
  637. goto bail;
  638. }
  639. bitmap = la->la_bitmap;
  640. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  641. *num_bits = bits_wanted;
  642. status = ocfs2_journal_access_di(handle,
  643. INODE_CACHE(local_alloc_inode),
  644. osb->local_alloc_bh,
  645. OCFS2_JOURNAL_ACCESS_WRITE);
  646. if (status < 0) {
  647. mlog_errno(status);
  648. goto bail;
  649. }
  650. ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
  651. bits_wanted);
  652. while(bits_wanted--)
  653. ocfs2_set_bit(start++, bitmap);
  654. le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
  655. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  656. bail:
  657. mlog_exit(status);
  658. return status;
  659. }
  660. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  661. {
  662. int i;
  663. u8 *buffer;
  664. u32 count = 0;
  665. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  666. mlog_entry_void();
  667. buffer = la->la_bitmap;
  668. for (i = 0; i < le16_to_cpu(la->la_size); i++)
  669. count += hweight8(buffer[i]);
  670. mlog_exit(count);
  671. return count;
  672. }
  673. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  674. struct ocfs2_dinode *alloc,
  675. u32 *numbits,
  676. struct ocfs2_alloc_reservation *resv)
  677. {
  678. int numfound, bitoff, left, startoff, lastzero;
  679. int local_resv = 0;
  680. struct ocfs2_alloc_reservation r;
  681. void *bitmap = NULL;
  682. struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
  683. mlog_entry("(numbits wanted = %u)\n", *numbits);
  684. if (!alloc->id1.bitmap1.i_total) {
  685. mlog(0, "No bits in my window!\n");
  686. bitoff = -1;
  687. goto bail;
  688. }
  689. if (!resv) {
  690. local_resv = 1;
  691. ocfs2_resv_init_once(&r);
  692. ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
  693. resv = &r;
  694. }
  695. numfound = *numbits;
  696. if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
  697. if (numfound < *numbits)
  698. *numbits = numfound;
  699. goto bail;
  700. }
  701. /*
  702. * Code error. While reservations are enabled, local
  703. * allocation should _always_ go through them.
  704. */
  705. BUG_ON(osb->osb_resv_level != 0);
  706. /*
  707. * Reservations are disabled. Handle this the old way.
  708. */
  709. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  710. numfound = bitoff = startoff = 0;
  711. lastzero = -1;
  712. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  713. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  714. if (bitoff == left) {
  715. /* mlog(0, "bitoff (%d) == left", bitoff); */
  716. break;
  717. }
  718. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  719. "numfound = %d\n", bitoff, startoff, numfound);*/
  720. /* Ok, we found a zero bit... is it contig. or do we
  721. * start over?*/
  722. if (bitoff == startoff) {
  723. /* we found a zero */
  724. numfound++;
  725. startoff++;
  726. } else {
  727. /* got a zero after some ones */
  728. numfound = 1;
  729. startoff = bitoff+1;
  730. }
  731. /* we got everything we needed */
  732. if (numfound == *numbits) {
  733. /* mlog(0, "Found it all!\n"); */
  734. break;
  735. }
  736. }
  737. mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
  738. numfound);
  739. if (numfound == *numbits)
  740. bitoff = startoff - numfound;
  741. else
  742. bitoff = -1;
  743. bail:
  744. if (local_resv)
  745. ocfs2_resv_discard(resmap, resv);
  746. mlog_exit(bitoff);
  747. return bitoff;
  748. }
  749. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  750. {
  751. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  752. int i;
  753. mlog_entry_void();
  754. alloc->id1.bitmap1.i_total = 0;
  755. alloc->id1.bitmap1.i_used = 0;
  756. la->la_bm_off = 0;
  757. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  758. la->la_bitmap[i] = 0;
  759. mlog_exit_void();
  760. }
  761. #if 0
  762. /* turn this on and uncomment below to aid debugging window shifts. */
  763. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  764. unsigned int start,
  765. unsigned int count)
  766. {
  767. unsigned int tmp = count;
  768. while(tmp--) {
  769. if (ocfs2_test_bit(start + tmp, bitmap)) {
  770. printk("ocfs2_verify_zero_bits: start = %u, count = "
  771. "%u\n", start, count);
  772. printk("ocfs2_verify_zero_bits: bit %u is set!",
  773. start + tmp);
  774. BUG();
  775. }
  776. }
  777. }
  778. #endif
  779. /*
  780. * sync the local alloc to main bitmap.
  781. *
  782. * assumes you've already locked the main bitmap -- the bitmap inode
  783. * passed is used for caching.
  784. */
  785. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  786. handle_t *handle,
  787. struct ocfs2_dinode *alloc,
  788. struct inode *main_bm_inode,
  789. struct buffer_head *main_bm_bh)
  790. {
  791. int status = 0;
  792. int bit_off, left, count, start;
  793. u64 la_start_blk;
  794. u64 blkno;
  795. void *bitmap;
  796. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  797. mlog_entry("total = %u, used = %u\n",
  798. le32_to_cpu(alloc->id1.bitmap1.i_total),
  799. le32_to_cpu(alloc->id1.bitmap1.i_used));
  800. if (!alloc->id1.bitmap1.i_total) {
  801. mlog(0, "nothing to sync!\n");
  802. goto bail;
  803. }
  804. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  805. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  806. mlog(0, "all bits were taken!\n");
  807. goto bail;
  808. }
  809. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  810. le32_to_cpu(la->la_bm_off));
  811. bitmap = la->la_bitmap;
  812. start = count = bit_off = 0;
  813. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  814. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  815. != -1) {
  816. if ((bit_off < left) && (bit_off == start)) {
  817. count++;
  818. start++;
  819. continue;
  820. }
  821. if (count) {
  822. blkno = la_start_blk +
  823. ocfs2_clusters_to_blocks(osb->sb,
  824. start - count);
  825. mlog(0, "freeing %u bits starting at local alloc bit "
  826. "%u (la_start_blk = %llu, blkno = %llu)\n",
  827. count, start - count,
  828. (unsigned long long)la_start_blk,
  829. (unsigned long long)blkno);
  830. status = ocfs2_release_clusters(handle,
  831. main_bm_inode,
  832. main_bm_bh, blkno,
  833. count);
  834. if (status < 0) {
  835. mlog_errno(status);
  836. goto bail;
  837. }
  838. }
  839. if (bit_off >= left)
  840. break;
  841. count = 1;
  842. start = bit_off + 1;
  843. }
  844. bail:
  845. mlog_exit(status);
  846. return status;
  847. }
  848. enum ocfs2_la_event {
  849. OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
  850. OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
  851. * enough bits theoretically
  852. * free, but a contiguous
  853. * allocation could not be
  854. * found. */
  855. OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
  856. * enough bits free to satisfy
  857. * our request. */
  858. };
  859. #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
  860. /*
  861. * Given an event, calculate the size of our next local alloc window.
  862. *
  863. * This should always be called under i_mutex of the local alloc inode
  864. * so that local alloc disabling doesn't race with processes trying to
  865. * use the allocator.
  866. *
  867. * Returns the state which the local alloc was left in. This value can
  868. * be ignored by some paths.
  869. */
  870. static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
  871. enum ocfs2_la_event event)
  872. {
  873. unsigned int bits;
  874. int state;
  875. spin_lock(&osb->osb_lock);
  876. if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
  877. WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
  878. goto out_unlock;
  879. }
  880. /*
  881. * ENOSPC and fragmentation are treated similarly for now.
  882. */
  883. if (event == OCFS2_LA_EVENT_ENOSPC ||
  884. event == OCFS2_LA_EVENT_FRAGMENTED) {
  885. /*
  886. * We ran out of contiguous space in the primary
  887. * bitmap. Drastically reduce the number of bits used
  888. * by local alloc until we have to disable it.
  889. */
  890. bits = osb->local_alloc_bits >> 1;
  891. if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
  892. /*
  893. * By setting state to THROTTLED, we'll keep
  894. * the number of local alloc bits used down
  895. * until an event occurs which would give us
  896. * reason to assume the bitmap situation might
  897. * have changed.
  898. */
  899. osb->local_alloc_state = OCFS2_LA_THROTTLED;
  900. osb->local_alloc_bits = bits;
  901. } else {
  902. osb->local_alloc_state = OCFS2_LA_DISABLED;
  903. }
  904. queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
  905. OCFS2_LA_ENABLE_INTERVAL);
  906. goto out_unlock;
  907. }
  908. /*
  909. * Don't increase the size of the local alloc window until we
  910. * know we might be able to fulfill the request. Otherwise, we
  911. * risk bouncing around the global bitmap during periods of
  912. * low space.
  913. */
  914. if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
  915. osb->local_alloc_bits = osb->local_alloc_default_bits;
  916. out_unlock:
  917. state = osb->local_alloc_state;
  918. spin_unlock(&osb->osb_lock);
  919. return state;
  920. }
  921. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  922. struct ocfs2_alloc_context **ac,
  923. struct inode **bitmap_inode,
  924. struct buffer_head **bitmap_bh)
  925. {
  926. int status;
  927. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  928. if (!(*ac)) {
  929. status = -ENOMEM;
  930. mlog_errno(status);
  931. goto bail;
  932. }
  933. retry_enospc:
  934. (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
  935. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  936. if (status == -ENOSPC) {
  937. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
  938. OCFS2_LA_DISABLED)
  939. goto bail;
  940. ocfs2_free_ac_resource(*ac);
  941. memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
  942. goto retry_enospc;
  943. }
  944. if (status < 0) {
  945. mlog_errno(status);
  946. goto bail;
  947. }
  948. *bitmap_inode = (*ac)->ac_inode;
  949. igrab(*bitmap_inode);
  950. *bitmap_bh = (*ac)->ac_bh;
  951. get_bh(*bitmap_bh);
  952. status = 0;
  953. bail:
  954. if ((status < 0) && *ac) {
  955. ocfs2_free_alloc_context(*ac);
  956. *ac = NULL;
  957. }
  958. mlog_exit(status);
  959. return status;
  960. }
  961. /*
  962. * pass it the bitmap lock in lock_bh if you have it.
  963. */
  964. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  965. handle_t *handle,
  966. struct ocfs2_alloc_context *ac)
  967. {
  968. int status = 0;
  969. u32 cluster_off, cluster_count;
  970. struct ocfs2_dinode *alloc = NULL;
  971. struct ocfs2_local_alloc *la;
  972. mlog_entry_void();
  973. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  974. la = OCFS2_LOCAL_ALLOC(alloc);
  975. if (alloc->id1.bitmap1.i_total)
  976. mlog(0, "asking me to alloc a new window over a non-empty "
  977. "one\n");
  978. mlog(0, "Allocating %u clusters for a new window.\n",
  979. osb->local_alloc_bits);
  980. /* Instruct the allocation code to try the most recently used
  981. * cluster group. We'll re-record the group used this pass
  982. * below. */
  983. ac->ac_last_group = osb->la_last_gd;
  984. /* we used the generic suballoc reserve function, but we set
  985. * everything up nicely, so there's no reason why we can't use
  986. * the more specific cluster api to claim bits. */
  987. status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
  988. &cluster_off, &cluster_count);
  989. if (status == -ENOSPC) {
  990. retry_enospc:
  991. /*
  992. * Note: We could also try syncing the journal here to
  993. * allow use of any free bits which the current
  994. * transaction can't give us access to. --Mark
  995. */
  996. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
  997. OCFS2_LA_DISABLED)
  998. goto bail;
  999. ac->ac_bits_wanted = osb->local_alloc_default_bits;
  1000. status = ocfs2_claim_clusters(handle, ac,
  1001. osb->local_alloc_bits,
  1002. &cluster_off,
  1003. &cluster_count);
  1004. if (status == -ENOSPC)
  1005. goto retry_enospc;
  1006. /*
  1007. * We only shrunk the *minimum* number of in our
  1008. * request - it's entirely possible that the allocator
  1009. * might give us more than we asked for.
  1010. */
  1011. if (status == 0) {
  1012. spin_lock(&osb->osb_lock);
  1013. osb->local_alloc_bits = cluster_count;
  1014. spin_unlock(&osb->osb_lock);
  1015. }
  1016. }
  1017. if (status < 0) {
  1018. if (status != -ENOSPC)
  1019. mlog_errno(status);
  1020. goto bail;
  1021. }
  1022. osb->la_last_gd = ac->ac_last_group;
  1023. la->la_bm_off = cpu_to_le32(cluster_off);
  1024. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  1025. /* just in case... In the future when we find space ourselves,
  1026. * we don't have to get all contiguous -- but we'll have to
  1027. * set all previously used bits in bitmap and update
  1028. * la_bits_set before setting the bits in the main bitmap. */
  1029. alloc->id1.bitmap1.i_used = 0;
  1030. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  1031. le16_to_cpu(la->la_size));
  1032. ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
  1033. OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
  1034. mlog(0, "New window allocated:\n");
  1035. mlog(0, "window la_bm_off = %u\n",
  1036. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  1037. mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
  1038. bail:
  1039. mlog_exit(status);
  1040. return status;
  1041. }
  1042. /* Note that we do *NOT* lock the local alloc inode here as
  1043. * it's been locked already for us. */
  1044. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  1045. struct inode *local_alloc_inode)
  1046. {
  1047. int status = 0;
  1048. struct buffer_head *main_bm_bh = NULL;
  1049. struct inode *main_bm_inode = NULL;
  1050. handle_t *handle = NULL;
  1051. struct ocfs2_dinode *alloc;
  1052. struct ocfs2_dinode *alloc_copy = NULL;
  1053. struct ocfs2_alloc_context *ac = NULL;
  1054. mlog_entry_void();
  1055. ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
  1056. /* This will lock the main bitmap for us. */
  1057. status = ocfs2_local_alloc_reserve_for_window(osb,
  1058. &ac,
  1059. &main_bm_inode,
  1060. &main_bm_bh);
  1061. if (status < 0) {
  1062. if (status != -ENOSPC)
  1063. mlog_errno(status);
  1064. goto bail;
  1065. }
  1066. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  1067. if (IS_ERR(handle)) {
  1068. status = PTR_ERR(handle);
  1069. handle = NULL;
  1070. mlog_errno(status);
  1071. goto bail;
  1072. }
  1073. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  1074. /* We want to clear the local alloc before doing anything
  1075. * else, so that if we error later during this operation,
  1076. * local alloc shutdown won't try to double free main bitmap
  1077. * bits. Make a copy so the sync function knows which bits to
  1078. * free. */
  1079. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
  1080. if (!alloc_copy) {
  1081. status = -ENOMEM;
  1082. mlog_errno(status);
  1083. goto bail;
  1084. }
  1085. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  1086. status = ocfs2_journal_access_di(handle,
  1087. INODE_CACHE(local_alloc_inode),
  1088. osb->local_alloc_bh,
  1089. OCFS2_JOURNAL_ACCESS_WRITE);
  1090. if (status < 0) {
  1091. mlog_errno(status);
  1092. goto bail;
  1093. }
  1094. ocfs2_clear_local_alloc(alloc);
  1095. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  1096. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  1097. main_bm_inode, main_bm_bh);
  1098. if (status < 0) {
  1099. mlog_errno(status);
  1100. goto bail;
  1101. }
  1102. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  1103. if (status < 0) {
  1104. if (status != -ENOSPC)
  1105. mlog_errno(status);
  1106. goto bail;
  1107. }
  1108. atomic_inc(&osb->alloc_stats.moves);
  1109. bail:
  1110. if (handle)
  1111. ocfs2_commit_trans(osb, handle);
  1112. brelse(main_bm_bh);
  1113. if (main_bm_inode)
  1114. iput(main_bm_inode);
  1115. if (alloc_copy)
  1116. kfree(alloc_copy);
  1117. if (ac)
  1118. ocfs2_free_alloc_context(ac);
  1119. mlog_exit(status);
  1120. return status;
  1121. }