localalloc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. /* Check to see if the local alloc window is within ac->ac_max_block */
  395. static int ocfs2_local_alloc_in_range(struct inode *inode,
  396. struct ocfs2_alloc_context *ac,
  397. u32 bits_wanted)
  398. {
  399. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  400. struct ocfs2_dinode *alloc;
  401. struct ocfs2_local_alloc *la;
  402. int start;
  403. u64 block_off;
  404. if (!ac->ac_max_block)
  405. return 1;
  406. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  407. la = OCFS2_LOCAL_ALLOC(alloc);
  408. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted, NULL);
  409. if (start == -1) {
  410. mlog_errno(-ENOSPC);
  411. return 0;
  412. }
  413. /*
  414. * Converting (bm_off + start + bits_wanted) to blocks gives us
  415. * the blkno just past our actual allocation. This is perfect
  416. * to compare with ac_max_block.
  417. */
  418. block_off = ocfs2_clusters_to_blocks(inode->i_sb,
  419. le32_to_cpu(la->la_bm_off) +
  420. start + bits_wanted);
  421. mlog(0, "Checking %llu against %llu\n",
  422. (unsigned long long)block_off,
  423. (unsigned long long)ac->ac_max_block);
  424. if (block_off > ac->ac_max_block)
  425. return 0;
  426. return 1;
  427. }
  428. /*
  429. * make sure we've got at least bits_wanted contiguous bits in the
  430. * local alloc. You lose them when you drop i_mutex.
  431. *
  432. * We will add ourselves to the transaction passed in, but may start
  433. * our own in order to shift windows.
  434. */
  435. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  436. u32 bits_wanted,
  437. struct ocfs2_alloc_context *ac)
  438. {
  439. int status;
  440. struct ocfs2_dinode *alloc;
  441. struct inode *local_alloc_inode;
  442. unsigned int free_bits;
  443. mlog_entry_void();
  444. BUG_ON(!ac);
  445. local_alloc_inode =
  446. ocfs2_get_system_file_inode(osb,
  447. LOCAL_ALLOC_SYSTEM_INODE,
  448. osb->slot_num);
  449. if (!local_alloc_inode) {
  450. status = -ENOENT;
  451. mlog_errno(status);
  452. goto bail;
  453. }
  454. mutex_lock(&local_alloc_inode->i_mutex);
  455. /*
  456. * We must double check state and allocator bits because
  457. * another process may have changed them while holding i_mutex.
  458. */
  459. spin_lock(&osb->osb_lock);
  460. if (!ocfs2_la_state_enabled(osb) ||
  461. (bits_wanted > osb->local_alloc_bits)) {
  462. spin_unlock(&osb->osb_lock);
  463. status = -ENOSPC;
  464. goto bail;
  465. }
  466. spin_unlock(&osb->osb_lock);
  467. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  468. #ifdef CONFIG_OCFS2_DEBUG_FS
  469. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  470. ocfs2_local_alloc_count_bits(alloc)) {
  471. ocfs2_error(osb->sb, "local alloc inode %llu says it has "
  472. "%u free bits, but a count shows %u",
  473. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  474. le32_to_cpu(alloc->id1.bitmap1.i_used),
  475. ocfs2_local_alloc_count_bits(alloc));
  476. status = -EIO;
  477. goto bail;
  478. }
  479. #endif
  480. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  481. le32_to_cpu(alloc->id1.bitmap1.i_used);
  482. if (bits_wanted > free_bits) {
  483. /* uhoh, window change time. */
  484. status =
  485. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  486. if (status < 0) {
  487. if (status != -ENOSPC)
  488. mlog_errno(status);
  489. goto bail;
  490. }
  491. /*
  492. * Under certain conditions, the window slide code
  493. * might have reduced the number of bits available or
  494. * disabled the the local alloc entirely. Re-check
  495. * here and return -ENOSPC if necessary.
  496. */
  497. status = -ENOSPC;
  498. if (!ocfs2_la_state_enabled(osb))
  499. goto bail;
  500. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  501. le32_to_cpu(alloc->id1.bitmap1.i_used);
  502. if (bits_wanted > free_bits)
  503. goto bail;
  504. }
  505. if (ac->ac_max_block)
  506. mlog(0, "Calling in_range for max block %llu\n",
  507. (unsigned long long)ac->ac_max_block);
  508. if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac,
  509. bits_wanted)) {
  510. /*
  511. * The window is outside ac->ac_max_block.
  512. * This errno tells the caller to keep localalloc enabled
  513. * but to get the allocation from the main bitmap.
  514. */
  515. status = -EFBIG;
  516. goto bail;
  517. }
  518. ac->ac_inode = local_alloc_inode;
  519. /* We should never use localalloc from another slot */
  520. ac->ac_alloc_slot = osb->slot_num;
  521. ac->ac_which = OCFS2_AC_USE_LOCAL;
  522. get_bh(osb->local_alloc_bh);
  523. ac->ac_bh = osb->local_alloc_bh;
  524. status = 0;
  525. bail:
  526. if (status < 0 && local_alloc_inode) {
  527. mutex_unlock(&local_alloc_inode->i_mutex);
  528. iput(local_alloc_inode);
  529. }
  530. mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
  531. status);
  532. mlog_exit(status);
  533. return status;
  534. }
  535. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  536. handle_t *handle,
  537. struct ocfs2_alloc_context *ac,
  538. u32 bits_wanted,
  539. u32 *bit_off,
  540. u32 *num_bits)
  541. {
  542. int status, start;
  543. struct inode *local_alloc_inode;
  544. void *bitmap;
  545. struct ocfs2_dinode *alloc;
  546. struct ocfs2_local_alloc *la;
  547. mlog_entry_void();
  548. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  549. local_alloc_inode = ac->ac_inode;
  550. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  551. la = OCFS2_LOCAL_ALLOC(alloc);
  552. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
  553. ac->ac_resv);
  554. if (start == -1) {
  555. /* TODO: Shouldn't we just BUG here? */
  556. status = -ENOSPC;
  557. mlog_errno(status);
  558. goto bail;
  559. }
  560. bitmap = la->la_bitmap;
  561. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  562. *num_bits = bits_wanted;
  563. status = ocfs2_journal_access_di(handle,
  564. INODE_CACHE(local_alloc_inode),
  565. osb->local_alloc_bh,
  566. OCFS2_JOURNAL_ACCESS_WRITE);
  567. if (status < 0) {
  568. mlog_errno(status);
  569. goto bail;
  570. }
  571. ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
  572. bits_wanted);
  573. while(bits_wanted--)
  574. ocfs2_set_bit(start++, bitmap);
  575. le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
  576. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  577. bail:
  578. mlog_exit(status);
  579. return status;
  580. }
  581. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  582. {
  583. int i;
  584. u8 *buffer;
  585. u32 count = 0;
  586. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  587. mlog_entry_void();
  588. buffer = la->la_bitmap;
  589. for (i = 0; i < le16_to_cpu(la->la_size); i++)
  590. count += hweight8(buffer[i]);
  591. mlog_exit(count);
  592. return count;
  593. }
  594. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  595. struct ocfs2_dinode *alloc,
  596. u32 *numbits,
  597. struct ocfs2_alloc_reservation *resv)
  598. {
  599. int numfound, bitoff, left, startoff, lastzero;
  600. int local_resv = 0;
  601. struct ocfs2_alloc_reservation r;
  602. void *bitmap = NULL;
  603. struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
  604. mlog_entry("(numbits wanted = %u)\n", *numbits);
  605. if (!alloc->id1.bitmap1.i_total) {
  606. mlog(0, "No bits in my window!\n");
  607. bitoff = -1;
  608. goto bail;
  609. }
  610. if (!resv) {
  611. local_resv = 1;
  612. ocfs2_resv_init_once(&r);
  613. ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
  614. resv = &r;
  615. }
  616. numfound = *numbits;
  617. if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
  618. if (numfound < *numbits)
  619. *numbits = numfound;
  620. goto bail;
  621. }
  622. /*
  623. * Code error. While reservations are enabled, local
  624. * allocation should _always_ go through them.
  625. */
  626. BUG_ON(osb->osb_resv_level != 0);
  627. /*
  628. * Reservations are disabled. Handle this the old way.
  629. */
  630. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  631. numfound = bitoff = startoff = 0;
  632. lastzero = -1;
  633. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  634. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  635. if (bitoff == left) {
  636. /* mlog(0, "bitoff (%d) == left", bitoff); */
  637. break;
  638. }
  639. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  640. "numfound = %d\n", bitoff, startoff, numfound);*/
  641. /* Ok, we found a zero bit... is it contig. or do we
  642. * start over?*/
  643. if (bitoff == startoff) {
  644. /* we found a zero */
  645. numfound++;
  646. startoff++;
  647. } else {
  648. /* got a zero after some ones */
  649. numfound = 1;
  650. startoff = bitoff+1;
  651. }
  652. /* we got everything we needed */
  653. if (numfound == *numbits) {
  654. /* mlog(0, "Found it all!\n"); */
  655. break;
  656. }
  657. }
  658. mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
  659. numfound);
  660. if (numfound == *numbits) {
  661. bitoff = startoff - numfound;
  662. *numbits = numfound;
  663. } else {
  664. numfound = 0;
  665. bitoff = -1;
  666. }
  667. bail:
  668. if (local_resv)
  669. ocfs2_resv_discard(resmap, resv);
  670. mlog_exit(bitoff);
  671. return bitoff;
  672. }
  673. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  674. {
  675. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  676. int i;
  677. mlog_entry_void();
  678. alloc->id1.bitmap1.i_total = 0;
  679. alloc->id1.bitmap1.i_used = 0;
  680. la->la_bm_off = 0;
  681. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  682. la->la_bitmap[i] = 0;
  683. mlog_exit_void();
  684. }
  685. #if 0
  686. /* turn this on and uncomment below to aid debugging window shifts. */
  687. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  688. unsigned int start,
  689. unsigned int count)
  690. {
  691. unsigned int tmp = count;
  692. while(tmp--) {
  693. if (ocfs2_test_bit(start + tmp, bitmap)) {
  694. printk("ocfs2_verify_zero_bits: start = %u, count = "
  695. "%u\n", start, count);
  696. printk("ocfs2_verify_zero_bits: bit %u is set!",
  697. start + tmp);
  698. BUG();
  699. }
  700. }
  701. }
  702. #endif
  703. /*
  704. * sync the local alloc to main bitmap.
  705. *
  706. * assumes you've already locked the main bitmap -- the bitmap inode
  707. * passed is used for caching.
  708. */
  709. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  710. handle_t *handle,
  711. struct ocfs2_dinode *alloc,
  712. struct inode *main_bm_inode,
  713. struct buffer_head *main_bm_bh)
  714. {
  715. int status = 0;
  716. int bit_off, left, count, start;
  717. u64 la_start_blk;
  718. u64 blkno;
  719. void *bitmap;
  720. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  721. mlog_entry("total = %u, used = %u\n",
  722. le32_to_cpu(alloc->id1.bitmap1.i_total),
  723. le32_to_cpu(alloc->id1.bitmap1.i_used));
  724. if (!alloc->id1.bitmap1.i_total) {
  725. mlog(0, "nothing to sync!\n");
  726. goto bail;
  727. }
  728. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  729. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  730. mlog(0, "all bits were taken!\n");
  731. goto bail;
  732. }
  733. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  734. le32_to_cpu(la->la_bm_off));
  735. bitmap = la->la_bitmap;
  736. start = count = bit_off = 0;
  737. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  738. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  739. != -1) {
  740. if ((bit_off < left) && (bit_off == start)) {
  741. count++;
  742. start++;
  743. continue;
  744. }
  745. if (count) {
  746. blkno = la_start_blk +
  747. ocfs2_clusters_to_blocks(osb->sb,
  748. start - count);
  749. mlog(0, "freeing %u bits starting at local alloc bit "
  750. "%u (la_start_blk = %llu, blkno = %llu)\n",
  751. count, start - count,
  752. (unsigned long long)la_start_blk,
  753. (unsigned long long)blkno);
  754. status = ocfs2_release_clusters(handle,
  755. main_bm_inode,
  756. main_bm_bh, blkno,
  757. count);
  758. if (status < 0) {
  759. mlog_errno(status);
  760. goto bail;
  761. }
  762. }
  763. if (bit_off >= left)
  764. break;
  765. count = 1;
  766. start = bit_off + 1;
  767. }
  768. bail:
  769. mlog_exit(status);
  770. return status;
  771. }
  772. enum ocfs2_la_event {
  773. OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
  774. OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
  775. * enough bits theoretically
  776. * free, but a contiguous
  777. * allocation could not be
  778. * found. */
  779. OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
  780. * enough bits free to satisfy
  781. * our request. */
  782. };
  783. #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
  784. /*
  785. * Given an event, calculate the size of our next local alloc window.
  786. *
  787. * This should always be called under i_mutex of the local alloc inode
  788. * so that local alloc disabling doesn't race with processes trying to
  789. * use the allocator.
  790. *
  791. * Returns the state which the local alloc was left in. This value can
  792. * be ignored by some paths.
  793. */
  794. static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
  795. enum ocfs2_la_event event)
  796. {
  797. unsigned int bits;
  798. int state;
  799. spin_lock(&osb->osb_lock);
  800. if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
  801. WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
  802. goto out_unlock;
  803. }
  804. /*
  805. * ENOSPC and fragmentation are treated similarly for now.
  806. */
  807. if (event == OCFS2_LA_EVENT_ENOSPC ||
  808. event == OCFS2_LA_EVENT_FRAGMENTED) {
  809. /*
  810. * We ran out of contiguous space in the primary
  811. * bitmap. Drastically reduce the number of bits used
  812. * by local alloc until we have to disable it.
  813. */
  814. bits = osb->local_alloc_bits >> 1;
  815. if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
  816. /*
  817. * By setting state to THROTTLED, we'll keep
  818. * the number of local alloc bits used down
  819. * until an event occurs which would give us
  820. * reason to assume the bitmap situation might
  821. * have changed.
  822. */
  823. osb->local_alloc_state = OCFS2_LA_THROTTLED;
  824. osb->local_alloc_bits = bits;
  825. } else {
  826. osb->local_alloc_state = OCFS2_LA_DISABLED;
  827. }
  828. queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
  829. OCFS2_LA_ENABLE_INTERVAL);
  830. goto out_unlock;
  831. }
  832. /*
  833. * Don't increase the size of the local alloc window until we
  834. * know we might be able to fulfill the request. Otherwise, we
  835. * risk bouncing around the global bitmap during periods of
  836. * low space.
  837. */
  838. if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
  839. osb->local_alloc_bits = osb->local_alloc_default_bits;
  840. out_unlock:
  841. state = osb->local_alloc_state;
  842. spin_unlock(&osb->osb_lock);
  843. return state;
  844. }
  845. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  846. struct ocfs2_alloc_context **ac,
  847. struct inode **bitmap_inode,
  848. struct buffer_head **bitmap_bh)
  849. {
  850. int status;
  851. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  852. if (!(*ac)) {
  853. status = -ENOMEM;
  854. mlog_errno(status);
  855. goto bail;
  856. }
  857. retry_enospc:
  858. (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
  859. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  860. if (status == -ENOSPC) {
  861. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
  862. OCFS2_LA_DISABLED)
  863. goto bail;
  864. ocfs2_free_ac_resource(*ac);
  865. memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
  866. goto retry_enospc;
  867. }
  868. if (status < 0) {
  869. mlog_errno(status);
  870. goto bail;
  871. }
  872. *bitmap_inode = (*ac)->ac_inode;
  873. igrab(*bitmap_inode);
  874. *bitmap_bh = (*ac)->ac_bh;
  875. get_bh(*bitmap_bh);
  876. status = 0;
  877. bail:
  878. if ((status < 0) && *ac) {
  879. ocfs2_free_alloc_context(*ac);
  880. *ac = NULL;
  881. }
  882. mlog_exit(status);
  883. return status;
  884. }
  885. /*
  886. * pass it the bitmap lock in lock_bh if you have it.
  887. */
  888. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  889. handle_t *handle,
  890. struct ocfs2_alloc_context *ac)
  891. {
  892. int status = 0;
  893. u32 cluster_off, cluster_count;
  894. struct ocfs2_dinode *alloc = NULL;
  895. struct ocfs2_local_alloc *la;
  896. mlog_entry_void();
  897. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  898. la = OCFS2_LOCAL_ALLOC(alloc);
  899. if (alloc->id1.bitmap1.i_total)
  900. mlog(0, "asking me to alloc a new window over a non-empty "
  901. "one\n");
  902. mlog(0, "Allocating %u clusters for a new window.\n",
  903. osb->local_alloc_bits);
  904. /* Instruct the allocation code to try the most recently used
  905. * cluster group. We'll re-record the group used this pass
  906. * below. */
  907. ac->ac_last_group = osb->la_last_gd;
  908. /* we used the generic suballoc reserve function, but we set
  909. * everything up nicely, so there's no reason why we can't use
  910. * the more specific cluster api to claim bits. */
  911. status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
  912. &cluster_off, &cluster_count);
  913. if (status == -ENOSPC) {
  914. retry_enospc:
  915. /*
  916. * Note: We could also try syncing the journal here to
  917. * allow use of any free bits which the current
  918. * transaction can't give us access to. --Mark
  919. */
  920. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
  921. OCFS2_LA_DISABLED)
  922. goto bail;
  923. ac->ac_bits_wanted = osb->local_alloc_default_bits;
  924. status = ocfs2_claim_clusters(osb, handle, ac,
  925. osb->local_alloc_bits,
  926. &cluster_off,
  927. &cluster_count);
  928. if (status == -ENOSPC)
  929. goto retry_enospc;
  930. /*
  931. * We only shrunk the *minimum* number of in our
  932. * request - it's entirely possible that the allocator
  933. * might give us more than we asked for.
  934. */
  935. if (status == 0) {
  936. spin_lock(&osb->osb_lock);
  937. osb->local_alloc_bits = cluster_count;
  938. spin_unlock(&osb->osb_lock);
  939. }
  940. }
  941. if (status < 0) {
  942. if (status != -ENOSPC)
  943. mlog_errno(status);
  944. goto bail;
  945. }
  946. osb->la_last_gd = ac->ac_last_group;
  947. la->la_bm_off = cpu_to_le32(cluster_off);
  948. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  949. /* just in case... In the future when we find space ourselves,
  950. * we don't have to get all contiguous -- but we'll have to
  951. * set all previously used bits in bitmap and update
  952. * la_bits_set before setting the bits in the main bitmap. */
  953. alloc->id1.bitmap1.i_used = 0;
  954. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  955. le16_to_cpu(la->la_size));
  956. ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
  957. OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
  958. mlog(0, "New window allocated:\n");
  959. mlog(0, "window la_bm_off = %u\n",
  960. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  961. mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
  962. bail:
  963. mlog_exit(status);
  964. return status;
  965. }
  966. /* Note that we do *NOT* lock the local alloc inode here as
  967. * it's been locked already for us. */
  968. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  969. struct inode *local_alloc_inode)
  970. {
  971. int status = 0;
  972. struct buffer_head *main_bm_bh = NULL;
  973. struct inode *main_bm_inode = NULL;
  974. handle_t *handle = NULL;
  975. struct ocfs2_dinode *alloc;
  976. struct ocfs2_dinode *alloc_copy = NULL;
  977. struct ocfs2_alloc_context *ac = NULL;
  978. mlog_entry_void();
  979. ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
  980. /* This will lock the main bitmap for us. */
  981. status = ocfs2_local_alloc_reserve_for_window(osb,
  982. &ac,
  983. &main_bm_inode,
  984. &main_bm_bh);
  985. if (status < 0) {
  986. if (status != -ENOSPC)
  987. mlog_errno(status);
  988. goto bail;
  989. }
  990. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  991. if (IS_ERR(handle)) {
  992. status = PTR_ERR(handle);
  993. handle = NULL;
  994. mlog_errno(status);
  995. goto bail;
  996. }
  997. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  998. /* We want to clear the local alloc before doing anything
  999. * else, so that if we error later during this operation,
  1000. * local alloc shutdown won't try to double free main bitmap
  1001. * bits. Make a copy so the sync function knows which bits to
  1002. * free. */
  1003. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
  1004. if (!alloc_copy) {
  1005. status = -ENOMEM;
  1006. mlog_errno(status);
  1007. goto bail;
  1008. }
  1009. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  1010. status = ocfs2_journal_access_di(handle,
  1011. INODE_CACHE(local_alloc_inode),
  1012. osb->local_alloc_bh,
  1013. OCFS2_JOURNAL_ACCESS_WRITE);
  1014. if (status < 0) {
  1015. mlog_errno(status);
  1016. goto bail;
  1017. }
  1018. ocfs2_clear_local_alloc(alloc);
  1019. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  1020. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  1021. main_bm_inode, main_bm_bh);
  1022. if (status < 0) {
  1023. mlog_errno(status);
  1024. goto bail;
  1025. }
  1026. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  1027. if (status < 0) {
  1028. if (status != -ENOSPC)
  1029. mlog_errno(status);
  1030. goto bail;
  1031. }
  1032. atomic_inc(&osb->alloc_stats.moves);
  1033. bail:
  1034. if (handle)
  1035. ocfs2_commit_trans(osb, handle);
  1036. brelse(main_bm_bh);
  1037. if (main_bm_inode)
  1038. iput(main_bm_inode);
  1039. if (alloc_copy)
  1040. kfree(alloc_copy);
  1041. if (ac)
  1042. ocfs2_free_alloc_context(ac);
  1043. mlog_exit(status);
  1044. return status;
  1045. }