alloc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 <linux/slab.h>
  29. #include "mdt.h"
  30. #include "alloc.h"
  31. /**
  32. * nilfs_palloc_groups_per_desc_block - get the number of groups that a group
  33. * descriptor block can maintain
  34. * @inode: inode of metadata file using this allocator
  35. */
  36. static inline unsigned long
  37. nilfs_palloc_groups_per_desc_block(const struct inode *inode)
  38. {
  39. return (1UL << inode->i_blkbits) /
  40. sizeof(struct nilfs_palloc_group_desc);
  41. }
  42. /**
  43. * nilfs_palloc_groups_count - get maximum number of groups
  44. * @inode: inode of metadata file using this allocator
  45. */
  46. static inline unsigned long
  47. nilfs_palloc_groups_count(const struct inode *inode)
  48. {
  49. return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
  50. }
  51. /**
  52. * nilfs_palloc_init_blockgroup - initialize private variables for allocator
  53. * @inode: inode of metadata file using this allocator
  54. * @entry_size: size of the persistent object
  55. */
  56. int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned entry_size)
  57. {
  58. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  59. mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS);
  60. if (!mi->mi_bgl)
  61. return -ENOMEM;
  62. bgl_lock_init(mi->mi_bgl);
  63. nilfs_mdt_set_entry_size(inode, entry_size, 0);
  64. mi->mi_blocks_per_group =
  65. DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode),
  66. mi->mi_entries_per_block) + 1;
  67. /* Number of blocks in a group including entry blocks and
  68. a bitmap block */
  69. mi->mi_blocks_per_desc_block =
  70. nilfs_palloc_groups_per_desc_block(inode) *
  71. mi->mi_blocks_per_group + 1;
  72. /* Number of blocks per descriptor including the
  73. descriptor block */
  74. return 0;
  75. }
  76. /**
  77. * nilfs_palloc_group - get group number and offset from an entry number
  78. * @inode: inode of metadata file using this allocator
  79. * @nr: serial number of the entry (e.g. inode number)
  80. * @offset: pointer to store offset number in the group
  81. */
  82. static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr,
  83. unsigned long *offset)
  84. {
  85. __u64 group = nr;
  86. *offset = do_div(group, nilfs_palloc_entries_per_group(inode));
  87. return group;
  88. }
  89. /**
  90. * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block
  91. * @inode: inode of metadata file using this allocator
  92. * @group: group number
  93. *
  94. * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
  95. * block which contains a descriptor of the specified group.
  96. */
  97. static unsigned long
  98. nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group)
  99. {
  100. unsigned long desc_block =
  101. group / nilfs_palloc_groups_per_desc_block(inode);
  102. return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block;
  103. }
  104. /**
  105. * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block
  106. * @inode: inode of metadata file using this allocator
  107. * @group: group number
  108. *
  109. * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
  110. * block used to allocate/deallocate entries in the specified group.
  111. */
  112. static unsigned long
  113. nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group)
  114. {
  115. unsigned long desc_offset =
  116. group % nilfs_palloc_groups_per_desc_block(inode);
  117. return nilfs_palloc_desc_blkoff(inode, group) + 1 +
  118. desc_offset * NILFS_MDT(inode)->mi_blocks_per_group;
  119. }
  120. /**
  121. * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
  122. * @inode: inode of metadata file using this allocator
  123. * @group: group number
  124. * @desc: pointer to descriptor structure for the group
  125. */
  126. static unsigned long
  127. nilfs_palloc_group_desc_nfrees(struct inode *inode, unsigned long group,
  128. const struct nilfs_palloc_group_desc *desc)
  129. {
  130. unsigned long nfree;
  131. spin_lock(nilfs_mdt_bgl_lock(inode, group));
  132. nfree = le32_to_cpu(desc->pg_nfrees);
  133. spin_unlock(nilfs_mdt_bgl_lock(inode, group));
  134. return nfree;
  135. }
  136. /**
  137. * nilfs_palloc_group_desc_add_entries - adjust count of free entries
  138. * @inode: inode of metadata file using this allocator
  139. * @group: group number
  140. * @desc: pointer to descriptor structure for the group
  141. * @n: delta to be added
  142. */
  143. static void
  144. nilfs_palloc_group_desc_add_entries(struct inode *inode,
  145. unsigned long group,
  146. struct nilfs_palloc_group_desc *desc,
  147. u32 n)
  148. {
  149. spin_lock(nilfs_mdt_bgl_lock(inode, group));
  150. le32_add_cpu(&desc->pg_nfrees, n);
  151. spin_unlock(nilfs_mdt_bgl_lock(inode, group));
  152. }
  153. /**
  154. * nilfs_palloc_entry_blkoff - get block offset of an entry block
  155. * @inode: inode of metadata file using this allocator
  156. * @nr: serial number of the entry (e.g. inode number)
  157. */
  158. static unsigned long
  159. nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
  160. {
  161. unsigned long group, group_offset;
  162. group = nilfs_palloc_group(inode, nr, &group_offset);
  163. return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
  164. group_offset / NILFS_MDT(inode)->mi_entries_per_block;
  165. }
  166. /**
  167. * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
  168. * @inode: inode of metadata file
  169. * @bh: buffer head of the buffer to be initialized
  170. * @kaddr: kernel address mapped for the page including the buffer
  171. */
  172. static void nilfs_palloc_desc_block_init(struct inode *inode,
  173. struct buffer_head *bh, void *kaddr)
  174. {
  175. struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
  176. unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
  177. __le32 nfrees;
  178. nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
  179. while (n-- > 0) {
  180. desc->pg_nfrees = nfrees;
  181. desc++;
  182. }
  183. }
  184. static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
  185. int create,
  186. void (*init_block)(struct inode *,
  187. struct buffer_head *,
  188. void *),
  189. struct buffer_head **bhp,
  190. struct nilfs_bh_assoc *prev,
  191. spinlock_t *lock)
  192. {
  193. int ret;
  194. spin_lock(lock);
  195. if (prev->bh && blkoff == prev->blkoff) {
  196. get_bh(prev->bh);
  197. *bhp = prev->bh;
  198. spin_unlock(lock);
  199. return 0;
  200. }
  201. spin_unlock(lock);
  202. ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
  203. if (!ret) {
  204. spin_lock(lock);
  205. /*
  206. * The following code must be safe for change of the
  207. * cache contents during the get block call.
  208. */
  209. brelse(prev->bh);
  210. get_bh(*bhp);
  211. prev->bh = *bhp;
  212. prev->blkoff = blkoff;
  213. spin_unlock(lock);
  214. }
  215. return ret;
  216. }
  217. /**
  218. * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
  219. * @inode: inode of metadata file using this allocator
  220. * @group: group number
  221. * @create: create flag
  222. * @bhp: pointer to store the resultant buffer head
  223. */
  224. static int nilfs_palloc_get_desc_block(struct inode *inode,
  225. unsigned long group,
  226. int create, struct buffer_head **bhp)
  227. {
  228. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  229. return nilfs_palloc_get_block(inode,
  230. nilfs_palloc_desc_blkoff(inode, group),
  231. create, nilfs_palloc_desc_block_init,
  232. bhp, &cache->prev_desc, &cache->lock);
  233. }
  234. /**
  235. * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
  236. * @inode: inode of metadata file using this allocator
  237. * @group: group number
  238. * @create: create flag
  239. * @bhp: pointer to store the resultant buffer head
  240. */
  241. static int nilfs_palloc_get_bitmap_block(struct inode *inode,
  242. unsigned long group,
  243. int create, struct buffer_head **bhp)
  244. {
  245. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  246. return nilfs_palloc_get_block(inode,
  247. nilfs_palloc_bitmap_blkoff(inode, group),
  248. create, NULL, bhp,
  249. &cache->prev_bitmap, &cache->lock);
  250. }
  251. /**
  252. * nilfs_palloc_get_entry_block - get buffer head of an entry block
  253. * @inode: inode of metadata file using this allocator
  254. * @nr: serial number of the entry (e.g. inode number)
  255. * @create: create flag
  256. * @bhp: pointer to store the resultant buffer head
  257. */
  258. int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
  259. int create, struct buffer_head **bhp)
  260. {
  261. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  262. return nilfs_palloc_get_block(inode,
  263. nilfs_palloc_entry_blkoff(inode, nr),
  264. create, NULL, bhp,
  265. &cache->prev_entry, &cache->lock);
  266. }
  267. /**
  268. * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
  269. * @inode: inode of metadata file using this allocator
  270. * @group: group number
  271. * @bh: buffer head of the buffer storing the group descriptor block
  272. * @kaddr: kernel address mapped for the page including the buffer
  273. */
  274. static struct nilfs_palloc_group_desc *
  275. nilfs_palloc_block_get_group_desc(const struct inode *inode,
  276. unsigned long group,
  277. const struct buffer_head *bh, void *kaddr)
  278. {
  279. return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
  280. group % nilfs_palloc_groups_per_desc_block(inode);
  281. }
  282. /**
  283. * nilfs_palloc_block_get_entry - get kernel address of an entry
  284. * @inode: inode of metadata file using this allocator
  285. * @nr: serial number of the entry (e.g. inode number)
  286. * @bh: buffer head of the buffer storing the entry block
  287. * @kaddr: kernel address mapped for the page including the buffer
  288. */
  289. void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
  290. const struct buffer_head *bh, void *kaddr)
  291. {
  292. unsigned long entry_offset, group_offset;
  293. nilfs_palloc_group(inode, nr, &group_offset);
  294. entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
  295. return kaddr + bh_offset(bh) +
  296. entry_offset * NILFS_MDT(inode)->mi_entry_size;
  297. }
  298. /**
  299. * nilfs_palloc_find_available_slot - find available slot in a group
  300. * @inode: inode of metadata file using this allocator
  301. * @group: group number
  302. * @target: offset number of an entry in the group (start point)
  303. * @bitmap: bitmap of the group
  304. * @bsize: size in bits
  305. */
  306. static int nilfs_palloc_find_available_slot(struct inode *inode,
  307. unsigned long group,
  308. unsigned long target,
  309. unsigned char *bitmap,
  310. int bsize)
  311. {
  312. int curr, pos, end, i;
  313. if (target > 0) {
  314. end = (target + BITS_PER_LONG - 1) & ~(BITS_PER_LONG - 1);
  315. if (end > bsize)
  316. end = bsize;
  317. pos = nilfs_find_next_zero_bit(bitmap, end, target);
  318. if (pos < end &&
  319. !nilfs_set_bit_atomic(
  320. nilfs_mdt_bgl_lock(inode, group), pos, bitmap))
  321. return pos;
  322. } else
  323. end = 0;
  324. for (i = 0, curr = end;
  325. i < bsize;
  326. i += BITS_PER_LONG, curr += BITS_PER_LONG) {
  327. /* wrap around */
  328. if (curr >= bsize)
  329. curr = 0;
  330. while (*((unsigned long *)bitmap + curr / BITS_PER_LONG)
  331. != ~0UL) {
  332. end = curr + BITS_PER_LONG;
  333. if (end > bsize)
  334. end = bsize;
  335. pos = nilfs_find_next_zero_bit(bitmap, end, curr);
  336. if ((pos < end) &&
  337. !nilfs_set_bit_atomic(
  338. nilfs_mdt_bgl_lock(inode, group), pos,
  339. bitmap))
  340. return pos;
  341. }
  342. }
  343. return -ENOSPC;
  344. }
  345. /**
  346. * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
  347. * in a group descriptor block
  348. * @inode: inode of metadata file using this allocator
  349. * @curr: current group number
  350. * @max: maximum number of groups
  351. */
  352. static unsigned long
  353. nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
  354. unsigned long curr, unsigned long max)
  355. {
  356. return min_t(unsigned long,
  357. nilfs_palloc_groups_per_desc_block(inode) -
  358. curr % nilfs_palloc_groups_per_desc_block(inode),
  359. max - curr + 1);
  360. }
  361. /**
  362. * nilfs_palloc_count_desc_blocks - count descriptor blocks number
  363. * @inode: inode of metadata file using this allocator
  364. * @desc_blocks: descriptor blocks number [out]
  365. */
  366. static int nilfs_palloc_count_desc_blocks(struct inode *inode,
  367. unsigned long *desc_blocks)
  368. {
  369. __u64 blknum;
  370. int ret;
  371. ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
  372. if (likely(!ret))
  373. *desc_blocks = DIV_ROUND_UP(
  374. (unsigned long)blknum,
  375. NILFS_MDT(inode)->mi_blocks_per_desc_block);
  376. return ret;
  377. }
  378. /**
  379. * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
  380. * MDT file growing
  381. * @inode: inode of metadata file using this allocator
  382. * @desc_blocks: known current descriptor blocks count
  383. */
  384. static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
  385. unsigned long desc_blocks)
  386. {
  387. return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
  388. nilfs_palloc_groups_count(inode);
  389. }
  390. /**
  391. * nilfs_palloc_count_max_entries - count max number of entries that can be
  392. * described by descriptor blocks count
  393. * @inode: inode of metadata file using this allocator
  394. * @nused: current number of used entries
  395. * @nmaxp: max number of entries [out]
  396. */
  397. int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
  398. {
  399. unsigned long desc_blocks = 0;
  400. u64 entries_per_desc_block, nmax;
  401. int err;
  402. err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
  403. if (unlikely(err))
  404. return err;
  405. entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
  406. nilfs_palloc_groups_per_desc_block(inode);
  407. nmax = entries_per_desc_block * desc_blocks;
  408. if (nused == nmax &&
  409. nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
  410. nmax += entries_per_desc_block;
  411. if (nused > nmax)
  412. return -ERANGE;
  413. *nmaxp = nmax;
  414. return 0;
  415. }
  416. /**
  417. * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
  418. * @inode: inode of metadata file using this allocator
  419. * @req: nilfs_palloc_req structure exchanged for the allocation
  420. */
  421. int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
  422. struct nilfs_palloc_req *req)
  423. {
  424. struct buffer_head *desc_bh, *bitmap_bh;
  425. struct nilfs_palloc_group_desc *desc;
  426. unsigned char *bitmap;
  427. void *desc_kaddr, *bitmap_kaddr;
  428. unsigned long group, maxgroup, ngroups;
  429. unsigned long group_offset, maxgroup_offset;
  430. unsigned long n, entries_per_group, groups_per_desc_block;
  431. unsigned long i, j;
  432. int pos, ret;
  433. ngroups = nilfs_palloc_groups_count(inode);
  434. maxgroup = ngroups - 1;
  435. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  436. entries_per_group = nilfs_palloc_entries_per_group(inode);
  437. groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
  438. for (i = 0; i < ngroups; i += n) {
  439. if (group >= ngroups) {
  440. /* wrap around */
  441. group = 0;
  442. maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
  443. &maxgroup_offset) - 1;
  444. }
  445. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  446. if (ret < 0)
  447. return ret;
  448. desc_kaddr = kmap(desc_bh->b_page);
  449. desc = nilfs_palloc_block_get_group_desc(
  450. inode, group, desc_bh, desc_kaddr);
  451. n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
  452. maxgroup);
  453. for (j = 0; j < n; j++, desc++, group++) {
  454. if (nilfs_palloc_group_desc_nfrees(inode, group, desc)
  455. > 0) {
  456. ret = nilfs_palloc_get_bitmap_block(
  457. inode, group, 1, &bitmap_bh);
  458. if (ret < 0)
  459. goto out_desc;
  460. bitmap_kaddr = kmap(bitmap_bh->b_page);
  461. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  462. pos = nilfs_palloc_find_available_slot(
  463. inode, group, group_offset, bitmap,
  464. entries_per_group);
  465. if (pos >= 0) {
  466. /* found a free entry */
  467. nilfs_palloc_group_desc_add_entries(
  468. inode, group, desc, -1);
  469. req->pr_entry_nr =
  470. entries_per_group * group + pos;
  471. kunmap(desc_bh->b_page);
  472. kunmap(bitmap_bh->b_page);
  473. req->pr_desc_bh = desc_bh;
  474. req->pr_bitmap_bh = bitmap_bh;
  475. return 0;
  476. }
  477. kunmap(bitmap_bh->b_page);
  478. brelse(bitmap_bh);
  479. }
  480. group_offset = 0;
  481. }
  482. kunmap(desc_bh->b_page);
  483. brelse(desc_bh);
  484. }
  485. /* no entries left */
  486. return -ENOSPC;
  487. out_desc:
  488. kunmap(desc_bh->b_page);
  489. brelse(desc_bh);
  490. return ret;
  491. }
  492. /**
  493. * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
  494. * @inode: inode of metadata file using this allocator
  495. * @req: nilfs_palloc_req structure exchanged for the allocation
  496. */
  497. void nilfs_palloc_commit_alloc_entry(struct inode *inode,
  498. struct nilfs_palloc_req *req)
  499. {
  500. mark_buffer_dirty(req->pr_bitmap_bh);
  501. mark_buffer_dirty(req->pr_desc_bh);
  502. nilfs_mdt_mark_dirty(inode);
  503. brelse(req->pr_bitmap_bh);
  504. brelse(req->pr_desc_bh);
  505. }
  506. /**
  507. * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
  508. * @inode: inode of metadata file using this allocator
  509. * @req: nilfs_palloc_req structure exchanged for the removal
  510. */
  511. void nilfs_palloc_commit_free_entry(struct inode *inode,
  512. struct nilfs_palloc_req *req)
  513. {
  514. struct nilfs_palloc_group_desc *desc;
  515. unsigned long group, group_offset;
  516. unsigned char *bitmap;
  517. void *desc_kaddr, *bitmap_kaddr;
  518. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  519. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  520. desc = nilfs_palloc_block_get_group_desc(inode, group,
  521. req->pr_desc_bh, desc_kaddr);
  522. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  523. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  524. if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
  525. group_offset, bitmap))
  526. printk(KERN_WARNING "%s: entry number %llu already freed\n",
  527. __func__, (unsigned long long)req->pr_entry_nr);
  528. else
  529. nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
  530. kunmap(req->pr_bitmap_bh->b_page);
  531. kunmap(req->pr_desc_bh->b_page);
  532. mark_buffer_dirty(req->pr_desc_bh);
  533. mark_buffer_dirty(req->pr_bitmap_bh);
  534. nilfs_mdt_mark_dirty(inode);
  535. brelse(req->pr_bitmap_bh);
  536. brelse(req->pr_desc_bh);
  537. }
  538. /**
  539. * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
  540. * @inode: inode of metadata file using this allocator
  541. * @req: nilfs_palloc_req structure exchanged for the allocation
  542. */
  543. void nilfs_palloc_abort_alloc_entry(struct inode *inode,
  544. struct nilfs_palloc_req *req)
  545. {
  546. struct nilfs_palloc_group_desc *desc;
  547. void *desc_kaddr, *bitmap_kaddr;
  548. unsigned char *bitmap;
  549. unsigned long group, group_offset;
  550. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  551. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  552. desc = nilfs_palloc_block_get_group_desc(inode, group,
  553. req->pr_desc_bh, desc_kaddr);
  554. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  555. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  556. if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
  557. group_offset, bitmap))
  558. printk(KERN_WARNING "%s: entry number %llu already freed\n",
  559. __func__, (unsigned long long)req->pr_entry_nr);
  560. else
  561. nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
  562. kunmap(req->pr_bitmap_bh->b_page);
  563. kunmap(req->pr_desc_bh->b_page);
  564. brelse(req->pr_bitmap_bh);
  565. brelse(req->pr_desc_bh);
  566. req->pr_entry_nr = 0;
  567. req->pr_bitmap_bh = NULL;
  568. req->pr_desc_bh = NULL;
  569. }
  570. /**
  571. * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
  572. * @inode: inode of metadata file using this allocator
  573. * @req: nilfs_palloc_req structure exchanged for the removal
  574. */
  575. int nilfs_palloc_prepare_free_entry(struct inode *inode,
  576. struct nilfs_palloc_req *req)
  577. {
  578. struct buffer_head *desc_bh, *bitmap_bh;
  579. unsigned long group, group_offset;
  580. int ret;
  581. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  582. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  583. if (ret < 0)
  584. return ret;
  585. ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
  586. if (ret < 0) {
  587. brelse(desc_bh);
  588. return ret;
  589. }
  590. req->pr_desc_bh = desc_bh;
  591. req->pr_bitmap_bh = bitmap_bh;
  592. return 0;
  593. }
  594. /**
  595. * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
  596. * @inode: inode of metadata file using this allocator
  597. * @req: nilfs_palloc_req structure exchanged for the removal
  598. */
  599. void nilfs_palloc_abort_free_entry(struct inode *inode,
  600. struct nilfs_palloc_req *req)
  601. {
  602. brelse(req->pr_bitmap_bh);
  603. brelse(req->pr_desc_bh);
  604. req->pr_entry_nr = 0;
  605. req->pr_bitmap_bh = NULL;
  606. req->pr_desc_bh = NULL;
  607. }
  608. /**
  609. * nilfs_palloc_group_is_in - judge if an entry is in a group
  610. * @inode: inode of metadata file using this allocator
  611. * @group: group number
  612. * @nr: serial number of the entry (e.g. inode number)
  613. */
  614. static int
  615. nilfs_palloc_group_is_in(struct inode *inode, unsigned long group, __u64 nr)
  616. {
  617. __u64 first, last;
  618. first = group * nilfs_palloc_entries_per_group(inode);
  619. last = first + nilfs_palloc_entries_per_group(inode) - 1;
  620. return (nr >= first) && (nr <= last);
  621. }
  622. /**
  623. * nilfs_palloc_freev - deallocate a set of persistent objects
  624. * @inode: inode of metadata file using this allocator
  625. * @entry_nrs: array of entry numbers to be deallocated
  626. * @nitems: number of entries stored in @entry_nrs
  627. */
  628. int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
  629. {
  630. struct buffer_head *desc_bh, *bitmap_bh;
  631. struct nilfs_palloc_group_desc *desc;
  632. unsigned char *bitmap;
  633. void *desc_kaddr, *bitmap_kaddr;
  634. unsigned long group, group_offset;
  635. int i, j, n, ret;
  636. for (i = 0; i < nitems; i = j) {
  637. group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
  638. ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
  639. if (ret < 0)
  640. return ret;
  641. ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
  642. &bitmap_bh);
  643. if (ret < 0) {
  644. brelse(desc_bh);
  645. return ret;
  646. }
  647. desc_kaddr = kmap(desc_bh->b_page);
  648. desc = nilfs_palloc_block_get_group_desc(
  649. inode, group, desc_bh, desc_kaddr);
  650. bitmap_kaddr = kmap(bitmap_bh->b_page);
  651. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  652. for (j = i, n = 0;
  653. (j < nitems) && nilfs_palloc_group_is_in(inode, group,
  654. entry_nrs[j]);
  655. j++) {
  656. nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
  657. if (!nilfs_clear_bit_atomic(
  658. nilfs_mdt_bgl_lock(inode, group),
  659. group_offset, bitmap)) {
  660. printk(KERN_WARNING
  661. "%s: entry number %llu already freed\n",
  662. __func__,
  663. (unsigned long long)entry_nrs[j]);
  664. } else {
  665. n++;
  666. }
  667. }
  668. nilfs_palloc_group_desc_add_entries(inode, group, desc, n);
  669. kunmap(bitmap_bh->b_page);
  670. kunmap(desc_bh->b_page);
  671. mark_buffer_dirty(desc_bh);
  672. mark_buffer_dirty(bitmap_bh);
  673. nilfs_mdt_mark_dirty(inode);
  674. brelse(bitmap_bh);
  675. brelse(desc_bh);
  676. }
  677. return 0;
  678. }
  679. void nilfs_palloc_setup_cache(struct inode *inode,
  680. struct nilfs_palloc_cache *cache)
  681. {
  682. NILFS_MDT(inode)->mi_palloc_cache = cache;
  683. spin_lock_init(&cache->lock);
  684. }
  685. void nilfs_palloc_clear_cache(struct inode *inode)
  686. {
  687. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  688. spin_lock(&cache->lock);
  689. brelse(cache->prev_desc.bh);
  690. brelse(cache->prev_bitmap.bh);
  691. brelse(cache->prev_entry.bh);
  692. cache->prev_desc.bh = NULL;
  693. cache->prev_bitmap.bh = NULL;
  694. cache->prev_entry.bh = NULL;
  695. spin_unlock(&cache->lock);
  696. }
  697. void nilfs_palloc_destroy_cache(struct inode *inode)
  698. {
  699. nilfs_palloc_clear_cache(inode);
  700. NILFS_MDT(inode)->mi_palloc_cache = NULL;
  701. }