reservations.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * reservations.c
  4. *
  5. * Allocation reservations implementation
  6. *
  7. * Some code borrowed from fs/ext3/balloc.c and is:
  8. *
  9. * Copyright (C) 1992, 1993, 1994, 1995
  10. * Remy Card ([email protected])
  11. * Laboratoire MASI - Institut Blaise Pascal
  12. * Universite Pierre et Marie Curie (Paris VI)
  13. *
  14. * The rest is copyright (C) 2010 Novell. All rights reserved.
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/types.h>
  18. #include <linux/highmem.h>
  19. #include <linux/bitops.h>
  20. #include <linux/list.h>
  21. #include <cluster/masklog.h>
  22. #include "ocfs2.h"
  23. #include "ocfs2_trace.h"
  24. #ifdef CONFIG_OCFS2_DEBUG_FS
  25. #define OCFS2_CHECK_RESERVATIONS
  26. #endif
  27. static DEFINE_SPINLOCK(resv_lock);
  28. int ocfs2_dir_resv_allowed(struct ocfs2_super *osb)
  29. {
  30. return (osb->osb_resv_level && osb->osb_dir_resv_level);
  31. }
  32. static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
  33. struct ocfs2_alloc_reservation *resv)
  34. {
  35. struct ocfs2_super *osb = resmap->m_osb;
  36. unsigned int bits;
  37. if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
  38. /* 8, 16, 32, 64, 128, 256, 512, 1024 */
  39. bits = 4 << osb->osb_resv_level;
  40. } else {
  41. bits = 4 << osb->osb_dir_resv_level;
  42. }
  43. return bits;
  44. }
  45. static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
  46. {
  47. if (resv->r_len)
  48. return resv->r_start + resv->r_len - 1;
  49. return resv->r_start;
  50. }
  51. static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
  52. {
  53. return !!(resv->r_len == 0);
  54. }
  55. static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
  56. {
  57. if (resmap->m_osb->osb_resv_level == 0)
  58. return 1;
  59. return 0;
  60. }
  61. static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
  62. {
  63. struct ocfs2_super *osb = resmap->m_osb;
  64. struct rb_node *node;
  65. struct ocfs2_alloc_reservation *resv;
  66. int i = 0;
  67. mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
  68. osb->dev_str, resmap->m_bitmap_len);
  69. node = rb_first(&resmap->m_reservations);
  70. while (node) {
  71. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  72. mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
  73. "\tlast_len: %u\n", resv->r_start,
  74. ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
  75. resv->r_last_len);
  76. node = rb_next(node);
  77. i++;
  78. }
  79. mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
  80. i = 0;
  81. list_for_each_entry(resv, &resmap->m_lru, r_lru) {
  82. mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
  83. "last_start: %u\tlast_len: %u\n", i, resv->r_start,
  84. ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
  85. resv->r_last_len);
  86. i++;
  87. }
  88. }
  89. #ifdef OCFS2_CHECK_RESERVATIONS
  90. static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
  91. int i,
  92. struct ocfs2_alloc_reservation *resv)
  93. {
  94. char *disk_bitmap = resmap->m_disk_bitmap;
  95. unsigned int start = resv->r_start;
  96. unsigned int end = ocfs2_resv_end(resv);
  97. while (start <= end) {
  98. if (ocfs2_test_bit(start, disk_bitmap)) {
  99. mlog(ML_ERROR,
  100. "reservation %d covers an allocated area "
  101. "starting at bit %u!\n", i, start);
  102. return 1;
  103. }
  104. start++;
  105. }
  106. return 0;
  107. }
  108. static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
  109. {
  110. unsigned int off = 0;
  111. int i = 0;
  112. struct rb_node *node;
  113. struct ocfs2_alloc_reservation *resv;
  114. node = rb_first(&resmap->m_reservations);
  115. while (node) {
  116. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  117. if (i > 0 && resv->r_start <= off) {
  118. mlog(ML_ERROR, "reservation %d has bad start off!\n",
  119. i);
  120. goto bad;
  121. }
  122. if (resv->r_len == 0) {
  123. mlog(ML_ERROR, "reservation %d has no length!\n",
  124. i);
  125. goto bad;
  126. }
  127. if (resv->r_start > ocfs2_resv_end(resv)) {
  128. mlog(ML_ERROR, "reservation %d has invalid range!\n",
  129. i);
  130. goto bad;
  131. }
  132. if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
  133. mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
  134. i);
  135. goto bad;
  136. }
  137. if (ocfs2_validate_resmap_bits(resmap, i, resv))
  138. goto bad;
  139. off = ocfs2_resv_end(resv);
  140. node = rb_next(node);
  141. i++;
  142. }
  143. return;
  144. bad:
  145. ocfs2_dump_resv(resmap);
  146. BUG();
  147. }
  148. #else
  149. static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
  150. {
  151. }
  152. #endif
  153. void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
  154. {
  155. memset(resv, 0, sizeof(*resv));
  156. INIT_LIST_HEAD(&resv->r_lru);
  157. }
  158. void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
  159. unsigned int flags)
  160. {
  161. BUG_ON(flags & ~OCFS2_RESV_TYPES);
  162. resv->r_flags |= flags;
  163. }
  164. void ocfs2_resmap_init(struct ocfs2_super *osb,
  165. struct ocfs2_reservation_map *resmap)
  166. {
  167. memset(resmap, 0, sizeof(*resmap));
  168. resmap->m_osb = osb;
  169. resmap->m_reservations = RB_ROOT;
  170. /* m_bitmap_len is initialized to zero by the above memset. */
  171. INIT_LIST_HEAD(&resmap->m_lru);
  172. }
  173. static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
  174. struct ocfs2_alloc_reservation *resv)
  175. {
  176. assert_spin_locked(&resv_lock);
  177. if (!list_empty(&resv->r_lru))
  178. list_del_init(&resv->r_lru);
  179. list_add_tail(&resv->r_lru, &resmap->m_lru);
  180. }
  181. static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
  182. {
  183. resv->r_len = 0;
  184. resv->r_start = 0;
  185. }
  186. static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
  187. struct ocfs2_alloc_reservation *resv)
  188. {
  189. if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
  190. list_del_init(&resv->r_lru);
  191. rb_erase(&resv->r_node, &resmap->m_reservations);
  192. resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
  193. }
  194. }
  195. static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
  196. struct ocfs2_alloc_reservation *resv)
  197. {
  198. assert_spin_locked(&resv_lock);
  199. __ocfs2_resv_trunc(resv);
  200. /*
  201. * last_len and last_start no longer make sense if
  202. * we're changing the range of our allocations.
  203. */
  204. resv->r_last_len = resv->r_last_start = 0;
  205. ocfs2_resv_remove(resmap, resv);
  206. }
  207. /* does nothing if 'resv' is null */
  208. void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
  209. struct ocfs2_alloc_reservation *resv)
  210. {
  211. if (resv) {
  212. spin_lock(&resv_lock);
  213. __ocfs2_resv_discard(resmap, resv);
  214. spin_unlock(&resv_lock);
  215. }
  216. }
  217. static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
  218. {
  219. struct rb_node *node;
  220. struct ocfs2_alloc_reservation *resv;
  221. assert_spin_locked(&resv_lock);
  222. while ((node = rb_last(&resmap->m_reservations)) != NULL) {
  223. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  224. __ocfs2_resv_discard(resmap, resv);
  225. }
  226. }
  227. void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
  228. unsigned int clen, char *disk_bitmap)
  229. {
  230. if (ocfs2_resmap_disabled(resmap))
  231. return;
  232. spin_lock(&resv_lock);
  233. ocfs2_resmap_clear_all_resv(resmap);
  234. resmap->m_bitmap_len = clen;
  235. resmap->m_disk_bitmap = disk_bitmap;
  236. spin_unlock(&resv_lock);
  237. }
  238. void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
  239. {
  240. /* Does nothing for now. Keep this around for API symmetry */
  241. }
  242. static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
  243. struct ocfs2_alloc_reservation *new)
  244. {
  245. struct rb_root *root = &resmap->m_reservations;
  246. struct rb_node *parent = NULL;
  247. struct rb_node **p = &root->rb_node;
  248. struct ocfs2_alloc_reservation *tmp;
  249. assert_spin_locked(&resv_lock);
  250. trace_ocfs2_resv_insert(new->r_start, new->r_len);
  251. while (*p) {
  252. parent = *p;
  253. tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
  254. if (new->r_start < tmp->r_start) {
  255. p = &(*p)->rb_left;
  256. /*
  257. * This is a good place to check for
  258. * overlapping reservations.
  259. */
  260. BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
  261. } else if (new->r_start > ocfs2_resv_end(tmp)) {
  262. p = &(*p)->rb_right;
  263. } else {
  264. /* This should never happen! */
  265. mlog(ML_ERROR, "Duplicate reservation window!\n");
  266. BUG();
  267. }
  268. }
  269. rb_link_node(&new->r_node, parent, p);
  270. rb_insert_color(&new->r_node, root);
  271. new->r_flags |= OCFS2_RESV_FLAG_INUSE;
  272. ocfs2_resv_mark_lru(resmap, new);
  273. ocfs2_check_resmap(resmap);
  274. }
  275. /**
  276. * ocfs2_find_resv_lhs() - find the window which contains goal
  277. * @resmap: reservation map to search
  278. * @goal: which bit to search for
  279. *
  280. * If a window containing that goal is not found, we return the window
  281. * which comes before goal. Returns NULL on empty rbtree or no window
  282. * before goal.
  283. */
  284. static struct ocfs2_alloc_reservation *
  285. ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
  286. {
  287. struct ocfs2_alloc_reservation *resv = NULL;
  288. struct ocfs2_alloc_reservation *prev_resv = NULL;
  289. struct rb_node *node = resmap->m_reservations.rb_node;
  290. assert_spin_locked(&resv_lock);
  291. if (!node)
  292. return NULL;
  293. node = rb_first(&resmap->m_reservations);
  294. while (node) {
  295. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  296. if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
  297. break;
  298. /* Check if we overshot the reservation just before goal? */
  299. if (resv->r_start > goal) {
  300. resv = prev_resv;
  301. break;
  302. }
  303. prev_resv = resv;
  304. node = rb_next(node);
  305. }
  306. return resv;
  307. }
  308. /*
  309. * We are given a range within the bitmap, which corresponds to a gap
  310. * inside the reservations tree (search_start, search_len). The range
  311. * can be anything from the whole bitmap, to a gap between
  312. * reservations.
  313. *
  314. * The start value of *rstart is insignificant.
  315. *
  316. * This function searches the bitmap range starting at search_start
  317. * with length search_len for a set of contiguous free bits. We try
  318. * to find up to 'wanted' bits, but can sometimes return less.
  319. *
  320. * Returns the length of allocation, 0 if no free bits are found.
  321. *
  322. * *cstart and *clen will also be populated with the result.
  323. */
  324. static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map *resmap,
  325. unsigned int wanted,
  326. unsigned int search_start,
  327. unsigned int search_len,
  328. unsigned int *rstart,
  329. unsigned int *rlen)
  330. {
  331. void *bitmap = resmap->m_disk_bitmap;
  332. unsigned int best_start, best_len = 0;
  333. int offset, start, found;
  334. trace_ocfs2_resmap_find_free_bits_begin(search_start, search_len,
  335. wanted, resmap->m_bitmap_len);
  336. found = best_start = best_len = 0;
  337. start = search_start;
  338. while ((offset = ocfs2_find_next_zero_bit(bitmap, resmap->m_bitmap_len,
  339. start)) != -1) {
  340. /* Search reached end of the region */
  341. if (offset >= (search_start + search_len))
  342. break;
  343. if (offset == start) {
  344. /* we found a zero */
  345. found++;
  346. /* move start to the next bit to test */
  347. start++;
  348. } else {
  349. /* got a zero after some ones */
  350. found = 1;
  351. start = offset + 1;
  352. }
  353. if (found > best_len) {
  354. best_len = found;
  355. best_start = start - found;
  356. }
  357. if (found >= wanted)
  358. break;
  359. }
  360. if (best_len == 0)
  361. return 0;
  362. if (best_len >= wanted)
  363. best_len = wanted;
  364. *rlen = best_len;
  365. *rstart = best_start;
  366. trace_ocfs2_resmap_find_free_bits_end(best_start, best_len);
  367. return *rlen;
  368. }
  369. static void __ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
  370. struct ocfs2_alloc_reservation *resv,
  371. unsigned int goal, unsigned int wanted)
  372. {
  373. struct rb_root *root = &resmap->m_reservations;
  374. unsigned int gap_start, gap_end, gap_len;
  375. struct ocfs2_alloc_reservation *prev_resv, *next_resv;
  376. struct rb_node *prev, *next;
  377. unsigned int cstart, clen;
  378. unsigned int best_start = 0, best_len = 0;
  379. /*
  380. * Nasty cases to consider:
  381. *
  382. * - rbtree is empty
  383. * - our window should be first in all reservations
  384. * - our window should be last in all reservations
  385. * - need to make sure we don't go past end of bitmap
  386. */
  387. trace_ocfs2_resv_find_window_begin(resv->r_start, ocfs2_resv_end(resv),
  388. goal, wanted, RB_EMPTY_ROOT(root));
  389. assert_spin_locked(&resv_lock);
  390. if (RB_EMPTY_ROOT(root)) {
  391. /*
  392. * Easiest case - empty tree. We can just take
  393. * whatever window of free bits we want.
  394. */
  395. clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
  396. resmap->m_bitmap_len - goal,
  397. &cstart, &clen);
  398. /*
  399. * This should never happen - the local alloc window
  400. * will always have free bits when we're called.
  401. */
  402. BUG_ON(goal == 0 && clen == 0);
  403. if (clen == 0)
  404. return;
  405. resv->r_start = cstart;
  406. resv->r_len = clen;
  407. ocfs2_resv_insert(resmap, resv);
  408. return;
  409. }
  410. prev_resv = ocfs2_find_resv_lhs(resmap, goal);
  411. if (prev_resv == NULL) {
  412. /*
  413. * A NULL here means that the search code couldn't
  414. * find a window that starts before goal.
  415. *
  416. * However, we can take the first window after goal,
  417. * which is also by definition, the leftmost window in
  418. * the entire tree. If we can find free bits in the
  419. * gap between goal and the LHS window, then the
  420. * reservation can safely be placed there.
  421. *
  422. * Otherwise we fall back to a linear search, checking
  423. * the gaps in between windows for a place to
  424. * allocate.
  425. */
  426. next = rb_first(root);
  427. next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
  428. r_node);
  429. /*
  430. * The search should never return such a window. (see
  431. * comment above
  432. */
  433. if (next_resv->r_start <= goal) {
  434. mlog(ML_ERROR, "goal: %u next_resv: start %u len %u\n",
  435. goal, next_resv->r_start, next_resv->r_len);
  436. ocfs2_dump_resv(resmap);
  437. BUG();
  438. }
  439. clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
  440. next_resv->r_start - goal,
  441. &cstart, &clen);
  442. if (clen) {
  443. best_len = clen;
  444. best_start = cstart;
  445. if (best_len == wanted)
  446. goto out_insert;
  447. }
  448. prev_resv = next_resv;
  449. next_resv = NULL;
  450. }
  451. trace_ocfs2_resv_find_window_prev(prev_resv->r_start,
  452. ocfs2_resv_end(prev_resv));
  453. prev = &prev_resv->r_node;
  454. /* Now we do a linear search for a window, starting at 'prev_rsv' */
  455. while (1) {
  456. next = rb_next(prev);
  457. if (next) {
  458. next_resv = rb_entry(next,
  459. struct ocfs2_alloc_reservation,
  460. r_node);
  461. gap_start = ocfs2_resv_end(prev_resv) + 1;
  462. gap_end = next_resv->r_start - 1;
  463. gap_len = gap_end - gap_start + 1;
  464. } else {
  465. /*
  466. * We're at the rightmost edge of the
  467. * tree. See if a reservation between this
  468. * window and the end of the bitmap will work.
  469. */
  470. gap_start = ocfs2_resv_end(prev_resv) + 1;
  471. gap_len = resmap->m_bitmap_len - gap_start;
  472. gap_end = resmap->m_bitmap_len - 1;
  473. }
  474. trace_ocfs2_resv_find_window_next(next ? next_resv->r_start: -1,
  475. next ? ocfs2_resv_end(next_resv) : -1);
  476. /*
  477. * No need to check this gap if we have already found
  478. * a larger region of free bits.
  479. */
  480. if (gap_len <= best_len)
  481. goto next_resv;
  482. clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
  483. gap_len, &cstart, &clen);
  484. if (clen == wanted) {
  485. best_len = clen;
  486. best_start = cstart;
  487. goto out_insert;
  488. } else if (clen > best_len) {
  489. best_len = clen;
  490. best_start = cstart;
  491. }
  492. next_resv:
  493. if (!next)
  494. break;
  495. prev = next;
  496. prev_resv = rb_entry(prev, struct ocfs2_alloc_reservation,
  497. r_node);
  498. }
  499. out_insert:
  500. if (best_len) {
  501. resv->r_start = best_start;
  502. resv->r_len = best_len;
  503. ocfs2_resv_insert(resmap, resv);
  504. }
  505. }
  506. static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map *resmap,
  507. struct ocfs2_alloc_reservation *resv,
  508. unsigned int wanted)
  509. {
  510. struct ocfs2_alloc_reservation *lru_resv;
  511. int tmpwindow = !!(resv->r_flags & OCFS2_RESV_FLAG_TMP);
  512. unsigned int min_bits;
  513. if (!tmpwindow)
  514. min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
  515. else
  516. min_bits = wanted; /* We at know the temp window will use all
  517. * of these bits */
  518. /*
  519. * Take the first reservation off the LRU as our 'target'. We
  520. * don't try to be smart about it. There might be a case for
  521. * searching based on size but I don't have enough data to be
  522. * sure. --Mark (3/16/2010)
  523. */
  524. lru_resv = list_first_entry(&resmap->m_lru,
  525. struct ocfs2_alloc_reservation, r_lru);
  526. trace_ocfs2_cannibalize_resv_begin(lru_resv->r_start,
  527. lru_resv->r_len,
  528. ocfs2_resv_end(lru_resv));
  529. /*
  530. * Cannibalize (some or all) of the target reservation and
  531. * feed it to the current window.
  532. */
  533. if (lru_resv->r_len <= min_bits) {
  534. /*
  535. * Discard completely if size is less than or equal to a
  536. * reasonable threshold - 50% of window bits for non temporary
  537. * windows.
  538. */
  539. resv->r_start = lru_resv->r_start;
  540. resv->r_len = lru_resv->r_len;
  541. __ocfs2_resv_discard(resmap, lru_resv);
  542. } else {
  543. unsigned int shrink;
  544. if (tmpwindow)
  545. shrink = min_bits;
  546. else
  547. shrink = lru_resv->r_len / 2;
  548. lru_resv->r_len -= shrink;
  549. resv->r_start = ocfs2_resv_end(lru_resv) + 1;
  550. resv->r_len = shrink;
  551. }
  552. trace_ocfs2_cannibalize_resv_end(resv->r_start, ocfs2_resv_end(resv),
  553. resv->r_len, resv->r_last_start,
  554. resv->r_last_len);
  555. ocfs2_resv_insert(resmap, resv);
  556. }
  557. static void ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
  558. struct ocfs2_alloc_reservation *resv,
  559. unsigned int wanted)
  560. {
  561. unsigned int goal = 0;
  562. BUG_ON(!ocfs2_resv_empty(resv));
  563. /*
  564. * Begin by trying to get a window as close to the previous
  565. * one as possible. Using the most recent allocation as a
  566. * start goal makes sense.
  567. */
  568. if (resv->r_last_len) {
  569. goal = resv->r_last_start + resv->r_last_len;
  570. if (goal >= resmap->m_bitmap_len)
  571. goal = 0;
  572. }
  573. __ocfs2_resv_find_window(resmap, resv, goal, wanted);
  574. /* Search from last alloc didn't work, try once more from beginning. */
  575. if (ocfs2_resv_empty(resv) && goal != 0)
  576. __ocfs2_resv_find_window(resmap, resv, 0, wanted);
  577. if (ocfs2_resv_empty(resv)) {
  578. /*
  579. * Still empty? Pull oldest one off the LRU, remove it from
  580. * tree, put this one in it's place.
  581. */
  582. ocfs2_cannibalize_resv(resmap, resv, wanted);
  583. }
  584. BUG_ON(ocfs2_resv_empty(resv));
  585. }
  586. int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
  587. struct ocfs2_alloc_reservation *resv,
  588. int *cstart, int *clen)
  589. {
  590. if (resv == NULL || ocfs2_resmap_disabled(resmap))
  591. return -ENOSPC;
  592. spin_lock(&resv_lock);
  593. if (ocfs2_resv_empty(resv)) {
  594. /*
  595. * We don't want to over-allocate for temporary
  596. * windows. Otherwise, we run the risk of fragmenting the
  597. * allocation space.
  598. */
  599. unsigned int wanted = ocfs2_resv_window_bits(resmap, resv);
  600. if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
  601. wanted = *clen;
  602. /*
  603. * Try to get a window here. If it works, we must fall
  604. * through and test the bitmap . This avoids some
  605. * ping-ponging of windows due to non-reserved space
  606. * being allocation before we initialize a window for
  607. * that inode.
  608. */
  609. ocfs2_resv_find_window(resmap, resv, wanted);
  610. trace_ocfs2_resmap_resv_bits(resv->r_start, resv->r_len);
  611. }
  612. BUG_ON(ocfs2_resv_empty(resv));
  613. *cstart = resv->r_start;
  614. *clen = resv->r_len;
  615. spin_unlock(&resv_lock);
  616. return 0;
  617. }
  618. static void
  619. ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
  620. struct ocfs2_alloc_reservation *resv,
  621. unsigned int start, unsigned int end)
  622. {
  623. unsigned int rhs = 0;
  624. unsigned int old_end = ocfs2_resv_end(resv);
  625. BUG_ON(start != resv->r_start || old_end < end);
  626. /*
  627. * Completely used? We can remove it then.
  628. */
  629. if (old_end == end) {
  630. __ocfs2_resv_discard(resmap, resv);
  631. return;
  632. }
  633. rhs = old_end - end;
  634. /*
  635. * This should have been trapped above.
  636. */
  637. BUG_ON(rhs == 0);
  638. resv->r_start = end + 1;
  639. resv->r_len = old_end - resv->r_start + 1;
  640. }
  641. void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
  642. struct ocfs2_alloc_reservation *resv,
  643. u32 cstart, u32 clen)
  644. {
  645. unsigned int cend = cstart + clen - 1;
  646. if (resmap == NULL || ocfs2_resmap_disabled(resmap))
  647. return;
  648. if (resv == NULL)
  649. return;
  650. BUG_ON(cstart != resv->r_start);
  651. spin_lock(&resv_lock);
  652. trace_ocfs2_resmap_claimed_bits_begin(cstart, cend, clen, resv->r_start,
  653. ocfs2_resv_end(resv), resv->r_len,
  654. resv->r_last_start,
  655. resv->r_last_len);
  656. BUG_ON(cstart < resv->r_start);
  657. BUG_ON(cstart > ocfs2_resv_end(resv));
  658. BUG_ON(cend > ocfs2_resv_end(resv));
  659. ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
  660. resv->r_last_start = cstart;
  661. resv->r_last_len = clen;
  662. /*
  663. * May have been discarded above from
  664. * ocfs2_adjust_resv_from_alloc().
  665. */
  666. if (!ocfs2_resv_empty(resv))
  667. ocfs2_resv_mark_lru(resmap, resv);
  668. trace_ocfs2_resmap_claimed_bits_end(resv->r_start, ocfs2_resv_end(resv),
  669. resv->r_len, resv->r_last_start,
  670. resv->r_last_len);
  671. ocfs2_check_resmap(resmap);
  672. spin_unlock(&resv_lock);
  673. }