cpfile.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * cpfile.c - NILFS checkpoint file.
  4. *
  5. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Koji Sato.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/fs.h>
  11. #include <linux/string.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/errno.h>
  14. #include "mdt.h"
  15. #include "cpfile.h"
  16. static inline unsigned long
  17. nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile)
  18. {
  19. return NILFS_MDT(cpfile)->mi_entries_per_block;
  20. }
  21. /* block number from the beginning of the file */
  22. static unsigned long
  23. nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
  24. {
  25. __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
  26. do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
  27. return (unsigned long)tcno;
  28. }
  29. /* offset in block */
  30. static unsigned long
  31. nilfs_cpfile_get_offset(const struct inode *cpfile, __u64 cno)
  32. {
  33. __u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
  34. return do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
  35. }
  36. static __u64 nilfs_cpfile_first_checkpoint_in_block(const struct inode *cpfile,
  37. unsigned long blkoff)
  38. {
  39. return (__u64)nilfs_cpfile_checkpoints_per_block(cpfile) * blkoff
  40. + 1 - NILFS_MDT(cpfile)->mi_first_entry_offset;
  41. }
  42. static unsigned long
  43. nilfs_cpfile_checkpoints_in_block(const struct inode *cpfile,
  44. __u64 curr,
  45. __u64 max)
  46. {
  47. return min_t(__u64,
  48. nilfs_cpfile_checkpoints_per_block(cpfile) -
  49. nilfs_cpfile_get_offset(cpfile, curr),
  50. max - curr);
  51. }
  52. static inline int nilfs_cpfile_is_in_first(const struct inode *cpfile,
  53. __u64 cno)
  54. {
  55. return nilfs_cpfile_get_blkoff(cpfile, cno) == 0;
  56. }
  57. static unsigned int
  58. nilfs_cpfile_block_add_valid_checkpoints(const struct inode *cpfile,
  59. struct buffer_head *bh,
  60. void *kaddr,
  61. unsigned int n)
  62. {
  63. struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
  64. unsigned int count;
  65. count = le32_to_cpu(cp->cp_checkpoints_count) + n;
  66. cp->cp_checkpoints_count = cpu_to_le32(count);
  67. return count;
  68. }
  69. static unsigned int
  70. nilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile,
  71. struct buffer_head *bh,
  72. void *kaddr,
  73. unsigned int n)
  74. {
  75. struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
  76. unsigned int count;
  77. WARN_ON(le32_to_cpu(cp->cp_checkpoints_count) < n);
  78. count = le32_to_cpu(cp->cp_checkpoints_count) - n;
  79. cp->cp_checkpoints_count = cpu_to_le32(count);
  80. return count;
  81. }
  82. static inline struct nilfs_cpfile_header *
  83. nilfs_cpfile_block_get_header(const struct inode *cpfile,
  84. struct buffer_head *bh,
  85. void *kaddr)
  86. {
  87. return kaddr + bh_offset(bh);
  88. }
  89. static struct nilfs_checkpoint *
  90. nilfs_cpfile_block_get_checkpoint(const struct inode *cpfile, __u64 cno,
  91. struct buffer_head *bh,
  92. void *kaddr)
  93. {
  94. return kaddr + bh_offset(bh) + nilfs_cpfile_get_offset(cpfile, cno) *
  95. NILFS_MDT(cpfile)->mi_entry_size;
  96. }
  97. static void nilfs_cpfile_block_init(struct inode *cpfile,
  98. struct buffer_head *bh,
  99. void *kaddr)
  100. {
  101. struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
  102. size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
  103. int n = nilfs_cpfile_checkpoints_per_block(cpfile);
  104. while (n-- > 0) {
  105. nilfs_checkpoint_set_invalid(cp);
  106. cp = (void *)cp + cpsz;
  107. }
  108. }
  109. static inline int nilfs_cpfile_get_header_block(struct inode *cpfile,
  110. struct buffer_head **bhp)
  111. {
  112. return nilfs_mdt_get_block(cpfile, 0, 0, NULL, bhp);
  113. }
  114. static inline int nilfs_cpfile_get_checkpoint_block(struct inode *cpfile,
  115. __u64 cno,
  116. int create,
  117. struct buffer_head **bhp)
  118. {
  119. return nilfs_mdt_get_block(cpfile,
  120. nilfs_cpfile_get_blkoff(cpfile, cno),
  121. create, nilfs_cpfile_block_init, bhp);
  122. }
  123. /**
  124. * nilfs_cpfile_find_checkpoint_block - find and get a buffer on cpfile
  125. * @cpfile: inode of cpfile
  126. * @start_cno: start checkpoint number (inclusive)
  127. * @end_cno: end checkpoint number (inclusive)
  128. * @cnop: place to store the next checkpoint number
  129. * @bhp: place to store a pointer to buffer_head struct
  130. *
  131. * Return Value: On success, it returns 0. On error, the following negative
  132. * error code is returned.
  133. *
  134. * %-ENOMEM - Insufficient memory available.
  135. *
  136. * %-EIO - I/O error
  137. *
  138. * %-ENOENT - no block exists in the range.
  139. */
  140. static int nilfs_cpfile_find_checkpoint_block(struct inode *cpfile,
  141. __u64 start_cno, __u64 end_cno,
  142. __u64 *cnop,
  143. struct buffer_head **bhp)
  144. {
  145. unsigned long start, end, blkoff;
  146. int ret;
  147. if (unlikely(start_cno > end_cno))
  148. return -ENOENT;
  149. start = nilfs_cpfile_get_blkoff(cpfile, start_cno);
  150. end = nilfs_cpfile_get_blkoff(cpfile, end_cno);
  151. ret = nilfs_mdt_find_block(cpfile, start, end, &blkoff, bhp);
  152. if (!ret)
  153. *cnop = (blkoff == start) ? start_cno :
  154. nilfs_cpfile_first_checkpoint_in_block(cpfile, blkoff);
  155. return ret;
  156. }
  157. static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
  158. __u64 cno)
  159. {
  160. return nilfs_mdt_delete_block(cpfile,
  161. nilfs_cpfile_get_blkoff(cpfile, cno));
  162. }
  163. /**
  164. * nilfs_cpfile_get_checkpoint - get a checkpoint
  165. * @cpfile: inode of checkpoint file
  166. * @cno: checkpoint number
  167. * @create: create flag
  168. * @cpp: pointer to a checkpoint
  169. * @bhp: pointer to a buffer head
  170. *
  171. * Description: nilfs_cpfile_get_checkpoint() acquires the checkpoint
  172. * specified by @cno. A new checkpoint will be created if @cno is the current
  173. * checkpoint number and @create is nonzero.
  174. *
  175. * Return Value: On success, 0 is returned, and the checkpoint and the
  176. * buffer head of the buffer on which the checkpoint is located are stored in
  177. * the place pointed by @cpp and @bhp, respectively. On error, one of the
  178. * following negative error codes is returned.
  179. *
  180. * %-EIO - I/O error.
  181. *
  182. * %-ENOMEM - Insufficient amount of memory available.
  183. *
  184. * %-ENOENT - No such checkpoint.
  185. *
  186. * %-EINVAL - invalid checkpoint.
  187. */
  188. int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
  189. __u64 cno,
  190. int create,
  191. struct nilfs_checkpoint **cpp,
  192. struct buffer_head **bhp)
  193. {
  194. struct buffer_head *header_bh, *cp_bh;
  195. struct nilfs_cpfile_header *header;
  196. struct nilfs_checkpoint *cp;
  197. void *kaddr;
  198. int ret;
  199. if (unlikely(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
  200. (cno < nilfs_mdt_cno(cpfile) && create)))
  201. return -EINVAL;
  202. down_write(&NILFS_MDT(cpfile)->mi_sem);
  203. ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
  204. if (ret < 0)
  205. goto out_sem;
  206. ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, create, &cp_bh);
  207. if (ret < 0)
  208. goto out_header;
  209. kaddr = kmap(cp_bh->b_page);
  210. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
  211. if (nilfs_checkpoint_invalid(cp)) {
  212. if (!create) {
  213. kunmap(cp_bh->b_page);
  214. brelse(cp_bh);
  215. ret = -ENOENT;
  216. goto out_header;
  217. }
  218. /* a newly-created checkpoint */
  219. nilfs_checkpoint_clear_invalid(cp);
  220. if (!nilfs_cpfile_is_in_first(cpfile, cno))
  221. nilfs_cpfile_block_add_valid_checkpoints(cpfile, cp_bh,
  222. kaddr, 1);
  223. mark_buffer_dirty(cp_bh);
  224. kaddr = kmap_atomic(header_bh->b_page);
  225. header = nilfs_cpfile_block_get_header(cpfile, header_bh,
  226. kaddr);
  227. le64_add_cpu(&header->ch_ncheckpoints, 1);
  228. kunmap_atomic(kaddr);
  229. mark_buffer_dirty(header_bh);
  230. nilfs_mdt_mark_dirty(cpfile);
  231. }
  232. if (cpp != NULL)
  233. *cpp = cp;
  234. *bhp = cp_bh;
  235. out_header:
  236. brelse(header_bh);
  237. out_sem:
  238. up_write(&NILFS_MDT(cpfile)->mi_sem);
  239. return ret;
  240. }
  241. /**
  242. * nilfs_cpfile_put_checkpoint - put a checkpoint
  243. * @cpfile: inode of checkpoint file
  244. * @cno: checkpoint number
  245. * @bh: buffer head
  246. *
  247. * Description: nilfs_cpfile_put_checkpoint() releases the checkpoint
  248. * specified by @cno. @bh must be the buffer head which has been returned by
  249. * a previous call to nilfs_cpfile_get_checkpoint() with @cno.
  250. */
  251. void nilfs_cpfile_put_checkpoint(struct inode *cpfile, __u64 cno,
  252. struct buffer_head *bh)
  253. {
  254. kunmap(bh->b_page);
  255. brelse(bh);
  256. }
  257. /**
  258. * nilfs_cpfile_delete_checkpoints - delete checkpoints
  259. * @cpfile: inode of checkpoint file
  260. * @start: start checkpoint number
  261. * @end: end checkpoint numer
  262. *
  263. * Description: nilfs_cpfile_delete_checkpoints() deletes the checkpoints in
  264. * the period from @start to @end, excluding @end itself. The checkpoints
  265. * which have been already deleted are ignored.
  266. *
  267. * Return Value: On success, 0 is returned. On error, one of the following
  268. * negative error codes is returned.
  269. *
  270. * %-EIO - I/O error.
  271. *
  272. * %-ENOMEM - Insufficient amount of memory available.
  273. *
  274. * %-EINVAL - invalid checkpoints.
  275. */
  276. int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
  277. __u64 start,
  278. __u64 end)
  279. {
  280. struct buffer_head *header_bh, *cp_bh;
  281. struct nilfs_cpfile_header *header;
  282. struct nilfs_checkpoint *cp;
  283. size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
  284. __u64 cno;
  285. void *kaddr;
  286. unsigned long tnicps;
  287. int ret, ncps, nicps, nss, count, i;
  288. if (unlikely(start == 0 || start > end)) {
  289. nilfs_err(cpfile->i_sb,
  290. "cannot delete checkpoints: invalid range [%llu, %llu)",
  291. (unsigned long long)start, (unsigned long long)end);
  292. return -EINVAL;
  293. }
  294. down_write(&NILFS_MDT(cpfile)->mi_sem);
  295. ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
  296. if (ret < 0)
  297. goto out_sem;
  298. tnicps = 0;
  299. nss = 0;
  300. for (cno = start; cno < end; cno += ncps) {
  301. ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, end);
  302. ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
  303. if (ret < 0) {
  304. if (ret != -ENOENT)
  305. break;
  306. /* skip hole */
  307. ret = 0;
  308. continue;
  309. }
  310. kaddr = kmap_atomic(cp_bh->b_page);
  311. cp = nilfs_cpfile_block_get_checkpoint(
  312. cpfile, cno, cp_bh, kaddr);
  313. nicps = 0;
  314. for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) {
  315. if (nilfs_checkpoint_snapshot(cp)) {
  316. nss++;
  317. } else if (!nilfs_checkpoint_invalid(cp)) {
  318. nilfs_checkpoint_set_invalid(cp);
  319. nicps++;
  320. }
  321. }
  322. if (nicps > 0) {
  323. tnicps += nicps;
  324. mark_buffer_dirty(cp_bh);
  325. nilfs_mdt_mark_dirty(cpfile);
  326. if (!nilfs_cpfile_is_in_first(cpfile, cno)) {
  327. count =
  328. nilfs_cpfile_block_sub_valid_checkpoints(
  329. cpfile, cp_bh, kaddr, nicps);
  330. if (count == 0) {
  331. /* make hole */
  332. kunmap_atomic(kaddr);
  333. brelse(cp_bh);
  334. ret =
  335. nilfs_cpfile_delete_checkpoint_block(
  336. cpfile, cno);
  337. if (ret == 0)
  338. continue;
  339. nilfs_err(cpfile->i_sb,
  340. "error %d deleting checkpoint block",
  341. ret);
  342. break;
  343. }
  344. }
  345. }
  346. kunmap_atomic(kaddr);
  347. brelse(cp_bh);
  348. }
  349. if (tnicps > 0) {
  350. kaddr = kmap_atomic(header_bh->b_page);
  351. header = nilfs_cpfile_block_get_header(cpfile, header_bh,
  352. kaddr);
  353. le64_add_cpu(&header->ch_ncheckpoints, -(u64)tnicps);
  354. mark_buffer_dirty(header_bh);
  355. nilfs_mdt_mark_dirty(cpfile);
  356. kunmap_atomic(kaddr);
  357. }
  358. brelse(header_bh);
  359. if (nss > 0)
  360. ret = -EBUSY;
  361. out_sem:
  362. up_write(&NILFS_MDT(cpfile)->mi_sem);
  363. return ret;
  364. }
  365. static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
  366. struct nilfs_checkpoint *cp,
  367. struct nilfs_cpinfo *ci)
  368. {
  369. ci->ci_flags = le32_to_cpu(cp->cp_flags);
  370. ci->ci_cno = le64_to_cpu(cp->cp_cno);
  371. ci->ci_create = le64_to_cpu(cp->cp_create);
  372. ci->ci_nblk_inc = le64_to_cpu(cp->cp_nblk_inc);
  373. ci->ci_inodes_count = le64_to_cpu(cp->cp_inodes_count);
  374. ci->ci_blocks_count = le64_to_cpu(cp->cp_blocks_count);
  375. ci->ci_next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
  376. }
  377. static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
  378. void *buf, unsigned int cisz,
  379. size_t nci)
  380. {
  381. struct nilfs_checkpoint *cp;
  382. struct nilfs_cpinfo *ci = buf;
  383. struct buffer_head *bh;
  384. size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
  385. __u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop;
  386. void *kaddr;
  387. int n, ret;
  388. int ncps, i;
  389. if (cno == 0)
  390. return -ENOENT; /* checkpoint number 0 is invalid */
  391. down_read(&NILFS_MDT(cpfile)->mi_sem);
  392. for (n = 0; n < nci; cno += ncps) {
  393. ret = nilfs_cpfile_find_checkpoint_block(
  394. cpfile, cno, cur_cno - 1, &cno, &bh);
  395. if (ret < 0) {
  396. if (likely(ret == -ENOENT))
  397. break;
  398. goto out;
  399. }
  400. ncps = nilfs_cpfile_checkpoints_in_block(cpfile, cno, cur_cno);
  401. kaddr = kmap_atomic(bh->b_page);
  402. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
  403. for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) {
  404. if (!nilfs_checkpoint_invalid(cp)) {
  405. nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp,
  406. ci);
  407. ci = (void *)ci + cisz;
  408. n++;
  409. }
  410. }
  411. kunmap_atomic(kaddr);
  412. brelse(bh);
  413. }
  414. ret = n;
  415. if (n > 0) {
  416. ci = (void *)ci - cisz;
  417. *cnop = ci->ci_cno + 1;
  418. }
  419. out:
  420. up_read(&NILFS_MDT(cpfile)->mi_sem);
  421. return ret;
  422. }
  423. static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
  424. void *buf, unsigned int cisz,
  425. size_t nci)
  426. {
  427. struct buffer_head *bh;
  428. struct nilfs_cpfile_header *header;
  429. struct nilfs_checkpoint *cp;
  430. struct nilfs_cpinfo *ci = buf;
  431. __u64 curr = *cnop, next;
  432. unsigned long curr_blkoff, next_blkoff;
  433. void *kaddr;
  434. int n = 0, ret;
  435. down_read(&NILFS_MDT(cpfile)->mi_sem);
  436. if (curr == 0) {
  437. ret = nilfs_cpfile_get_header_block(cpfile, &bh);
  438. if (ret < 0)
  439. goto out;
  440. kaddr = kmap_atomic(bh->b_page);
  441. header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
  442. curr = le64_to_cpu(header->ch_snapshot_list.ssl_next);
  443. kunmap_atomic(kaddr);
  444. brelse(bh);
  445. if (curr == 0) {
  446. ret = 0;
  447. goto out;
  448. }
  449. } else if (unlikely(curr == ~(__u64)0)) {
  450. ret = 0;
  451. goto out;
  452. }
  453. curr_blkoff = nilfs_cpfile_get_blkoff(cpfile, curr);
  454. ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr, 0, &bh);
  455. if (unlikely(ret < 0)) {
  456. if (ret == -ENOENT)
  457. ret = 0; /* No snapshots (started from a hole block) */
  458. goto out;
  459. }
  460. kaddr = kmap_atomic(bh->b_page);
  461. while (n < nci) {
  462. cp = nilfs_cpfile_block_get_checkpoint(cpfile, curr, bh, kaddr);
  463. curr = ~(__u64)0; /* Terminator */
  464. if (unlikely(nilfs_checkpoint_invalid(cp) ||
  465. !nilfs_checkpoint_snapshot(cp)))
  466. break;
  467. nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
  468. ci = (void *)ci + cisz;
  469. n++;
  470. next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
  471. if (next == 0)
  472. break; /* reach end of the snapshot list */
  473. next_blkoff = nilfs_cpfile_get_blkoff(cpfile, next);
  474. if (curr_blkoff != next_blkoff) {
  475. kunmap_atomic(kaddr);
  476. brelse(bh);
  477. ret = nilfs_cpfile_get_checkpoint_block(cpfile, next,
  478. 0, &bh);
  479. if (unlikely(ret < 0)) {
  480. WARN_ON(ret == -ENOENT);
  481. goto out;
  482. }
  483. kaddr = kmap_atomic(bh->b_page);
  484. }
  485. curr = next;
  486. curr_blkoff = next_blkoff;
  487. }
  488. kunmap_atomic(kaddr);
  489. brelse(bh);
  490. *cnop = curr;
  491. ret = n;
  492. out:
  493. up_read(&NILFS_MDT(cpfile)->mi_sem);
  494. return ret;
  495. }
  496. /**
  497. * nilfs_cpfile_get_cpinfo -
  498. * @cpfile:
  499. * @cno:
  500. * @ci:
  501. * @nci:
  502. */
  503. ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
  504. void *buf, unsigned int cisz, size_t nci)
  505. {
  506. switch (mode) {
  507. case NILFS_CHECKPOINT:
  508. return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
  509. case NILFS_SNAPSHOT:
  510. return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
  511. default:
  512. return -EINVAL;
  513. }
  514. }
  515. /**
  516. * nilfs_cpfile_delete_checkpoint -
  517. * @cpfile:
  518. * @cno:
  519. */
  520. int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
  521. {
  522. struct nilfs_cpinfo ci;
  523. __u64 tcno = cno;
  524. ssize_t nci;
  525. nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
  526. if (nci < 0)
  527. return nci;
  528. else if (nci == 0 || ci.ci_cno != cno)
  529. return -ENOENT;
  530. else if (nilfs_cpinfo_snapshot(&ci))
  531. return -EBUSY;
  532. return nilfs_cpfile_delete_checkpoints(cpfile, cno, cno + 1);
  533. }
  534. static struct nilfs_snapshot_list *
  535. nilfs_cpfile_block_get_snapshot_list(const struct inode *cpfile,
  536. __u64 cno,
  537. struct buffer_head *bh,
  538. void *kaddr)
  539. {
  540. struct nilfs_cpfile_header *header;
  541. struct nilfs_checkpoint *cp;
  542. struct nilfs_snapshot_list *list;
  543. if (cno != 0) {
  544. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
  545. list = &cp->cp_snapshot_list;
  546. } else {
  547. header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
  548. list = &header->ch_snapshot_list;
  549. }
  550. return list;
  551. }
  552. static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
  553. {
  554. struct buffer_head *header_bh, *curr_bh, *prev_bh, *cp_bh;
  555. struct nilfs_cpfile_header *header;
  556. struct nilfs_checkpoint *cp;
  557. struct nilfs_snapshot_list *list;
  558. __u64 curr, prev;
  559. unsigned long curr_blkoff, prev_blkoff;
  560. void *kaddr;
  561. int ret;
  562. if (cno == 0)
  563. return -ENOENT; /* checkpoint number 0 is invalid */
  564. down_write(&NILFS_MDT(cpfile)->mi_sem);
  565. ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
  566. if (ret < 0)
  567. goto out_sem;
  568. kaddr = kmap_atomic(cp_bh->b_page);
  569. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
  570. if (nilfs_checkpoint_invalid(cp)) {
  571. ret = -ENOENT;
  572. kunmap_atomic(kaddr);
  573. goto out_cp;
  574. }
  575. if (nilfs_checkpoint_snapshot(cp)) {
  576. ret = 0;
  577. kunmap_atomic(kaddr);
  578. goto out_cp;
  579. }
  580. kunmap_atomic(kaddr);
  581. ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
  582. if (ret < 0)
  583. goto out_cp;
  584. kaddr = kmap_atomic(header_bh->b_page);
  585. header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
  586. list = &header->ch_snapshot_list;
  587. curr_bh = header_bh;
  588. get_bh(curr_bh);
  589. curr = 0;
  590. curr_blkoff = 0;
  591. prev = le64_to_cpu(list->ssl_prev);
  592. while (prev > cno) {
  593. prev_blkoff = nilfs_cpfile_get_blkoff(cpfile, prev);
  594. curr = prev;
  595. if (curr_blkoff != prev_blkoff) {
  596. kunmap_atomic(kaddr);
  597. brelse(curr_bh);
  598. ret = nilfs_cpfile_get_checkpoint_block(cpfile, curr,
  599. 0, &curr_bh);
  600. if (ret < 0)
  601. goto out_header;
  602. kaddr = kmap_atomic(curr_bh->b_page);
  603. }
  604. curr_blkoff = prev_blkoff;
  605. cp = nilfs_cpfile_block_get_checkpoint(
  606. cpfile, curr, curr_bh, kaddr);
  607. list = &cp->cp_snapshot_list;
  608. prev = le64_to_cpu(list->ssl_prev);
  609. }
  610. kunmap_atomic(kaddr);
  611. if (prev != 0) {
  612. ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
  613. &prev_bh);
  614. if (ret < 0)
  615. goto out_curr;
  616. } else {
  617. prev_bh = header_bh;
  618. get_bh(prev_bh);
  619. }
  620. kaddr = kmap_atomic(curr_bh->b_page);
  621. list = nilfs_cpfile_block_get_snapshot_list(
  622. cpfile, curr, curr_bh, kaddr);
  623. list->ssl_prev = cpu_to_le64(cno);
  624. kunmap_atomic(kaddr);
  625. kaddr = kmap_atomic(cp_bh->b_page);
  626. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
  627. cp->cp_snapshot_list.ssl_next = cpu_to_le64(curr);
  628. cp->cp_snapshot_list.ssl_prev = cpu_to_le64(prev);
  629. nilfs_checkpoint_set_snapshot(cp);
  630. kunmap_atomic(kaddr);
  631. kaddr = kmap_atomic(prev_bh->b_page);
  632. list = nilfs_cpfile_block_get_snapshot_list(
  633. cpfile, prev, prev_bh, kaddr);
  634. list->ssl_next = cpu_to_le64(cno);
  635. kunmap_atomic(kaddr);
  636. kaddr = kmap_atomic(header_bh->b_page);
  637. header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
  638. le64_add_cpu(&header->ch_nsnapshots, 1);
  639. kunmap_atomic(kaddr);
  640. mark_buffer_dirty(prev_bh);
  641. mark_buffer_dirty(curr_bh);
  642. mark_buffer_dirty(cp_bh);
  643. mark_buffer_dirty(header_bh);
  644. nilfs_mdt_mark_dirty(cpfile);
  645. brelse(prev_bh);
  646. out_curr:
  647. brelse(curr_bh);
  648. out_header:
  649. brelse(header_bh);
  650. out_cp:
  651. brelse(cp_bh);
  652. out_sem:
  653. up_write(&NILFS_MDT(cpfile)->mi_sem);
  654. return ret;
  655. }
  656. static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
  657. {
  658. struct buffer_head *header_bh, *next_bh, *prev_bh, *cp_bh;
  659. struct nilfs_cpfile_header *header;
  660. struct nilfs_checkpoint *cp;
  661. struct nilfs_snapshot_list *list;
  662. __u64 next, prev;
  663. void *kaddr;
  664. int ret;
  665. if (cno == 0)
  666. return -ENOENT; /* checkpoint number 0 is invalid */
  667. down_write(&NILFS_MDT(cpfile)->mi_sem);
  668. ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
  669. if (ret < 0)
  670. goto out_sem;
  671. kaddr = kmap_atomic(cp_bh->b_page);
  672. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
  673. if (nilfs_checkpoint_invalid(cp)) {
  674. ret = -ENOENT;
  675. kunmap_atomic(kaddr);
  676. goto out_cp;
  677. }
  678. if (!nilfs_checkpoint_snapshot(cp)) {
  679. ret = 0;
  680. kunmap_atomic(kaddr);
  681. goto out_cp;
  682. }
  683. list = &cp->cp_snapshot_list;
  684. next = le64_to_cpu(list->ssl_next);
  685. prev = le64_to_cpu(list->ssl_prev);
  686. kunmap_atomic(kaddr);
  687. ret = nilfs_cpfile_get_header_block(cpfile, &header_bh);
  688. if (ret < 0)
  689. goto out_cp;
  690. if (next != 0) {
  691. ret = nilfs_cpfile_get_checkpoint_block(cpfile, next, 0,
  692. &next_bh);
  693. if (ret < 0)
  694. goto out_header;
  695. } else {
  696. next_bh = header_bh;
  697. get_bh(next_bh);
  698. }
  699. if (prev != 0) {
  700. ret = nilfs_cpfile_get_checkpoint_block(cpfile, prev, 0,
  701. &prev_bh);
  702. if (ret < 0)
  703. goto out_next;
  704. } else {
  705. prev_bh = header_bh;
  706. get_bh(prev_bh);
  707. }
  708. kaddr = kmap_atomic(next_bh->b_page);
  709. list = nilfs_cpfile_block_get_snapshot_list(
  710. cpfile, next, next_bh, kaddr);
  711. list->ssl_prev = cpu_to_le64(prev);
  712. kunmap_atomic(kaddr);
  713. kaddr = kmap_atomic(prev_bh->b_page);
  714. list = nilfs_cpfile_block_get_snapshot_list(
  715. cpfile, prev, prev_bh, kaddr);
  716. list->ssl_next = cpu_to_le64(next);
  717. kunmap_atomic(kaddr);
  718. kaddr = kmap_atomic(cp_bh->b_page);
  719. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, cp_bh, kaddr);
  720. cp->cp_snapshot_list.ssl_next = cpu_to_le64(0);
  721. cp->cp_snapshot_list.ssl_prev = cpu_to_le64(0);
  722. nilfs_checkpoint_clear_snapshot(cp);
  723. kunmap_atomic(kaddr);
  724. kaddr = kmap_atomic(header_bh->b_page);
  725. header = nilfs_cpfile_block_get_header(cpfile, header_bh, kaddr);
  726. le64_add_cpu(&header->ch_nsnapshots, -1);
  727. kunmap_atomic(kaddr);
  728. mark_buffer_dirty(next_bh);
  729. mark_buffer_dirty(prev_bh);
  730. mark_buffer_dirty(cp_bh);
  731. mark_buffer_dirty(header_bh);
  732. nilfs_mdt_mark_dirty(cpfile);
  733. brelse(prev_bh);
  734. out_next:
  735. brelse(next_bh);
  736. out_header:
  737. brelse(header_bh);
  738. out_cp:
  739. brelse(cp_bh);
  740. out_sem:
  741. up_write(&NILFS_MDT(cpfile)->mi_sem);
  742. return ret;
  743. }
  744. /**
  745. * nilfs_cpfile_is_snapshot -
  746. * @cpfile: inode of checkpoint file
  747. * @cno: checkpoint number
  748. *
  749. * Description:
  750. *
  751. * Return Value: On success, 1 is returned if the checkpoint specified by
  752. * @cno is a snapshot, or 0 if not. On error, one of the following negative
  753. * error codes is returned.
  754. *
  755. * %-EIO - I/O error.
  756. *
  757. * %-ENOMEM - Insufficient amount of memory available.
  758. *
  759. * %-ENOENT - No such checkpoint.
  760. */
  761. int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
  762. {
  763. struct buffer_head *bh;
  764. struct nilfs_checkpoint *cp;
  765. void *kaddr;
  766. int ret;
  767. /*
  768. * CP number is invalid if it's zero or larger than the
  769. * largest existing one.
  770. */
  771. if (cno == 0 || cno >= nilfs_mdt_cno(cpfile))
  772. return -ENOENT;
  773. down_read(&NILFS_MDT(cpfile)->mi_sem);
  774. ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
  775. if (ret < 0)
  776. goto out;
  777. kaddr = kmap_atomic(bh->b_page);
  778. cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
  779. if (nilfs_checkpoint_invalid(cp))
  780. ret = -ENOENT;
  781. else
  782. ret = nilfs_checkpoint_snapshot(cp);
  783. kunmap_atomic(kaddr);
  784. brelse(bh);
  785. out:
  786. up_read(&NILFS_MDT(cpfile)->mi_sem);
  787. return ret;
  788. }
  789. /**
  790. * nilfs_cpfile_change_cpmode - change checkpoint mode
  791. * @cpfile: inode of checkpoint file
  792. * @cno: checkpoint number
  793. * @mode: mode of checkpoint
  794. *
  795. * Description: nilfs_change_cpmode() changes the mode of the checkpoint
  796. * specified by @cno. The mode @mode is NILFS_CHECKPOINT or NILFS_SNAPSHOT.
  797. *
  798. * Return Value: On success, 0 is returned. On error, one of the following
  799. * negative error codes is returned.
  800. *
  801. * %-EIO - I/O error.
  802. *
  803. * %-ENOMEM - Insufficient amount of memory available.
  804. *
  805. * %-ENOENT - No such checkpoint.
  806. */
  807. int nilfs_cpfile_change_cpmode(struct inode *cpfile, __u64 cno, int mode)
  808. {
  809. int ret;
  810. switch (mode) {
  811. case NILFS_CHECKPOINT:
  812. if (nilfs_checkpoint_is_mounted(cpfile->i_sb, cno))
  813. /*
  814. * Current implementation does not have to protect
  815. * plain read-only mounts since they are exclusive
  816. * with a read/write mount and are protected from the
  817. * cleaner.
  818. */
  819. ret = -EBUSY;
  820. else
  821. ret = nilfs_cpfile_clear_snapshot(cpfile, cno);
  822. return ret;
  823. case NILFS_SNAPSHOT:
  824. return nilfs_cpfile_set_snapshot(cpfile, cno);
  825. default:
  826. return -EINVAL;
  827. }
  828. }
  829. /**
  830. * nilfs_cpfile_get_stat - get checkpoint statistics
  831. * @cpfile: inode of checkpoint file
  832. * @cpstat: pointer to a structure of checkpoint statistics
  833. *
  834. * Description: nilfs_cpfile_get_stat() returns information about checkpoints.
  835. *
  836. * Return Value: On success, 0 is returned, and checkpoints information is
  837. * stored in the place pointed by @cpstat. On error, one of the following
  838. * negative error codes is returned.
  839. *
  840. * %-EIO - I/O error.
  841. *
  842. * %-ENOMEM - Insufficient amount of memory available.
  843. */
  844. int nilfs_cpfile_get_stat(struct inode *cpfile, struct nilfs_cpstat *cpstat)
  845. {
  846. struct buffer_head *bh;
  847. struct nilfs_cpfile_header *header;
  848. void *kaddr;
  849. int ret;
  850. down_read(&NILFS_MDT(cpfile)->mi_sem);
  851. ret = nilfs_cpfile_get_header_block(cpfile, &bh);
  852. if (ret < 0)
  853. goto out_sem;
  854. kaddr = kmap_atomic(bh->b_page);
  855. header = nilfs_cpfile_block_get_header(cpfile, bh, kaddr);
  856. cpstat->cs_cno = nilfs_mdt_cno(cpfile);
  857. cpstat->cs_ncps = le64_to_cpu(header->ch_ncheckpoints);
  858. cpstat->cs_nsss = le64_to_cpu(header->ch_nsnapshots);
  859. kunmap_atomic(kaddr);
  860. brelse(bh);
  861. out_sem:
  862. up_read(&NILFS_MDT(cpfile)->mi_sem);
  863. return ret;
  864. }
  865. /**
  866. * nilfs_cpfile_read - read or get cpfile inode
  867. * @sb: super block instance
  868. * @cpsize: size of a checkpoint entry
  869. * @raw_inode: on-disk cpfile inode
  870. * @inodep: buffer to store the inode
  871. */
  872. int nilfs_cpfile_read(struct super_block *sb, size_t cpsize,
  873. struct nilfs_inode *raw_inode, struct inode **inodep)
  874. {
  875. struct inode *cpfile;
  876. int err;
  877. if (cpsize > sb->s_blocksize) {
  878. nilfs_err(sb, "too large checkpoint size: %zu bytes", cpsize);
  879. return -EINVAL;
  880. } else if (cpsize < NILFS_MIN_CHECKPOINT_SIZE) {
  881. nilfs_err(sb, "too small checkpoint size: %zu bytes", cpsize);
  882. return -EINVAL;
  883. }
  884. cpfile = nilfs_iget_locked(sb, NULL, NILFS_CPFILE_INO);
  885. if (unlikely(!cpfile))
  886. return -ENOMEM;
  887. if (!(cpfile->i_state & I_NEW))
  888. goto out;
  889. err = nilfs_mdt_init(cpfile, NILFS_MDT_GFP, 0);
  890. if (err)
  891. goto failed;
  892. nilfs_mdt_set_entry_size(cpfile, cpsize,
  893. sizeof(struct nilfs_cpfile_header));
  894. err = nilfs_read_inode_common(cpfile, raw_inode);
  895. if (err)
  896. goto failed;
  897. unlock_new_inode(cpfile);
  898. out:
  899. *inodep = cpfile;
  900. return 0;
  901. failed:
  902. iget_failed(cpfile);
  903. return err;
  904. }