reservations.c 21 KB

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