reservations.c 21 KB

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