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_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. INIT_LIST_HEAD(&jd->extent_list);
  332. jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
  333. if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
  334. if (!jd->jd_inode)
  335. error = -ENOENT;
  336. else
  337. error = PTR_ERR(jd->jd_inode);
  338. kfree(jd);
  339. break;
  340. }
  341. spin_lock(&sdp->sd_jindex_spin);
  342. jd->jd_jid = sdp->sd_journals++;
  343. list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
  344. spin_unlock(&sdp->sd_jindex_spin);
  345. }
  346. mutex_unlock(&sdp->sd_jindex_mutex);
  347. return error;
  348. }
  349. /**
  350. * gfs2_jindex_free - Clear all the journal index information
  351. * @sdp: The GFS2 superblock
  352. *
  353. */
  354. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  355. {
  356. struct list_head list, *head;
  357. struct gfs2_jdesc *jd;
  358. struct gfs2_journal_extent *jext;
  359. spin_lock(&sdp->sd_jindex_spin);
  360. list_add(&list, &sdp->sd_jindex_list);
  361. list_del_init(&sdp->sd_jindex_list);
  362. sdp->sd_journals = 0;
  363. spin_unlock(&sdp->sd_jindex_spin);
  364. while (!list_empty(&list)) {
  365. jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
  366. head = &jd->extent_list;
  367. while (!list_empty(head)) {
  368. jext = list_entry(head->next,
  369. struct gfs2_journal_extent,
  370. extent_list);
  371. list_del(&jext->extent_list);
  372. kfree(jext);
  373. }
  374. list_del(&jd->jd_list);
  375. iput(jd->jd_inode);
  376. kfree(jd);
  377. }
  378. }
  379. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  380. {
  381. struct gfs2_jdesc *jd;
  382. int found = 0;
  383. list_for_each_entry(jd, head, jd_list) {
  384. if (jd->jd_jid == jid) {
  385. found = 1;
  386. break;
  387. }
  388. }
  389. if (!found)
  390. jd = NULL;
  391. return jd;
  392. }
  393. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  394. {
  395. struct gfs2_jdesc *jd;
  396. spin_lock(&sdp->sd_jindex_spin);
  397. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  398. spin_unlock(&sdp->sd_jindex_spin);
  399. return jd;
  400. }
  401. void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
  402. {
  403. struct gfs2_jdesc *jd;
  404. spin_lock(&sdp->sd_jindex_spin);
  405. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  406. if (jd)
  407. jd->jd_dirty = 1;
  408. spin_unlock(&sdp->sd_jindex_spin);
  409. }
  410. struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
  411. {
  412. struct gfs2_jdesc *jd;
  413. int found = 0;
  414. spin_lock(&sdp->sd_jindex_spin);
  415. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  416. if (jd->jd_dirty) {
  417. jd->jd_dirty = 0;
  418. found = 1;
  419. break;
  420. }
  421. }
  422. spin_unlock(&sdp->sd_jindex_spin);
  423. if (!found)
  424. jd = NULL;
  425. return jd;
  426. }
  427. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  428. {
  429. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  430. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  431. int ar;
  432. int error;
  433. if (ip->i_di.di_size < (8 << 20) || ip->i_di.di_size > (1 << 30) ||
  434. (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
  435. gfs2_consist_inode(ip);
  436. return -EIO;
  437. }
  438. jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
  439. error = gfs2_write_alloc_required(ip, 0, ip->i_di.di_size, &ar);
  440. if (!error && ar) {
  441. gfs2_consist_inode(ip);
  442. error = -EIO;
  443. }
  444. return error;
  445. }
  446. /**
  447. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  448. * @sdp: the filesystem
  449. *
  450. * Returns: errno
  451. */
  452. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  453. {
  454. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  455. struct gfs2_glock *j_gl = ip->i_gl;
  456. struct gfs2_holder t_gh;
  457. struct gfs2_log_header_host head;
  458. int error;
  459. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
  460. if (error)
  461. return error;
  462. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  463. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  464. if (error)
  465. goto fail;
  466. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  467. gfs2_consist(sdp);
  468. error = -EIO;
  469. goto fail;
  470. }
  471. /* Initialize some head of the log stuff */
  472. sdp->sd_log_sequence = head.lh_sequence + 1;
  473. gfs2_log_pointers_init(sdp, head.lh_blkno);
  474. error = gfs2_quota_init(sdp);
  475. if (error)
  476. goto fail;
  477. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  478. gfs2_glock_dq_uninit(&t_gh);
  479. return 0;
  480. fail:
  481. t_gh.gh_flags |= GL_NOCACHE;
  482. gfs2_glock_dq_uninit(&t_gh);
  483. return error;
  484. }
  485. /**
  486. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  487. * @sdp: the filesystem
  488. *
  489. * Returns: errno
  490. */
  491. int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  492. {
  493. struct gfs2_holder t_gh;
  494. int error;
  495. gfs2_quota_sync(sdp);
  496. gfs2_statfs_sync(sdp);
  497. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
  498. &t_gh);
  499. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  500. return error;
  501. gfs2_meta_syncfs(sdp);
  502. gfs2_log_shutdown(sdp);
  503. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  504. if (t_gh.gh_gl)
  505. gfs2_glock_dq_uninit(&t_gh);
  506. gfs2_quota_cleanup(sdp);
  507. return error;
  508. }
  509. static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  510. {
  511. const struct gfs2_statfs_change *str = buf;
  512. sc->sc_total = be64_to_cpu(str->sc_total);
  513. sc->sc_free = be64_to_cpu(str->sc_free);
  514. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  515. }
  516. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  517. {
  518. struct gfs2_statfs_change *str = buf;
  519. str->sc_total = cpu_to_be64(sc->sc_total);
  520. str->sc_free = cpu_to_be64(sc->sc_free);
  521. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  522. }
  523. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  524. {
  525. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  526. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  527. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  528. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  529. struct buffer_head *m_bh, *l_bh;
  530. struct gfs2_holder gh;
  531. int error;
  532. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  533. &gh);
  534. if (error)
  535. return error;
  536. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  537. if (error)
  538. goto out;
  539. if (sdp->sd_args.ar_spectator) {
  540. spin_lock(&sdp->sd_statfs_spin);
  541. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  542. sizeof(struct gfs2_dinode));
  543. spin_unlock(&sdp->sd_statfs_spin);
  544. } else {
  545. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  546. if (error)
  547. goto out_m_bh;
  548. spin_lock(&sdp->sd_statfs_spin);
  549. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  550. sizeof(struct gfs2_dinode));
  551. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  552. sizeof(struct gfs2_dinode));
  553. spin_unlock(&sdp->sd_statfs_spin);
  554. brelse(l_bh);
  555. }
  556. out_m_bh:
  557. brelse(m_bh);
  558. out:
  559. gfs2_glock_dq_uninit(&gh);
  560. return 0;
  561. }
  562. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  563. s64 dinodes)
  564. {
  565. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  566. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  567. struct buffer_head *l_bh;
  568. int error;
  569. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  570. if (error)
  571. return;
  572. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  573. spin_lock(&sdp->sd_statfs_spin);
  574. l_sc->sc_total += total;
  575. l_sc->sc_free += free;
  576. l_sc->sc_dinodes += dinodes;
  577. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  578. spin_unlock(&sdp->sd_statfs_spin);
  579. brelse(l_bh);
  580. }
  581. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  582. {
  583. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  584. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  585. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  586. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  587. struct gfs2_holder gh;
  588. struct buffer_head *m_bh, *l_bh;
  589. int error;
  590. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  591. &gh);
  592. if (error)
  593. return error;
  594. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  595. if (error)
  596. goto out;
  597. spin_lock(&sdp->sd_statfs_spin);
  598. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  599. sizeof(struct gfs2_dinode));
  600. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  601. spin_unlock(&sdp->sd_statfs_spin);
  602. goto out_bh;
  603. }
  604. spin_unlock(&sdp->sd_statfs_spin);
  605. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  606. if (error)
  607. goto out_bh;
  608. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  609. if (error)
  610. goto out_bh2;
  611. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  612. spin_lock(&sdp->sd_statfs_spin);
  613. m_sc->sc_total += l_sc->sc_total;
  614. m_sc->sc_free += l_sc->sc_free;
  615. m_sc->sc_dinodes += l_sc->sc_dinodes;
  616. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  617. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  618. 0, sizeof(struct gfs2_statfs_change));
  619. spin_unlock(&sdp->sd_statfs_spin);
  620. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  621. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  622. gfs2_trans_end(sdp);
  623. out_bh2:
  624. brelse(l_bh);
  625. out_bh:
  626. brelse(m_bh);
  627. out:
  628. gfs2_glock_dq_uninit(&gh);
  629. return error;
  630. }
  631. /**
  632. * gfs2_statfs_i - Do a statfs
  633. * @sdp: the filesystem
  634. * @sg: the sg structure
  635. *
  636. * Returns: errno
  637. */
  638. int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  639. {
  640. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  641. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  642. spin_lock(&sdp->sd_statfs_spin);
  643. *sc = *m_sc;
  644. sc->sc_total += l_sc->sc_total;
  645. sc->sc_free += l_sc->sc_free;
  646. sc->sc_dinodes += l_sc->sc_dinodes;
  647. spin_unlock(&sdp->sd_statfs_spin);
  648. if (sc->sc_free < 0)
  649. sc->sc_free = 0;
  650. if (sc->sc_free > sc->sc_total)
  651. sc->sc_free = sc->sc_total;
  652. if (sc->sc_dinodes < 0)
  653. sc->sc_dinodes = 0;
  654. return 0;
  655. }
  656. /**
  657. * statfs_fill - fill in the sg for a given RG
  658. * @rgd: the RG
  659. * @sc: the sc structure
  660. *
  661. * Returns: 0 on success, -ESTALE if the LVB is invalid
  662. */
  663. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  664. struct gfs2_statfs_change_host *sc)
  665. {
  666. gfs2_rgrp_verify(rgd);
  667. sc->sc_total += rgd->rd_data;
  668. sc->sc_free += rgd->rd_rg.rg_free;
  669. sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
  670. return 0;
  671. }
  672. /**
  673. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  674. * @sdp: the filesystem
  675. * @sc: the sc info that will be returned
  676. *
  677. * Any error (other than a signal) will cause this routine to fall back
  678. * to the synchronous version.
  679. *
  680. * FIXME: This really shouldn't busy wait like this.
  681. *
  682. * Returns: errno
  683. */
  684. int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  685. {
  686. struct gfs2_holder ri_gh;
  687. struct gfs2_rgrpd *rgd_next;
  688. struct gfs2_holder *gha, *gh;
  689. unsigned int slots = 64;
  690. unsigned int x;
  691. int done;
  692. int error = 0, err;
  693. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  694. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  695. if (!gha)
  696. return -ENOMEM;
  697. error = gfs2_rindex_hold(sdp, &ri_gh);
  698. if (error)
  699. goto out;
  700. rgd_next = gfs2_rgrpd_get_first(sdp);
  701. for (;;) {
  702. done = 1;
  703. for (x = 0; x < slots; x++) {
  704. gh = gha + x;
  705. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  706. err = gfs2_glock_wait(gh);
  707. if (err) {
  708. gfs2_holder_uninit(gh);
  709. error = err;
  710. } else {
  711. if (!error)
  712. error = statfs_slow_fill(
  713. gh->gh_gl->gl_object, sc);
  714. gfs2_glock_dq_uninit(gh);
  715. }
  716. }
  717. if (gh->gh_gl)
  718. done = 0;
  719. else if (rgd_next && !error) {
  720. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  721. LM_ST_SHARED,
  722. GL_ASYNC,
  723. gh);
  724. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  725. done = 0;
  726. }
  727. if (signal_pending(current))
  728. error = -ERESTARTSYS;
  729. }
  730. if (done)
  731. break;
  732. yield();
  733. }
  734. gfs2_glock_dq_uninit(&ri_gh);
  735. out:
  736. kfree(gha);
  737. return error;
  738. }
  739. struct lfcc {
  740. struct list_head list;
  741. struct gfs2_holder gh;
  742. };
  743. /**
  744. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  745. * journals are clean
  746. * @sdp: the file system
  747. * @state: the state to put the transaction lock into
  748. * @t_gh: the hold on the transaction lock
  749. *
  750. * Returns: errno
  751. */
  752. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  753. struct gfs2_holder *t_gh)
  754. {
  755. struct gfs2_inode *ip;
  756. struct gfs2_holder ji_gh;
  757. struct gfs2_jdesc *jd;
  758. struct lfcc *lfcc;
  759. LIST_HEAD(list);
  760. struct gfs2_log_header_host lh;
  761. int error;
  762. error = gfs2_jindex_hold(sdp, &ji_gh);
  763. if (error)
  764. return error;
  765. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  766. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  767. if (!lfcc) {
  768. error = -ENOMEM;
  769. goto out;
  770. }
  771. ip = GFS2_I(jd->jd_inode);
  772. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  773. if (error) {
  774. kfree(lfcc);
  775. goto out;
  776. }
  777. list_add(&lfcc->list, &list);
  778. }
  779. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  780. LM_FLAG_PRIORITY | GL_NOCACHE,
  781. 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. }