super.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/crc32.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <linux/bio.h>
  17. #include <linux/lm_interface.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "bmap.h"
  21. #include "dir.h"
  22. #include "glock.h"
  23. #include "glops.h"
  24. #include "inode.h"
  25. #include "log.h"
  26. #include "meta_io.h"
  27. #include "quota.h"
  28. #include "recovery.h"
  29. #include "rgrp.h"
  30. #include "super.h"
  31. #include "trans.h"
  32. #include "util.h"
  33. static const u32 gfs2_old_fs_formats[] = {
  34. 0
  35. };
  36. static const u32 gfs2_old_multihost_formats[] = {
  37. 0
  38. };
  39. /**
  40. * gfs2_tune_init - Fill a gfs2_tune structure with default values
  41. * @gt: tune
  42. *
  43. */
  44. void gfs2_tune_init(struct gfs2_tune *gt)
  45. {
  46. spin_lock_init(&gt->gt_spin);
  47. gt->gt_demote_secs = 300;
  48. gt->gt_incore_log_blocks = 1024;
  49. gt->gt_log_flush_secs = 60;
  50. gt->gt_recoverd_secs = 60;
  51. gt->gt_logd_secs = 1;
  52. gt->gt_quotad_secs = 5;
  53. gt->gt_quota_simul_sync = 64;
  54. gt->gt_quota_warn_period = 10;
  55. gt->gt_quota_scale_num = 1;
  56. gt->gt_quota_scale_den = 1;
  57. gt->gt_quota_cache_secs = 300;
  58. gt->gt_quota_quantum = 60;
  59. gt->gt_atime_quantum = 3600;
  60. gt->gt_new_files_jdata = 0;
  61. gt->gt_max_readahead = 1 << 18;
  62. gt->gt_stall_secs = 600;
  63. gt->gt_complain_secs = 10;
  64. gt->gt_statfs_quantum = 30;
  65. gt->gt_statfs_slow = 0;
  66. }
  67. /**
  68. * gfs2_check_sb - Check superblock
  69. * @sdp: the filesystem
  70. * @sb: The superblock
  71. * @silent: Don't print a message if the check fails
  72. *
  73. * Checks the version code of the FS is one that we understand how to
  74. * read and that the sizes of the various on-disk structures have not
  75. * changed.
  76. */
  77. int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb_host *sb, int silent)
  78. {
  79. unsigned int x;
  80. if (sb->sb_magic != GFS2_MAGIC ||
  81. sb->sb_type != GFS2_METATYPE_SB) {
  82. if (!silent)
  83. printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
  84. return -EINVAL;
  85. }
  86. /* If format numbers match exactly, we're done. */
  87. if (sb->sb_fs_format == GFS2_FORMAT_FS &&
  88. sb->sb_multihost_format == GFS2_FORMAT_MULTI)
  89. return 0;
  90. if (sb->sb_fs_format != GFS2_FORMAT_FS) {
  91. for (x = 0; gfs2_old_fs_formats[x]; x++)
  92. if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
  93. break;
  94. if (!gfs2_old_fs_formats[x]) {
  95. printk(KERN_WARNING
  96. "GFS2: code version (%u, %u) is incompatible "
  97. "with ondisk format (%u, %u)\n",
  98. GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
  99. sb->sb_fs_format, sb->sb_multihost_format);
  100. printk(KERN_WARNING
  101. "GFS2: I don't know how to upgrade this FS\n");
  102. return -EINVAL;
  103. }
  104. }
  105. if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
  106. for (x = 0; gfs2_old_multihost_formats[x]; x++)
  107. if (gfs2_old_multihost_formats[x] ==
  108. sb->sb_multihost_format)
  109. break;
  110. if (!gfs2_old_multihost_formats[x]) {
  111. printk(KERN_WARNING
  112. "GFS2: code version (%u, %u) is incompatible "
  113. "with ondisk format (%u, %u)\n",
  114. GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
  115. sb->sb_fs_format, sb->sb_multihost_format);
  116. printk(KERN_WARNING
  117. "GFS2: I don't know how to upgrade this FS\n");
  118. return -EINVAL;
  119. }
  120. }
  121. if (!sdp->sd_args.ar_upgrade) {
  122. printk(KERN_WARNING
  123. "GFS2: code version (%u, %u) is incompatible "
  124. "with ondisk format (%u, %u)\n",
  125. GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
  126. sb->sb_fs_format, sb->sb_multihost_format);
  127. printk(KERN_INFO
  128. "GFS2: Use the \"upgrade\" mount option to upgrade "
  129. "the FS\n");
  130. printk(KERN_INFO "GFS2: See the manual for more details\n");
  131. return -EINVAL;
  132. }
  133. return 0;
  134. }
  135. static void end_bio_io_page(struct bio *bio, int error)
  136. {
  137. struct page *page = bio->bi_private;
  138. if (!error)
  139. SetPageUptodate(page);
  140. else
  141. printk(KERN_WARNING "gfs2: error %d reading superblock\n", error);
  142. unlock_page(page);
  143. }
  144. static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf)
  145. {
  146. const struct gfs2_sb *str = buf;
  147. sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
  148. sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
  149. sb->sb_format = be32_to_cpu(str->sb_header.mh_format);
  150. sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
  151. sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
  152. sb->sb_bsize = be32_to_cpu(str->sb_bsize);
  153. sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
  154. sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
  155. sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
  156. sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
  157. sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
  158. memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
  159. memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
  160. }
  161. /**
  162. * gfs2_read_super - Read the gfs2 super block from disk
  163. * @sdp: The GFS2 super block
  164. * @sector: The location of the super block
  165. * @error: The error code to return
  166. *
  167. * This uses the bio functions to read the super block from disk
  168. * because we want to be 100% sure that we never read cached data.
  169. * A super block is read twice only during each GFS2 mount and is
  170. * never written to by the filesystem. The first time its read no
  171. * locks are held, and the only details which are looked at are those
  172. * relating to the locking protocol. Once locking is up and working,
  173. * the sb is read again under the lock to establish the location of
  174. * the master directory (contains pointers to journals etc) and the
  175. * root directory.
  176. *
  177. * Returns: 0 on success or error
  178. */
  179. int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector)
  180. {
  181. struct super_block *sb = sdp->sd_vfs;
  182. struct gfs2_sb *p;
  183. struct page *page;
  184. struct bio *bio;
  185. page = alloc_page(GFP_NOFS);
  186. if (unlikely(!page))
  187. return -ENOBUFS;
  188. ClearPageUptodate(page);
  189. ClearPageDirty(page);
  190. lock_page(page);
  191. bio = bio_alloc(GFP_NOFS, 1);
  192. if (unlikely(!bio)) {
  193. __free_page(page);
  194. return -ENOBUFS;
  195. }
  196. bio->bi_sector = sector * (sb->s_blocksize >> 9);
  197. bio->bi_bdev = sb->s_bdev;
  198. bio_add_page(bio, page, PAGE_SIZE, 0);
  199. bio->bi_end_io = end_bio_io_page;
  200. bio->bi_private = page;
  201. submit_bio(READ_SYNC | (1 << BIO_RW_META), bio);
  202. wait_on_page_locked(page);
  203. bio_put(bio);
  204. if (!PageUptodate(page)) {
  205. __free_page(page);
  206. return -EIO;
  207. }
  208. p = kmap(page);
  209. gfs2_sb_in(&sdp->sd_sb, p);
  210. kunmap(page);
  211. __free_page(page);
  212. return 0;
  213. }
  214. /**
  215. * gfs2_read_sb - Read super block
  216. * @sdp: The GFS2 superblock
  217. * @gl: the glock for the superblock (assumed to be held)
  218. * @silent: Don't print message if mount fails
  219. *
  220. */
  221. int gfs2_read_sb(struct gfs2_sbd *sdp, struct gfs2_glock *gl, int silent)
  222. {
  223. u32 hash_blocks, ind_blocks, leaf_blocks;
  224. u32 tmp_blocks;
  225. unsigned int x;
  226. int error;
  227. error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
  228. if (error) {
  229. if (!silent)
  230. fs_err(sdp, "can't read superblock\n");
  231. return error;
  232. }
  233. error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
  234. if (error)
  235. return error;
  236. sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
  237. GFS2_BASIC_BLOCK_SHIFT;
  238. sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
  239. sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
  240. sizeof(struct gfs2_dinode)) / sizeof(u64);
  241. sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
  242. sizeof(struct gfs2_meta_header)) / sizeof(u64);
  243. sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
  244. sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
  245. sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
  246. sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
  247. sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
  248. sizeof(struct gfs2_meta_header)) /
  249. sizeof(struct gfs2_quota_change);
  250. /* Compute maximum reservation required to add a entry to a directory */
  251. hash_blocks = DIV_ROUND_UP(sizeof(u64) * (1 << GFS2_DIR_MAX_DEPTH),
  252. sdp->sd_jbsize);
  253. ind_blocks = 0;
  254. for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
  255. tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
  256. ind_blocks += tmp_blocks;
  257. }
  258. leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
  259. sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
  260. sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
  261. sizeof(struct gfs2_dinode);
  262. sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
  263. for (x = 2;; x++) {
  264. u64 space, d;
  265. u32 m;
  266. space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
  267. d = space;
  268. m = do_div(d, sdp->sd_inptrs);
  269. if (d != sdp->sd_heightsize[x - 1] || m)
  270. break;
  271. sdp->sd_heightsize[x] = space;
  272. }
  273. sdp->sd_max_height = x;
  274. sdp->sd_heightsize[x] = ~0;
  275. gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
  276. sdp->sd_jheightsize[0] = sdp->sd_sb.sb_bsize -
  277. sizeof(struct gfs2_dinode);
  278. sdp->sd_jheightsize[1] = sdp->sd_jbsize * sdp->sd_diptrs;
  279. for (x = 2;; x++) {
  280. u64 space, d;
  281. u32 m;
  282. space = sdp->sd_jheightsize[x - 1] * sdp->sd_inptrs;
  283. d = space;
  284. m = do_div(d, sdp->sd_inptrs);
  285. if (d != sdp->sd_jheightsize[x - 1] || m)
  286. break;
  287. sdp->sd_jheightsize[x] = space;
  288. }
  289. sdp->sd_max_jheight = x;
  290. sdp->sd_jheightsize[x] = ~0;
  291. gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
  292. return 0;
  293. }
  294. /**
  295. * gfs2_jindex_hold - Grab a lock on the jindex
  296. * @sdp: The GFS2 superblock
  297. * @ji_gh: the holder for the jindex glock
  298. *
  299. * This is very similar to the gfs2_rindex_hold() function, except that
  300. * in general we hold the jindex lock for longer periods of time and
  301. * we grab it far less frequently (in general) then the rgrp lock.
  302. *
  303. * Returns: errno
  304. */
  305. int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
  306. {
  307. struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
  308. struct qstr name;
  309. char buf[20];
  310. struct gfs2_jdesc *jd;
  311. int error;
  312. name.name = buf;
  313. mutex_lock(&sdp->sd_jindex_mutex);
  314. for (;;) {
  315. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
  316. if (error)
  317. break;
  318. name.len = sprintf(buf, "journal%u", sdp->sd_journals);
  319. name.hash = gfs2_disk_hash(name.name, name.len);
  320. error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
  321. if (error == -ENOENT) {
  322. error = 0;
  323. break;
  324. }
  325. gfs2_glock_dq_uninit(ji_gh);
  326. if (error)
  327. break;
  328. error = -ENOMEM;
  329. jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
  330. if (!jd)
  331. break;
  332. INIT_LIST_HEAD(&jd->extent_list);
  333. jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
  334. if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
  335. if (!jd->jd_inode)
  336. error = -ENOENT;
  337. else
  338. error = PTR_ERR(jd->jd_inode);
  339. kfree(jd);
  340. break;
  341. }
  342. spin_lock(&sdp->sd_jindex_spin);
  343. jd->jd_jid = sdp->sd_journals++;
  344. list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
  345. spin_unlock(&sdp->sd_jindex_spin);
  346. }
  347. mutex_unlock(&sdp->sd_jindex_mutex);
  348. return error;
  349. }
  350. /**
  351. * gfs2_jindex_free - Clear all the journal index information
  352. * @sdp: The GFS2 superblock
  353. *
  354. */
  355. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  356. {
  357. struct list_head list, *head;
  358. struct gfs2_jdesc *jd;
  359. struct gfs2_journal_extent *jext;
  360. spin_lock(&sdp->sd_jindex_spin);
  361. list_add(&list, &sdp->sd_jindex_list);
  362. list_del_init(&sdp->sd_jindex_list);
  363. sdp->sd_journals = 0;
  364. spin_unlock(&sdp->sd_jindex_spin);
  365. while (!list_empty(&list)) {
  366. jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
  367. head = &jd->extent_list;
  368. while (!list_empty(head)) {
  369. jext = list_entry(head->next,
  370. struct gfs2_journal_extent,
  371. extent_list);
  372. list_del(&jext->extent_list);
  373. kfree(jext);
  374. }
  375. list_del(&jd->jd_list);
  376. iput(jd->jd_inode);
  377. kfree(jd);
  378. }
  379. }
  380. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  381. {
  382. struct gfs2_jdesc *jd;
  383. int found = 0;
  384. list_for_each_entry(jd, head, jd_list) {
  385. if (jd->jd_jid == jid) {
  386. found = 1;
  387. break;
  388. }
  389. }
  390. if (!found)
  391. jd = NULL;
  392. return jd;
  393. }
  394. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  395. {
  396. struct gfs2_jdesc *jd;
  397. spin_lock(&sdp->sd_jindex_spin);
  398. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  399. spin_unlock(&sdp->sd_jindex_spin);
  400. return jd;
  401. }
  402. void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
  403. {
  404. struct gfs2_jdesc *jd;
  405. spin_lock(&sdp->sd_jindex_spin);
  406. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  407. if (jd)
  408. jd->jd_dirty = 1;
  409. spin_unlock(&sdp->sd_jindex_spin);
  410. }
  411. struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
  412. {
  413. struct gfs2_jdesc *jd;
  414. int found = 0;
  415. spin_lock(&sdp->sd_jindex_spin);
  416. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  417. if (jd->jd_dirty) {
  418. jd->jd_dirty = 0;
  419. found = 1;
  420. break;
  421. }
  422. }
  423. spin_unlock(&sdp->sd_jindex_spin);
  424. if (!found)
  425. jd = NULL;
  426. return jd;
  427. }
  428. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  429. {
  430. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  431. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  432. int ar;
  433. int error;
  434. if (ip->i_di.di_size < (8 << 20) || ip->i_di.di_size > (1 << 30) ||
  435. (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
  436. gfs2_consist_inode(ip);
  437. return -EIO;
  438. }
  439. jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
  440. error = gfs2_write_alloc_required(ip, 0, ip->i_di.di_size, &ar);
  441. if (!error && ar) {
  442. gfs2_consist_inode(ip);
  443. error = -EIO;
  444. }
  445. return error;
  446. }
  447. /**
  448. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  449. * @sdp: the filesystem
  450. *
  451. * Returns: errno
  452. */
  453. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  454. {
  455. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  456. struct gfs2_glock *j_gl = ip->i_gl;
  457. struct gfs2_holder t_gh;
  458. struct gfs2_log_header_host head;
  459. int error;
  460. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
  461. if (error)
  462. return error;
  463. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  464. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  465. if (error)
  466. goto fail;
  467. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  468. gfs2_consist(sdp);
  469. error = -EIO;
  470. goto fail;
  471. }
  472. /* Initialize some head of the log stuff */
  473. sdp->sd_log_sequence = head.lh_sequence + 1;
  474. gfs2_log_pointers_init(sdp, head.lh_blkno);
  475. error = gfs2_quota_init(sdp);
  476. if (error)
  477. goto fail;
  478. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  479. gfs2_glock_dq_uninit(&t_gh);
  480. return 0;
  481. fail:
  482. t_gh.gh_flags |= GL_NOCACHE;
  483. gfs2_glock_dq_uninit(&t_gh);
  484. return error;
  485. }
  486. /**
  487. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  488. * @sdp: the filesystem
  489. *
  490. * Returns: errno
  491. */
  492. int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  493. {
  494. struct gfs2_holder t_gh;
  495. int error;
  496. gfs2_quota_sync(sdp);
  497. gfs2_statfs_sync(sdp);
  498. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
  499. &t_gh);
  500. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  501. return error;
  502. gfs2_meta_syncfs(sdp);
  503. gfs2_log_shutdown(sdp);
  504. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  505. if (t_gh.gh_gl)
  506. gfs2_glock_dq_uninit(&t_gh);
  507. gfs2_quota_cleanup(sdp);
  508. return error;
  509. }
  510. static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  511. {
  512. const struct gfs2_statfs_change *str = buf;
  513. sc->sc_total = be64_to_cpu(str->sc_total);
  514. sc->sc_free = be64_to_cpu(str->sc_free);
  515. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  516. }
  517. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  518. {
  519. struct gfs2_statfs_change *str = buf;
  520. str->sc_total = cpu_to_be64(sc->sc_total);
  521. str->sc_free = cpu_to_be64(sc->sc_free);
  522. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  523. }
  524. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  525. {
  526. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  527. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  528. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  529. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  530. struct buffer_head *m_bh, *l_bh;
  531. struct gfs2_holder gh;
  532. int error;
  533. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  534. &gh);
  535. if (error)
  536. return error;
  537. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  538. if (error)
  539. goto out;
  540. if (sdp->sd_args.ar_spectator) {
  541. spin_lock(&sdp->sd_statfs_spin);
  542. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  543. sizeof(struct gfs2_dinode));
  544. spin_unlock(&sdp->sd_statfs_spin);
  545. } else {
  546. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  547. if (error)
  548. goto out_m_bh;
  549. spin_lock(&sdp->sd_statfs_spin);
  550. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  551. sizeof(struct gfs2_dinode));
  552. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  553. sizeof(struct gfs2_dinode));
  554. spin_unlock(&sdp->sd_statfs_spin);
  555. brelse(l_bh);
  556. }
  557. out_m_bh:
  558. brelse(m_bh);
  559. out:
  560. gfs2_glock_dq_uninit(&gh);
  561. return 0;
  562. }
  563. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  564. s64 dinodes)
  565. {
  566. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  567. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  568. struct buffer_head *l_bh;
  569. int error;
  570. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  571. if (error)
  572. return;
  573. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  574. spin_lock(&sdp->sd_statfs_spin);
  575. l_sc->sc_total += total;
  576. l_sc->sc_free += free;
  577. l_sc->sc_dinodes += dinodes;
  578. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  579. spin_unlock(&sdp->sd_statfs_spin);
  580. brelse(l_bh);
  581. }
  582. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  583. {
  584. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  585. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  586. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  587. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  588. struct gfs2_holder gh;
  589. struct buffer_head *m_bh, *l_bh;
  590. int error;
  591. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  592. &gh);
  593. if (error)
  594. return error;
  595. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  596. if (error)
  597. goto out;
  598. spin_lock(&sdp->sd_statfs_spin);
  599. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  600. sizeof(struct gfs2_dinode));
  601. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  602. spin_unlock(&sdp->sd_statfs_spin);
  603. goto out_bh;
  604. }
  605. spin_unlock(&sdp->sd_statfs_spin);
  606. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  607. if (error)
  608. goto out_bh;
  609. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  610. if (error)
  611. goto out_bh2;
  612. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  613. spin_lock(&sdp->sd_statfs_spin);
  614. m_sc->sc_total += l_sc->sc_total;
  615. m_sc->sc_free += l_sc->sc_free;
  616. m_sc->sc_dinodes += l_sc->sc_dinodes;
  617. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  618. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  619. 0, sizeof(struct gfs2_statfs_change));
  620. spin_unlock(&sdp->sd_statfs_spin);
  621. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  622. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  623. gfs2_trans_end(sdp);
  624. out_bh2:
  625. brelse(l_bh);
  626. out_bh:
  627. brelse(m_bh);
  628. out:
  629. gfs2_glock_dq_uninit(&gh);
  630. return error;
  631. }
  632. /**
  633. * gfs2_statfs_i - Do a statfs
  634. * @sdp: the filesystem
  635. * @sg: the sg structure
  636. *
  637. * Returns: errno
  638. */
  639. int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  640. {
  641. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  642. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  643. spin_lock(&sdp->sd_statfs_spin);
  644. *sc = *m_sc;
  645. sc->sc_total += l_sc->sc_total;
  646. sc->sc_free += l_sc->sc_free;
  647. sc->sc_dinodes += l_sc->sc_dinodes;
  648. spin_unlock(&sdp->sd_statfs_spin);
  649. if (sc->sc_free < 0)
  650. sc->sc_free = 0;
  651. if (sc->sc_free > sc->sc_total)
  652. sc->sc_free = sc->sc_total;
  653. if (sc->sc_dinodes < 0)
  654. sc->sc_dinodes = 0;
  655. return 0;
  656. }
  657. /**
  658. * statfs_fill - fill in the sg for a given RG
  659. * @rgd: the RG
  660. * @sc: the sc structure
  661. *
  662. * Returns: 0 on success, -ESTALE if the LVB is invalid
  663. */
  664. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  665. struct gfs2_statfs_change_host *sc)
  666. {
  667. gfs2_rgrp_verify(rgd);
  668. sc->sc_total += rgd->rd_data;
  669. sc->sc_free += rgd->rd_rg.rg_free;
  670. sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
  671. return 0;
  672. }
  673. /**
  674. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  675. * @sdp: the filesystem
  676. * @sc: the sc info that will be returned
  677. *
  678. * Any error (other than a signal) will cause this routine to fall back
  679. * to the synchronous version.
  680. *
  681. * FIXME: This really shouldn't busy wait like this.
  682. *
  683. * Returns: errno
  684. */
  685. int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  686. {
  687. struct gfs2_holder ri_gh;
  688. struct gfs2_rgrpd *rgd_next;
  689. struct gfs2_holder *gha, *gh;
  690. unsigned int slots = 64;
  691. unsigned int x;
  692. int done;
  693. int error = 0, err;
  694. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  695. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  696. if (!gha)
  697. return -ENOMEM;
  698. error = gfs2_rindex_hold(sdp, &ri_gh);
  699. if (error)
  700. goto out;
  701. rgd_next = gfs2_rgrpd_get_first(sdp);
  702. for (;;) {
  703. done = 1;
  704. for (x = 0; x < slots; x++) {
  705. gh = gha + x;
  706. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  707. err = gfs2_glock_wait(gh);
  708. if (err) {
  709. gfs2_holder_uninit(gh);
  710. error = err;
  711. } else {
  712. if (!error)
  713. error = statfs_slow_fill(
  714. gh->gh_gl->gl_object, sc);
  715. gfs2_glock_dq_uninit(gh);
  716. }
  717. }
  718. if (gh->gh_gl)
  719. done = 0;
  720. else if (rgd_next && !error) {
  721. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  722. LM_ST_SHARED,
  723. GL_ASYNC,
  724. gh);
  725. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  726. done = 0;
  727. }
  728. if (signal_pending(current))
  729. error = -ERESTARTSYS;
  730. }
  731. if (done)
  732. break;
  733. yield();
  734. }
  735. gfs2_glock_dq_uninit(&ri_gh);
  736. out:
  737. kfree(gha);
  738. return error;
  739. }
  740. struct lfcc {
  741. struct list_head list;
  742. struct gfs2_holder gh;
  743. };
  744. /**
  745. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  746. * journals are clean
  747. * @sdp: the file system
  748. * @state: the state to put the transaction lock into
  749. * @t_gh: the hold on the transaction lock
  750. *
  751. * Returns: errno
  752. */
  753. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  754. struct gfs2_holder *t_gh)
  755. {
  756. struct gfs2_inode *ip;
  757. struct gfs2_holder ji_gh;
  758. struct gfs2_jdesc *jd;
  759. struct lfcc *lfcc;
  760. LIST_HEAD(list);
  761. struct gfs2_log_header_host lh;
  762. int error;
  763. error = gfs2_jindex_hold(sdp, &ji_gh);
  764. if (error)
  765. return error;
  766. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  767. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  768. if (!lfcc) {
  769. error = -ENOMEM;
  770. goto out;
  771. }
  772. ip = GFS2_I(jd->jd_inode);
  773. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  774. if (error) {
  775. kfree(lfcc);
  776. goto out;
  777. }
  778. list_add(&lfcc->list, &list);
  779. }
  780. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  781. GL_NOCACHE, t_gh);
  782. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  783. error = gfs2_jdesc_check(jd);
  784. if (error)
  785. break;
  786. error = gfs2_find_jhead(jd, &lh);
  787. if (error)
  788. break;
  789. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  790. error = -EBUSY;
  791. break;
  792. }
  793. }
  794. if (error)
  795. gfs2_glock_dq_uninit(t_gh);
  796. out:
  797. while (!list_empty(&list)) {
  798. lfcc = list_entry(list.next, struct lfcc, list);
  799. list_del(&lfcc->list);
  800. gfs2_glock_dq_uninit(&lfcc->gh);
  801. kfree(lfcc);
  802. }
  803. gfs2_glock_dq_uninit(&ji_gh);
  804. return error;
  805. }
  806. /**
  807. * gfs2_freeze_fs - freezes the file system
  808. * @sdp: the file system
  809. *
  810. * This function flushes data and meta data for all machines by
  811. * aquiring the transaction log exclusively. All journals are
  812. * ensured to be in a clean state as well.
  813. *
  814. * Returns: errno
  815. */
  816. int gfs2_freeze_fs(struct gfs2_sbd *sdp)
  817. {
  818. int error = 0;
  819. mutex_lock(&sdp->sd_freeze_lock);
  820. if (!sdp->sd_freeze_count++) {
  821. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  822. if (error)
  823. sdp->sd_freeze_count--;
  824. }
  825. mutex_unlock(&sdp->sd_freeze_lock);
  826. return error;
  827. }
  828. /**
  829. * gfs2_unfreeze_fs - unfreezes the file system
  830. * @sdp: the file system
  831. *
  832. * This function allows the file system to proceed by unlocking
  833. * the exclusively held transaction lock. Other GFS2 nodes are
  834. * now free to acquire the lock shared and go on with their lives.
  835. *
  836. */
  837. void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
  838. {
  839. mutex_lock(&sdp->sd_freeze_lock);
  840. if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
  841. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  842. mutex_unlock(&sdp->sd_freeze_lock);
  843. }