localalloc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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, "Local alloc hasn't been recovered!\n"
  303. "found = %u, set = %u, taken = %u, off = %u\n",
  304. num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
  305. le32_to_cpu(alloc->id1.bitmap1.i_total),
  306. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  307. osb->local_alloc_bh = alloc_bh;
  308. osb->local_alloc_state = OCFS2_LA_ENABLED;
  309. bail:
  310. if (status < 0)
  311. brelse(alloc_bh);
  312. iput(inode);
  313. trace_ocfs2_load_local_alloc(osb->local_alloc_bits);
  314. if (status)
  315. mlog_errno(status);
  316. return status;
  317. }
  318. /*
  319. * return any unused bits to the bitmap and write out a clean
  320. * local_alloc.
  321. *
  322. * local_alloc_bh is optional. If not passed, we will simply use the
  323. * one off osb. If you do pass it however, be warned that it *will* be
  324. * returned brelse'd and NULL'd out.*/
  325. void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
  326. {
  327. int status;
  328. handle_t *handle;
  329. struct inode *local_alloc_inode = NULL;
  330. struct buffer_head *bh = NULL;
  331. struct buffer_head *main_bm_bh = NULL;
  332. struct inode *main_bm_inode = NULL;
  333. struct ocfs2_dinode *alloc_copy = NULL;
  334. struct ocfs2_dinode *alloc = NULL;
  335. cancel_delayed_work(&osb->la_enable_wq);
  336. flush_workqueue(osb->ocfs2_wq);
  337. if (osb->local_alloc_state == OCFS2_LA_UNUSED)
  338. goto out;
  339. local_alloc_inode =
  340. ocfs2_get_system_file_inode(osb,
  341. LOCAL_ALLOC_SYSTEM_INODE,
  342. osb->slot_num);
  343. if (!local_alloc_inode) {
  344. status = -ENOENT;
  345. mlog_errno(status);
  346. goto out;
  347. }
  348. osb->local_alloc_state = OCFS2_LA_DISABLED;
  349. ocfs2_resmap_uninit(&osb->osb_la_resmap);
  350. main_bm_inode = ocfs2_get_system_file_inode(osb,
  351. GLOBAL_BITMAP_SYSTEM_INODE,
  352. OCFS2_INVALID_SLOT);
  353. if (!main_bm_inode) {
  354. status = -EINVAL;
  355. mlog_errno(status);
  356. goto out;
  357. }
  358. inode_lock(main_bm_inode);
  359. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  360. if (status < 0) {
  361. mlog_errno(status);
  362. goto out_mutex;
  363. }
  364. /* WINDOW_MOVE_CREDITS is a bit heavy... */
  365. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  366. if (IS_ERR(handle)) {
  367. mlog_errno(PTR_ERR(handle));
  368. handle = NULL;
  369. goto out_unlock;
  370. }
  371. bh = osb->local_alloc_bh;
  372. alloc = (struct ocfs2_dinode *) bh->b_data;
  373. alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
  374. if (!alloc_copy) {
  375. status = -ENOMEM;
  376. goto out_commit;
  377. }
  378. memcpy(alloc_copy, alloc, bh->b_size);
  379. status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
  380. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  381. if (status < 0) {
  382. mlog_errno(status);
  383. goto out_commit;
  384. }
  385. ocfs2_clear_local_alloc(alloc);
  386. ocfs2_journal_dirty(handle, bh);
  387. brelse(bh);
  388. osb->local_alloc_bh = NULL;
  389. osb->local_alloc_state = OCFS2_LA_UNUSED;
  390. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  391. main_bm_inode, main_bm_bh);
  392. if (status < 0)
  393. mlog_errno(status);
  394. out_commit:
  395. ocfs2_commit_trans(osb, handle);
  396. out_unlock:
  397. brelse(main_bm_bh);
  398. ocfs2_inode_unlock(main_bm_inode, 1);
  399. out_mutex:
  400. inode_unlock(main_bm_inode);
  401. iput(main_bm_inode);
  402. out:
  403. iput(local_alloc_inode);
  404. kfree(alloc_copy);
  405. }
  406. /*
  407. * We want to free the bitmap bits outside of any recovery context as
  408. * we'll need a cluster lock to do so, but we must clear the local
  409. * alloc before giving up the recovered nodes journal. To solve this,
  410. * we kmalloc a copy of the local alloc before it's change for the
  411. * caller to process with ocfs2_complete_local_alloc_recovery
  412. */
  413. int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
  414. int slot_num,
  415. struct ocfs2_dinode **alloc_copy)
  416. {
  417. int status = 0;
  418. struct buffer_head *alloc_bh = NULL;
  419. struct inode *inode = NULL;
  420. struct ocfs2_dinode *alloc;
  421. trace_ocfs2_begin_local_alloc_recovery(slot_num);
  422. *alloc_copy = NULL;
  423. inode = ocfs2_get_system_file_inode(osb,
  424. LOCAL_ALLOC_SYSTEM_INODE,
  425. slot_num);
  426. if (!inode) {
  427. status = -EINVAL;
  428. mlog_errno(status);
  429. goto bail;
  430. }
  431. inode_lock(inode);
  432. status = ocfs2_read_inode_block_full(inode, &alloc_bh,
  433. OCFS2_BH_IGNORE_CACHE);
  434. if (status < 0) {
  435. mlog_errno(status);
  436. goto bail;
  437. }
  438. *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
  439. if (!(*alloc_copy)) {
  440. status = -ENOMEM;
  441. goto bail;
  442. }
  443. memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
  444. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  445. ocfs2_clear_local_alloc(alloc);
  446. ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
  447. status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
  448. if (status < 0)
  449. mlog_errno(status);
  450. bail:
  451. if (status < 0) {
  452. kfree(*alloc_copy);
  453. *alloc_copy = NULL;
  454. }
  455. brelse(alloc_bh);
  456. if (inode) {
  457. inode_unlock(inode);
  458. iput(inode);
  459. }
  460. if (status)
  461. mlog_errno(status);
  462. return status;
  463. }
  464. /*
  465. * Step 2: By now, we've completed the journal recovery, we've stamped
  466. * a clean local alloc on disk and dropped the node out of the
  467. * recovery map. Dlm locks will no longer stall, so lets clear out the
  468. * main bitmap.
  469. */
  470. int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
  471. struct ocfs2_dinode *alloc)
  472. {
  473. int status;
  474. handle_t *handle;
  475. struct buffer_head *main_bm_bh = NULL;
  476. struct inode *main_bm_inode;
  477. main_bm_inode = ocfs2_get_system_file_inode(osb,
  478. GLOBAL_BITMAP_SYSTEM_INODE,
  479. OCFS2_INVALID_SLOT);
  480. if (!main_bm_inode) {
  481. status = -EINVAL;
  482. mlog_errno(status);
  483. goto out;
  484. }
  485. inode_lock(main_bm_inode);
  486. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  487. if (status < 0) {
  488. mlog_errno(status);
  489. goto out_mutex;
  490. }
  491. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  492. if (IS_ERR(handle)) {
  493. status = PTR_ERR(handle);
  494. handle = NULL;
  495. mlog_errno(status);
  496. goto out_unlock;
  497. }
  498. /* we want the bitmap change to be recorded on disk asap */
  499. handle->h_sync = 1;
  500. status = ocfs2_sync_local_to_main(osb, handle, alloc,
  501. main_bm_inode, main_bm_bh);
  502. if (status < 0)
  503. mlog_errno(status);
  504. ocfs2_commit_trans(osb, handle);
  505. out_unlock:
  506. ocfs2_inode_unlock(main_bm_inode, 1);
  507. out_mutex:
  508. inode_unlock(main_bm_inode);
  509. brelse(main_bm_bh);
  510. iput(main_bm_inode);
  511. out:
  512. if (!status)
  513. ocfs2_init_steal_slots(osb);
  514. if (status)
  515. mlog_errno(status);
  516. return status;
  517. }
  518. /*
  519. * make sure we've got at least bits_wanted contiguous bits in the
  520. * local alloc. You lose them when you drop i_mutex.
  521. *
  522. * We will add ourselves to the transaction passed in, but may start
  523. * our own in order to shift windows.
  524. */
  525. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  526. u32 bits_wanted,
  527. struct ocfs2_alloc_context *ac)
  528. {
  529. int status;
  530. struct ocfs2_dinode *alloc;
  531. struct inode *local_alloc_inode;
  532. unsigned int free_bits;
  533. BUG_ON(!ac);
  534. local_alloc_inode =
  535. ocfs2_get_system_file_inode(osb,
  536. LOCAL_ALLOC_SYSTEM_INODE,
  537. osb->slot_num);
  538. if (!local_alloc_inode) {
  539. status = -ENOENT;
  540. mlog_errno(status);
  541. goto bail;
  542. }
  543. inode_lock(local_alloc_inode);
  544. /*
  545. * We must double check state and allocator bits because
  546. * another process may have changed them while holding i_mutex.
  547. */
  548. spin_lock(&osb->osb_lock);
  549. if (!ocfs2_la_state_enabled(osb) ||
  550. (bits_wanted > osb->local_alloc_bits)) {
  551. spin_unlock(&osb->osb_lock);
  552. status = -ENOSPC;
  553. goto bail;
  554. }
  555. spin_unlock(&osb->osb_lock);
  556. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  557. #ifdef CONFIG_OCFS2_DEBUG_FS
  558. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  559. ocfs2_local_alloc_count_bits(alloc)) {
  560. status = ocfs2_error(osb->sb, "local alloc inode %llu says it has %u used bits, but a count shows %u\n",
  561. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  562. le32_to_cpu(alloc->id1.bitmap1.i_used),
  563. ocfs2_local_alloc_count_bits(alloc));
  564. goto bail;
  565. }
  566. #endif
  567. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  568. le32_to_cpu(alloc->id1.bitmap1.i_used);
  569. if (bits_wanted > free_bits) {
  570. /* uhoh, window change time. */
  571. status =
  572. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  573. if (status < 0) {
  574. if (status != -ENOSPC)
  575. mlog_errno(status);
  576. goto bail;
  577. }
  578. /*
  579. * Under certain conditions, the window slide code
  580. * might have reduced the number of bits available or
  581. * disabled the the local alloc entirely. Re-check
  582. * here and return -ENOSPC if necessary.
  583. */
  584. status = -ENOSPC;
  585. if (!ocfs2_la_state_enabled(osb))
  586. goto bail;
  587. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  588. le32_to_cpu(alloc->id1.bitmap1.i_used);
  589. if (bits_wanted > free_bits)
  590. goto bail;
  591. }
  592. ac->ac_inode = local_alloc_inode;
  593. /* We should never use localalloc from another slot */
  594. ac->ac_alloc_slot = osb->slot_num;
  595. ac->ac_which = OCFS2_AC_USE_LOCAL;
  596. get_bh(osb->local_alloc_bh);
  597. ac->ac_bh = osb->local_alloc_bh;
  598. status = 0;
  599. bail:
  600. if (status < 0 && local_alloc_inode) {
  601. inode_unlock(local_alloc_inode);
  602. iput(local_alloc_inode);
  603. }
  604. trace_ocfs2_reserve_local_alloc_bits(
  605. (unsigned long long)ac->ac_max_block,
  606. bits_wanted, osb->slot_num, status);
  607. if (status)
  608. mlog_errno(status);
  609. return status;
  610. }
  611. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  612. handle_t *handle,
  613. struct ocfs2_alloc_context *ac,
  614. u32 bits_wanted,
  615. u32 *bit_off,
  616. u32 *num_bits)
  617. {
  618. int status, start;
  619. struct inode *local_alloc_inode;
  620. void *bitmap;
  621. struct ocfs2_dinode *alloc;
  622. struct ocfs2_local_alloc *la;
  623. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  624. local_alloc_inode = ac->ac_inode;
  625. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  626. la = OCFS2_LOCAL_ALLOC(alloc);
  627. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
  628. ac->ac_resv);
  629. if (start == -1) {
  630. /* TODO: Shouldn't we just BUG here? */
  631. status = -ENOSPC;
  632. mlog_errno(status);
  633. goto bail;
  634. }
  635. bitmap = la->la_bitmap;
  636. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  637. *num_bits = bits_wanted;
  638. status = ocfs2_journal_access_di(handle,
  639. INODE_CACHE(local_alloc_inode),
  640. osb->local_alloc_bh,
  641. OCFS2_JOURNAL_ACCESS_WRITE);
  642. if (status < 0) {
  643. mlog_errno(status);
  644. goto bail;
  645. }
  646. ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
  647. bits_wanted);
  648. while(bits_wanted--)
  649. ocfs2_set_bit(start++, bitmap);
  650. le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
  651. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  652. bail:
  653. if (status)
  654. mlog_errno(status);
  655. return status;
  656. }
  657. int ocfs2_free_local_alloc_bits(struct ocfs2_super *osb,
  658. handle_t *handle,
  659. struct ocfs2_alloc_context *ac,
  660. u32 bit_off,
  661. u32 num_bits)
  662. {
  663. int status, start;
  664. u32 clear_bits;
  665. struct inode *local_alloc_inode;
  666. void *bitmap;
  667. struct ocfs2_dinode *alloc;
  668. struct ocfs2_local_alloc *la;
  669. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  670. local_alloc_inode = ac->ac_inode;
  671. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  672. la = OCFS2_LOCAL_ALLOC(alloc);
  673. bitmap = la->la_bitmap;
  674. start = bit_off - le32_to_cpu(la->la_bm_off);
  675. clear_bits = num_bits;
  676. status = ocfs2_journal_access_di(handle,
  677. INODE_CACHE(local_alloc_inode),
  678. osb->local_alloc_bh,
  679. OCFS2_JOURNAL_ACCESS_WRITE);
  680. if (status < 0) {
  681. mlog_errno(status);
  682. goto bail;
  683. }
  684. while (clear_bits--)
  685. ocfs2_clear_bit(start++, bitmap);
  686. le32_add_cpu(&alloc->id1.bitmap1.i_used, -num_bits);
  687. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  688. bail:
  689. return status;
  690. }
  691. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  692. {
  693. u32 count;
  694. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  695. count = memweight(la->la_bitmap, le16_to_cpu(la->la_size));
  696. trace_ocfs2_local_alloc_count_bits(count);
  697. return count;
  698. }
  699. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  700. struct ocfs2_dinode *alloc,
  701. u32 *numbits,
  702. struct ocfs2_alloc_reservation *resv)
  703. {
  704. int numfound = 0, bitoff, left, startoff, lastzero;
  705. int local_resv = 0;
  706. struct ocfs2_alloc_reservation r;
  707. void *bitmap = NULL;
  708. struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
  709. if (!alloc->id1.bitmap1.i_total) {
  710. bitoff = -1;
  711. goto bail;
  712. }
  713. if (!resv) {
  714. local_resv = 1;
  715. ocfs2_resv_init_once(&r);
  716. ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
  717. resv = &r;
  718. }
  719. numfound = *numbits;
  720. if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
  721. if (numfound < *numbits)
  722. *numbits = numfound;
  723. goto bail;
  724. }
  725. /*
  726. * Code error. While reservations are enabled, local
  727. * allocation should _always_ go through them.
  728. */
  729. BUG_ON(osb->osb_resv_level != 0);
  730. /*
  731. * Reservations are disabled. Handle this the old way.
  732. */
  733. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  734. numfound = bitoff = startoff = 0;
  735. lastzero = -1;
  736. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  737. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  738. if (bitoff == left) {
  739. /* mlog(0, "bitoff (%d) == left", bitoff); */
  740. break;
  741. }
  742. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  743. "numfound = %d\n", bitoff, startoff, numfound);*/
  744. /* Ok, we found a zero bit... is it contig. or do we
  745. * start over?*/
  746. if (bitoff == startoff) {
  747. /* we found a zero */
  748. numfound++;
  749. startoff++;
  750. } else {
  751. /* got a zero after some ones */
  752. numfound = 1;
  753. startoff = bitoff+1;
  754. }
  755. /* we got everything we needed */
  756. if (numfound == *numbits) {
  757. /* mlog(0, "Found it all!\n"); */
  758. break;
  759. }
  760. }
  761. trace_ocfs2_local_alloc_find_clear_bits_search_bitmap(bitoff, numfound);
  762. if (numfound == *numbits)
  763. bitoff = startoff - numfound;
  764. else
  765. bitoff = -1;
  766. bail:
  767. if (local_resv)
  768. ocfs2_resv_discard(resmap, resv);
  769. trace_ocfs2_local_alloc_find_clear_bits(*numbits,
  770. le32_to_cpu(alloc->id1.bitmap1.i_total),
  771. bitoff, numfound);
  772. return bitoff;
  773. }
  774. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  775. {
  776. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  777. int i;
  778. alloc->id1.bitmap1.i_total = 0;
  779. alloc->id1.bitmap1.i_used = 0;
  780. la->la_bm_off = 0;
  781. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  782. la->la_bitmap[i] = 0;
  783. }
  784. #if 0
  785. /* turn this on and uncomment below to aid debugging window shifts. */
  786. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  787. unsigned int start,
  788. unsigned int count)
  789. {
  790. unsigned int tmp = count;
  791. while(tmp--) {
  792. if (ocfs2_test_bit(start + tmp, bitmap)) {
  793. printk("ocfs2_verify_zero_bits: start = %u, count = "
  794. "%u\n", start, count);
  795. printk("ocfs2_verify_zero_bits: bit %u is set!",
  796. start + tmp);
  797. BUG();
  798. }
  799. }
  800. }
  801. #endif
  802. /*
  803. * sync the local alloc to main bitmap.
  804. *
  805. * assumes you've already locked the main bitmap -- the bitmap inode
  806. * passed is used for caching.
  807. */
  808. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  809. handle_t *handle,
  810. struct ocfs2_dinode *alloc,
  811. struct inode *main_bm_inode,
  812. struct buffer_head *main_bm_bh)
  813. {
  814. int status = 0;
  815. int bit_off, left, count, start;
  816. u64 la_start_blk;
  817. u64 blkno;
  818. void *bitmap;
  819. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  820. trace_ocfs2_sync_local_to_main(
  821. le32_to_cpu(alloc->id1.bitmap1.i_total),
  822. le32_to_cpu(alloc->id1.bitmap1.i_used));
  823. if (!alloc->id1.bitmap1.i_total) {
  824. goto bail;
  825. }
  826. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  827. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  828. goto bail;
  829. }
  830. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  831. le32_to_cpu(la->la_bm_off));
  832. bitmap = la->la_bitmap;
  833. start = count = bit_off = 0;
  834. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  835. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  836. != -1) {
  837. if ((bit_off < left) && (bit_off == start)) {
  838. count++;
  839. start++;
  840. continue;
  841. }
  842. if (count) {
  843. blkno = la_start_blk +
  844. ocfs2_clusters_to_blocks(osb->sb,
  845. start - count);
  846. trace_ocfs2_sync_local_to_main_free(
  847. count, start - count,
  848. (unsigned long long)la_start_blk,
  849. (unsigned long long)blkno);
  850. status = ocfs2_release_clusters(handle,
  851. main_bm_inode,
  852. main_bm_bh, blkno,
  853. count);
  854. if (status < 0) {
  855. mlog_errno(status);
  856. goto bail;
  857. }
  858. }
  859. if (bit_off >= left)
  860. break;
  861. count = 1;
  862. start = bit_off + 1;
  863. }
  864. bail:
  865. if (status)
  866. mlog_errno(status);
  867. return status;
  868. }
  869. enum ocfs2_la_event {
  870. OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
  871. OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
  872. * enough bits theoretically
  873. * free, but a contiguous
  874. * allocation could not be
  875. * found. */
  876. OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
  877. * enough bits free to satisfy
  878. * our request. */
  879. };
  880. #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
  881. /*
  882. * Given an event, calculate the size of our next local alloc window.
  883. *
  884. * This should always be called under i_mutex of the local alloc inode
  885. * so that local alloc disabling doesn't race with processes trying to
  886. * use the allocator.
  887. *
  888. * Returns the state which the local alloc was left in. This value can
  889. * be ignored by some paths.
  890. */
  891. static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
  892. enum ocfs2_la_event event)
  893. {
  894. unsigned int bits;
  895. int state;
  896. spin_lock(&osb->osb_lock);
  897. if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
  898. WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
  899. goto out_unlock;
  900. }
  901. /*
  902. * ENOSPC and fragmentation are treated similarly for now.
  903. */
  904. if (event == OCFS2_LA_EVENT_ENOSPC ||
  905. event == OCFS2_LA_EVENT_FRAGMENTED) {
  906. /*
  907. * We ran out of contiguous space in the primary
  908. * bitmap. Drastically reduce the number of bits used
  909. * by local alloc until we have to disable it.
  910. */
  911. bits = osb->local_alloc_bits >> 1;
  912. if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
  913. /*
  914. * By setting state to THROTTLED, we'll keep
  915. * the number of local alloc bits used down
  916. * until an event occurs which would give us
  917. * reason to assume the bitmap situation might
  918. * have changed.
  919. */
  920. osb->local_alloc_state = OCFS2_LA_THROTTLED;
  921. osb->local_alloc_bits = bits;
  922. } else {
  923. osb->local_alloc_state = OCFS2_LA_DISABLED;
  924. }
  925. queue_delayed_work(osb->ocfs2_wq, &osb->la_enable_wq,
  926. OCFS2_LA_ENABLE_INTERVAL);
  927. goto out_unlock;
  928. }
  929. /*
  930. * Don't increase the size of the local alloc window until we
  931. * know we might be able to fulfill the request. Otherwise, we
  932. * risk bouncing around the global bitmap during periods of
  933. * low space.
  934. */
  935. if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
  936. osb->local_alloc_bits = osb->local_alloc_default_bits;
  937. out_unlock:
  938. state = osb->local_alloc_state;
  939. spin_unlock(&osb->osb_lock);
  940. return state;
  941. }
  942. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  943. struct ocfs2_alloc_context **ac,
  944. struct inode **bitmap_inode,
  945. struct buffer_head **bitmap_bh)
  946. {
  947. int status;
  948. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  949. if (!(*ac)) {
  950. status = -ENOMEM;
  951. mlog_errno(status);
  952. goto bail;
  953. }
  954. retry_enospc:
  955. (*ac)->ac_bits_wanted = osb->local_alloc_bits;
  956. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  957. if (status == -ENOSPC) {
  958. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
  959. OCFS2_LA_DISABLED)
  960. goto bail;
  961. ocfs2_free_ac_resource(*ac);
  962. memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
  963. goto retry_enospc;
  964. }
  965. if (status < 0) {
  966. mlog_errno(status);
  967. goto bail;
  968. }
  969. *bitmap_inode = (*ac)->ac_inode;
  970. igrab(*bitmap_inode);
  971. *bitmap_bh = (*ac)->ac_bh;
  972. get_bh(*bitmap_bh);
  973. status = 0;
  974. bail:
  975. if ((status < 0) && *ac) {
  976. ocfs2_free_alloc_context(*ac);
  977. *ac = NULL;
  978. }
  979. if (status)
  980. mlog_errno(status);
  981. return status;
  982. }
  983. /*
  984. * pass it the bitmap lock in lock_bh if you have it.
  985. */
  986. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  987. handle_t *handle,
  988. struct ocfs2_alloc_context *ac)
  989. {
  990. int status = 0;
  991. u32 cluster_off, cluster_count;
  992. struct ocfs2_dinode *alloc = NULL;
  993. struct ocfs2_local_alloc *la;
  994. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  995. la = OCFS2_LOCAL_ALLOC(alloc);
  996. trace_ocfs2_local_alloc_new_window(
  997. le32_to_cpu(alloc->id1.bitmap1.i_total),
  998. osb->local_alloc_bits);
  999. /* Instruct the allocation code to try the most recently used
  1000. * cluster group. We'll re-record the group used this pass
  1001. * below. */
  1002. ac->ac_last_group = osb->la_last_gd;
  1003. /* we used the generic suballoc reserve function, but we set
  1004. * everything up nicely, so there's no reason why we can't use
  1005. * the more specific cluster api to claim bits. */
  1006. status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
  1007. &cluster_off, &cluster_count);
  1008. if (status == -ENOSPC) {
  1009. retry_enospc:
  1010. /*
  1011. * Note: We could also try syncing the journal here to
  1012. * allow use of any free bits which the current
  1013. * transaction can't give us access to. --Mark
  1014. */
  1015. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
  1016. OCFS2_LA_DISABLED)
  1017. goto bail;
  1018. ac->ac_bits_wanted = osb->local_alloc_bits;
  1019. status = ocfs2_claim_clusters(handle, ac,
  1020. osb->local_alloc_bits,
  1021. &cluster_off,
  1022. &cluster_count);
  1023. if (status == -ENOSPC)
  1024. goto retry_enospc;
  1025. /*
  1026. * We only shrunk the *minimum* number of in our
  1027. * request - it's entirely possible that the allocator
  1028. * might give us more than we asked for.
  1029. */
  1030. if (status == 0) {
  1031. spin_lock(&osb->osb_lock);
  1032. osb->local_alloc_bits = cluster_count;
  1033. spin_unlock(&osb->osb_lock);
  1034. }
  1035. }
  1036. if (status < 0) {
  1037. if (status != -ENOSPC)
  1038. mlog_errno(status);
  1039. goto bail;
  1040. }
  1041. osb->la_last_gd = ac->ac_last_group;
  1042. la->la_bm_off = cpu_to_le32(cluster_off);
  1043. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  1044. /* just in case... In the future when we find space ourselves,
  1045. * we don't have to get all contiguous -- but we'll have to
  1046. * set all previously used bits in bitmap and update
  1047. * la_bits_set before setting the bits in the main bitmap. */
  1048. alloc->id1.bitmap1.i_used = 0;
  1049. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  1050. le16_to_cpu(la->la_size));
  1051. ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
  1052. OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
  1053. trace_ocfs2_local_alloc_new_window_result(
  1054. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off,
  1055. le32_to_cpu(alloc->id1.bitmap1.i_total));
  1056. bail:
  1057. if (status)
  1058. mlog_errno(status);
  1059. return status;
  1060. }
  1061. /* Note that we do *NOT* lock the local alloc inode here as
  1062. * it's been locked already for us. */
  1063. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  1064. struct inode *local_alloc_inode)
  1065. {
  1066. int status = 0;
  1067. struct buffer_head *main_bm_bh = NULL;
  1068. struct inode *main_bm_inode = NULL;
  1069. handle_t *handle = NULL;
  1070. struct ocfs2_dinode *alloc;
  1071. struct ocfs2_dinode *alloc_copy = NULL;
  1072. struct ocfs2_alloc_context *ac = NULL;
  1073. ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
  1074. /* This will lock the main bitmap for us. */
  1075. status = ocfs2_local_alloc_reserve_for_window(osb,
  1076. &ac,
  1077. &main_bm_inode,
  1078. &main_bm_bh);
  1079. if (status < 0) {
  1080. if (status != -ENOSPC)
  1081. mlog_errno(status);
  1082. goto bail;
  1083. }
  1084. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  1085. if (IS_ERR(handle)) {
  1086. status = PTR_ERR(handle);
  1087. handle = NULL;
  1088. mlog_errno(status);
  1089. goto bail;
  1090. }
  1091. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  1092. /* We want to clear the local alloc before doing anything
  1093. * else, so that if we error later during this operation,
  1094. * local alloc shutdown won't try to double free main bitmap
  1095. * bits. Make a copy so the sync function knows which bits to
  1096. * free. */
  1097. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
  1098. if (!alloc_copy) {
  1099. status = -ENOMEM;
  1100. mlog_errno(status);
  1101. goto bail;
  1102. }
  1103. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  1104. status = ocfs2_journal_access_di(handle,
  1105. INODE_CACHE(local_alloc_inode),
  1106. osb->local_alloc_bh,
  1107. OCFS2_JOURNAL_ACCESS_WRITE);
  1108. if (status < 0) {
  1109. mlog_errno(status);
  1110. goto bail;
  1111. }
  1112. ocfs2_clear_local_alloc(alloc);
  1113. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  1114. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  1115. main_bm_inode, main_bm_bh);
  1116. if (status < 0) {
  1117. mlog_errno(status);
  1118. goto bail;
  1119. }
  1120. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  1121. if (status < 0) {
  1122. if (status != -ENOSPC)
  1123. mlog_errno(status);
  1124. goto bail;
  1125. }
  1126. atomic_inc(&osb->alloc_stats.moves);
  1127. bail:
  1128. if (handle)
  1129. ocfs2_commit_trans(osb, handle);
  1130. brelse(main_bm_bh);
  1131. iput(main_bm_inode);
  1132. kfree(alloc_copy);
  1133. if (ac)
  1134. ocfs2_free_alloc_context(ac);
  1135. if (status)
  1136. mlog_errno(status);
  1137. return status;
  1138. }