Whamcloud - gitweb
b=6334
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-mmp-2.6-sles10.patch
1 Index: linux-2.6.16.46-0.14/fs/ext3/super.c
2 ===================================================================
3 --- linux-2.6.16.46-0.14.orig/fs/ext3/super.c
4 +++ linux-2.6.16.46-0.14/fs/ext3/super.c
5 @@ -36,6 +36,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,9 @@ static void ext3_put_super (struct super
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 +
21         sb->s_fs_info = NULL;
22         kfree(sbi);
23         return;
24 @@ -1521,6 +1526,313 @@ static unsigned long descriptor_loc(stru
25         return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
26  }
27  
28 +/*
29 + * Write the MMP block using WRITE_SYNC to try to get the block on-disk
30 + * faster.
31 + */
32 +static int write_mmp_block(struct buffer_head *bh)
33 +{
34 +       mark_buffer_dirty(bh);
35 +       lock_buffer(bh);
36 +       bh->b_end_io = end_buffer_write_sync;
37 +       get_bh(bh);
38 +       submit_bh(WRITE_SYNC, bh);
39 +       wait_on_buffer(bh);
40 +       if (unlikely(!buffer_uptodate(bh)))
41 +               return 1;
42 +
43 +       return 0;
44 +}
45 +
46 +/*
47 + * Read the MMP block. It _must_ be read from disk and hence we clear the
48 + * uptodate flag on the buffer.
49 + */
50 +static int read_mmp_block(struct super_block *sb, struct buffer_head **bh,
51 +                         unsigned long mmp_block)
52 +{
53 +       struct mmp_struct *mmp;
54 +
55 +       if (*bh)
56 +               clear_buffer_uptodate(*bh);
57 +
58 +       brelse(*bh);
59 +
60 +       *bh = sb_bread(sb, mmp_block);
61 +       if (!*bh) {
62 +               ext3_warning(sb, __FUNCTION__,
63 +                            "Error while reading MMP block %lu", mmp_block);
64 +               return -EIO;
65 +       }
66 +
67 +       mmp = (struct mmp_struct *)((*bh)->b_data);
68 +       if (le32_to_cpu(mmp->mmp_magic) != EXT3_MMP_MAGIC)
69 +               return -EINVAL;
70 +
71 +       return 0;
72 +}
73 +
74 +/*
75 + * Dump as much information as possible to help the admin.
76 + */
77 +static void dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp,
78 +                        const char *function, const char *msg)
79 +{
80 +       ext3_warning(sb, function, msg);
81 +       ext3_warning(sb, function, "MMP failure info: last update time: %llu, "
82 +                    "last update node: %s, last update device: %s\n",
83 +                    le64_to_cpu(mmp->mmp_time), mmp->mmp_nodename,
84 +                    mmp->mmp_bdevname);
85 +}
86 +
87 +/*
88 + * kmmpd will update the MMP sequence every s_mmp_update_interval seconds
89 + */
90 +static int kmmpd(void *data)
91 +{
92 +       struct super_block *sb = (struct super_block *) data;
93 +       struct ext3_super_block *es = EXT3_SB(sb)->s_es;
94 +       struct buffer_head *bh = NULL;
95 +       struct mmp_struct *mmp;
96 +       unsigned long mmp_block;
97 +       u32 seq = 0;
98 +       unsigned long failed_writes = 0;
99 +       int mmp_update_interval = le16_to_cpu(es->s_mmp_update_interval);
100 +       unsigned mmp_check_interval;
101 +       unsigned long last_update_time;
102 +       unsigned long diff;
103 +       int retval;
104 +
105 +       mmp_block = le64_to_cpu(es->s_mmp_block);
106 +       retval = read_mmp_block(sb, &bh, mmp_block);
107 +       if (retval)
108 +               goto failed;
109 +
110 +       mmp = (struct mmp_struct *)(bh->b_data);
111 +       mmp->mmp_time = cpu_to_le64(get_seconds());
112 +       /*
113 +        * Start with the higher mmp_check_interval and reduce it if
114 +        * the MMP block is being updated on time.
115 +        */
116 +       mmp_check_interval = max(5 * mmp_update_interval,
117 +                                EXT3_MMP_MIN_CHECK_INTERVAL);
118 +       mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
119 +       bdevname(bh->b_bdev, mmp->mmp_bdevname);
120 +
121 +       down_read(&uts_sem);
122 +       memcpy(mmp->mmp_nodename, system_utsname.nodename,
123 +              sizeof(mmp->mmp_nodename));
124 +       up_read(&uts_sem);
125 +
126 +       while (!kthread_should_stop()) {
127 +               if (++seq > EXT3_MMP_SEQ_MAX)
128 +                       seq = 1;
129 +
130 +               mmp->mmp_seq = cpu_to_le32(seq);
131 +               mmp->mmp_time = cpu_to_le64(get_seconds());
132 +               last_update_time = jiffies;
133 +
134 +               retval = write_mmp_block(bh);
135 +               /*
136 +                * Don't spew too many error messages. Print one every
137 +                * (s_mmp_update_interval * 60) seconds.
138 +                */
139 +               if (retval && (failed_writes % 60) == 0) {
140 +                       ext3_error(sb, __FUNCTION__,
141 +                                  "Error writing to MMP block");
142 +                       failed_writes++;
143 +               }
144 +
145 +               if (!(le32_to_cpu(es->s_feature_incompat) &
146 +                   EXT3_FEATURE_INCOMPAT_MMP)) {
147 +                       ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
148 +                                    "since MMP feature has been disabled.");
149 +                       EXT3_SB(sb)->s_mmp_tsk = 0;
150 +                       goto failed;
151 +               }
152 +
153 +               if (sb->s_flags & MS_RDONLY) {
154 +                       ext3_warning(sb, __FUNCTION__, "kmmpd being stopped "
155 +                                    "since filesystem has been remounted as "
156 +                                    "readonly.");
157 +                       EXT3_SB(sb)->s_mmp_tsk = 0;
158 +                       goto failed;
159 +               }
160 +
161 +               diff = jiffies - last_update_time;
162 +               if (diff < mmp_update_interval * HZ)
163 +                       schedule_timeout_interruptible(EXT3_MMP_UPDATE_INTERVAL*
164 +                                                      HZ - diff);
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 @@ -1828,6 +2140,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 @@ -2438,7 +2755,7 @@ static int ext3_remount (struct super_bl
351         unsigned long n_blocks_count = 0;
352         unsigned long old_sb_flags;
353         struct ext3_mount_options old_opts;
354 -       int err;
355 +       int err = 0;
356  #ifdef CONFIG_QUOTA
357         int i;
358  #endif
359 @@ -2522,6 +2839,11 @@ static int ext3_remount (struct super_bl
360                         }
361                         if (!ext3_setup_super (sb, es, 0))
362                                 sb->s_flags &= ~MS_RDONLY;
363 +                       if (EXT3_HAS_INCOMPAT_FEATURE(sb,
364 +                                                   EXT3_FEATURE_INCOMPAT_MMP))
365 +                               if (ext3_multi_mount_protect(sb,
366 +                                               le64_to_cpu(es->s_mmp_block)))
367 +                                       goto restore_opts;
368                 }
369         }
370  #ifdef CONFIG_QUOTA
371 Index: linux-2.6.16.46-0.14/include/linux/ext3_fs.h
372 ===================================================================
373 --- linux-2.6.16.46-0.14.orig/include/linux/ext3_fs.h
374 +++ linux-2.6.16.46-0.14/include/linux/ext3_fs.h
375 @@ -599,13 +599,17 @@ struct ext3_super_block {
376         __le32  s_first_meta_bg;        /* First metablock block group */
377         __le32  s_mkfs_time;            /* When the filesystem was created */
378         __le32  s_jnl_blocks[17];       /* Backup of the journal inode */
379 -       __le32  s_blocks_count_hi;      /* Blocks count high 32 bits */
380 +/*150*/ __le32 s_blocks_count_hi;      /* Blocks count high 32 bits */
381         __le32  s_r_blocks_count_hi;    /* Reserved blocks count high 32 bits*/
382         __le32  s_free_blocks_hi;       /* Free blocks count high 32 bits */
383         __le16  s_min_extra_isize;      /* All inodes have at least # bytes */
384         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
385 -       __le32  s_flags;                /* Miscellaneous flags */
386 -       __u32   s_reserved[167];        /* Padding to the end of the block */
387 +/*160*/ __le32 s_flags;                /* Miscellaneous flags */
388 +       __le16  s_raid_stride;          /* RAID stride */
389 +       __le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
390 +       __le64  s_mmp_block;            /* Block for multi-mount protection */
391 +/*170*/ __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
392 +       __le32  s_reserved[163];        /* Padding to the end of the block */
393  };
394  
395  #ifdef __KERNEL__
396 @@ -699,12 +703,14 @@ static inline int ext3_valid_inum(struct
397  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
398  #define EXT3_FEATURE_INCOMPAT_META_BG          0x0010
399  #define EXT3_FEATURE_INCOMPAT_EXTENTS          0x0040 /* extents support */
400 +#define EXT3_FEATURE_INCOMPAT_MMP              0x0100
401  
402  #define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
403  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
404                                          EXT3_FEATURE_INCOMPAT_RECOVER| \
405                                          EXT3_FEATURE_INCOMPAT_META_BG| \
406 -                                        EXT3_FEATURE_INCOMPAT_EXTENTS)
407 +                                        EXT3_FEATURE_INCOMPAT_EXTENTS| \
408 +                                        EXT3_FEATURE_INCOMPAT_MMP)
409  #define EXT3_FEATURE_RO_COMPAT_SUPP    (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
410                                          EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
411                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
412 @@ -864,6 +870,39 @@ struct dir_private_info {
413  #define ERR_BAD_DX_DIR -75000
414  
415  /*
416 + * This structure will be used for multiple mount protection. It will be
417 + * written into the block number saved in the s_mmp_block field in the
418 + * superblock. Programs that check MMP should assume that if
419 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
420 + * to use the filesystem, regardless of how old the timestamp is.
421 + */
422 +#define EXT3_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
423 +#define EXT3_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
424 +#define EXT3_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
425 +#define EXT3_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
426 +
427 +struct mmp_struct {
428 +       __le32  mmp_magic;
429 +       __le32  mmp_seq;
430 +       __le64  mmp_time;
431 +       char    mmp_nodename[64];
432 +       char    mmp_bdevname[32];
433 +       __le16  mmp_check_interval;
434 +       __le16  mmp_pad1;
435 +       __le32  mmp_pad2[227];
436 +};
437 +
438 +/*
439 + * Default interval in seconds to update the MMP sequence number.
440 + */
441 +#define EXT3_MMP_UPDATE_INTERVAL   1
442 +
443 +/*
444 + * Minimum interval for MMP checking in seconds.
445 + */
446 +#define EXT3_MMP_MIN_CHECK_INTERVAL    5
447 +
448 +/*
449   * Function prototypes
450   */
451  
452 Index: linux-2.6.16.46-0.14/include/linux/ext3_fs_sb.h
453 ===================================================================
454 --- linux-2.6.16.46-0.14.orig/include/linux/ext3_fs_sb.h
455 +++ linux-2.6.16.46-0.14/include/linux/ext3_fs_sb.h
456 @@ -140,6 +140,7 @@ struct ext3_sb_info {
457         /* locality groups */
458         struct ext3_locality_group *s_locality_groups;
459  
460 +       struct task_struct *s_mmp_tsk;  /* Kernel thread for multiple mount protection */
461  };
462  
463  #define EXT3_GROUP_INFO(sb, group)                                        \