localalloc.c 30 KB

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