alloc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * alloc.c - NILFS dat/inode allocator
  3. *
  4. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Original code was written by Koji Sato <koji@osrg.net>.
  21. * Two allocators were unified by Ryusuke Konishi <ryusuke@osrg.net>,
  22. * Amagai Yoshiji <amagai@osrg.net>.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/buffer_head.h>
  26. #include <linux/fs.h>
  27. #include <linux/bitops.h>
  28. #include "mdt.h"
  29. #include "alloc.h"
  30. static inline unsigned long
  31. nilfs_palloc_groups_per_desc_block(const struct inode *inode)
  32. {
  33. return (1UL << inode->i_blkbits) /
  34. sizeof(struct nilfs_palloc_group_desc);
  35. }
  36. static inline unsigned long
  37. nilfs_palloc_groups_count(const struct inode *inode)
  38. {
  39. return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
  40. }
  41. int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned entry_size)
  42. {
  43. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  44. mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS);
  45. if (!mi->mi_bgl)
  46. return -ENOMEM;
  47. bgl_lock_init(mi->mi_bgl);
  48. nilfs_mdt_set_entry_size(inode, entry_size, 0);
  49. mi->mi_blocks_per_group =
  50. DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode),
  51. mi->mi_entries_per_block) + 1;
  52. /* Number of blocks in a group including entry blocks and
  53. a bitmap block */
  54. mi->mi_blocks_per_desc_block =
  55. nilfs_palloc_groups_per_desc_block(inode) *
  56. mi->mi_blocks_per_group + 1;
  57. /* Number of blocks per descriptor including the
  58. descriptor block */
  59. return 0;
  60. }
  61. static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr,
  62. unsigned long *offset)
  63. {
  64. __u64 group = nr;
  65. *offset = do_div(group, nilfs_palloc_entries_per_group(inode));
  66. return group;
  67. }
  68. static unsigned long
  69. nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group)
  70. {
  71. unsigned long desc_block =
  72. group / nilfs_palloc_groups_per_desc_block(inode);
  73. return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block;
  74. }
  75. static unsigned long
  76. nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group)
  77. {
  78. unsigned long desc_offset =
  79. group % nilfs_palloc_groups_per_desc_block(inode);
  80. return nilfs_palloc_desc_blkoff(inode, group) + 1 +
  81. desc_offset * NILFS_MDT(inode)->mi_blocks_per_group;
  82. }
  83. static unsigned long
  84. nilfs_palloc_group_desc_nfrees(struct inode *inode, unsigned long group,
  85. const struct nilfs_palloc_group_desc *desc)
  86. {
  87. unsigned long nfree;
  88. spin_lock(nilfs_mdt_bgl_lock(inode, group));
  89. nfree = le32_to_cpu(desc->pg_nfrees);
  90. spin_unlock(nilfs_mdt_bgl_lock(inode, group));
  91. return nfree;
  92. }
  93. static void
  94. nilfs_palloc_group_desc_add_entries(struct inode *inode,
  95. unsigned long group,
  96. struct nilfs_palloc_group_desc *desc,
  97. u32 n)
  98. {
  99. spin_lock(nilfs_mdt_bgl_lock(inode, group));
  100. le32_add_cpu(&desc->pg_nfrees, n);
  101. spin_unlock(nilfs_mdt_bgl_lock(inode, group));
  102. }
  103. static unsigned long
  104. nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
  105. {
  106. unsigned long group, group_offset;
  107. group = nilfs_palloc_group(inode, nr, &group_offset);
  108. return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
  109. group_offset / NILFS_MDT(inode)->mi_entries_per_block;
  110. }
  111. static void nilfs_palloc_desc_block_init(struct inode *inode,
  112. struct buffer_head *bh, void *kaddr)
  113. {
  114. struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
  115. unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
  116. __le32 nfrees;
  117. nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
  118. while (n-- > 0) {
  119. desc->pg_nfrees = nfrees;
  120. desc++;
  121. }
  122. }
  123. static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
  124. int create,
  125. void (*init_block)(struct inode *,
  126. struct buffer_head *,
  127. void *),
  128. struct buffer_head **bhp,
  129. struct nilfs_bh_assoc *prev,
  130. spinlock_t *lock)
  131. {
  132. int ret;
  133. spin_lock(lock);
  134. if (prev->bh && blkoff == prev->blkoff) {
  135. get_bh(prev->bh);
  136. *bhp = prev->bh;
  137. spin_unlock(lock);
  138. return 0;
  139. }
  140. spin_unlock(lock);
  141. ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
  142. if (!ret) {
  143. spin_lock(lock);
  144. /*
  145. * The following code must be safe for change of the
  146. * cache contents during the get block call.
  147. */
  148. brelse(prev->bh);
  149. get_bh(*bhp);
  150. prev->bh = *bhp;
  151. prev->blkoff = blkoff;
  152. spin_unlock(lock);
  153. }
  154. return ret;
  155. }
  156. static int nilfs_palloc_get_desc_block(struct inode *inode,
  157. unsigned long group,
  158. int create, struct buffer_head **bhp)
  159. {
  160. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  161. return nilfs_palloc_get_block(inode,
  162. nilfs_palloc_desc_blkoff(inode, group),
  163. create, nilfs_palloc_desc_block_init,
  164. bhp, &cache->prev_desc, &cache->lock);
  165. }
  166. static int nilfs_palloc_get_bitmap_block(struct inode *inode,
  167. unsigned long group,
  168. int create, struct buffer_head **bhp)
  169. {
  170. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  171. return nilfs_palloc_get_block(inode,
  172. nilfs_palloc_bitmap_blkoff(inode, group),
  173. create, NULL, bhp,
  174. &cache->prev_bitmap, &cache->lock);
  175. }
  176. int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
  177. int create, struct buffer_head **bhp)
  178. {
  179. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  180. return nilfs_palloc_get_block(inode,
  181. nilfs_palloc_entry_blkoff(inode, nr),
  182. create, NULL, bhp,
  183. &cache->prev_entry, &cache->lock);
  184. }
  185. static struct nilfs_palloc_group_desc *
  186. nilfs_palloc_block_get_group_desc(const struct inode *inode,
  187. unsigned long group,
  188. const struct buffer_head *bh, void *kaddr)
  189. {
  190. return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
  191. group % nilfs_palloc_groups_per_desc_block(inode);
  192. }
  193. void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
  194. const struct buffer_head *bh, void *kaddr)
  195. {
  196. unsigned long entry_offset, group_offset;
  197. nilfs_palloc_group(inode, nr, &group_offset);
  198. entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
  199. return kaddr + bh_offset(bh) +
  200. entry_offset * NILFS_MDT(inode)->mi_entry_size;
  201. }
  202. static int nilfs_palloc_find_available_slot(struct inode *inode,
  203. unsigned long group,
  204. unsigned long target,
  205. unsigned char *bitmap,
  206. int bsize) /* size in bits */
  207. {
  208. int curr, pos, end, i;
  209. if (target > 0) {
  210. end = (target + BITS_PER_LONG - 1) & ~(BITS_PER_LONG - 1);
  211. if (end > bsize)
  212. end = bsize;
  213. pos = nilfs_find_next_zero_bit(bitmap, end, target);
  214. if (pos < end &&
  215. !nilfs_set_bit_atomic(
  216. nilfs_mdt_bgl_lock(inode, group), pos, bitmap))
  217. return pos;
  218. } else
  219. end = 0;
  220. for (i = 0, curr = end;
  221. i < bsize;
  222. i += BITS_PER_LONG, curr += BITS_PER_LONG) {
  223. /* wrap around */
  224. if (curr >= bsize)
  225. curr = 0;
  226. while (*((unsigned long *)bitmap + curr / BITS_PER_LONG)
  227. != ~0UL) {
  228. end = curr + BITS_PER_LONG;
  229. if (end > bsize)
  230. end = bsize;
  231. pos = nilfs_find_next_zero_bit(bitmap, end, curr);
  232. if ((pos < end) &&
  233. !nilfs_set_bit_atomic(
  234. nilfs_mdt_bgl_lock(inode, group), pos,
  235. bitmap))
  236. return pos;
  237. }
  238. }
  239. return -ENOSPC;
  240. }
  241. static unsigned long
  242. nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
  243. unsigned long curr, unsigned long max)
  244. {
  245. return min_t(unsigned long,
  246. nilfs_palloc_groups_per_desc_block(inode) -
  247. curr % nilfs_palloc_groups_per_desc_block(inode),
  248. max - curr + 1);
  249. }
  250. int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
  251. struct nilfs_palloc_req *req)
  252. {
  253. struct buffer_head *desc_bh, *bitmap_bh;
  254. struct nilfs_palloc_group_desc *desc;
  255. unsigned char *bitmap;
  256. void *desc_kaddr, *bitmap_kaddr;
  257. unsigned long group, maxgroup, ngroups;
  258. unsigned long group_offset, maxgroup_offset;
  259. unsigned long n, entries_per_group, groups_per_desc_block;
  260. unsigned long i, j;
  261. int pos, ret;
  262. ngroups = nilfs_palloc_groups_count(inode);
  263. maxgroup = ngroups - 1;
  264. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  265. entries_per_group = nilfs_palloc_entries_per_group(inode);
  266. groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
  267. for (i = 0; i < ngroups; i += n) {
  268. if (group >= ngroups) {
  269. /* wrap around */
  270. group = 0;
  271. maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
  272. &maxgroup_offset) - 1;
  273. }
  274. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  275. if (ret < 0)
  276. return ret;
  277. desc_kaddr = kmap(desc_bh->b_page);
  278. desc = nilfs_palloc_block_get_group_desc(
  279. inode, group, desc_bh, desc_kaddr);
  280. n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
  281. maxgroup);
  282. for (j = 0; j < n; j++, desc++, group++) {
  283. if (nilfs_palloc_group_desc_nfrees(inode, group, desc)
  284. > 0) {
  285. ret = nilfs_palloc_get_bitmap_block(
  286. inode, group, 1, &bitmap_bh);
  287. if (ret < 0)
  288. goto out_desc;
  289. bitmap_kaddr = kmap(bitmap_bh->b_page);
  290. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  291. pos = nilfs_palloc_find_available_slot(
  292. inode, group, group_offset, bitmap,
  293. entries_per_group);
  294. if (pos >= 0) {
  295. /* found a free entry */
  296. nilfs_palloc_group_desc_add_entries(
  297. inode, group, desc, -1);
  298. req->pr_entry_nr =
  299. entries_per_group * group + pos;
  300. kunmap(desc_bh->b_page);
  301. kunmap(bitmap_bh->b_page);
  302. req->pr_desc_bh = desc_bh;
  303. req->pr_bitmap_bh = bitmap_bh;
  304. return 0;
  305. }
  306. kunmap(bitmap_bh->b_page);
  307. brelse(bitmap_bh);
  308. }
  309. group_offset = 0;
  310. }
  311. kunmap(desc_bh->b_page);
  312. brelse(desc_bh);
  313. }
  314. /* no entries left */
  315. return -ENOSPC;
  316. out_desc:
  317. kunmap(desc_bh->b_page);
  318. brelse(desc_bh);
  319. return ret;
  320. }
  321. void nilfs_palloc_commit_alloc_entry(struct inode *inode,
  322. struct nilfs_palloc_req *req)
  323. {
  324. nilfs_mdt_mark_buffer_dirty(req->pr_bitmap_bh);
  325. nilfs_mdt_mark_buffer_dirty(req->pr_desc_bh);
  326. nilfs_mdt_mark_dirty(inode);
  327. brelse(req->pr_bitmap_bh);
  328. brelse(req->pr_desc_bh);
  329. }
  330. void nilfs_palloc_commit_free_entry(struct inode *inode,
  331. struct nilfs_palloc_req *req)
  332. {
  333. struct nilfs_palloc_group_desc *desc;
  334. unsigned long group, group_offset;
  335. unsigned char *bitmap;
  336. void *desc_kaddr, *bitmap_kaddr;
  337. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  338. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  339. desc = nilfs_palloc_block_get_group_desc(inode, group,
  340. req->pr_desc_bh, desc_kaddr);
  341. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  342. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  343. if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
  344. group_offset, bitmap))
  345. printk(KERN_WARNING "%s: entry number %llu already freed\n",
  346. __func__, (unsigned long long)req->pr_entry_nr);
  347. nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
  348. kunmap(req->pr_bitmap_bh->b_page);
  349. kunmap(req->pr_desc_bh->b_page);
  350. nilfs_mdt_mark_buffer_dirty(req->pr_desc_bh);
  351. nilfs_mdt_mark_buffer_dirty(req->pr_bitmap_bh);
  352. nilfs_mdt_mark_dirty(inode);
  353. brelse(req->pr_bitmap_bh);
  354. brelse(req->pr_desc_bh);
  355. }
  356. void nilfs_palloc_abort_alloc_entry(struct inode *inode,
  357. struct nilfs_palloc_req *req)
  358. {
  359. struct nilfs_palloc_group_desc *desc;
  360. void *desc_kaddr, *bitmap_kaddr;
  361. unsigned char *bitmap;
  362. unsigned long group, group_offset;
  363. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  364. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  365. desc = nilfs_palloc_block_get_group_desc(inode, group,
  366. req->pr_desc_bh, desc_kaddr);
  367. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  368. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  369. if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
  370. group_offset, bitmap))
  371. printk(KERN_WARNING "%s: entry numer %llu already freed\n",
  372. __func__, (unsigned long long)req->pr_entry_nr);
  373. nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
  374. kunmap(req->pr_bitmap_bh->b_page);
  375. kunmap(req->pr_desc_bh->b_page);
  376. brelse(req->pr_bitmap_bh);
  377. brelse(req->pr_desc_bh);
  378. req->pr_entry_nr = 0;
  379. req->pr_bitmap_bh = NULL;
  380. req->pr_desc_bh = NULL;
  381. }
  382. int nilfs_palloc_prepare_free_entry(struct inode *inode,
  383. struct nilfs_palloc_req *req)
  384. {
  385. struct buffer_head *desc_bh, *bitmap_bh;
  386. unsigned long group, group_offset;
  387. int ret;
  388. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  389. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  390. if (ret < 0)
  391. return ret;
  392. ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
  393. if (ret < 0) {
  394. brelse(desc_bh);
  395. return ret;
  396. }
  397. req->pr_desc_bh = desc_bh;
  398. req->pr_bitmap_bh = bitmap_bh;
  399. return 0;
  400. }
  401. void nilfs_palloc_abort_free_entry(struct inode *inode,
  402. struct nilfs_palloc_req *req)
  403. {
  404. brelse(req->pr_bitmap_bh);
  405. brelse(req->pr_desc_bh);
  406. req->pr_entry_nr = 0;
  407. req->pr_bitmap_bh = NULL;
  408. req->pr_desc_bh = NULL;
  409. }
  410. static int
  411. nilfs_palloc_group_is_in(struct inode *inode, unsigned long group, __u64 nr)
  412. {
  413. __u64 first, last;
  414. first = group * nilfs_palloc_entries_per_group(inode);
  415. last = first + nilfs_palloc_entries_per_group(inode) - 1;
  416. return (nr >= first) && (nr <= last);
  417. }
  418. int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
  419. {
  420. struct buffer_head *desc_bh, *bitmap_bh;
  421. struct nilfs_palloc_group_desc *desc;
  422. unsigned char *bitmap;
  423. void *desc_kaddr, *bitmap_kaddr;
  424. unsigned long group, group_offset;
  425. int i, j, n, ret;
  426. for (i = 0; i < nitems; i += n) {
  427. group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
  428. ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
  429. if (ret < 0)
  430. return ret;
  431. ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
  432. &bitmap_bh);
  433. if (ret < 0) {
  434. brelse(desc_bh);
  435. return ret;
  436. }
  437. desc_kaddr = kmap(desc_bh->b_page);
  438. desc = nilfs_palloc_block_get_group_desc(
  439. inode, group, desc_bh, desc_kaddr);
  440. bitmap_kaddr = kmap(bitmap_bh->b_page);
  441. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  442. for (j = i, n = 0;
  443. (j < nitems) && nilfs_palloc_group_is_in(inode, group,
  444. entry_nrs[j]);
  445. j++, n++) {
  446. nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
  447. if (!nilfs_clear_bit_atomic(
  448. nilfs_mdt_bgl_lock(inode, group),
  449. group_offset, bitmap)) {
  450. printk(KERN_WARNING
  451. "%s: entry number %llu already freed\n",
  452. __func__,
  453. (unsigned long long)entry_nrs[j]);
  454. }
  455. }
  456. nilfs_palloc_group_desc_add_entries(inode, group, desc, n);
  457. kunmap(bitmap_bh->b_page);
  458. kunmap(desc_bh->b_page);
  459. nilfs_mdt_mark_buffer_dirty(desc_bh);
  460. nilfs_mdt_mark_buffer_dirty(bitmap_bh);
  461. nilfs_mdt_mark_dirty(inode);
  462. brelse(bitmap_bh);
  463. brelse(desc_bh);
  464. }
  465. return 0;
  466. }
  467. void nilfs_palloc_setup_cache(struct inode *inode,
  468. struct nilfs_palloc_cache *cache)
  469. {
  470. NILFS_MDT(inode)->mi_palloc_cache = cache;
  471. spin_lock_init(&cache->lock);
  472. }
  473. void nilfs_palloc_clear_cache(struct inode *inode)
  474. {
  475. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  476. spin_lock(&cache->lock);
  477. brelse(cache->prev_desc.bh);
  478. brelse(cache->prev_bitmap.bh);
  479. brelse(cache->prev_entry.bh);
  480. cache->prev_desc.bh = NULL;
  481. cache->prev_bitmap.bh = NULL;
  482. cache->prev_entry.bh = NULL;
  483. spin_unlock(&cache->lock);
  484. }
  485. void nilfs_palloc_destroy_cache(struct inode *inode)
  486. {
  487. nilfs_palloc_clear_cache(inode);
  488. NILFS_MDT(inode)->mi_palloc_cache = NULL;
  489. }