super.c 24 KB

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