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