Whamcloud - gitweb
be2723cd24d5c16036e8cfab780dd06efb42f30e
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-delete_thread-2.4.20.patch
1 diff -puNr origin/fs/ext3/super.c linux/fs/ext3/super.c
2 --- origin/fs/ext3/super.c      2003-05-04 17:23:52.000000000 +0400
3 +++ linux/fs/ext3/super.c       2003-05-04 17:09:20.000000000 +0400
4 @@ -398,6 +398,210 @@ static void dump_orphan_list(struct supe
5         }
6  }
7  
8 +#ifdef EXT3_DELETE_THREAD
9 +/*
10 + * Delete inodes in a loop until there are no more to be deleted.
11 + * Normally, we run in the background doing the deletes and sleeping again,
12 + * and clients just add new inodes to be deleted onto the end of the list.
13 + * If someone is concerned about free space (e.g. block allocation or similar)
14 + * then they can sleep on s_delete_waiter_queue and be woken up when space
15 + * has been freed.
16 + */
17 +int ext3_delete_thread(void *data)
18 +{
19 +       struct super_block *sb = data;
20 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
21 +       struct task_struct *tsk = current;
22 +
23 +       /* Almost like daemonize, but not quite */
24 +       exit_mm(current);
25 +       tsk->session = 1;
26 +       tsk->pgrp = 1;
27 +       tsk->tty = NULL;
28 +       exit_files(current);
29 +       reparent_to_init();
30 +
31 +       sprintf(tsk->comm, "kdelext3-%s", kdevname(sb->s_dev));
32 +       sigfillset(&tsk->blocked);
33 +
34 +       /*tsk->flags |= PF_KERNTHREAD;*/
35 +
36 +       INIT_LIST_HEAD(&sbi->s_delete_list);
37 +       wake_up(&sbi->s_delete_waiter_queue);
38 +       ext3_debug("EXT3-fs: delete thread on %s started\n",
39 +              kdevname(sb->s_dev));
40 +
41 +       /* main loop */
42 +       for (;;) {
43 +               sleep_on(&sbi->s_delete_thread_queue);
44 +               ext3_debug("%s woken up: %lu inodes, %lu blocks\n",
45 +                          tsk->comm,sbi->s_delete_inodes,sbi->s_delete_blocks);
46 +
47 +               spin_lock(&sbi->s_delete_lock);
48 +               if (list_empty(&sbi->s_delete_list)) {
49 +                       memset(&sbi->s_delete_list, 0,
50 +                              sizeof(sbi->s_delete_list));
51 +                       spin_unlock(&sbi->s_delete_lock);
52 +                       ext3_debug("ext3 delete thread on %s exiting\n",
53 +                              kdevname(sb->s_dev));
54 +                       wake_up(&sbi->s_delete_waiter_queue);
55 +                       break;
56 +               }
57 +
58 +               while (!list_empty(&sbi->s_delete_list)) {
59 +                       struct inode *inode=list_entry(sbi->s_delete_list.next,
60 +                                                      struct inode, i_dentry);
61 +                       unsigned long blocks = inode->i_blocks >>
62 +                                                       (inode->i_blkbits - 9);
63 +
64 +                       list_del_init(&inode->i_dentry);
65 +                       spin_unlock(&sbi->s_delete_lock);
66 +                       ext3_debug("%s delete ino %lu blk %lu\n",
67 +                                  tsk->comm, inode->i_ino, blocks);
68 +
69 +                       iput(inode);
70 +
71 +                       spin_lock(&sbi->s_delete_lock);
72 +                       sbi->s_delete_blocks -= blocks;
73 +                       sbi->s_delete_inodes--;
74 +               }
75 +               if (sbi->s_delete_blocks != 0 || sbi->s_delete_inodes != 0)
76 +                       ext3_warning(sb, __FUNCTION__,
77 +                                    "%lu blocks, %lu inodes on list?\n",
78 +                                    sbi->s_delete_blocks,sbi->s_delete_inodes);
79 +               sbi->s_delete_blocks = 0;
80 +               sbi->s_delete_inodes = 0;
81 +               spin_unlock(&sbi->s_delete_lock);
82 +               wake_up(&sbi->s_delete_waiter_queue);
83 +       }
84 +
85 +       return 0;
86 +}
87 +
88 +static void ext3_start_delete_thread(struct super_block *sb)
89 +{
90 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
91 +       int rc;
92 +
93 +       spin_lock_init(&sbi->s_delete_lock);
94 +       memset(&sbi->s_delete_list, 0, sizeof(sbi->s_delete_list));
95 +       init_waitqueue_head(&sbi->s_delete_thread_queue);
96 +       init_waitqueue_head(&sbi->s_delete_waiter_queue);
97 +       sbi->s_delete_blocks = 0;
98 +       sbi->s_delete_inodes = 0;
99 +
100 +       rc = kernel_thread(ext3_delete_thread, sb, CLONE_VM | CLONE_FILES);
101 +       if (rc < 0)
102 +               printk(KERN_ERR "EXT3-fs: cannot start delete thread: rc %d\n",
103 +                      rc);
104 +       else
105 +               wait_event(sbi->s_delete_waiter_queue, sbi->s_delete_list.next);
106 +}
107 +
108 +static void ext3_stop_delete_thread(struct ext3_sb_info *sbi)
109 +{
110 +       wake_up(&sbi->s_delete_thread_queue);
111 +       wait_event(sbi->s_delete_waiter_queue, list_empty(&sbi->s_delete_list));
112 +}
113 +
114 +/* Instead of playing games with the inode flags, destruction, etc we just
115 + * create a new inode locally and put it on a list for the truncate thread.
116 + * We need large parts of the inode struct in order to complete the
117 + * truncate and unlink, so we may as well just have a real inode to do it.
118 + *
119 + * If we have any problem deferring the delete, just delete it right away.
120 + * If we defer it, we also mark how many blocks it would free, so that we
121 + * can keep the statfs data correct, and we know if we should sleep on the
122 + * truncate thread when we run out of space.
123 + *
124 + * In 2.5 this can be done much more cleanly by just registering a "drop"
125 + * method in the super_operations struct.
126 + */
127 +static void ext3_delete_inode_thread(struct inode *old_inode)
128 +{
129 +       struct ext3_sb_info *sbi = EXT3_SB(old_inode->i_sb);
130 +       struct inode *new_inode;
131 +       unsigned long blocks = old_inode->i_blocks >> (old_inode->i_blkbits-9);
132 +
133 +       if (is_bad_inode(old_inode)) {
134 +               clear_inode(old_inode);
135 +               return;
136 +       }
137 +       
138 +       if (!test_opt (old_inode->i_sb, ASYNCDEL)) {
139 +               ext3_delete_inode(old_inode);
140 +               return;
141 +       }
142 +
143 +       /* We may want to delete the inode immediately and not defer it */
144 +       if (IS_SYNC(old_inode) || blocks <= EXT3_NDIR_BLOCKS ||
145 +           !sbi->s_delete_list.next) {
146 +               ext3_delete_inode(old_inode);
147 +               return;
148 +       }
149 +
150 +       if (EXT3_I(old_inode)->i_state & EXT3_STATE_DELETE) {
151 +               ext3_debug("doing deferred inode %lu delete (%lu blocks)\n",
152 +                          old_inode->i_ino, blocks);
153 +               ext3_delete_inode(old_inode);
154 +               return;
155 +       }
156 +
157 +       /* We can iget this inode again here, because our caller has unhashed
158 +        * old_inode, so new_inode will be in a different inode struct.
159 +        *
160 +        * We need to ensure that the i_orphan pointers in the other inodes
161 +        * point at the new inode copy instead of the old one so the orphan
162 +        * list doesn't get corrupted when the old orphan inode is freed.
163 +        */
164 +       down(&sbi->s_orphan_lock);
165 +
166 +       EXT3_SB(old_inode->i_sb)->s_mount_state |= EXT3_ORPHAN_FS;
167 +       new_inode = iget(old_inode->i_sb, old_inode->i_ino);
168 +       EXT3_SB(old_inode->i_sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
169 +       if (is_bad_inode(new_inode)) {
170 +               printk(KERN_WARNING "read bad inode %lu\n", old_inode->i_ino);
171 +               iput(new_inode);
172 +               new_inode = NULL;
173 +       }
174 +       if (!new_inode) {
175 +               up(&sbi->s_orphan_lock);
176 +               ext3_debug(KERN_DEBUG "delete inode %lu directly (bad read)\n",
177 +                          old_inode->i_ino);
178 +               ext3_delete_inode(old_inode);
179 +               return;
180 +       }
181 +       J_ASSERT(new_inode != old_inode);
182 +
183 +       J_ASSERT(!list_empty(&EXT3_I(old_inode)->i_orphan));
184 +       /* Ugh.  We need to insert new_inode into the same spot on the list
185 +        * as old_inode was, to ensure the in-memory orphan list is still
186 +        * in the same order as the on-disk orphan list (badness otherwise).
187 +        */
188 +       EXT3_I(new_inode)->i_orphan = EXT3_I(old_inode)->i_orphan;
189 +       EXT3_I(new_inode)->i_orphan.next->prev = &EXT3_I(new_inode)->i_orphan;
190 +       EXT3_I(new_inode)->i_orphan.prev->next = &EXT3_I(new_inode)->i_orphan;
191 +       EXT3_I(new_inode)->i_state |= EXT3_STATE_DELETE;
192 +       up(&sbi->s_orphan_lock);
193 +
194 +       clear_inode(old_inode);
195 +
196 +       ext3_debug("delete inode %lu (%lu blocks) by thread\n",
197 +                  new_inode->i_ino, blocks);
198 +       spin_lock(&sbi->s_delete_lock);
199 +       J_ASSERT(list_empty(&new_inode->i_dentry));
200 +       list_add_tail(&new_inode->i_dentry, &sbi->s_delete_list);
201 +       sbi->s_delete_blocks += blocks;
202 +       sbi->s_delete_inodes++;
203 +       spin_unlock(&sbi->s_delete_lock);
204 +
205 +       wake_up(&sbi->s_delete_thread_queue);
206 +}
207 +#else
208 +#define ext3_start_delete_thread(sbi) do {} while(0)
209 +#define ext3_stop_delete_thread(sbi) do {} while(0)
210 +#endif /* EXT3_DELETE_THREAD */
211 +
212  void ext3_put_super (struct super_block * sb)
213  {
214         struct ext3_sb_info *sbi = EXT3_SB(sb);
215 @@ -405,6 +611,7 @@ void ext3_put_super (struct super_block 
216         kdev_t j_dev = sbi->s_journal->j_dev;
217         int i;
218  
219 +       ext3_stop_delete_thread(sbi);
220         ext3_xattr_put_super(sb);
221         journal_destroy(sbi->s_journal);
222         if (!(sb->s_flags & MS_RDONLY)) {
223 @@ -453,7 +660,11 @@ static struct super_operations ext3_sops
224         write_inode:    ext3_write_inode,       /* BKL not held.  Don't need */
225         dirty_inode:    ext3_dirty_inode,       /* BKL not held.  We take it */
226         put_inode:      ext3_put_inode,         /* BKL not held.  Don't need */
227 +#ifdef EXT3_DELETE_THREAD
228 +       delete_inode:   ext3_delete_inode_thread,/* BKL not held. We take it */
229 +#else
230         delete_inode:   ext3_delete_inode,      /* BKL not held.  We take it */
231 +#endif
232         put_super:      ext3_put_super,         /* BKL held */
233         write_super:    ext3_write_super,       /* BKL held */
234         write_super_lockfs: ext3_write_super_lockfs, /* BKL not held. Take it */
235 @@ -514,6 +725,11 @@ static int parse_options (char * options
236              this_char = strtok (NULL, ",")) {
237                 if ((value = strchr (this_char, '=')) != NULL)
238                         *value++ = 0;
239 +#ifdef EXT3_DELETE_THREAD
240 +               if (!strcmp(this_char, "asyncdel"))
241 +                       set_opt(*mount_options, ASYNCDEL);
242 +               else
243 +#endif
244  #ifdef CONFIG_EXT3_FS_XATTR_USER
245                 if (!strcmp (this_char, "user_xattr"))
246                         set_opt (*mount_options, XATTR_USER);
247 @@ -1220,6 +1436,7 @@ struct super_block * ext3_read_super (st
248         }
249  
250         ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
251 +       ext3_start_delete_thread(sb);
252         /*
253          * akpm: core read_super() calls in here with the superblock locked.
254          * That deadlocks, because orphan cleanup needs to lock the superblock
255 diff -puNr origin/include/linux/ext3_fs.h linux/include/linux/ext3_fs.h
256 --- origin/include/linux/ext3_fs.h      2003-05-04 17:22:49.000000000 +0400
257 +++ linux/include/linux/ext3_fs.h       2003-05-04 15:06:10.000000000 +0400
258 @@ -193,6 +193,7 @@ struct ext3_group_desc
259   */
260  #define EXT3_STATE_JDATA               0x00000001 /* journaled data exists */
261  #define EXT3_STATE_NEW                 0x00000002 /* inode is newly created */
262 +#define EXT3_STATE_DELETE              0x00000010 /* deferred delete inode */
263  
264  /*
265   * ioctl commands
266 @@ -321,6 +322,7 @@ struct ext3_inode {
267  #define EXT3_MOUNT_UPDATE_JOURNAL      0x1000  /* Update the journal format */
268  #define EXT3_MOUNT_NO_UID32            0x2000  /* Disable 32-bit UIDs */
269  #define EXT3_MOUNT_XATTR_USER          0x4000  /* Extended user attributes */
270 +#define EXT3_MOUNT_ASYNCDEL            0x20000 /* Delayed deletion */
271  
272  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
273  #ifndef _LINUX_EXT2_FS_H
274 diff -puNr origin/include/linux/ext3_fs_sb.h linux/include/linux/ext3_fs_sb.h
275 --- origin/include/linux/ext3_fs_sb.h   2003-05-04 17:23:52.000000000 +0400
276 +++ linux/include/linux/ext3_fs_sb.h    2003-05-04 11:37:04.000000000 +0400
277 @@ -29,6 +29,8 @@
278  
279  #define EXT3_MAX_GROUP_LOADED  8
280  
281 +#define EXT3_DELETE_THREAD
282 +
283  /*
284   * third extended-fs super-block data in memory
285   */
286 @@ -76,6 +78,14 @@ struct ext3_sb_info {
287         struct timer_list turn_ro_timer;        /* For turning read-only (crash simulation) */
288         wait_queue_head_t ro_wait_queue;        /* For people waiting for the fs to go read-only */
289  #endif
290 +#ifdef EXT3_DELETE_THREAD
291 +       spinlock_t s_delete_lock;
292 +       struct list_head s_delete_list;
293 +       unsigned long s_delete_blocks;
294 +       unsigned long s_delete_inodes;
295 +       wait_queue_head_t s_delete_thread_queue;
296 +       wait_queue_head_t s_delete_waiter_queue;
297 +#endif
298  };
299  
300  #endif /* _LINUX_EXT3_FS_SB */