David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Bob Peterson | da6dd40 | 2007-12-11 18:49:21 -0600 | [diff] [blame] | 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 15 | #include <linux/crc32.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 16 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | f45b7dd | 2006-07-27 13:53:53 -0400 | [diff] [blame] | 17 | #include <linux/bio.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 18 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 19 | |
| 20 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 21 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include "bmap.h" |
| 23 | #include "dir.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "glock.h" |
| 25 | #include "glops.h" |
| 26 | #include "inode.h" |
| 27 | #include "log.h" |
| 28 | #include "meta_io.h" |
| 29 | #include "quota.h" |
| 30 | #include "recovery.h" |
| 31 | #include "rgrp.h" |
| 32 | #include "super.h" |
| 33 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 34 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | /** |
| 37 | * gfs2_jindex_hold - Grab a lock on the jindex |
| 38 | * @sdp: The GFS2 superblock |
| 39 | * @ji_gh: the holder for the jindex glock |
| 40 | * |
| 41 | * This is very similar to the gfs2_rindex_hold() function, except that |
| 42 | * in general we hold the jindex lock for longer periods of time and |
| 43 | * we grab it far less frequently (in general) then the rgrp lock. |
| 44 | * |
| 45 | * Returns: errno |
| 46 | */ |
| 47 | |
| 48 | int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh) |
| 49 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 50 | struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 51 | struct qstr name; |
| 52 | char buf[20]; |
| 53 | struct gfs2_jdesc *jd; |
| 54 | int error; |
| 55 | |
| 56 | name.name = buf; |
| 57 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 58 | mutex_lock(&sdp->sd_jindex_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 59 | |
| 60 | for (;;) { |
Steven Whitehouse | 1c0f487 | 2007-01-22 12:10:39 -0500 | [diff] [blame] | 61 | error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 62 | if (error) |
| 63 | break; |
| 64 | |
| 65 | name.len = sprintf(buf, "journal%u", sdp->sd_journals); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 66 | name.hash = gfs2_disk_hash(name.name, name.len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 67 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 68 | error = gfs2_dir_check(sdp->sd_jindex, &name, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 69 | if (error == -ENOENT) { |
| 70 | error = 0; |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | gfs2_glock_dq_uninit(ji_gh); |
| 75 | |
| 76 | if (error) |
| 77 | break; |
| 78 | |
| 79 | error = -ENOMEM; |
| 80 | jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL); |
| 81 | if (!jd) |
| 82 | break; |
| 83 | |
Bob Peterson | 0811a12 | 2008-01-03 09:24:53 -0600 | [diff] [blame] | 84 | INIT_LIST_HEAD(&jd->extent_list); |
Al Viro | a569c71 | 2008-07-23 14:42:05 -0400 | [diff] [blame] | 85 | jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 86 | if (!jd->jd_inode || IS_ERR(jd->jd_inode)) { |
| 87 | if (!jd->jd_inode) |
| 88 | error = -ENOENT; |
| 89 | else |
| 90 | error = PTR_ERR(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 91 | kfree(jd); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | spin_lock(&sdp->sd_jindex_spin); |
| 96 | jd->jd_jid = sdp->sd_journals++; |
| 97 | list_add_tail(&jd->jd_list, &sdp->sd_jindex_list); |
| 98 | spin_unlock(&sdp->sd_jindex_spin); |
| 99 | } |
| 100 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 101 | mutex_unlock(&sdp->sd_jindex_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 102 | |
| 103 | return error; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * gfs2_jindex_free - Clear all the journal index information |
| 108 | * @sdp: The GFS2 superblock |
| 109 | * |
| 110 | */ |
| 111 | |
| 112 | void gfs2_jindex_free(struct gfs2_sbd *sdp) |
| 113 | { |
Bob Peterson | da6dd40 | 2007-12-11 18:49:21 -0600 | [diff] [blame] | 114 | struct list_head list, *head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 115 | struct gfs2_jdesc *jd; |
Bob Peterson | da6dd40 | 2007-12-11 18:49:21 -0600 | [diff] [blame] | 116 | struct gfs2_journal_extent *jext; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | |
| 118 | spin_lock(&sdp->sd_jindex_spin); |
| 119 | list_add(&list, &sdp->sd_jindex_list); |
| 120 | list_del_init(&sdp->sd_jindex_list); |
| 121 | sdp->sd_journals = 0; |
| 122 | spin_unlock(&sdp->sd_jindex_spin); |
| 123 | |
| 124 | while (!list_empty(&list)) { |
| 125 | jd = list_entry(list.next, struct gfs2_jdesc, jd_list); |
Bob Peterson | da6dd40 | 2007-12-11 18:49:21 -0600 | [diff] [blame] | 126 | head = &jd->extent_list; |
| 127 | while (!list_empty(head)) { |
| 128 | jext = list_entry(head->next, |
| 129 | struct gfs2_journal_extent, |
| 130 | extent_list); |
| 131 | list_del(&jext->extent_list); |
| 132 | kfree(jext); |
| 133 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 134 | list_del(&jd->jd_list); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 135 | iput(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 136 | kfree(jd); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid) |
| 141 | { |
| 142 | struct gfs2_jdesc *jd; |
| 143 | int found = 0; |
| 144 | |
| 145 | list_for_each_entry(jd, head, jd_list) { |
| 146 | if (jd->jd_jid == jid) { |
| 147 | found = 1; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (!found) |
| 153 | jd = NULL; |
| 154 | |
| 155 | return jd; |
| 156 | } |
| 157 | |
| 158 | struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid) |
| 159 | { |
| 160 | struct gfs2_jdesc *jd; |
| 161 | |
| 162 | spin_lock(&sdp->sd_jindex_spin); |
| 163 | jd = jdesc_find_i(&sdp->sd_jindex_list, jid); |
| 164 | spin_unlock(&sdp->sd_jindex_spin); |
| 165 | |
| 166 | return jd; |
| 167 | } |
| 168 | |
| 169 | void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid) |
| 170 | { |
| 171 | struct gfs2_jdesc *jd; |
| 172 | |
| 173 | spin_lock(&sdp->sd_jindex_spin); |
| 174 | jd = jdesc_find_i(&sdp->sd_jindex_list, jid); |
| 175 | if (jd) |
| 176 | jd->jd_dirty = 1; |
| 177 | spin_unlock(&sdp->sd_jindex_spin); |
| 178 | } |
| 179 | |
| 180 | struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp) |
| 181 | { |
| 182 | struct gfs2_jdesc *jd; |
| 183 | int found = 0; |
| 184 | |
| 185 | spin_lock(&sdp->sd_jindex_spin); |
| 186 | |
| 187 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { |
| 188 | if (jd->jd_dirty) { |
| 189 | jd->jd_dirty = 0; |
| 190 | found = 1; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | spin_unlock(&sdp->sd_jindex_spin); |
| 195 | |
| 196 | if (!found) |
| 197 | jd = NULL; |
| 198 | |
| 199 | return jd; |
| 200 | } |
| 201 | |
| 202 | int gfs2_jdesc_check(struct gfs2_jdesc *jd) |
| 203 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 204 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 205 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 206 | int ar; |
| 207 | int error; |
| 208 | |
Steven Whitehouse | c9e9888 | 2008-11-04 09:47:33 +0000 | [diff] [blame^] | 209 | if (ip->i_disksize < (8 << 20) || ip->i_disksize > (1 << 30) || |
| 210 | (ip->i_disksize & (sdp->sd_sb.sb_bsize - 1))) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 211 | gfs2_consist_inode(ip); |
| 212 | return -EIO; |
| 213 | } |
Steven Whitehouse | c9e9888 | 2008-11-04 09:47:33 +0000 | [diff] [blame^] | 214 | jd->jd_blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 215 | |
Steven Whitehouse | c9e9888 | 2008-11-04 09:47:33 +0000 | [diff] [blame^] | 216 | error = gfs2_write_alloc_required(ip, 0, ip->i_disksize, &ar); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 217 | if (!error && ar) { |
| 218 | gfs2_consist_inode(ip); |
| 219 | error = -EIO; |
| 220 | } |
| 221 | |
| 222 | return error; |
| 223 | } |
| 224 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 225 | /** |
| 226 | * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one |
| 227 | * @sdp: the filesystem |
| 228 | * |
| 229 | * Returns: errno |
| 230 | */ |
| 231 | |
| 232 | int gfs2_make_fs_rw(struct gfs2_sbd *sdp) |
| 233 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 234 | struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 235 | struct gfs2_glock *j_gl = ip->i_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 | struct gfs2_holder t_gh; |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 237 | struct gfs2_log_header_host head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 238 | int error; |
| 239 | |
Steven Whitehouse | 1c0f487 | 2007-01-22 12:10:39 -0500 | [diff] [blame] | 240 | error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 241 | if (error) |
| 242 | return error; |
| 243 | |
Steven Whitehouse | 1a14d3a | 2006-11-20 10:37:45 -0500 | [diff] [blame] | 244 | j_gl->gl_ops->go_inval(j_gl, DIO_METADATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 245 | |
| 246 | error = gfs2_find_jhead(sdp->sd_jdesc, &head); |
| 247 | if (error) |
| 248 | goto fail; |
| 249 | |
| 250 | if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) { |
| 251 | gfs2_consist(sdp); |
| 252 | error = -EIO; |
| 253 | goto fail; |
| 254 | } |
| 255 | |
| 256 | /* Initialize some head of the log stuff */ |
| 257 | sdp->sd_log_sequence = head.lh_sequence + 1; |
| 258 | gfs2_log_pointers_init(sdp, head.lh_blkno); |
| 259 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 260 | error = gfs2_quota_init(sdp); |
| 261 | if (error) |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 262 | goto fail; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 263 | |
| 264 | set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); |
| 265 | |
| 266 | gfs2_glock_dq_uninit(&t_gh); |
| 267 | |
| 268 | return 0; |
| 269 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 270 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 271 | t_gh.gh_flags |= GL_NOCACHE; |
| 272 | gfs2_glock_dq_uninit(&t_gh); |
| 273 | |
| 274 | return error; |
| 275 | } |
| 276 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 277 | static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf) |
| 278 | { |
| 279 | const struct gfs2_statfs_change *str = buf; |
| 280 | |
| 281 | sc->sc_total = be64_to_cpu(str->sc_total); |
| 282 | sc->sc_free = be64_to_cpu(str->sc_free); |
| 283 | sc->sc_dinodes = be64_to_cpu(str->sc_dinodes); |
| 284 | } |
| 285 | |
| 286 | static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf) |
| 287 | { |
| 288 | struct gfs2_statfs_change *str = buf; |
| 289 | |
| 290 | str->sc_total = cpu_to_be64(sc->sc_total); |
| 291 | str->sc_free = cpu_to_be64(sc->sc_free); |
| 292 | str->sc_dinodes = cpu_to_be64(sc->sc_dinodes); |
| 293 | } |
| 294 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 295 | int gfs2_statfs_init(struct gfs2_sbd *sdp) |
| 296 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 297 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 298 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 299 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 300 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 301 | struct buffer_head *m_bh, *l_bh; |
| 302 | struct gfs2_holder gh; |
| 303 | int error; |
| 304 | |
| 305 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE, |
| 306 | &gh); |
| 307 | if (error) |
| 308 | return error; |
| 309 | |
| 310 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); |
| 311 | if (error) |
| 312 | goto out; |
| 313 | |
| 314 | if (sdp->sd_args.ar_spectator) { |
| 315 | spin_lock(&sdp->sd_statfs_spin); |
| 316 | gfs2_statfs_change_in(m_sc, m_bh->b_data + |
| 317 | sizeof(struct gfs2_dinode)); |
| 318 | spin_unlock(&sdp->sd_statfs_spin); |
| 319 | } else { |
| 320 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); |
| 321 | if (error) |
| 322 | goto out_m_bh; |
| 323 | |
| 324 | spin_lock(&sdp->sd_statfs_spin); |
| 325 | gfs2_statfs_change_in(m_sc, m_bh->b_data + |
| 326 | sizeof(struct gfs2_dinode)); |
| 327 | gfs2_statfs_change_in(l_sc, l_bh->b_data + |
| 328 | sizeof(struct gfs2_dinode)); |
| 329 | spin_unlock(&sdp->sd_statfs_spin); |
| 330 | |
| 331 | brelse(l_bh); |
| 332 | } |
| 333 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 334 | out_m_bh: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 335 | brelse(m_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 336 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 337 | gfs2_glock_dq_uninit(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 341 | void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free, |
| 342 | s64 dinodes) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 343 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 344 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 345 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 346 | struct buffer_head *l_bh; |
| 347 | int error; |
| 348 | |
| 349 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); |
| 350 | if (error) |
| 351 | return; |
| 352 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 353 | gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 354 | |
| 355 | spin_lock(&sdp->sd_statfs_spin); |
| 356 | l_sc->sc_total += total; |
| 357 | l_sc->sc_free += free; |
| 358 | l_sc->sc_dinodes += dinodes; |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 359 | gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 360 | spin_unlock(&sdp->sd_statfs_spin); |
| 361 | |
| 362 | brelse(l_bh); |
| 363 | } |
| 364 | |
| 365 | int gfs2_statfs_sync(struct gfs2_sbd *sdp) |
| 366 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 367 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); |
| 368 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 369 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
| 370 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 371 | struct gfs2_holder gh; |
| 372 | struct buffer_head *m_bh, *l_bh; |
| 373 | int error; |
| 374 | |
| 375 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE, |
| 376 | &gh); |
| 377 | if (error) |
| 378 | return error; |
| 379 | |
| 380 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); |
| 381 | if (error) |
| 382 | goto out; |
| 383 | |
| 384 | spin_lock(&sdp->sd_statfs_spin); |
| 385 | gfs2_statfs_change_in(m_sc, m_bh->b_data + |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 386 | sizeof(struct gfs2_dinode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 387 | if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) { |
| 388 | spin_unlock(&sdp->sd_statfs_spin); |
| 389 | goto out_bh; |
| 390 | } |
| 391 | spin_unlock(&sdp->sd_statfs_spin); |
| 392 | |
| 393 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); |
| 394 | if (error) |
| 395 | goto out_bh; |
| 396 | |
| 397 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); |
| 398 | if (error) |
| 399 | goto out_bh2; |
| 400 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 401 | gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | |
| 403 | spin_lock(&sdp->sd_statfs_spin); |
| 404 | m_sc->sc_total += l_sc->sc_total; |
| 405 | m_sc->sc_free += l_sc->sc_free; |
| 406 | m_sc->sc_dinodes += l_sc->sc_dinodes; |
| 407 | memset(l_sc, 0, sizeof(struct gfs2_statfs_change)); |
| 408 | memset(l_bh->b_data + sizeof(struct gfs2_dinode), |
| 409 | 0, sizeof(struct gfs2_statfs_change)); |
| 410 | spin_unlock(&sdp->sd_statfs_spin); |
| 411 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 412 | gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 413 | gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode)); |
| 414 | |
| 415 | gfs2_trans_end(sdp); |
| 416 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 417 | out_bh2: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 418 | brelse(l_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 419 | out_bh: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 420 | brelse(m_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 421 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 422 | gfs2_glock_dq_uninit(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | return error; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * gfs2_statfs_i - Do a statfs |
| 428 | * @sdp: the filesystem |
| 429 | * @sg: the sg structure |
| 430 | * |
| 431 | * Returns: errno |
| 432 | */ |
| 433 | |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 434 | int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 435 | { |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 436 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
| 437 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 438 | |
| 439 | spin_lock(&sdp->sd_statfs_spin); |
| 440 | |
| 441 | *sc = *m_sc; |
| 442 | sc->sc_total += l_sc->sc_total; |
| 443 | sc->sc_free += l_sc->sc_free; |
| 444 | sc->sc_dinodes += l_sc->sc_dinodes; |
| 445 | |
| 446 | spin_unlock(&sdp->sd_statfs_spin); |
| 447 | |
| 448 | if (sc->sc_free < 0) |
| 449 | sc->sc_free = 0; |
| 450 | if (sc->sc_free > sc->sc_total) |
| 451 | sc->sc_free = sc->sc_total; |
| 452 | if (sc->sc_dinodes < 0) |
| 453 | sc->sc_dinodes = 0; |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * statfs_fill - fill in the sg for a given RG |
| 460 | * @rgd: the RG |
| 461 | * @sc: the sc structure |
| 462 | * |
| 463 | * Returns: 0 on success, -ESTALE if the LVB is invalid |
| 464 | */ |
| 465 | |
| 466 | static int statfs_slow_fill(struct gfs2_rgrpd *rgd, |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 467 | struct gfs2_statfs_change_host *sc) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 468 | { |
| 469 | gfs2_rgrp_verify(rgd); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 470 | sc->sc_total += rgd->rd_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 471 | sc->sc_free += rgd->rd_rg.rg_free; |
| 472 | sc->sc_dinodes += rgd->rd_rg.rg_dinodes; |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * gfs2_statfs_slow - Stat a filesystem using asynchronous locking |
| 478 | * @sdp: the filesystem |
| 479 | * @sc: the sc info that will be returned |
| 480 | * |
| 481 | * Any error (other than a signal) will cause this routine to fall back |
| 482 | * to the synchronous version. |
| 483 | * |
| 484 | * FIXME: This really shouldn't busy wait like this. |
| 485 | * |
| 486 | * Returns: errno |
| 487 | */ |
| 488 | |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 489 | int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 490 | { |
| 491 | struct gfs2_holder ri_gh; |
| 492 | struct gfs2_rgrpd *rgd_next; |
| 493 | struct gfs2_holder *gha, *gh; |
| 494 | unsigned int slots = 64; |
| 495 | unsigned int x; |
| 496 | int done; |
| 497 | int error = 0, err; |
| 498 | |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 499 | memset(sc, 0, sizeof(struct gfs2_statfs_change_host)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 500 | gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL); |
| 501 | if (!gha) |
| 502 | return -ENOMEM; |
| 503 | |
| 504 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 505 | if (error) |
| 506 | goto out; |
| 507 | |
| 508 | rgd_next = gfs2_rgrpd_get_first(sdp); |
| 509 | |
| 510 | for (;;) { |
| 511 | done = 1; |
| 512 | |
| 513 | for (x = 0; x < slots; x++) { |
| 514 | gh = gha + x; |
| 515 | |
| 516 | if (gh->gh_gl && gfs2_glock_poll(gh)) { |
| 517 | err = gfs2_glock_wait(gh); |
| 518 | if (err) { |
| 519 | gfs2_holder_uninit(gh); |
| 520 | error = err; |
| 521 | } else { |
| 522 | if (!error) |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 523 | error = statfs_slow_fill( |
| 524 | gh->gh_gl->gl_object, sc); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 525 | gfs2_glock_dq_uninit(gh); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (gh->gh_gl) |
| 530 | done = 0; |
| 531 | else if (rgd_next && !error) { |
| 532 | error = gfs2_glock_nq_init(rgd_next->rd_gl, |
| 533 | LM_ST_SHARED, |
| 534 | GL_ASYNC, |
| 535 | gh); |
| 536 | rgd_next = gfs2_rgrpd_get_next(rgd_next); |
| 537 | done = 0; |
| 538 | } |
| 539 | |
| 540 | if (signal_pending(current)) |
| 541 | error = -ERESTARTSYS; |
| 542 | } |
| 543 | |
| 544 | if (done) |
| 545 | break; |
| 546 | |
| 547 | yield(); |
| 548 | } |
| 549 | |
| 550 | gfs2_glock_dq_uninit(&ri_gh); |
| 551 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 552 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 553 | kfree(gha); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 554 | return error; |
| 555 | } |
| 556 | |
| 557 | struct lfcc { |
| 558 | struct list_head list; |
| 559 | struct gfs2_holder gh; |
| 560 | }; |
| 561 | |
| 562 | /** |
| 563 | * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all |
| 564 | * journals are clean |
| 565 | * @sdp: the file system |
| 566 | * @state: the state to put the transaction lock into |
| 567 | * @t_gh: the hold on the transaction lock |
| 568 | * |
| 569 | * Returns: errno |
| 570 | */ |
| 571 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 572 | static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp, |
| 573 | struct gfs2_holder *t_gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 574 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 575 | struct gfs2_inode *ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 576 | struct gfs2_holder ji_gh; |
| 577 | struct gfs2_jdesc *jd; |
| 578 | struct lfcc *lfcc; |
| 579 | LIST_HEAD(list); |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 580 | struct gfs2_log_header_host lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 581 | int error; |
| 582 | |
| 583 | error = gfs2_jindex_hold(sdp, &ji_gh); |
| 584 | if (error) |
| 585 | return error; |
| 586 | |
| 587 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { |
| 588 | lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL); |
| 589 | if (!lfcc) { |
| 590 | error = -ENOMEM; |
| 591 | goto out; |
| 592 | } |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 593 | ip = GFS2_I(jd->jd_inode); |
| 594 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 595 | if (error) { |
| 596 | kfree(lfcc); |
| 597 | goto out; |
| 598 | } |
| 599 | list_add(&lfcc->list, &list); |
| 600 | } |
| 601 | |
| 602 | error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED, |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 603 | GL_NOCACHE, t_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 604 | |
| 605 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { |
| 606 | error = gfs2_jdesc_check(jd); |
| 607 | if (error) |
| 608 | break; |
| 609 | error = gfs2_find_jhead(jd, &lh); |
| 610 | if (error) |
| 611 | break; |
| 612 | if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) { |
| 613 | error = -EBUSY; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (error) |
| 619 | gfs2_glock_dq_uninit(t_gh); |
| 620 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 621 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 622 | while (!list_empty(&list)) { |
| 623 | lfcc = list_entry(list.next, struct lfcc, list); |
| 624 | list_del(&lfcc->list); |
| 625 | gfs2_glock_dq_uninit(&lfcc->gh); |
| 626 | kfree(lfcc); |
| 627 | } |
| 628 | gfs2_glock_dq_uninit(&ji_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 629 | return error; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * gfs2_freeze_fs - freezes the file system |
| 634 | * @sdp: the file system |
| 635 | * |
| 636 | * This function flushes data and meta data for all machines by |
| 637 | * aquiring the transaction log exclusively. All journals are |
| 638 | * ensured to be in a clean state as well. |
| 639 | * |
| 640 | * Returns: errno |
| 641 | */ |
| 642 | |
| 643 | int gfs2_freeze_fs(struct gfs2_sbd *sdp) |
| 644 | { |
| 645 | int error = 0; |
| 646 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 647 | mutex_lock(&sdp->sd_freeze_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 648 | |
| 649 | if (!sdp->sd_freeze_count++) { |
| 650 | error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh); |
| 651 | if (error) |
| 652 | sdp->sd_freeze_count--; |
| 653 | } |
| 654 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 655 | mutex_unlock(&sdp->sd_freeze_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 656 | |
| 657 | return error; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * gfs2_unfreeze_fs - unfreezes the file system |
| 662 | * @sdp: the file system |
| 663 | * |
| 664 | * This function allows the file system to proceed by unlocking |
| 665 | * the exclusively held transaction lock. Other GFS2 nodes are |
| 666 | * now free to acquire the lock shared and go on with their lives. |
| 667 | * |
| 668 | */ |
| 669 | |
| 670 | void gfs2_unfreeze_fs(struct gfs2_sbd *sdp) |
| 671 | { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 672 | mutex_lock(&sdp->sd_freeze_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 673 | |
| 674 | if (sdp->sd_freeze_count && !--sdp->sd_freeze_count) |
| 675 | gfs2_glock_dq_uninit(&sdp->sd_freeze_gh); |
| 676 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 677 | mutex_unlock(&sdp->sd_freeze_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 678 | } |
| 679 | |