localalloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 "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 "buffer_head_io.h"
  42. #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
  43. static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
  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. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
  49. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  50. handle_t *handle,
  51. struct ocfs2_dinode *alloc,
  52. struct inode *main_bm_inode,
  53. struct buffer_head *main_bm_bh);
  54. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  55. struct ocfs2_alloc_context **ac,
  56. struct inode **bitmap_inode,
  57. struct buffer_head **bitmap_bh);
  58. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  59. handle_t *handle,
  60. struct ocfs2_alloc_context *ac);
  61. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  62. struct inode *local_alloc_inode);
  63. static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
  64. {
  65. BUG_ON(osb->s_clustersize_bits > 20);
  66. /* Size local alloc windows by the megabyte */
  67. return osb->local_alloc_size << (20 - osb->s_clustersize_bits);
  68. }
  69. /*
  70. * Tell us whether a given allocation should use the local alloc
  71. * file. Otherwise, it has to go to the main bitmap.
  72. */
  73. int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
  74. {
  75. int la_bits = ocfs2_local_alloc_window_bits(osb);
  76. int ret = 0;
  77. if (osb->local_alloc_state != OCFS2_LA_ENABLED)
  78. goto bail;
  79. /* la_bits should be at least twice the size (in clusters) of
  80. * a new block group. We want to be sure block group
  81. * allocations go through the local alloc, so allow an
  82. * allocation to take up to half the bitmap. */
  83. if (bits > (la_bits / 2))
  84. goto bail;
  85. ret = 1;
  86. bail:
  87. mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
  88. osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
  89. return ret;
  90. }
  91. int ocfs2_load_local_alloc(struct ocfs2_super *osb)
  92. {
  93. int status = 0;
  94. struct ocfs2_dinode *alloc = NULL;
  95. struct buffer_head *alloc_bh = NULL;
  96. u32 num_used;
  97. struct inode *inode = NULL;
  98. struct ocfs2_local_alloc *la;
  99. mlog_entry_void();
  100. if (osb->local_alloc_size == 0)
  101. goto bail;
  102. if (ocfs2_local_alloc_window_bits(osb) >= osb->bitmap_cpg) {
  103. mlog(ML_NOTICE, "Requested local alloc window %d is larger "
  104. "than max possible %u. Using defaults.\n",
  105. ocfs2_local_alloc_window_bits(osb), (osb->bitmap_cpg - 1));
  106. osb->local_alloc_size = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
  107. }
  108. /* read the alloc off disk */
  109. inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
  110. osb->slot_num);
  111. if (!inode) {
  112. status = -EINVAL;
  113. mlog_errno(status);
  114. goto bail;
  115. }
  116. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
  117. &alloc_bh, 0, inode);
  118. if (status < 0) {
  119. mlog_errno(status);
  120. goto bail;
  121. }
  122. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  123. la = OCFS2_LOCAL_ALLOC(alloc);
  124. if (!(le32_to_cpu(alloc->i_flags) &
  125. (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
  126. mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
  127. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  128. status = -EINVAL;
  129. goto bail;
  130. }
  131. if ((la->la_size == 0) ||
  132. (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
  133. mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
  134. le16_to_cpu(la->la_size));
  135. status = -EINVAL;
  136. goto bail;
  137. }
  138. /* do a little verification. */
  139. num_used = ocfs2_local_alloc_count_bits(alloc);
  140. /* hopefully the local alloc has always been recovered before
  141. * we load it. */
  142. if (num_used
  143. || alloc->id1.bitmap1.i_used
  144. || alloc->id1.bitmap1.i_total
  145. || la->la_bm_off)
  146. mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
  147. "found = %u, set = %u, taken = %u, off = %u\n",
  148. num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
  149. le32_to_cpu(alloc->id1.bitmap1.i_total),
  150. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  151. osb->local_alloc_bh = alloc_bh;
  152. osb->local_alloc_state = OCFS2_LA_ENABLED;
  153. bail:
  154. if (status < 0)
  155. if (alloc_bh)
  156. brelse(alloc_bh);
  157. if (inode)
  158. iput(inode);
  159. mlog(0, "Local alloc window bits = %d\n",
  160. ocfs2_local_alloc_window_bits(osb));
  161. mlog_exit(status);
  162. return status;
  163. }
  164. /*
  165. * return any unused bits to the bitmap and write out a clean
  166. * local_alloc.
  167. *
  168. * local_alloc_bh is optional. If not passed, we will simply use the
  169. * one off osb. If you do pass it however, be warned that it *will* be
  170. * returned brelse'd and NULL'd out.*/
  171. void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
  172. {
  173. int status;
  174. handle_t *handle;
  175. struct inode *local_alloc_inode = NULL;
  176. struct buffer_head *bh = NULL;
  177. struct buffer_head *main_bm_bh = NULL;
  178. struct inode *main_bm_inode = NULL;
  179. struct ocfs2_dinode *alloc_copy = NULL;
  180. struct ocfs2_dinode *alloc = NULL;
  181. mlog_entry_void();
  182. if (osb->local_alloc_state == OCFS2_LA_UNUSED)
  183. goto out;
  184. local_alloc_inode =
  185. ocfs2_get_system_file_inode(osb,
  186. LOCAL_ALLOC_SYSTEM_INODE,
  187. osb->slot_num);
  188. if (!local_alloc_inode) {
  189. status = -ENOENT;
  190. mlog_errno(status);
  191. goto out;
  192. }
  193. osb->local_alloc_state = OCFS2_LA_DISABLED;
  194. main_bm_inode = ocfs2_get_system_file_inode(osb,
  195. GLOBAL_BITMAP_SYSTEM_INODE,
  196. OCFS2_INVALID_SLOT);
  197. if (!main_bm_inode) {
  198. status = -EINVAL;
  199. mlog_errno(status);
  200. goto out;
  201. }
  202. mutex_lock(&main_bm_inode->i_mutex);
  203. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  204. if (status < 0) {
  205. mlog_errno(status);
  206. goto out_mutex;
  207. }
  208. /* WINDOW_MOVE_CREDITS is a bit heavy... */
  209. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  210. if (IS_ERR(handle)) {
  211. mlog_errno(PTR_ERR(handle));
  212. handle = NULL;
  213. goto out_unlock;
  214. }
  215. bh = osb->local_alloc_bh;
  216. alloc = (struct ocfs2_dinode *) bh->b_data;
  217. alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
  218. if (!alloc_copy) {
  219. status = -ENOMEM;
  220. goto out_commit;
  221. }
  222. memcpy(alloc_copy, alloc, bh->b_size);
  223. status = ocfs2_journal_access(handle, local_alloc_inode, bh,
  224. OCFS2_JOURNAL_ACCESS_WRITE);
  225. if (status < 0) {
  226. mlog_errno(status);
  227. goto out_commit;
  228. }
  229. ocfs2_clear_local_alloc(alloc);
  230. status = ocfs2_journal_dirty(handle, bh);
  231. if (status < 0) {
  232. mlog_errno(status);
  233. goto out_commit;
  234. }
  235. brelse(bh);
  236. osb->local_alloc_bh = NULL;
  237. osb->local_alloc_state = OCFS2_LA_UNUSED;
  238. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  239. main_bm_inode, main_bm_bh);
  240. if (status < 0)
  241. mlog_errno(status);
  242. out_commit:
  243. ocfs2_commit_trans(osb, handle);
  244. out_unlock:
  245. if (main_bm_bh)
  246. brelse(main_bm_bh);
  247. ocfs2_inode_unlock(main_bm_inode, 1);
  248. out_mutex:
  249. mutex_unlock(&main_bm_inode->i_mutex);
  250. iput(main_bm_inode);
  251. out:
  252. if (local_alloc_inode)
  253. iput(local_alloc_inode);
  254. if (alloc_copy)
  255. kfree(alloc_copy);
  256. mlog_exit_void();
  257. }
  258. /*
  259. * We want to free the bitmap bits outside of any recovery context as
  260. * we'll need a cluster lock to do so, but we must clear the local
  261. * alloc before giving up the recovered nodes journal. To solve this,
  262. * we kmalloc a copy of the local alloc before it's change for the
  263. * caller to process with ocfs2_complete_local_alloc_recovery
  264. */
  265. int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
  266. int slot_num,
  267. struct ocfs2_dinode **alloc_copy)
  268. {
  269. int status = 0;
  270. struct buffer_head *alloc_bh = NULL;
  271. struct inode *inode = NULL;
  272. struct ocfs2_dinode *alloc;
  273. mlog_entry("(slot_num = %d)\n", slot_num);
  274. *alloc_copy = NULL;
  275. inode = ocfs2_get_system_file_inode(osb,
  276. LOCAL_ALLOC_SYSTEM_INODE,
  277. slot_num);
  278. if (!inode) {
  279. status = -EINVAL;
  280. mlog_errno(status);
  281. goto bail;
  282. }
  283. mutex_lock(&inode->i_mutex);
  284. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
  285. &alloc_bh, 0, inode);
  286. if (status < 0) {
  287. mlog_errno(status);
  288. goto bail;
  289. }
  290. *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
  291. if (!(*alloc_copy)) {
  292. status = -ENOMEM;
  293. goto bail;
  294. }
  295. memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
  296. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  297. ocfs2_clear_local_alloc(alloc);
  298. status = ocfs2_write_block(osb, alloc_bh, inode);
  299. if (status < 0)
  300. mlog_errno(status);
  301. bail:
  302. if ((status < 0) && (*alloc_copy)) {
  303. kfree(*alloc_copy);
  304. *alloc_copy = NULL;
  305. }
  306. if (alloc_bh)
  307. brelse(alloc_bh);
  308. if (inode) {
  309. mutex_unlock(&inode->i_mutex);
  310. iput(inode);
  311. }
  312. mlog_exit(status);
  313. return status;
  314. }
  315. /*
  316. * Step 2: By now, we've completed the journal recovery, we've stamped
  317. * a clean local alloc on disk and dropped the node out of the
  318. * recovery map. Dlm locks will no longer stall, so lets clear out the
  319. * main bitmap.
  320. */
  321. int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
  322. struct ocfs2_dinode *alloc)
  323. {
  324. int status;
  325. handle_t *handle;
  326. struct buffer_head *main_bm_bh = NULL;
  327. struct inode *main_bm_inode;
  328. mlog_entry_void();
  329. main_bm_inode = ocfs2_get_system_file_inode(osb,
  330. GLOBAL_BITMAP_SYSTEM_INODE,
  331. OCFS2_INVALID_SLOT);
  332. if (!main_bm_inode) {
  333. status = -EINVAL;
  334. mlog_errno(status);
  335. goto out;
  336. }
  337. mutex_lock(&main_bm_inode->i_mutex);
  338. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  339. if (status < 0) {
  340. mlog_errno(status);
  341. goto out_mutex;
  342. }
  343. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  344. if (IS_ERR(handle)) {
  345. status = PTR_ERR(handle);
  346. handle = NULL;
  347. mlog_errno(status);
  348. goto out_unlock;
  349. }
  350. /* we want the bitmap change to be recorded on disk asap */
  351. handle->h_sync = 1;
  352. status = ocfs2_sync_local_to_main(osb, handle, alloc,
  353. main_bm_inode, main_bm_bh);
  354. if (status < 0)
  355. mlog_errno(status);
  356. ocfs2_commit_trans(osb, handle);
  357. out_unlock:
  358. ocfs2_inode_unlock(main_bm_inode, 1);
  359. out_mutex:
  360. mutex_unlock(&main_bm_inode->i_mutex);
  361. if (main_bm_bh)
  362. brelse(main_bm_bh);
  363. iput(main_bm_inode);
  364. out:
  365. if (!status)
  366. ocfs2_init_inode_steal_slot(osb);
  367. mlog_exit(status);
  368. return status;
  369. }
  370. /*
  371. * make sure we've got at least bitswanted contiguous bits in the
  372. * local alloc. You lose them when you drop i_mutex.
  373. *
  374. * We will add ourselves to the transaction passed in, but may start
  375. * our own in order to shift windows.
  376. */
  377. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  378. u32 bits_wanted,
  379. struct ocfs2_alloc_context *ac)
  380. {
  381. int status;
  382. struct ocfs2_dinode *alloc;
  383. struct inode *local_alloc_inode;
  384. unsigned int free_bits;
  385. mlog_entry_void();
  386. BUG_ON(!ac);
  387. local_alloc_inode =
  388. ocfs2_get_system_file_inode(osb,
  389. LOCAL_ALLOC_SYSTEM_INODE,
  390. osb->slot_num);
  391. if (!local_alloc_inode) {
  392. status = -ENOENT;
  393. mlog_errno(status);
  394. goto bail;
  395. }
  396. mutex_lock(&local_alloc_inode->i_mutex);
  397. if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
  398. status = -ENOSPC;
  399. goto bail;
  400. }
  401. if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
  402. mlog(0, "Asking for more than my max window size!\n");
  403. status = -ENOSPC;
  404. goto bail;
  405. }
  406. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  407. #ifdef OCFS2_DEBUG_FS
  408. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  409. ocfs2_local_alloc_count_bits(alloc)) {
  410. ocfs2_error(osb->sb, "local alloc inode %llu says it has "
  411. "%u free bits, but a count shows %u",
  412. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  413. le32_to_cpu(alloc->id1.bitmap1.i_used),
  414. ocfs2_local_alloc_count_bits(alloc));
  415. status = -EIO;
  416. goto bail;
  417. }
  418. #endif
  419. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  420. le32_to_cpu(alloc->id1.bitmap1.i_used);
  421. if (bits_wanted > free_bits) {
  422. /* uhoh, window change time. */
  423. status =
  424. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  425. if (status < 0) {
  426. if (status != -ENOSPC)
  427. mlog_errno(status);
  428. goto bail;
  429. }
  430. }
  431. ac->ac_inode = local_alloc_inode;
  432. /* We should never use localalloc from another slot */
  433. ac->ac_alloc_slot = osb->slot_num;
  434. ac->ac_which = OCFS2_AC_USE_LOCAL;
  435. get_bh(osb->local_alloc_bh);
  436. ac->ac_bh = osb->local_alloc_bh;
  437. status = 0;
  438. bail:
  439. if (status < 0 && local_alloc_inode) {
  440. mutex_unlock(&local_alloc_inode->i_mutex);
  441. iput(local_alloc_inode);
  442. }
  443. mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
  444. status);
  445. mlog_exit(status);
  446. return status;
  447. }
  448. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  449. handle_t *handle,
  450. struct ocfs2_alloc_context *ac,
  451. u32 bits_wanted,
  452. u32 *bit_off,
  453. u32 *num_bits)
  454. {
  455. int status, start;
  456. struct inode *local_alloc_inode;
  457. void *bitmap;
  458. struct ocfs2_dinode *alloc;
  459. struct ocfs2_local_alloc *la;
  460. mlog_entry_void();
  461. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  462. local_alloc_inode = ac->ac_inode;
  463. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  464. la = OCFS2_LOCAL_ALLOC(alloc);
  465. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
  466. if (start == -1) {
  467. /* TODO: Shouldn't we just BUG here? */
  468. status = -ENOSPC;
  469. mlog_errno(status);
  470. goto bail;
  471. }
  472. bitmap = la->la_bitmap;
  473. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  474. /* local alloc is always contiguous by nature -- we never
  475. * delete bits from it! */
  476. *num_bits = bits_wanted;
  477. status = ocfs2_journal_access(handle, local_alloc_inode,
  478. osb->local_alloc_bh,
  479. OCFS2_JOURNAL_ACCESS_WRITE);
  480. if (status < 0) {
  481. mlog_errno(status);
  482. goto bail;
  483. }
  484. while(bits_wanted--)
  485. ocfs2_set_bit(start++, bitmap);
  486. le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
  487. status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  488. if (status < 0) {
  489. mlog_errno(status);
  490. goto bail;
  491. }
  492. status = 0;
  493. bail:
  494. mlog_exit(status);
  495. return status;
  496. }
  497. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  498. {
  499. int i;
  500. u8 *buffer;
  501. u32 count = 0;
  502. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  503. mlog_entry_void();
  504. buffer = la->la_bitmap;
  505. for (i = 0; i < le16_to_cpu(la->la_size); i++)
  506. count += hweight8(buffer[i]);
  507. mlog_exit(count);
  508. return count;
  509. }
  510. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  511. struct ocfs2_dinode *alloc,
  512. u32 numbits)
  513. {
  514. int numfound, bitoff, left, startoff, lastzero;
  515. void *bitmap = NULL;
  516. mlog_entry("(numbits wanted = %u)\n", numbits);
  517. if (!alloc->id1.bitmap1.i_total) {
  518. mlog(0, "No bits in my window!\n");
  519. bitoff = -1;
  520. goto bail;
  521. }
  522. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  523. numfound = bitoff = startoff = 0;
  524. lastzero = -1;
  525. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  526. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  527. if (bitoff == left) {
  528. /* mlog(0, "bitoff (%d) == left", bitoff); */
  529. break;
  530. }
  531. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  532. "numfound = %d\n", bitoff, startoff, numfound);*/
  533. /* Ok, we found a zero bit... is it contig. or do we
  534. * start over?*/
  535. if (bitoff == startoff) {
  536. /* we found a zero */
  537. numfound++;
  538. startoff++;
  539. } else {
  540. /* got a zero after some ones */
  541. numfound = 1;
  542. startoff = bitoff+1;
  543. }
  544. /* we got everything we needed */
  545. if (numfound == numbits) {
  546. /* mlog(0, "Found it all!\n"); */
  547. break;
  548. }
  549. }
  550. mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
  551. numfound);
  552. if (numfound == numbits)
  553. bitoff = startoff - numfound;
  554. else
  555. bitoff = -1;
  556. bail:
  557. mlog_exit(bitoff);
  558. return bitoff;
  559. }
  560. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  561. {
  562. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  563. int i;
  564. mlog_entry_void();
  565. alloc->id1.bitmap1.i_total = 0;
  566. alloc->id1.bitmap1.i_used = 0;
  567. la->la_bm_off = 0;
  568. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  569. la->la_bitmap[i] = 0;
  570. mlog_exit_void();
  571. }
  572. #if 0
  573. /* turn this on and uncomment below to aid debugging window shifts. */
  574. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  575. unsigned int start,
  576. unsigned int count)
  577. {
  578. unsigned int tmp = count;
  579. while(tmp--) {
  580. if (ocfs2_test_bit(start + tmp, bitmap)) {
  581. printk("ocfs2_verify_zero_bits: start = %u, count = "
  582. "%u\n", start, count);
  583. printk("ocfs2_verify_zero_bits: bit %u is set!",
  584. start + tmp);
  585. BUG();
  586. }
  587. }
  588. }
  589. #endif
  590. /*
  591. * sync the local alloc to main bitmap.
  592. *
  593. * assumes you've already locked the main bitmap -- the bitmap inode
  594. * passed is used for caching.
  595. */
  596. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  597. handle_t *handle,
  598. struct ocfs2_dinode *alloc,
  599. struct inode *main_bm_inode,
  600. struct buffer_head *main_bm_bh)
  601. {
  602. int status = 0;
  603. int bit_off, left, count, start;
  604. u64 la_start_blk;
  605. u64 blkno;
  606. void *bitmap;
  607. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  608. mlog_entry("total = %u, used = %u\n",
  609. le32_to_cpu(alloc->id1.bitmap1.i_total),
  610. le32_to_cpu(alloc->id1.bitmap1.i_used));
  611. if (!alloc->id1.bitmap1.i_total) {
  612. mlog(0, "nothing to sync!\n");
  613. goto bail;
  614. }
  615. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  616. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  617. mlog(0, "all bits were taken!\n");
  618. goto bail;
  619. }
  620. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  621. le32_to_cpu(la->la_bm_off));
  622. bitmap = la->la_bitmap;
  623. start = count = bit_off = 0;
  624. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  625. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  626. != -1) {
  627. if ((bit_off < left) && (bit_off == start)) {
  628. count++;
  629. start++;
  630. continue;
  631. }
  632. if (count) {
  633. blkno = la_start_blk +
  634. ocfs2_clusters_to_blocks(osb->sb,
  635. start - count);
  636. mlog(0, "freeing %u bits starting at local alloc bit "
  637. "%u (la_start_blk = %llu, blkno = %llu)\n",
  638. count, start - count,
  639. (unsigned long long)la_start_blk,
  640. (unsigned long long)blkno);
  641. status = ocfs2_free_clusters(handle, main_bm_inode,
  642. main_bm_bh, blkno, count);
  643. if (status < 0) {
  644. mlog_errno(status);
  645. goto bail;
  646. }
  647. }
  648. if (bit_off >= left)
  649. break;
  650. count = 1;
  651. start = bit_off + 1;
  652. }
  653. bail:
  654. mlog_exit(status);
  655. return status;
  656. }
  657. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  658. struct ocfs2_alloc_context **ac,
  659. struct inode **bitmap_inode,
  660. struct buffer_head **bitmap_bh)
  661. {
  662. int status;
  663. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  664. if (!(*ac)) {
  665. status = -ENOMEM;
  666. mlog_errno(status);
  667. goto bail;
  668. }
  669. (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
  670. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  671. if (status < 0) {
  672. if (status != -ENOSPC)
  673. mlog_errno(status);
  674. goto bail;
  675. }
  676. *bitmap_inode = (*ac)->ac_inode;
  677. igrab(*bitmap_inode);
  678. *bitmap_bh = (*ac)->ac_bh;
  679. get_bh(*bitmap_bh);
  680. status = 0;
  681. bail:
  682. if ((status < 0) && *ac) {
  683. ocfs2_free_alloc_context(*ac);
  684. *ac = NULL;
  685. }
  686. mlog_exit(status);
  687. return status;
  688. }
  689. /*
  690. * pass it the bitmap lock in lock_bh if you have it.
  691. */
  692. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  693. handle_t *handle,
  694. struct ocfs2_alloc_context *ac)
  695. {
  696. int status = 0;
  697. u32 cluster_off, cluster_count;
  698. struct ocfs2_dinode *alloc = NULL;
  699. struct ocfs2_local_alloc *la;
  700. mlog_entry_void();
  701. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  702. la = OCFS2_LOCAL_ALLOC(alloc);
  703. if (alloc->id1.bitmap1.i_total)
  704. mlog(0, "asking me to alloc a new window over a non-empty "
  705. "one\n");
  706. mlog(0, "Allocating %u clusters for a new window.\n",
  707. ocfs2_local_alloc_window_bits(osb));
  708. /* Instruct the allocation code to try the most recently used
  709. * cluster group. We'll re-record the group used this pass
  710. * below. */
  711. ac->ac_last_group = osb->la_last_gd;
  712. /* we used the generic suballoc reserve function, but we set
  713. * everything up nicely, so there's no reason why we can't use
  714. * the more specific cluster api to claim bits. */
  715. status = ocfs2_claim_clusters(osb, handle, ac,
  716. ocfs2_local_alloc_window_bits(osb),
  717. &cluster_off, &cluster_count);
  718. if (status < 0) {
  719. if (status != -ENOSPC)
  720. mlog_errno(status);
  721. goto bail;
  722. }
  723. osb->la_last_gd = ac->ac_last_group;
  724. la->la_bm_off = cpu_to_le32(cluster_off);
  725. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  726. /* just in case... In the future when we find space ourselves,
  727. * we don't have to get all contiguous -- but we'll have to
  728. * set all previously used bits in bitmap and update
  729. * la_bits_set before setting the bits in the main bitmap. */
  730. alloc->id1.bitmap1.i_used = 0;
  731. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  732. le16_to_cpu(la->la_size));
  733. mlog(0, "New window allocated:\n");
  734. mlog(0, "window la_bm_off = %u\n",
  735. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  736. mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
  737. bail:
  738. mlog_exit(status);
  739. return status;
  740. }
  741. /* Note that we do *NOT* lock the local alloc inode here as
  742. * it's been locked already for us. */
  743. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  744. struct inode *local_alloc_inode)
  745. {
  746. int status = 0;
  747. struct buffer_head *main_bm_bh = NULL;
  748. struct inode *main_bm_inode = NULL;
  749. handle_t *handle = NULL;
  750. struct ocfs2_dinode *alloc;
  751. struct ocfs2_dinode *alloc_copy = NULL;
  752. struct ocfs2_alloc_context *ac = NULL;
  753. mlog_entry_void();
  754. /* This will lock the main bitmap for us. */
  755. status = ocfs2_local_alloc_reserve_for_window(osb,
  756. &ac,
  757. &main_bm_inode,
  758. &main_bm_bh);
  759. if (status < 0) {
  760. if (status != -ENOSPC)
  761. mlog_errno(status);
  762. goto bail;
  763. }
  764. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  765. if (IS_ERR(handle)) {
  766. status = PTR_ERR(handle);
  767. handle = NULL;
  768. mlog_errno(status);
  769. goto bail;
  770. }
  771. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  772. /* We want to clear the local alloc before doing anything
  773. * else, so that if we error later during this operation,
  774. * local alloc shutdown won't try to double free main bitmap
  775. * bits. Make a copy so the sync function knows which bits to
  776. * free. */
  777. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
  778. if (!alloc_copy) {
  779. status = -ENOMEM;
  780. mlog_errno(status);
  781. goto bail;
  782. }
  783. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  784. status = ocfs2_journal_access(handle, local_alloc_inode,
  785. osb->local_alloc_bh,
  786. OCFS2_JOURNAL_ACCESS_WRITE);
  787. if (status < 0) {
  788. mlog_errno(status);
  789. goto bail;
  790. }
  791. ocfs2_clear_local_alloc(alloc);
  792. status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  793. if (status < 0) {
  794. mlog_errno(status);
  795. goto bail;
  796. }
  797. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  798. main_bm_inode, main_bm_bh);
  799. if (status < 0) {
  800. mlog_errno(status);
  801. goto bail;
  802. }
  803. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  804. if (status < 0) {
  805. if (status != -ENOSPC)
  806. mlog_errno(status);
  807. goto bail;
  808. }
  809. atomic_inc(&osb->alloc_stats.moves);
  810. status = 0;
  811. bail:
  812. if (handle)
  813. ocfs2_commit_trans(osb, handle);
  814. if (main_bm_bh)
  815. brelse(main_bm_bh);
  816. if (main_bm_inode)
  817. iput(main_bm_inode);
  818. if (alloc_copy)
  819. kfree(alloc_copy);
  820. if (ac)
  821. ocfs2_free_alloc_context(ac);
  822. mlog_exit(status);
  823. return status;
  824. }