localalloc.c 29 KB

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