localalloc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. #include <cluster/masklog.h>
  31. #include "ocfs2.h"
  32. #include "alloc.h"
  33. #include "blockcheck.h"
  34. #include "dlmglue.h"
  35. #include "inode.h"
  36. #include "journal.h"
  37. #include "localalloc.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "sysfile.h"
  41. #include "ocfs2_trace.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. trace_ocfs2_la_set_sizes(requested_mb, la_max_mb, la_default_mb);
  178. if (requested_mb == -1) {
  179. /* No user request - use defaults */
  180. osb->local_alloc_default_bits =
  181. ocfs2_megabytes_to_clusters(sb, la_default_mb);
  182. } else if (requested_mb > la_max_mb) {
  183. /* Request is too big, we give the maximum available */
  184. osb->local_alloc_default_bits =
  185. ocfs2_megabytes_to_clusters(sb, la_max_mb);
  186. } else {
  187. osb->local_alloc_default_bits =
  188. ocfs2_megabytes_to_clusters(sb, requested_mb);
  189. }
  190. osb->local_alloc_bits = osb->local_alloc_default_bits;
  191. }
  192. static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
  193. {
  194. return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
  195. osb->local_alloc_state == OCFS2_LA_ENABLED);
  196. }
  197. void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
  198. unsigned int num_clusters)
  199. {
  200. spin_lock(&osb->osb_lock);
  201. if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
  202. osb->local_alloc_state == OCFS2_LA_THROTTLED)
  203. if (num_clusters >= osb->local_alloc_default_bits) {
  204. cancel_delayed_work(&osb->la_enable_wq);
  205. osb->local_alloc_state = OCFS2_LA_ENABLED;
  206. }
  207. spin_unlock(&osb->osb_lock);
  208. }
  209. void ocfs2_la_enable_worker(struct work_struct *work)
  210. {
  211. struct ocfs2_super *osb =
  212. container_of(work, struct ocfs2_super,
  213. la_enable_wq.work);
  214. spin_lock(&osb->osb_lock);
  215. osb->local_alloc_state = OCFS2_LA_ENABLED;
  216. spin_unlock(&osb->osb_lock);
  217. }
  218. /*
  219. * Tell us whether a given allocation should use the local alloc
  220. * file. Otherwise, it has to go to the main bitmap.
  221. *
  222. * This function does semi-dirty reads of local alloc size and state!
  223. * This is ok however, as the values are re-checked once under mutex.
  224. */
  225. int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
  226. {
  227. int ret = 0;
  228. int la_bits;
  229. spin_lock(&osb->osb_lock);
  230. la_bits = osb->local_alloc_bits;
  231. if (!ocfs2_la_state_enabled(osb))
  232. goto bail;
  233. /* la_bits should be at least twice the size (in clusters) of
  234. * a new block group. We want to be sure block group
  235. * allocations go through the local alloc, so allow an
  236. * allocation to take up to half the bitmap. */
  237. if (bits > (la_bits / 2))
  238. goto bail;
  239. ret = 1;
  240. bail:
  241. trace_ocfs2_alloc_should_use_local(
  242. (unsigned long long)bits, osb->local_alloc_state, la_bits, ret);
  243. spin_unlock(&osb->osb_lock);
  244. return ret;
  245. }
  246. int ocfs2_load_local_alloc(struct ocfs2_super *osb)
  247. {
  248. int status = 0;
  249. struct ocfs2_dinode *alloc = NULL;
  250. struct buffer_head *alloc_bh = NULL;
  251. u32 num_used;
  252. struct inode *inode = NULL;
  253. struct ocfs2_local_alloc *la;
  254. if (osb->local_alloc_bits == 0)
  255. goto bail;
  256. if (osb->local_alloc_bits >= osb->bitmap_cpg) {
  257. mlog(ML_NOTICE, "Requested local alloc window %d is larger "
  258. "than max possible %u. Using defaults.\n",
  259. osb->local_alloc_bits, (osb->bitmap_cpg - 1));
  260. osb->local_alloc_bits =
  261. ocfs2_megabytes_to_clusters(osb->sb,
  262. ocfs2_la_default_mb(osb));
  263. }
  264. /* read the alloc off disk */
  265. inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
  266. osb->slot_num);
  267. if (!inode) {
  268. status = -EINVAL;
  269. mlog_errno(status);
  270. goto bail;
  271. }
  272. status = ocfs2_read_inode_block_full(inode, &alloc_bh,
  273. OCFS2_BH_IGNORE_CACHE);
  274. if (status < 0) {
  275. mlog_errno(status);
  276. goto bail;
  277. }
  278. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  279. la = OCFS2_LOCAL_ALLOC(alloc);
  280. if (!(le32_to_cpu(alloc->i_flags) &
  281. (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
  282. mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
  283. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  284. status = -EINVAL;
  285. goto bail;
  286. }
  287. if ((la->la_size == 0) ||
  288. (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
  289. mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
  290. le16_to_cpu(la->la_size));
  291. status = -EINVAL;
  292. goto bail;
  293. }
  294. /* do a little verification. */
  295. num_used = ocfs2_local_alloc_count_bits(alloc);
  296. /* hopefully the local alloc has always been recovered before
  297. * we load it. */
  298. if (num_used
  299. || alloc->id1.bitmap1.i_used
  300. || alloc->id1.bitmap1.i_total
  301. || la->la_bm_off) {
  302. mlog(ML_ERROR, "inconsistent detected, clean journal with"
  303. " unrecovered local alloc, please run fsck.ocfs2!\n"
  304. "found = %u, set = %u, taken = %u, off = %u\n",
  305. num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
  306. le32_to_cpu(alloc->id1.bitmap1.i_total),
  307. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  308. status = -EINVAL;
  309. goto bail;
  310. }
  311. osb->local_alloc_bh = alloc_bh;
  312. osb->local_alloc_state = OCFS2_LA_ENABLED;
  313. bail:
  314. if (status < 0)
  315. brelse(alloc_bh);
  316. if (inode)
  317. iput(inode);
  318. trace_ocfs2_load_local_alloc(osb->local_alloc_bits);
  319. if (status)
  320. mlog_errno(status);
  321. return status;
  322. }
  323. /*
  324. * return any unused bits to the bitmap and write out a clean
  325. * local_alloc.
  326. *
  327. * local_alloc_bh is optional. If not passed, we will simply use the
  328. * one off osb. If you do pass it however, be warned that it *will* be
  329. * returned brelse'd and NULL'd out.*/
  330. void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
  331. {
  332. int status;
  333. handle_t *handle;
  334. struct inode *local_alloc_inode = NULL;
  335. struct buffer_head *bh = NULL;
  336. struct buffer_head *main_bm_bh = NULL;
  337. struct inode *main_bm_inode = NULL;
  338. struct ocfs2_dinode *alloc_copy = NULL;
  339. struct ocfs2_dinode *alloc = NULL;
  340. cancel_delayed_work(&osb->la_enable_wq);
  341. flush_workqueue(ocfs2_wq);
  342. if (osb->local_alloc_state == OCFS2_LA_UNUSED)
  343. goto out;
  344. local_alloc_inode =
  345. ocfs2_get_system_file_inode(osb,
  346. LOCAL_ALLOC_SYSTEM_INODE,
  347. osb->slot_num);
  348. if (!local_alloc_inode) {
  349. status = -ENOENT;
  350. mlog_errno(status);
  351. goto out;
  352. }
  353. osb->local_alloc_state = OCFS2_LA_DISABLED;
  354. ocfs2_resmap_uninit(&osb->osb_la_resmap);
  355. main_bm_inode = ocfs2_get_system_file_inode(osb,
  356. GLOBAL_BITMAP_SYSTEM_INODE,
  357. OCFS2_INVALID_SLOT);
  358. if (!main_bm_inode) {
  359. status = -EINVAL;
  360. mlog_errno(status);
  361. goto out;
  362. }
  363. mutex_lock(&main_bm_inode->i_mutex);
  364. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  365. if (status < 0) {
  366. mlog_errno(status);
  367. goto out_mutex;
  368. }
  369. /* WINDOW_MOVE_CREDITS is a bit heavy... */
  370. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  371. if (IS_ERR(handle)) {
  372. mlog_errno(PTR_ERR(handle));
  373. handle = NULL;
  374. goto out_unlock;
  375. }
  376. bh = osb->local_alloc_bh;
  377. alloc = (struct ocfs2_dinode *) bh->b_data;
  378. alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
  379. if (!alloc_copy) {
  380. status = -ENOMEM;
  381. goto out_commit;
  382. }
  383. memcpy(alloc_copy, alloc, bh->b_size);
  384. status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
  385. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  386. if (status < 0) {
  387. mlog_errno(status);
  388. goto out_commit;
  389. }
  390. ocfs2_clear_local_alloc(alloc);
  391. ocfs2_journal_dirty(handle, bh);
  392. brelse(bh);
  393. osb->local_alloc_bh = NULL;
  394. osb->local_alloc_state = OCFS2_LA_UNUSED;
  395. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  396. main_bm_inode, main_bm_bh);
  397. if (status < 0)
  398. mlog_errno(status);
  399. out_commit:
  400. ocfs2_commit_trans(osb, handle);
  401. out_unlock:
  402. brelse(main_bm_bh);
  403. ocfs2_inode_unlock(main_bm_inode, 1);
  404. out_mutex:
  405. mutex_unlock(&main_bm_inode->i_mutex);
  406. iput(main_bm_inode);
  407. out:
  408. if (local_alloc_inode)
  409. iput(local_alloc_inode);
  410. kfree(alloc_copy);
  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. trace_ocfs2_begin_local_alloc_recovery(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) {
  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. if (status)
  467. mlog_errno(status);
  468. return status;
  469. }
  470. /*
  471. * Step 2: By now, we've completed the journal recovery, we've stamped
  472. * a clean local alloc on disk and dropped the node out of the
  473. * recovery map. Dlm locks will no longer stall, so lets clear out the
  474. * main bitmap.
  475. */
  476. int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
  477. struct ocfs2_dinode *alloc)
  478. {
  479. int status;
  480. handle_t *handle;
  481. struct buffer_head *main_bm_bh = NULL;
  482. struct inode *main_bm_inode;
  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. if (status)
  521. mlog_errno(status);
  522. return status;
  523. }
  524. /*
  525. * make sure we've got at least bits_wanted contiguous bits in the
  526. * local alloc. You lose them when you drop i_mutex.
  527. *
  528. * We will add ourselves to the transaction passed in, but may start
  529. * our own in order to shift windows.
  530. */
  531. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  532. u32 bits_wanted,
  533. struct ocfs2_alloc_context *ac)
  534. {
  535. int status;
  536. struct ocfs2_dinode *alloc;
  537. struct inode *local_alloc_inode;
  538. unsigned int free_bits;
  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 %u used bits, but a count shows %u\n",
  567. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  568. le32_to_cpu(alloc->id1.bitmap1.i_used),
  569. ocfs2_local_alloc_count_bits(alloc));
  570. status = -EIO;
  571. goto bail;
  572. }
  573. #endif
  574. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  575. le32_to_cpu(alloc->id1.bitmap1.i_used);
  576. if (bits_wanted > free_bits) {
  577. /* uhoh, window change time. */
  578. status =
  579. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  580. if (status < 0) {
  581. if (status != -ENOSPC)
  582. mlog_errno(status);
  583. goto bail;
  584. }
  585. /*
  586. * Under certain conditions, the window slide code
  587. * might have reduced the number of bits available or
  588. * disabled the the local alloc entirely. Re-check
  589. * here and return -ENOSPC if necessary.
  590. */
  591. status = -ENOSPC;
  592. if (!ocfs2_la_state_enabled(osb))
  593. goto bail;
  594. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  595. le32_to_cpu(alloc->id1.bitmap1.i_used);
  596. if (bits_wanted > free_bits)
  597. goto bail;
  598. }
  599. ac->ac_inode = local_alloc_inode;
  600. /* We should never use localalloc from another slot */
  601. ac->ac_alloc_slot = osb->slot_num;
  602. ac->ac_which = OCFS2_AC_USE_LOCAL;
  603. get_bh(osb->local_alloc_bh);
  604. ac->ac_bh = osb->local_alloc_bh;
  605. status = 0;
  606. bail:
  607. if (status < 0 && local_alloc_inode) {
  608. mutex_unlock(&local_alloc_inode->i_mutex);
  609. iput(local_alloc_inode);
  610. }
  611. trace_ocfs2_reserve_local_alloc_bits(
  612. (unsigned long long)ac->ac_max_block,
  613. bits_wanted, osb->slot_num, status);
  614. if (status)
  615. mlog_errno(status);
  616. return status;
  617. }
  618. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  619. handle_t *handle,
  620. struct ocfs2_alloc_context *ac,
  621. u32 bits_wanted,
  622. u32 *bit_off,
  623. u32 *num_bits)
  624. {
  625. int status, start;
  626. struct inode *local_alloc_inode;
  627. void *bitmap;
  628. struct ocfs2_dinode *alloc;
  629. struct ocfs2_local_alloc *la;
  630. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  631. local_alloc_inode = ac->ac_inode;
  632. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  633. la = OCFS2_LOCAL_ALLOC(alloc);
  634. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
  635. ac->ac_resv);
  636. if (start == -1) {
  637. /* TODO: Shouldn't we just BUG here? */
  638. status = -ENOSPC;
  639. mlog_errno(status);
  640. goto bail;
  641. }
  642. bitmap = la->la_bitmap;
  643. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  644. *num_bits = bits_wanted;
  645. status = ocfs2_journal_access_di(handle,
  646. INODE_CACHE(local_alloc_inode),
  647. osb->local_alloc_bh,
  648. OCFS2_JOURNAL_ACCESS_WRITE);
  649. if (status < 0) {
  650. mlog_errno(status);
  651. goto bail;
  652. }
  653. ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
  654. bits_wanted);
  655. while(bits_wanted--)
  656. ocfs2_set_bit(start++, bitmap);
  657. le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
  658. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  659. bail:
  660. if (status)
  661. mlog_errno(status);
  662. return status;
  663. }
  664. int ocfs2_free_local_alloc_bits(struct ocfs2_super *osb,
  665. handle_t *handle,
  666. struct ocfs2_alloc_context *ac,
  667. u32 bit_off,
  668. u32 num_bits)
  669. {
  670. int status, start;
  671. u32 clear_bits;
  672. struct inode *local_alloc_inode;
  673. void *bitmap;
  674. struct ocfs2_dinode *alloc;
  675. struct ocfs2_local_alloc *la;
  676. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  677. local_alloc_inode = ac->ac_inode;
  678. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  679. la = OCFS2_LOCAL_ALLOC(alloc);
  680. bitmap = la->la_bitmap;
  681. start = bit_off - le32_to_cpu(la->la_bm_off);
  682. clear_bits = num_bits;
  683. status = ocfs2_journal_access_di(handle,
  684. INODE_CACHE(local_alloc_inode),
  685. osb->local_alloc_bh,
  686. OCFS2_JOURNAL_ACCESS_WRITE);
  687. if (status < 0) {
  688. mlog_errno(status);
  689. goto bail;
  690. }
  691. while (clear_bits--)
  692. ocfs2_clear_bit(start++, bitmap);
  693. le32_add_cpu(&alloc->id1.bitmap1.i_used, -num_bits);
  694. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  695. bail:
  696. return status;
  697. }
  698. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  699. {
  700. u32 count;
  701. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  702. count = memweight(la->la_bitmap, le16_to_cpu(la->la_size));
  703. trace_ocfs2_local_alloc_count_bits(count);
  704. return count;
  705. }
  706. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  707. struct ocfs2_dinode *alloc,
  708. u32 *numbits,
  709. struct ocfs2_alloc_reservation *resv)
  710. {
  711. int numfound = 0, bitoff, left, startoff, lastzero;
  712. int local_resv = 0;
  713. struct ocfs2_alloc_reservation r;
  714. void *bitmap = NULL;
  715. struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
  716. if (!alloc->id1.bitmap1.i_total) {
  717. bitoff = -1;
  718. goto bail;
  719. }
  720. if (!resv) {
  721. local_resv = 1;
  722. ocfs2_resv_init_once(&r);
  723. ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
  724. resv = &r;
  725. }
  726. numfound = *numbits;
  727. if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
  728. if (numfound < *numbits)
  729. *numbits = numfound;
  730. goto bail;
  731. }
  732. /*
  733. * Code error. While reservations are enabled, local
  734. * allocation should _always_ go through them.
  735. */
  736. BUG_ON(osb->osb_resv_level != 0);
  737. /*
  738. * Reservations are disabled. Handle this the old way.
  739. */
  740. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  741. numfound = bitoff = startoff = 0;
  742. lastzero = -1;
  743. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  744. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  745. if (bitoff == left) {
  746. /* mlog(0, "bitoff (%d) == left", bitoff); */
  747. break;
  748. }
  749. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  750. "numfound = %d\n", bitoff, startoff, numfound);*/
  751. /* Ok, we found a zero bit... is it contig. or do we
  752. * start over?*/
  753. if (bitoff == startoff) {
  754. /* we found a zero */
  755. numfound++;
  756. startoff++;
  757. } else {
  758. /* got a zero after some ones */
  759. numfound = 1;
  760. startoff = bitoff+1;
  761. }
  762. /* we got everything we needed */
  763. if (numfound == *numbits) {
  764. /* mlog(0, "Found it all!\n"); */
  765. break;
  766. }
  767. }
  768. trace_ocfs2_local_alloc_find_clear_bits_search_bitmap(bitoff, numfound);
  769. if (numfound == *numbits)
  770. bitoff = startoff - numfound;
  771. else
  772. bitoff = -1;
  773. bail:
  774. if (local_resv)
  775. ocfs2_resv_discard(resmap, resv);
  776. trace_ocfs2_local_alloc_find_clear_bits(*numbits,
  777. le32_to_cpu(alloc->id1.bitmap1.i_total),
  778. bitoff, numfound);
  779. return bitoff;
  780. }
  781. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  782. {
  783. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  784. int i;
  785. alloc->id1.bitmap1.i_total = 0;
  786. alloc->id1.bitmap1.i_used = 0;
  787. la->la_bm_off = 0;
  788. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  789. la->la_bitmap[i] = 0;
  790. }
  791. #if 0
  792. /* turn this on and uncomment below to aid debugging window shifts. */
  793. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  794. unsigned int start,
  795. unsigned int count)
  796. {
  797. unsigned int tmp = count;
  798. while(tmp--) {
  799. if (ocfs2_test_bit(start + tmp, bitmap)) {
  800. printk("ocfs2_verify_zero_bits: start = %u, count = "
  801. "%u\n", start, count);
  802. printk("ocfs2_verify_zero_bits: bit %u is set!",
  803. start + tmp);
  804. BUG();
  805. }
  806. }
  807. }
  808. #endif
  809. /*
  810. * sync the local alloc to main bitmap.
  811. *
  812. * assumes you've already locked the main bitmap -- the bitmap inode
  813. * passed is used for caching.
  814. */
  815. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  816. handle_t *handle,
  817. struct ocfs2_dinode *alloc,
  818. struct inode *main_bm_inode,
  819. struct buffer_head *main_bm_bh)
  820. {
  821. int status = 0;
  822. int bit_off, left, count, start;
  823. u64 la_start_blk;
  824. u64 blkno;
  825. void *bitmap;
  826. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  827. trace_ocfs2_sync_local_to_main(
  828. le32_to_cpu(alloc->id1.bitmap1.i_total),
  829. le32_to_cpu(alloc->id1.bitmap1.i_used));
  830. if (!alloc->id1.bitmap1.i_total) {
  831. goto bail;
  832. }
  833. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  834. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  835. goto bail;
  836. }
  837. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  838. le32_to_cpu(la->la_bm_off));
  839. bitmap = la->la_bitmap;
  840. start = count = bit_off = 0;
  841. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  842. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  843. != -1) {
  844. if ((bit_off < left) && (bit_off == start)) {
  845. count++;
  846. start++;
  847. continue;
  848. }
  849. if (count) {
  850. blkno = la_start_blk +
  851. ocfs2_clusters_to_blocks(osb->sb,
  852. start - count);
  853. trace_ocfs2_sync_local_to_main_free(
  854. count, start - count,
  855. (unsigned long long)la_start_blk,
  856. (unsigned long long)blkno);
  857. status = ocfs2_release_clusters(handle,
  858. main_bm_inode,
  859. main_bm_bh, blkno,
  860. count);
  861. if (status < 0) {
  862. mlog_errno(status);
  863. goto bail;
  864. }
  865. }
  866. if (bit_off >= left)
  867. break;
  868. count = 1;
  869. start = bit_off + 1;
  870. }
  871. bail:
  872. if (status)
  873. mlog_errno(status);
  874. return status;
  875. }
  876. enum ocfs2_la_event {
  877. OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
  878. OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
  879. * enough bits theoretically
  880. * free, but a contiguous
  881. * allocation could not be
  882. * found. */
  883. OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
  884. * enough bits free to satisfy
  885. * our request. */
  886. };
  887. #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
  888. /*
  889. * Given an event, calculate the size of our next local alloc window.
  890. *
  891. * This should always be called under i_mutex of the local alloc inode
  892. * so that local alloc disabling doesn't race with processes trying to
  893. * use the allocator.
  894. *
  895. * Returns the state which the local alloc was left in. This value can
  896. * be ignored by some paths.
  897. */
  898. static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
  899. enum ocfs2_la_event event)
  900. {
  901. unsigned int bits;
  902. int state;
  903. spin_lock(&osb->osb_lock);
  904. if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
  905. WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
  906. goto out_unlock;
  907. }
  908. /*
  909. * ENOSPC and fragmentation are treated similarly for now.
  910. */
  911. if (event == OCFS2_LA_EVENT_ENOSPC ||
  912. event == OCFS2_LA_EVENT_FRAGMENTED) {
  913. /*
  914. * We ran out of contiguous space in the primary
  915. * bitmap. Drastically reduce the number of bits used
  916. * by local alloc until we have to disable it.
  917. */
  918. bits = osb->local_alloc_bits >> 1;
  919. if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
  920. /*
  921. * By setting state to THROTTLED, we'll keep
  922. * the number of local alloc bits used down
  923. * until an event occurs which would give us
  924. * reason to assume the bitmap situation might
  925. * have changed.
  926. */
  927. osb->local_alloc_state = OCFS2_LA_THROTTLED;
  928. osb->local_alloc_bits = bits;
  929. } else {
  930. osb->local_alloc_state = OCFS2_LA_DISABLED;
  931. }
  932. queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
  933. OCFS2_LA_ENABLE_INTERVAL);
  934. goto out_unlock;
  935. }
  936. /*
  937. * Don't increase the size of the local alloc window until we
  938. * know we might be able to fulfill the request. Otherwise, we
  939. * risk bouncing around the global bitmap during periods of
  940. * low space.
  941. */
  942. if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
  943. osb->local_alloc_bits = osb->local_alloc_default_bits;
  944. out_unlock:
  945. state = osb->local_alloc_state;
  946. spin_unlock(&osb->osb_lock);
  947. return state;
  948. }
  949. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  950. struct ocfs2_alloc_context **ac,
  951. struct inode **bitmap_inode,
  952. struct buffer_head **bitmap_bh)
  953. {
  954. int status;
  955. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  956. if (!(*ac)) {
  957. status = -ENOMEM;
  958. mlog_errno(status);
  959. goto bail;
  960. }
  961. retry_enospc:
  962. (*ac)->ac_bits_wanted = osb->local_alloc_bits;
  963. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  964. if (status == -ENOSPC) {
  965. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
  966. OCFS2_LA_DISABLED)
  967. goto bail;
  968. ocfs2_free_ac_resource(*ac);
  969. memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
  970. goto retry_enospc;
  971. }
  972. if (status < 0) {
  973. mlog_errno(status);
  974. goto bail;
  975. }
  976. *bitmap_inode = (*ac)->ac_inode;
  977. igrab(*bitmap_inode);
  978. *bitmap_bh = (*ac)->ac_bh;
  979. get_bh(*bitmap_bh);
  980. status = 0;
  981. bail:
  982. if ((status < 0) && *ac) {
  983. ocfs2_free_alloc_context(*ac);
  984. *ac = NULL;
  985. }
  986. if (status)
  987. mlog_errno(status);
  988. return status;
  989. }
  990. /*
  991. * pass it the bitmap lock in lock_bh if you have it.
  992. */
  993. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  994. handle_t *handle,
  995. struct ocfs2_alloc_context *ac)
  996. {
  997. int status = 0;
  998. u32 cluster_off, cluster_count;
  999. struct ocfs2_dinode *alloc = NULL;
  1000. struct ocfs2_local_alloc *la;
  1001. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  1002. la = OCFS2_LOCAL_ALLOC(alloc);
  1003. trace_ocfs2_local_alloc_new_window(
  1004. le32_to_cpu(alloc->id1.bitmap1.i_total),
  1005. osb->local_alloc_bits);
  1006. /* Instruct the allocation code to try the most recently used
  1007. * cluster group. We'll re-record the group used this pass
  1008. * below. */
  1009. ac->ac_last_group = osb->la_last_gd;
  1010. /* we used the generic suballoc reserve function, but we set
  1011. * everything up nicely, so there's no reason why we can't use
  1012. * the more specific cluster api to claim bits. */
  1013. status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
  1014. &cluster_off, &cluster_count);
  1015. if (status == -ENOSPC) {
  1016. retry_enospc:
  1017. /*
  1018. * Note: We could also try syncing the journal here to
  1019. * allow use of any free bits which the current
  1020. * transaction can't give us access to. --Mark
  1021. */
  1022. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
  1023. OCFS2_LA_DISABLED)
  1024. goto bail;
  1025. ac->ac_bits_wanted = osb->local_alloc_bits;
  1026. status = ocfs2_claim_clusters(handle, ac,
  1027. osb->local_alloc_bits,
  1028. &cluster_off,
  1029. &cluster_count);
  1030. if (status == -ENOSPC)
  1031. goto retry_enospc;
  1032. /*
  1033. * We only shrunk the *minimum* number of in our
  1034. * request - it's entirely possible that the allocator
  1035. * might give us more than we asked for.
  1036. */
  1037. if (status == 0) {
  1038. spin_lock(&osb->osb_lock);
  1039. osb->local_alloc_bits = cluster_count;
  1040. spin_unlock(&osb->osb_lock);
  1041. }
  1042. }
  1043. if (status < 0) {
  1044. if (status != -ENOSPC)
  1045. mlog_errno(status);
  1046. goto bail;
  1047. }
  1048. osb->la_last_gd = ac->ac_last_group;
  1049. la->la_bm_off = cpu_to_le32(cluster_off);
  1050. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  1051. /* just in case... In the future when we find space ourselves,
  1052. * we don't have to get all contiguous -- but we'll have to
  1053. * set all previously used bits in bitmap and update
  1054. * la_bits_set before setting the bits in the main bitmap. */
  1055. alloc->id1.bitmap1.i_used = 0;
  1056. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  1057. le16_to_cpu(la->la_size));
  1058. ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
  1059. OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
  1060. trace_ocfs2_local_alloc_new_window_result(
  1061. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off,
  1062. le32_to_cpu(alloc->id1.bitmap1.i_total));
  1063. bail:
  1064. if (status)
  1065. mlog_errno(status);
  1066. return status;
  1067. }
  1068. /* Note that we do *NOT* lock the local alloc inode here as
  1069. * it's been locked already for us. */
  1070. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  1071. struct inode *local_alloc_inode)
  1072. {
  1073. int status = 0;
  1074. struct buffer_head *main_bm_bh = NULL;
  1075. struct inode *main_bm_inode = NULL;
  1076. handle_t *handle = NULL;
  1077. struct ocfs2_dinode *alloc;
  1078. struct ocfs2_dinode *alloc_copy = NULL;
  1079. struct ocfs2_alloc_context *ac = NULL;
  1080. ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
  1081. /* This will lock the main bitmap for us. */
  1082. status = ocfs2_local_alloc_reserve_for_window(osb,
  1083. &ac,
  1084. &main_bm_inode,
  1085. &main_bm_bh);
  1086. if (status < 0) {
  1087. if (status != -ENOSPC)
  1088. mlog_errno(status);
  1089. goto bail;
  1090. }
  1091. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  1092. if (IS_ERR(handle)) {
  1093. status = PTR_ERR(handle);
  1094. handle = NULL;
  1095. mlog_errno(status);
  1096. goto bail;
  1097. }
  1098. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  1099. /* We want to clear the local alloc before doing anything
  1100. * else, so that if we error later during this operation,
  1101. * local alloc shutdown won't try to double free main bitmap
  1102. * bits. Make a copy so the sync function knows which bits to
  1103. * free. */
  1104. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
  1105. if (!alloc_copy) {
  1106. status = -ENOMEM;
  1107. mlog_errno(status);
  1108. goto bail;
  1109. }
  1110. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  1111. status = ocfs2_journal_access_di(handle,
  1112. INODE_CACHE(local_alloc_inode),
  1113. osb->local_alloc_bh,
  1114. OCFS2_JOURNAL_ACCESS_WRITE);
  1115. if (status < 0) {
  1116. mlog_errno(status);
  1117. goto bail;
  1118. }
  1119. ocfs2_clear_local_alloc(alloc);
  1120. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  1121. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  1122. main_bm_inode, main_bm_bh);
  1123. if (status < 0) {
  1124. mlog_errno(status);
  1125. goto bail;
  1126. }
  1127. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  1128. if (status < 0) {
  1129. if (status != -ENOSPC)
  1130. mlog_errno(status);
  1131. goto bail;
  1132. }
  1133. atomic_inc(&osb->alloc_stats.moves);
  1134. bail:
  1135. if (handle)
  1136. ocfs2_commit_trans(osb, handle);
  1137. brelse(main_bm_bh);
  1138. if (main_bm_inode)
  1139. iput(main_bm_inode);
  1140. kfree(alloc_copy);
  1141. if (ac)
  1142. ocfs2_free_alloc_context(ac);
  1143. if (status)
  1144. mlog_errno(status);
  1145. return status;
  1146. }