alloc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. * @desc: pointer to descriptor structure for the group
  123. * @lock: spin lock protecting @desc
  124. */
  125. static unsigned long
  126. nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc,
  127. spinlock_t *lock)
  128. {
  129. unsigned long nfree;
  130. spin_lock(lock);
  131. nfree = le32_to_cpu(desc->pg_nfrees);
  132. spin_unlock(lock);
  133. return nfree;
  134. }
  135. /**
  136. * nilfs_palloc_group_desc_add_entries - adjust count of free entries
  137. * @desc: pointer to descriptor structure for the group
  138. * @lock: spin lock protecting @desc
  139. * @n: delta to be added
  140. */
  141. static u32
  142. nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc,
  143. spinlock_t *lock, u32 n)
  144. {
  145. u32 nfree;
  146. spin_lock(lock);
  147. le32_add_cpu(&desc->pg_nfrees, n);
  148. nfree = le32_to_cpu(desc->pg_nfrees);
  149. spin_unlock(lock);
  150. return nfree;
  151. }
  152. /**
  153. * nilfs_palloc_entry_blkoff - get block offset of an entry block
  154. * @inode: inode of metadata file using this allocator
  155. * @nr: serial number of the entry (e.g. inode number)
  156. */
  157. static unsigned long
  158. nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
  159. {
  160. unsigned long group, group_offset;
  161. group = nilfs_palloc_group(inode, nr, &group_offset);
  162. return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
  163. group_offset / NILFS_MDT(inode)->mi_entries_per_block;
  164. }
  165. /**
  166. * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
  167. * @inode: inode of metadata file
  168. * @bh: buffer head of the buffer to be initialized
  169. * @kaddr: kernel address mapped for the page including the buffer
  170. */
  171. static void nilfs_palloc_desc_block_init(struct inode *inode,
  172. struct buffer_head *bh, void *kaddr)
  173. {
  174. struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
  175. unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
  176. __le32 nfrees;
  177. nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
  178. while (n-- > 0) {
  179. desc->pg_nfrees = nfrees;
  180. desc++;
  181. }
  182. }
  183. static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
  184. int create,
  185. void (*init_block)(struct inode *,
  186. struct buffer_head *,
  187. void *),
  188. struct buffer_head **bhp,
  189. struct nilfs_bh_assoc *prev,
  190. spinlock_t *lock)
  191. {
  192. int ret;
  193. spin_lock(lock);
  194. if (prev->bh && blkoff == prev->blkoff) {
  195. get_bh(prev->bh);
  196. *bhp = prev->bh;
  197. spin_unlock(lock);
  198. return 0;
  199. }
  200. spin_unlock(lock);
  201. ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
  202. if (!ret) {
  203. spin_lock(lock);
  204. /*
  205. * The following code must be safe for change of the
  206. * cache contents during the get block call.
  207. */
  208. brelse(prev->bh);
  209. get_bh(*bhp);
  210. prev->bh = *bhp;
  211. prev->blkoff = blkoff;
  212. spin_unlock(lock);
  213. }
  214. return ret;
  215. }
  216. /**
  217. * nilfs_palloc_delete_block - delete a block on the persistent allocator file
  218. * @inode: inode of metadata file using this allocator
  219. * @blkoff: block offset
  220. * @prev: nilfs_bh_assoc struct of the last used buffer
  221. * @lock: spin lock protecting @prev
  222. */
  223. static int nilfs_palloc_delete_block(struct inode *inode, unsigned long blkoff,
  224. struct nilfs_bh_assoc *prev,
  225. spinlock_t *lock)
  226. {
  227. spin_lock(lock);
  228. if (prev->bh && blkoff == prev->blkoff) {
  229. brelse(prev->bh);
  230. prev->bh = NULL;
  231. }
  232. spin_unlock(lock);
  233. return nilfs_mdt_delete_block(inode, blkoff);
  234. }
  235. /**
  236. * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
  237. * @inode: inode of metadata file using this allocator
  238. * @group: group number
  239. * @create: create flag
  240. * @bhp: pointer to store the resultant buffer head
  241. */
  242. static int nilfs_palloc_get_desc_block(struct inode *inode,
  243. unsigned long group,
  244. int create, struct buffer_head **bhp)
  245. {
  246. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  247. return nilfs_palloc_get_block(inode,
  248. nilfs_palloc_desc_blkoff(inode, group),
  249. create, nilfs_palloc_desc_block_init,
  250. bhp, &cache->prev_desc, &cache->lock);
  251. }
  252. /**
  253. * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
  254. * @inode: inode of metadata file using this allocator
  255. * @group: group number
  256. * @create: create flag
  257. * @bhp: pointer to store the resultant buffer head
  258. */
  259. static int nilfs_palloc_get_bitmap_block(struct inode *inode,
  260. unsigned long group,
  261. int create, struct buffer_head **bhp)
  262. {
  263. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  264. return nilfs_palloc_get_block(inode,
  265. nilfs_palloc_bitmap_blkoff(inode, group),
  266. create, NULL, bhp,
  267. &cache->prev_bitmap, &cache->lock);
  268. }
  269. /**
  270. * nilfs_palloc_delete_bitmap_block - delete a bitmap block
  271. * @inode: inode of metadata file using this allocator
  272. * @group: group number
  273. */
  274. static int nilfs_palloc_delete_bitmap_block(struct inode *inode,
  275. unsigned long group)
  276. {
  277. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  278. return nilfs_palloc_delete_block(inode,
  279. nilfs_palloc_bitmap_blkoff(inode,
  280. group),
  281. &cache->prev_bitmap, &cache->lock);
  282. }
  283. /**
  284. * nilfs_palloc_get_entry_block - get buffer head of an entry block
  285. * @inode: inode of metadata file using this allocator
  286. * @nr: serial number of the entry (e.g. inode number)
  287. * @create: create flag
  288. * @bhp: pointer to store the resultant buffer head
  289. */
  290. int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
  291. int create, struct buffer_head **bhp)
  292. {
  293. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  294. return nilfs_palloc_get_block(inode,
  295. nilfs_palloc_entry_blkoff(inode, nr),
  296. create, NULL, bhp,
  297. &cache->prev_entry, &cache->lock);
  298. }
  299. /**
  300. * nilfs_palloc_delete_entry_block - delete an entry block
  301. * @inode: inode of metadata file using this allocator
  302. * @nr: serial number of the entry
  303. */
  304. static int nilfs_palloc_delete_entry_block(struct inode *inode, __u64 nr)
  305. {
  306. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  307. return nilfs_palloc_delete_block(inode,
  308. nilfs_palloc_entry_blkoff(inode, nr),
  309. &cache->prev_entry, &cache->lock);
  310. }
  311. /**
  312. * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
  313. * @inode: inode of metadata file using this allocator
  314. * @group: group number
  315. * @bh: buffer head of the buffer storing the group descriptor block
  316. * @kaddr: kernel address mapped for the page including the buffer
  317. */
  318. static struct nilfs_palloc_group_desc *
  319. nilfs_palloc_block_get_group_desc(const struct inode *inode,
  320. unsigned long group,
  321. const struct buffer_head *bh, void *kaddr)
  322. {
  323. return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
  324. group % nilfs_palloc_groups_per_desc_block(inode);
  325. }
  326. /**
  327. * nilfs_palloc_block_get_entry - get kernel address of an entry
  328. * @inode: inode of metadata file using this allocator
  329. * @nr: serial number of the entry (e.g. inode number)
  330. * @bh: buffer head of the buffer storing the entry block
  331. * @kaddr: kernel address mapped for the page including the buffer
  332. */
  333. void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
  334. const struct buffer_head *bh, void *kaddr)
  335. {
  336. unsigned long entry_offset, group_offset;
  337. nilfs_palloc_group(inode, nr, &group_offset);
  338. entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
  339. return kaddr + bh_offset(bh) +
  340. entry_offset * NILFS_MDT(inode)->mi_entry_size;
  341. }
  342. /**
  343. * nilfs_palloc_find_available_slot - find available slot in a group
  344. * @bitmap: bitmap of the group
  345. * @target: offset number of an entry in the group (start point)
  346. * @bsize: size in bits
  347. * @lock: spin lock protecting @bitmap
  348. */
  349. static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
  350. unsigned long target,
  351. unsigned bsize,
  352. spinlock_t *lock)
  353. {
  354. int pos, end = bsize;
  355. if (likely(target < bsize)) {
  356. pos = target;
  357. do {
  358. pos = nilfs_find_next_zero_bit(bitmap, end, pos);
  359. if (pos >= end)
  360. break;
  361. if (!nilfs_set_bit_atomic(lock, pos, bitmap))
  362. return pos;
  363. } while (++pos < end);
  364. end = target;
  365. }
  366. /* wrap around */
  367. for (pos = 0; pos < end; pos++) {
  368. pos = nilfs_find_next_zero_bit(bitmap, end, pos);
  369. if (pos >= end)
  370. break;
  371. if (!nilfs_set_bit_atomic(lock, pos, bitmap))
  372. return pos;
  373. }
  374. return -ENOSPC;
  375. }
  376. /**
  377. * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
  378. * in a group descriptor block
  379. * @inode: inode of metadata file using this allocator
  380. * @curr: current group number
  381. * @max: maximum number of groups
  382. */
  383. static unsigned long
  384. nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
  385. unsigned long curr, unsigned long max)
  386. {
  387. return min_t(unsigned long,
  388. nilfs_palloc_groups_per_desc_block(inode) -
  389. curr % nilfs_palloc_groups_per_desc_block(inode),
  390. max - curr + 1);
  391. }
  392. /**
  393. * nilfs_palloc_count_desc_blocks - count descriptor blocks number
  394. * @inode: inode of metadata file using this allocator
  395. * @desc_blocks: descriptor blocks number [out]
  396. */
  397. static int nilfs_palloc_count_desc_blocks(struct inode *inode,
  398. unsigned long *desc_blocks)
  399. {
  400. __u64 blknum;
  401. int ret;
  402. ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
  403. if (likely(!ret))
  404. *desc_blocks = DIV_ROUND_UP(
  405. (unsigned long)blknum,
  406. NILFS_MDT(inode)->mi_blocks_per_desc_block);
  407. return ret;
  408. }
  409. /**
  410. * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
  411. * MDT file growing
  412. * @inode: inode of metadata file using this allocator
  413. * @desc_blocks: known current descriptor blocks count
  414. */
  415. static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
  416. unsigned long desc_blocks)
  417. {
  418. return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
  419. nilfs_palloc_groups_count(inode);
  420. }
  421. /**
  422. * nilfs_palloc_count_max_entries - count max number of entries that can be
  423. * described by descriptor blocks count
  424. * @inode: inode of metadata file using this allocator
  425. * @nused: current number of used entries
  426. * @nmaxp: max number of entries [out]
  427. */
  428. int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
  429. {
  430. unsigned long desc_blocks = 0;
  431. u64 entries_per_desc_block, nmax;
  432. int err;
  433. err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
  434. if (unlikely(err))
  435. return err;
  436. entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
  437. nilfs_palloc_groups_per_desc_block(inode);
  438. nmax = entries_per_desc_block * desc_blocks;
  439. if (nused == nmax &&
  440. nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
  441. nmax += entries_per_desc_block;
  442. if (nused > nmax)
  443. return -ERANGE;
  444. *nmaxp = nmax;
  445. return 0;
  446. }
  447. /**
  448. * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
  449. * @inode: inode of metadata file using this allocator
  450. * @req: nilfs_palloc_req structure exchanged for the allocation
  451. */
  452. int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
  453. struct nilfs_palloc_req *req)
  454. {
  455. struct buffer_head *desc_bh, *bitmap_bh;
  456. struct nilfs_palloc_group_desc *desc;
  457. unsigned char *bitmap;
  458. void *desc_kaddr, *bitmap_kaddr;
  459. unsigned long group, maxgroup, ngroups;
  460. unsigned long group_offset, maxgroup_offset;
  461. unsigned long n, entries_per_group;
  462. unsigned long i, j;
  463. spinlock_t *lock;
  464. int pos, ret;
  465. ngroups = nilfs_palloc_groups_count(inode);
  466. maxgroup = ngroups - 1;
  467. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  468. entries_per_group = nilfs_palloc_entries_per_group(inode);
  469. for (i = 0; i < ngroups; i += n) {
  470. if (group >= ngroups) {
  471. /* wrap around */
  472. group = 0;
  473. maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
  474. &maxgroup_offset) - 1;
  475. }
  476. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  477. if (ret < 0)
  478. return ret;
  479. desc_kaddr = kmap(desc_bh->b_page);
  480. desc = nilfs_palloc_block_get_group_desc(
  481. inode, group, desc_bh, desc_kaddr);
  482. n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
  483. maxgroup);
  484. for (j = 0; j < n; j++, desc++, group++) {
  485. lock = nilfs_mdt_bgl_lock(inode, group);
  486. if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
  487. ret = nilfs_palloc_get_bitmap_block(
  488. inode, group, 1, &bitmap_bh);
  489. if (ret < 0)
  490. goto out_desc;
  491. bitmap_kaddr = kmap(bitmap_bh->b_page);
  492. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  493. pos = nilfs_palloc_find_available_slot(
  494. bitmap, group_offset,
  495. entries_per_group, lock);
  496. if (pos >= 0) {
  497. /* found a free entry */
  498. nilfs_palloc_group_desc_add_entries(
  499. desc, lock, -1);
  500. req->pr_entry_nr =
  501. entries_per_group * group + pos;
  502. kunmap(desc_bh->b_page);
  503. kunmap(bitmap_bh->b_page);
  504. req->pr_desc_bh = desc_bh;
  505. req->pr_bitmap_bh = bitmap_bh;
  506. return 0;
  507. }
  508. kunmap(bitmap_bh->b_page);
  509. brelse(bitmap_bh);
  510. }
  511. group_offset = 0;
  512. }
  513. kunmap(desc_bh->b_page);
  514. brelse(desc_bh);
  515. }
  516. /* no entries left */
  517. return -ENOSPC;
  518. out_desc:
  519. kunmap(desc_bh->b_page);
  520. brelse(desc_bh);
  521. return ret;
  522. }
  523. /**
  524. * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
  525. * @inode: inode of metadata file using this allocator
  526. * @req: nilfs_palloc_req structure exchanged for the allocation
  527. */
  528. void nilfs_palloc_commit_alloc_entry(struct inode *inode,
  529. struct nilfs_palloc_req *req)
  530. {
  531. mark_buffer_dirty(req->pr_bitmap_bh);
  532. mark_buffer_dirty(req->pr_desc_bh);
  533. nilfs_mdt_mark_dirty(inode);
  534. brelse(req->pr_bitmap_bh);
  535. brelse(req->pr_desc_bh);
  536. }
  537. /**
  538. * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
  539. * @inode: inode of metadata file using this allocator
  540. * @req: nilfs_palloc_req structure exchanged for the removal
  541. */
  542. void nilfs_palloc_commit_free_entry(struct inode *inode,
  543. struct nilfs_palloc_req *req)
  544. {
  545. struct nilfs_palloc_group_desc *desc;
  546. unsigned long group, group_offset;
  547. unsigned char *bitmap;
  548. void *desc_kaddr, *bitmap_kaddr;
  549. spinlock_t *lock;
  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. lock = nilfs_mdt_bgl_lock(inode, group);
  557. if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
  558. nilfs_warning(inode->i_sb, __func__,
  559. "entry number %llu already freed: ino=%lu\n",
  560. (unsigned long long)req->pr_entry_nr,
  561. (unsigned long)inode->i_ino);
  562. else
  563. nilfs_palloc_group_desc_add_entries(desc, lock, 1);
  564. kunmap(req->pr_bitmap_bh->b_page);
  565. kunmap(req->pr_desc_bh->b_page);
  566. mark_buffer_dirty(req->pr_desc_bh);
  567. mark_buffer_dirty(req->pr_bitmap_bh);
  568. nilfs_mdt_mark_dirty(inode);
  569. brelse(req->pr_bitmap_bh);
  570. brelse(req->pr_desc_bh);
  571. }
  572. /**
  573. * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
  574. * @inode: inode of metadata file using this allocator
  575. * @req: nilfs_palloc_req structure exchanged for the allocation
  576. */
  577. void nilfs_palloc_abort_alloc_entry(struct inode *inode,
  578. struct nilfs_palloc_req *req)
  579. {
  580. struct nilfs_palloc_group_desc *desc;
  581. void *desc_kaddr, *bitmap_kaddr;
  582. unsigned char *bitmap;
  583. unsigned long group, group_offset;
  584. spinlock_t *lock;
  585. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  586. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  587. desc = nilfs_palloc_block_get_group_desc(inode, group,
  588. req->pr_desc_bh, desc_kaddr);
  589. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  590. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  591. lock = nilfs_mdt_bgl_lock(inode, group);
  592. if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
  593. nilfs_warning(inode->i_sb, __func__,
  594. "entry number %llu already freed: ino=%lu\n",
  595. (unsigned long long)req->pr_entry_nr,
  596. (unsigned long)inode->i_ino);
  597. else
  598. nilfs_palloc_group_desc_add_entries(desc, lock, 1);
  599. kunmap(req->pr_bitmap_bh->b_page);
  600. kunmap(req->pr_desc_bh->b_page);
  601. brelse(req->pr_bitmap_bh);
  602. brelse(req->pr_desc_bh);
  603. req->pr_entry_nr = 0;
  604. req->pr_bitmap_bh = NULL;
  605. req->pr_desc_bh = NULL;
  606. }
  607. /**
  608. * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
  609. * @inode: inode of metadata file using this allocator
  610. * @req: nilfs_palloc_req structure exchanged for the removal
  611. */
  612. int nilfs_palloc_prepare_free_entry(struct inode *inode,
  613. struct nilfs_palloc_req *req)
  614. {
  615. struct buffer_head *desc_bh, *bitmap_bh;
  616. unsigned long group, group_offset;
  617. int ret;
  618. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  619. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  620. if (ret < 0)
  621. return ret;
  622. ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
  623. if (ret < 0) {
  624. brelse(desc_bh);
  625. return ret;
  626. }
  627. req->pr_desc_bh = desc_bh;
  628. req->pr_bitmap_bh = bitmap_bh;
  629. return 0;
  630. }
  631. /**
  632. * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
  633. * @inode: inode of metadata file using this allocator
  634. * @req: nilfs_palloc_req structure exchanged for the removal
  635. */
  636. void nilfs_palloc_abort_free_entry(struct inode *inode,
  637. struct nilfs_palloc_req *req)
  638. {
  639. brelse(req->pr_bitmap_bh);
  640. brelse(req->pr_desc_bh);
  641. req->pr_entry_nr = 0;
  642. req->pr_bitmap_bh = NULL;
  643. req->pr_desc_bh = NULL;
  644. }
  645. /**
  646. * nilfs_palloc_freev - deallocate a set of persistent objects
  647. * @inode: inode of metadata file using this allocator
  648. * @entry_nrs: array of entry numbers to be deallocated
  649. * @nitems: number of entries stored in @entry_nrs
  650. */
  651. int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
  652. {
  653. struct buffer_head *desc_bh, *bitmap_bh;
  654. struct nilfs_palloc_group_desc *desc;
  655. unsigned char *bitmap;
  656. void *desc_kaddr, *bitmap_kaddr;
  657. unsigned long group, group_offset;
  658. __u64 group_min_nr, last_nrs[8];
  659. const unsigned long epg = nilfs_palloc_entries_per_group(inode);
  660. const unsigned epb = NILFS_MDT(inode)->mi_entries_per_block;
  661. unsigned entry_start, end, pos;
  662. spinlock_t *lock;
  663. int i, j, k, ret;
  664. u32 nfree;
  665. for (i = 0; i < nitems; i = j) {
  666. int change_group = false;
  667. int nempties = 0, n = 0;
  668. group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
  669. ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
  670. if (ret < 0)
  671. return ret;
  672. ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
  673. &bitmap_bh);
  674. if (ret < 0) {
  675. brelse(desc_bh);
  676. return ret;
  677. }
  678. /* Get the first entry number of the group */
  679. group_min_nr = (__u64)group * epg;
  680. bitmap_kaddr = kmap(bitmap_bh->b_page);
  681. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  682. lock = nilfs_mdt_bgl_lock(inode, group);
  683. j = i;
  684. entry_start = rounddown(group_offset, epb);
  685. do {
  686. if (!nilfs_clear_bit_atomic(lock, group_offset,
  687. bitmap)) {
  688. nilfs_warning(inode->i_sb, __func__,
  689. "entry number %llu already freed: ino=%lu\n",
  690. (unsigned long long)entry_nrs[j],
  691. (unsigned long)inode->i_ino);
  692. } else {
  693. n++;
  694. }
  695. j++;
  696. if (j >= nitems || entry_nrs[j] < group_min_nr ||
  697. entry_nrs[j] >= group_min_nr + epg) {
  698. change_group = true;
  699. } else {
  700. group_offset = entry_nrs[j] - group_min_nr;
  701. if (group_offset >= entry_start &&
  702. group_offset < entry_start + epb) {
  703. /* This entry is in the same block */
  704. continue;
  705. }
  706. }
  707. /* Test if the entry block is empty or not */
  708. end = entry_start + epb;
  709. pos = nilfs_find_next_bit(bitmap, end, entry_start);
  710. if (pos >= end) {
  711. last_nrs[nempties++] = entry_nrs[j - 1];
  712. if (nempties >= ARRAY_SIZE(last_nrs))
  713. break;
  714. }
  715. if (change_group)
  716. break;
  717. /* Go on to the next entry block */
  718. entry_start = rounddown(group_offset, epb);
  719. } while (true);
  720. kunmap(bitmap_bh->b_page);
  721. mark_buffer_dirty(bitmap_bh);
  722. brelse(bitmap_bh);
  723. for (k = 0; k < nempties; k++) {
  724. ret = nilfs_palloc_delete_entry_block(inode,
  725. last_nrs[k]);
  726. if (ret && ret != -ENOENT) {
  727. nilfs_warning(inode->i_sb, __func__,
  728. "failed to delete block of entry %llu: ino=%lu, err=%d\n",
  729. (unsigned long long)last_nrs[k],
  730. (unsigned long)inode->i_ino, ret);
  731. }
  732. }
  733. desc_kaddr = kmap_atomic(desc_bh->b_page);
  734. desc = nilfs_palloc_block_get_group_desc(
  735. inode, group, desc_bh, desc_kaddr);
  736. nfree = nilfs_palloc_group_desc_add_entries(desc, lock, n);
  737. kunmap_atomic(desc_kaddr);
  738. mark_buffer_dirty(desc_bh);
  739. nilfs_mdt_mark_dirty(inode);
  740. brelse(desc_bh);
  741. if (nfree == nilfs_palloc_entries_per_group(inode)) {
  742. ret = nilfs_palloc_delete_bitmap_block(inode, group);
  743. if (ret && ret != -ENOENT) {
  744. nilfs_warning(inode->i_sb, __func__,
  745. "failed to delete bitmap block of group %lu: ino=%lu, err=%d\n",
  746. group,
  747. (unsigned long)inode->i_ino, ret);
  748. }
  749. }
  750. }
  751. return 0;
  752. }
  753. void nilfs_palloc_setup_cache(struct inode *inode,
  754. struct nilfs_palloc_cache *cache)
  755. {
  756. NILFS_MDT(inode)->mi_palloc_cache = cache;
  757. spin_lock_init(&cache->lock);
  758. }
  759. void nilfs_palloc_clear_cache(struct inode *inode)
  760. {
  761. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  762. spin_lock(&cache->lock);
  763. brelse(cache->prev_desc.bh);
  764. brelse(cache->prev_bitmap.bh);
  765. brelse(cache->prev_entry.bh);
  766. cache->prev_desc.bh = NULL;
  767. cache->prev_bitmap.bh = NULL;
  768. cache->prev_entry.bh = NULL;
  769. spin_unlock(&cache->lock);
  770. }
  771. void nilfs_palloc_destroy_cache(struct inode *inode)
  772. {
  773. nilfs_palloc_clear_cache(inode);
  774. NILFS_MDT(inode)->mi_palloc_cache = NULL;
  775. }