Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-mmp-2.6-rhel4.patch
1 Index: linux-2.6.9/fs/ext3/super.c
2 ===================================================================
3 --- linux-2.6.9.orig/fs/ext3/super.c
4 +++ linux-2.6.9/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,9 @@ 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 +
21         remove_proc_entry(sb->s_id, proc_root_ext3);
22         sbi->s_dev_proc = NULL;
23         sb->s_fs_info = NULL;
24 @@ -1439,6 +1444,314 @@ 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 +                       set_current_state(TASK_INTERRUPTIBLE);
164 +                       schedule_timeout(EXT3_MMP_UPDATE_INTERVAL * HZ - diff);
165 +               }
166 +
167 +               /*
168 +                * We need to make sure that more than mmp_check_interval
169 +                * seconds have not passed since writing. If that has happened
170 +                * we need to check if the MMP block is as we left it.
171 +                */
172 +               diff = jiffies - last_update_time;
173 +               if (diff > mmp_check_interval * HZ) {
174 +                       struct buffer_head *bh_check = NULL;
175 +                       struct mmp_struct *mmp_check;
176 +
177 +                       retval = read_mmp_block(sb, &bh_check, mmp_block);
178 +                       if (retval) {
179 +                               EXT3_SB(sb)->s_mmp_tsk = 0;
180 +                               goto failed;
181 +                       }
182 +
183 +                       mmp_check = (struct mmp_struct *)(bh_check->b_data);
184 +                       if (mmp->mmp_time != mmp_check->mmp_time ||
185 +                           memcmp(mmp->mmp_nodename, mmp_check->mmp_nodename,
186 +                                  sizeof(mmp->mmp_nodename)))
187 +                               dump_mmp_msg(sb, mmp_check, __FUNCTION__,
188 +                                            "Error while updating MMP info. "
189 +                                            "The filesystem seems to have "
190 +                                            "been multiply mounted.");
191 +
192 +                       put_bh(bh_check);
193 +               }
194 +
195 +               /*
196 +                * Adjust the mmp_check_interval depending on how much time
197 +                * it took for the MMP block to be written.
198 +                */
199 +               mmp_check_interval = max(5 * diff / HZ,
200 +                                (unsigned long) EXT3_MMP_MIN_CHECK_INTERVAL);
201 +               mmp->mmp_check_interval = cpu_to_le16(mmp_check_interval);
202 +       }
203 +
204 +       /*
205 +        * Unmount seems to be clean.
206 +        */
207 +       mmp->mmp_seq = cpu_to_le32(EXT3_MMP_SEQ_CLEAN);
208 +       mmp->mmp_time = cpu_to_le64(get_seconds());
209 +
210 +       retval = write_mmp_block(bh);
211 +
212 +failed:
213 +       brelse(bh);
214 +       return retval;
215 +}
216 +
217 +/*
218 + * Get a random new sequence number but make sure it is not greater than
219 + * EXT3_MMP_SEQ_MAX.
220 + */
221 +static unsigned int mmp_new_seq(void)
222 +{
223 +       u32 new_seq;
224 +
225 +       do {
226 +               get_random_bytes(&new_seq, sizeof(u32));
227 +       } while (new_seq > EXT3_MMP_SEQ_MAX);
228 +
229 +       return new_seq;
230 +}
231 +
232 +/*
233 + * Protect the filesystem from being mounted more than once.
234 + */
235 +static int ext3_multi_mount_protect(struct super_block *sb,
236 +                                   unsigned long mmp_block)
237 +{
238 +       struct ext3_super_block *es = EXT3_SB(sb)->s_es;
239 +       struct buffer_head *bh = NULL;
240 +       struct mmp_struct *mmp = NULL;
241 +       u32 seq;
242 +       unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval);
243 +       int retval;
244 +
245 +       if (mmp_block < le32_to_cpu(es->s_first_data_block) ||
246 +           mmp_block >= le32_to_cpu(es->s_blocks_count)) {
247 +               ext3_warning(sb, __FUNCTION__,
248 +                            "Invalid MMP block in superblock");
249 +               goto failed;
250 +       }
251 +
252 +       retval = read_mmp_block(sb, &bh, mmp_block);
253 +       if (retval)
254 +               goto failed;
255 +
256 +       mmp = (struct mmp_struct *)(bh->b_data);
257 +
258 +       if (mmp_check_interval < EXT3_MMP_MIN_CHECK_INTERVAL)
259 +               mmp_check_interval = EXT3_MMP_MIN_CHECK_INTERVAL;
260 +
261 +       /*
262 +        * If check_interval in MMP block is larger, use that instead of
263 +        * update_interval from the superblock.
264 +        */
265 +       if (mmp->mmp_check_interval > mmp_check_interval)
266 +               mmp_check_interval = mmp->mmp_check_interval;
267 +
268 +       seq = le32_to_cpu(mmp->mmp_seq);
269 +       if (seq == EXT3_MMP_SEQ_CLEAN)
270 +               goto skip;
271 +
272 +       if (seq == EXT3_MMP_SEQ_FSCK) {
273 +               dump_mmp_msg(sb, mmp, __FUNCTION__,
274 +                            "fsck is running on the filesystem");
275 +               goto failed;
276 +       }
277 +
278 +       schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
279 +
280 +       retval = read_mmp_block(sb, &bh, mmp_block);
281 +       if (retval)
282 +               goto failed;
283 +       mmp = (struct mmp_struct *)(bh->b_data);
284 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
285 +               dump_mmp_msg(sb, mmp, __FUNCTION__,
286 +                            "Device is already active on another node.");
287 +               goto failed;
288 +       }
289 +
290 +skip:
291 +       /*
292 +        * write a new random sequence number.
293 +        */
294 +       mmp->mmp_seq = seq = cpu_to_le32(mmp_new_seq());
295 +
296 +       retval = write_mmp_block(bh);
297 +       if (retval)
298 +               goto failed;
299 +
300 +       /*
301 +        * wait for MMP interval and check mmp_seq.
302 +        */
303 +       schedule_timeout_uninterruptible(HZ * (2 * mmp_check_interval + 1));
304 +
305 +       retval = read_mmp_block(sb, &bh, mmp_block);
306 +       if (retval)
307 +               goto failed;
308 +       mmp = (struct mmp_struct *)(bh->b_data);
309 +       if (seq != le32_to_cpu(mmp->mmp_seq)) {
310 +               dump_mmp_msg(sb, mmp, __FUNCTION__,
311 +                            "Device is already active on another node.");
312 +               goto failed;
313 +       }
314 +
315 +       /*
316 +        * Start a kernel thread to update the MMP block periodically.
317 +        */
318 +       EXT3_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%02x:%02x",
319 +                                            MAJOR(sb->s_dev),
320 +                                            MINOR(sb->s_dev));
321 +       if (IS_ERR(EXT3_SB(sb)->s_mmp_tsk)) {
322 +               EXT3_SB(sb)->s_mmp_tsk = 0;
323 +               ext3_warning(sb, __FUNCTION__, "Unable to create kmmpd thread "
324 +                            "for %s.", sb->s_id);
325 +               goto failed;
326 +       }
327 +
328 +       brelse(bh);
329 +       return 0;
330 +
331 +failed:
332 +       brelse(bh);
333 +       return 1;
334 +}
335 +
336  
337  static int ext3_fill_super (struct super_block *sb, void *data, int silent)
338  {
339 @@ -1738,6 +2051,11 @@ static int ext3_fill_super (struct super
340                           EXT3_HAS_INCOMPAT_FEATURE(sb,
341                                     EXT3_FEATURE_INCOMPAT_RECOVER));
342  
343 +       if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MMP) &&
344 +           !(sb->s_flags & MS_RDONLY))
345 +               if (ext3_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
346 +                       goto failed_mount3;
347 +
348         /*
349          * The first inode we look at is the journal inode.  Don't try
350          * root first: it may be modified in the journal!
351 @@ -2371,6 +2689,11 @@ int ext3_remount (struct super_block * s
352                                 return ret;
353                         if (!ext3_setup_super (sb, es, 0))
354                                 sb->s_flags &= ~MS_RDONLY;
355 +                       if (EXT3_HAS_INCOMPAT_FEATURE(sb,
356 +                                                   EXT3_FEATURE_INCOMPAT_MMP))
357 +                               if (ext3_multi_mount_protect(sb,
358 +                                               le64_to_cpu(es->s_mmp_block)))
359 +                                       return -EROFS;
360                 }
361         }
362         return 0;
363 Index: linux-2.6.9/include/linux/ext3_fs.h
364 ===================================================================
365 --- linux-2.6.9.orig/include/linux/ext3_fs.h
366 +++ linux-2.6.9/include/linux/ext3_fs.h
367 @@ -577,13 +577,17 @@ struct ext3_super_block {
368         __le32  s_first_meta_bg;        /* First metablock block group */
369         __le32  s_mkfs_time;            /* When the filesystem was created */
370         __le32  s_jnl_blocks[17];       /* Backup of the journal inode */
371 -       __le32  s_blocks_count_hi;      /* Blocks count high 32 bits */
372 +/*150*/ __le32 s_blocks_count_hi;      /* Blocks count high 32 bits */
373         __le32  s_r_blocks_count_hi;    /* Reserved blocks count high 32 bits*/
374         __le32  s_free_blocks_hi;       /* Free blocks count high 32 bits */
375         __le16  s_min_extra_isize;      /* All inodes have at least # bytes */
376         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
377 -       __le32  s_flags;                /* Miscellaneous flags */
378 -       __u32   s_reserved[167];        /* Padding to the end of the block */
379 +/*160*/ __le32 s_flags;                /* Miscellaneous flags */
380 +       __le16  s_raid_stride;          /* RAID stride */
381 +       __le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
382 +       __le64  s_mmp_block;            /* Block for multi-mount protection */
383 +/*170*/ __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
384 +       __le32  s_reserved[163];        /* Padding to the end of the block */
385  };
386  
387  #ifdef __KERNEL__
388 @@ -667,12 +671,14 @@ static inline struct ext3_inode_info *EX
389  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
390  #define EXT3_FEATURE_INCOMPAT_META_BG          0x0010
391  #define EXT3_FEATURE_INCOMPAT_EXTENTS          0x0040 /* extents support */
392 +#define EXT3_FEATURE_INCOMPAT_MMP              0x0100
393  
394  #define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
395  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
396                                          EXT3_FEATURE_INCOMPAT_RECOVER| \
397                                          EXT3_FEATURE_INCOMPAT_META_BG| \
398 -                                        EXT3_FEATURE_INCOMPAT_EXTENTS)
399 +                                        EXT3_FEATURE_INCOMPAT_EXTENTS| \
400 +                                        EXT3_FEATURE_INCOMPAT_MMP)
401  #define EXT3_FEATURE_RO_COMPAT_SUPP    (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
402                                          EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
403                                          EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
404 @@ -832,6 +838,39 @@ struct dir_private_info {
405  #define ERR_BAD_DX_DIR -75000
406  
407  /*
408 + * This structure will be used for multiple mount protection. It will be
409 + * written into the block number saved in the s_mmp_block field in the
410 + * superblock. Programs that check MMP should assume that if
411 + * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
412 + * to use the filesystem, regardless of how old the timestamp is.
413 + */
414 +#define EXT3_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
415 +#define EXT3_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
416 +#define EXT3_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
417 +#define EXT3_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
418 +
419 +struct mmp_struct {
420 +       __le32  mmp_magic;
421 +       __le32  mmp_seq;
422 +       __le64  mmp_time;
423 +       char    mmp_nodename[64];
424 +       char    mmp_bdevname[32];
425 +       __le16  mmp_check_interval;
426 +       __le16  mmp_pad1;
427 +       __le32  mmp_pad2[227];
428 +};
429 +
430 +/*
431 + * Default interval in seconds to update the MMP sequence number.
432 + */
433 +#define EXT3_MMP_UPDATE_INTERVAL   1
434 +
435 +/*
436 + * Minimum interval for MMP checking in seconds.
437 + */
438 +#define EXT3_MMP_MIN_CHECK_INTERVAL    5
439 +
440 +/*
441   * Function prototypes
442   */
443  
444 Index: linux-2.6.9/include/linux/ext3_fs_sb.h
445 ===================================================================
446 --- linux-2.6.9.orig/include/linux/ext3_fs_sb.h
447 +++ linux-2.6.9/include/linux/ext3_fs_sb.h
448 @@ -143,6 +143,7 @@ struct ext3_sb_info {
449         /* locality groups */
450         struct ext3_locality_group *s_locality_groups;
451  
452 +       struct task_struct *s_mmp_tsk;  /* Kernel thread for multiple mount protection */
453  };
454  
455  #define EXT3_GROUP_INFO(sb, group)                                        \