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