Whamcloud - gitweb
080bff0bfca21606850744298c19a0e443b822bc
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-mmp-2.6-rhel4.patch
1 Index: linux-2.6.9-67.0.22/fs/ext3/super.c
2 ===================================================================
3 --- linux-2.6.9-67.0.22.orig/fs/ext3/super.c
4 +++ linux-2.6.9-67.0.22/fs/ext3/super.c
5 @@ -35,6 +35,8 @@
6  #include <linux/mount.h>
7  #include <linux/namei.h>
8  #include <linux/quotaops.h>
9 +#include <linux/kthread.h>
10 +#include <linux/utsname.h>
11  #include <asm/uaccess.h>
12  #include "xattr.h"
13  #include "acl.h"
14 @@ -440,6 +442,8 @@ void ext3_put_super (struct super_block 
15                 invalidate_bdev(sbi->journal_bdev, 0);
16                 ext3_blkdev_remove(sbi);
17         }
18 +       if (sbi->s_mmp_tsk)
19 +               kthread_stop(sbi->s_mmp_tsk);
20         if (sbi->s_dev_proc) {
21                 remove_proc_entry(sbi->s_dev_proc->name, proc_root_ext3);
22                 sbi->s_dev_proc = NULL;
23 @@ -1430,6 +1434,314 @@ static unsigned long descriptor_loc(stru
24         return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
25  }
26  
27 +/*
28 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
29 + * faster.
30 + */
31 +static int write_mmp_block(struct buffer_head *bh)
32 +{
33 +       mark_buffer_dirty(bh);
34 +       lock_buffer(bh);
35 +       bh->b_end_io = end_buffer_write_sync;
36 +       get_bh(bh);
37 +       submit_bh(WRITE_SYNC, bh);
38 +       wait_on_buffer(bh);
39 +       if (unlikely(!buffer_uptodate(bh)))
40 +               return 1;
41 +
42 +       return 0;
43 +}
44 +
45 +/*
46 + * Read the MMP block. It _must_ be read from disk and hence we clear the
47 + * uptodate flag on the buffer.
48 + */
49 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
50 +                         unsigned long mmp_block)
51 +{
52 +       struct mmp_struct *mmp;
53 +
54 +       if (*bh)
55 +               clear_buffer_uptodate(*bh);
56 +
57 +       brelse(*bh);
58 +
59 +       *bh = sb_bread(sb, mmp_block);
60 +       if (!*bh) {
61 +               ext3_warning(sb, __FUNCTION__,
62 +                            "Error while reading MMP block %lu", mmp_block);
63 +               return -EIO;
64 +       }
65 +
66 +       mmp = (struct mmp_struct *)((*bh)->b_data);
67 +       if (le32_to_cpu(mmp->mmp_magic) != EXT3_MMP_MAGIC)
68 +               return -EINVAL;
69 +
70 +       return 0;
71 +}
72 +
73 +/*
74 + * Dump as much information as possible to help the admin.
75 + */
76 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
77 +                        const char *function, const char *msg)
78 +{
79 +       ext3_warning(sb, function, msg);
80 +       ext3_warning(sb, function, "MMP failure info: last update time: %llu, "
81 +                    "last update node: %s, last update device: %s\n",
82 +                    le64_to_cpu(mmp->mmp_time), mmp->mmp_nodename,
83 +                    mmp->mmp_bdevname);
84 +}
85 +
86 +/*
87 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
88 + */
89 +static int kmmpd(void *data)
90 +{
91 +       struct super_block *sb = (struct super_block *) data;
92 +       struct ext3_super_block *es = EXT3_SB(sb)->s_es;
93 +       struct buffer_head *bh = NULL;
94 +       struct mmp_struct *mmp;
95 +       unsigned long mmp_block;
96 +       u32 seq = 0;
97 +       unsigned long failed_writes = 0;
98 +       int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
99 +       unsigned mmp_check_interval;
100 +       unsigned long last_update_time;
101 +       unsigned long diff;
102 +       int retval;
103 +
104 +       mmp_block = le64_to_cpu(es->s_mmp_block);
105 +       retval = read_mmp_block(sb, &bh, mmp_block);
106 +       if (retval)
107 +               goto failed;
108 +
109 +       mmp = (struct mmp_struct *)(bh->b_data);
110 +       mmp->mmp_time = cpu_to_le64(get_seconds());
111 +       /*
112 +        * Start with the higher mmp_check_interval and reduce it if
113 +        * the MMP block is being updated on time.
114 +        */
115 +       mmp_check_interval = max(5 * mmp_update_interval,
116 +                                EXT3_MMP_MIN_CHECK_INTERVAL);
117 +       mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
118 +       bdevname(bh->b_bdev, mmp->mmp_bdevname);
119 +
120 +       down_read(&uts_sem);
121 +       memcpy(mmp->mmp_nodename, system_utsname.nodename,
122 +              sizeof(mmp->mmp_nodename));
123 +       up_read(&uts_sem);
124 +
125 +       while (!kthread_should_stop()) {
126 +               if (++seq > EXT3_MMP_SEQ_MAX)
127 +                       seq = 1;
128 +
129 +               mmp->mmp_seq = cpu_to_le32(seq);
130 +               mmp->mmp_time = cpu_to_le64(get_seconds());
131 +               last_update_time = jiffies;
132 +
133 +               retval = write_mmp_block(bh);
134 +               /*
135 +                * Don't spew too many error messages. Print one every
136 +                * (s_mmp_update_interval * 60) seconds.
137 +                */
138 +               if (retval && (failed_writes % 60) == 0) {
139 +                       ext3_error(sb, __FUNCTION__,
140 +                                  "Error writing to MMP block");
141 +                       failed_writes++;
142 +               }
143 +
144 +               if (!(le32_to_cpu(es->s_feature_incompat) &
145 +                   EXT3_FEATURE_INCOMPAT_MMP)) {
146 +                       ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
147 +                                    "since MMP feature has been disabled.");
148 +                       EXT3_SB(sb)->s_mmp_tsk = 0;
149 +                       goto failed;
150 +               }
151 +
152 +               if (sb->s_flags & MS_RDONLY) {
153 +                       ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
154 +                                    "since filesystem has been remounted as "
155 +                                    "readonly.");
156 +                       EXT3_SB(sb)->s_mmp_tsk = 0;
157 +                       goto failed;
158 +               }
159 +
160 +               diff = jiffies - last_update_time;
161 +               if (diff < mmp_update_interval * HZ) {
162 +                       set_current_state(TASK_INTERRUPTIBLE);
163 +                       schedule_timeout(EXT3_MMP_UPDATE_INTERVAL * HZ - diff);
164 +               }
165 +
166 +               /*
167 +                * We need to make sure that more than mmp_check_interval
168 +                * seconds have not passed since writing. If that has happened
169 +                * we need to check if the MMP block is as we left it.
170 +                */
171 +               diff = jiffies - last_update_time;
172 +               if (diff > mmp_check_interval * HZ) {
173 +                       struct buffer_head *bh_check = NULL;
174 +                       struct mmp_struct *mmp_check;
175 +
176 +                       retval = read_mmp_block(sb, &bh_check, mmp_block);
177 +                       if (retval) {
178 +                               EXT3_SB(sb)->s_mmp_tsk = 0;
179 +                               goto failed;
180 +                       }
181 +
182 +                       mmp_check = (struct mmp_struct *)(bh_check->b_data);
183 +                       if (mmp->mmp_time != mmp_check->mmp_time ||
184 +                           memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
185 +                                  sizeof(mmp->mmp_nodename)))
186 +                               dump_mmp_msg(sb, mmp_check, __FUNCTION__,
187 +                                            "Error while updating MMP info. "
188 +                                            "The filesystem seems to have "
189 +                                            "been multiply mounted.");
190 +
191 +                       put_bh(bh_check);
192 +               }
193 +
194 +               /*
195 +                * Adjust the mmp_check_interval depending on how much time
196 +                * it took for the MMP block to be written.
197 +                */
198 +               mmp_check_interval = max(5 * diff / HZ,
199 +                                (unsigned long) EXT3_MMP_MIN_CHECK_INTERVAL);
200 +               mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
201 +       }
202 +
203 +       /*
204 +        * Unmount seems to be clean.
205 +        */
206 +       mmp->mmp_seq = cpu_to_le32(EXT3_MMP_SEQ_CLEAN);
207 +       mmp->mmp_time = cpu_to_le64(get_seconds());
208 +
209 +       retval = write_mmp_block(bh);
210 +
211 +failed:
212 +       brelse(bh);
213 +       return retval;
214 +}
215 +
216 +/*
217 + * Get a random new sequence number but make sure it is not greater than
218 + * EXT3_MMP_SEQ_MAX.
219 + */
220 +static unsigned int mmp_new_seq(void)
221 +{
222 +       u32 new_seq;
223 +
224 +       do {
225 +               get_random_bytes(&new_seq, sizeof(u32));
226 +       } while (new_seq > EXT3_MMP_SEQ_MAX);
227 +
228 +       return new_seq;
229 +}
230 +
231 +/*
232 + * Protect the filesystem from being mounted more than once.
233 + */
234 +static int ext3_multi_mount_protect(struct super_block *sb,
235 +                                   unsigned long mmp_block)
236 +{
237 +       struct ext3_super_block *es = EXT3_SB(sb)->s_es;
238 +       struct buffer_head *bh = NULL;
239 +       struct mmp_struct *mmp = NULL;
240 +       u32 seq;
241 +       unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
242 +       int retval;
243 +
244 +       if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
245 +           mmp_block >= le32_to_cpu(es->s_blocks_count)) {
246 +               ext3_warning(sb, __FUNCTION__,
247 +                            "Invalid MMP block in superblock");
248 +               goto failed;
249 +       }
250 +
251 +       retval = read_mmp_block(sb, &bh, mmp_block);
252 +       if (retval)
253 +               goto failed;
254 +
255 +       mmp = (struct mmp_struct *)(bh->b_data);
256 +
257 +       if (mmp_check_interval < EXT3_MMP_MIN_CHECK_INTERVAL)
258 +               mmp_check_interval = EXT3_MMP_MIN_CHECK_INTERVAL;
259 +
260 +       /*
261 +        * If check_interval in MMP block is larger, use that instead of
262 +        * update_interval from the superblock.
263 +        */
264 +       if (mmp->mmp_check_interval > mmp_check_interval)
265 +               mmp_check_interval = mmp->mmp_check_interval;
266 +
267 +       seq = le32_to_cpu(mmp->mmp_seq);
268 +       if (seq == EXT3_MMP_SEQ_CLEAN)
269 +               goto skip;
270 +
271 +       if (seq == EXT3_MMP_SEQ_FSCK) {
272 +               dump_mmp_msg(sb, mmp, __FUNCTION__,
273 +                            "fsck is running on the filesystem");
274 +               goto failed;
275 +       }
276 +
277 +       schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
278 +
279 +       retval = read_mmp_block(sb, &bh, mmp_block);
280 +       if (retval)
281 +               goto failed;
282 +       mmp = (struct mmp_struct *)(bh->b_data);
283 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
284 +               dump_mmp_msg(sb, mmp, __FUNCTION__,
285 +                            "Device is already active on another node.");
286 +               goto failed;
287 +       }
288 +
289 +skip:
290 +       /*
291 +        * write a new random sequence number.
292 +        */
293 +       mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
294 +
295 +       retval = write_mmp_block(bh);
296 +       if (retval)
297 +               goto failed;
298 +
299 +       /*
300 +        * wait for MMP interval and check mmp_seq.
301 +        */
302 +       schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
303 +
304 +       retval = read_mmp_block(sb, &bh, mmp_block);
305 +       if (retval)
306 +               goto failed;
307 +       mmp = (struct mmp_struct *)(bh->b_data);
308 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
309 +               dump_mmp_msg(sb, mmp, __FUNCTION__,
310 +                            "Device is already active on another node.");
311 +               goto failed;
312 +       }
313 +
314 +       /*
315 +        * Start a kernel thread to update the MMP block periodically.
316 +        */
317 +       EXT3_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
318 +                                            MAJOR(sb->s_dev),
319 +                                            MINOR(sb->s_dev));
320 +       if (IS_ERR(EXT3_SB(sb)->s_mmp_tsk)) {
321 +               EXT3_SB(sb)->s_mmp_tsk = 0;
322 +               ext3_warning(sb, __FUNCTION__, "Unable to create kmmpd thread "
323 +                            "for %s.", sb->s_id);
324 +               goto failed;
325 +       }
326 +
327 +       brelse(bh);
328 +       return 0;
329 +
330 +failed:
331 +       brelse(bh);
332 +       return 1;
333 +}
334 +
335  
336  static int ext3_fill_super (struct super_block *sb, void *data, int silent)
337  {
338 @@ -1754,6 +2066,11 @@ static int ext3_fill_super (struct super
339                           EXT3_HAS_INCOMPAT_FEATURE(sb,
340                                     EXT3_FEATURE_INCOMPAT_RECOVER));
341  
342 +       if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MMP) &&
343 +           !(sb->s_flags & MS_RDONLY))
344 +               if (ext3_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
345 +                       goto failed_mount3;
346 +
347         /*
348          * The first inode we look at is the journal inode.  Don't try
349          * root first: it may be modified in the journal!
350 @@ -2404,6 +2721,11 @@ int ext3_remount (struct super_block * s
351                                 return ret;
352                         if (!ext3_setup_super (sb, es, 0))
353                                 sb->s_flags &= ~MS_RDONLY;
354 +                       if (EXT3_HAS_INCOMPAT_FEATURE(sb,
355 +                                                   EXT3_FEATURE_INCOMPAT_MMP))
356 +                               if (ext3_multi_mount_protect(sb,
357 +                                               le64_to_cpu(es->s_mmp_block)))
358 +                                       return -EROFS;
359                 }
360         }
361         return 0;
362 Index: linux-2.6.9-67.0.22/include/linux/ext3_fs.h
363 ===================================================================
364 --- linux-2.6.9-67.0.22.orig/include/linux/ext3_fs.h
365 +++ linux-2.6.9-67.0.22/include/linux/ext3_fs.h
366 @@ -575,13 +575,17 @@ struct ext3_super_block {
367         __le32  s_first_meta_bg;        /* First metablock block group */
368         __le32  s_mkfs_time;            /* When the filesystem was created */
369         __le32  s_jnl_blocks[17];       /* Backup of the journal inode */
370 -       __le32  s_blocks_count_hi;      /* Blocks count high 32 bits */
371 +/*150*/ __le32 s_blocks_count_hi;      /* Blocks count high 32 bits */
372         __le32  s_r_blocks_count_hi;    /* Reserved blocks count high 32 bits*/
373         __le32  s_free_blocks_hi;       /* Free blocks count high 32 bits */
374         __le16  s_min_extra_isize;      /* All inodes have at least # bytes */
375         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
376 -       __le32  s_flags;                /* Miscellaneous flags */
377 -       __u32   s_reserved[167];        /* Padding to the end of the block */
378 +/*160*/ __le32 s_flags;                /* Miscellaneous flags */
379 +       __le16  s_raid_stride;          /* RAID stride */
380 +       __le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
381 +       __le64  s_mmp_block;            /* Block for multi-mount protection */
382 +/*170*/ __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
383 +       __le32  s_reserved[163];        /* Padding to the end of the block */
384  };
385  
386  #ifdef __KERNEL__
387 @@ -665,12 +669,14 @@ static inline struct ext3_inode_info *EX
388  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
389  #define EXT3_FEATURE_INCOMPAT_META_BG          0x0010
390  #define EXT3_FEATURE_INCOMPAT_EXTENTS          0x0040 /* extents support */
391 +#define EXT3_FEATURE_INCOMPAT_MMP              0x0100
392  
393  #define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
394  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
395                                          EXT3_FEATURE_INCOMPAT_RECOVER| \
396                                          EXT3_FEATURE_INCOMPAT_META_BG| \
397 -                                        EXT3_FEATURE_INCOMPAT_EXTENTS)
398 +                                        EXT3_FEATURE_INCOMPAT_EXTENTS| \
399 +                                        EXT3_FEATURE_INCOMPAT_MMP)
400  #define EXT3_FEATURE_RO_COMPAT_SUPP    (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
401                                          EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
402                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
403 @@ -830,6 +836,39 @@ struct dir_private_info {
404  #define ERR_BAD_DX_DIR -75000
405  
406  /*
407 + * This structure will be used for multiple mount protection. It will be
408 + * written into the block number saved in the s_mmp_block field in the
409 + * superblock. Programs that check MMP should assume that if
410 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
411 + * to use the filesystem, regardless of how old the timestamp is.
412 + */
413 +#define EXT3_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
414 +#define EXT3_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
415 +#define EXT3_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
416 +#define EXT3_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
417 +
418 +struct mmp_struct {
419 +       __le32  mmp_magic;
420 +       __le32  mmp_seq;
421 +       __le64  mmp_time;
422 +       char    mmp_nodename[64];
423 +       char    mmp_bdevname[32];
424 +       __le16  mmp_check_interval;
425 +       __le16  mmp_pad1;
426 +       __le32  mmp_pad2[227];
427 +};
428 +
429 +/*
430 + * Default interval in seconds to update the MMP sequence number.
431 + */
432 +#define EXT3_MMP_UPDATE_INTERVAL   1
433 +
434 +/*
435 + * Minimum interval for MMP checking in seconds.
436 + */
437 +#define EXT3_MMP_MIN_CHECK_INTERVAL    5
438 +
439 +/*
440   * Function prototypes
441   */
442  
443 Index: linux-2.6.9-67.0.22/include/linux/ext3_fs_sb.h
444 ===================================================================
445 --- linux-2.6.9-67.0.22.orig/include/linux/ext3_fs_sb.h
446 +++ linux-2.6.9-67.0.22/include/linux/ext3_fs_sb.h
447 @@ -150,6 +150,7 @@ struct ext3_sb_info {
448         /* locality groups */
449         struct ext3_locality_group *s_locality_groups;
450  
451 +       struct task_struct *s_mmp_tsk;  /* Kernel thread for multiple mount protection */
452  };
453  
454  #define EXT3_GROUP_INFO(sb, group)                                        \