localalloc.c 24 KB

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