localalloc.c 33 KB

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