localalloc.c 34 KB

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