Whamcloud - gitweb
- initial release of reworked ext3-delete-thread for 2.4.24. the idea
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-delete_thread-2.4.24.patch
1  fs/ext3/file.c             |    4 
2  fs/ext3/inode.c            |  116 ++++++++++++++++++++++
3  fs/ext3/super.c            |  230 +++++++++++++++++++++++++++++++++++++++++++++
4  include/linux/ext3_fs.h    |    5 
5  include/linux/ext3_fs_sb.h |   10 +
6  5 files changed, 365 insertions(+)
7
8 Index: linux-2.4.24-vanilla/fs/ext3/super.c
9 ===================================================================
10 --- linux-2.4.24-vanilla.orig/fs/ext3/super.c   2004-01-10 17:49:10.000000000 +0300
11 +++ linux-2.4.24-vanilla/fs/ext3/super.c        2004-01-11 22:30:24.000000000 +0300
12 @@ -400,6 +400,126 @@
13         }
14  }
15  
16 +#ifdef EXT3_DELETE_THREAD
17 +/*
18 + * Delete inodes in a loop until there are no more to be deleted.
19 + * Normally, we run in the background doing the deletes and sleeping again,
20 + * and clients just add new inodes to be deleted onto the end of the list.
21 + * If someone is concerned about free space (e.g. block allocation or similar)
22 + * then they can sleep on s_delete_waiter_queue and be woken up when space
23 + * has been freed.
24 + */
25 +int ext3_delete_thread(void *data)
26 +{
27 +       struct super_block *sb = data;
28 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
29 +       struct task_struct *tsk = current;
30 +       struct inode *inode;
31 +       unsigned long blocks;
32 +
33 +       /* Almost like daemonize, but not quite */
34 +       exit_mm(current);
35 +       tsk->session = 1;
36 +       tsk->pgrp = 1;
37 +       tsk->tty = NULL;
38 +       exit_files(current);
39 +       reparent_to_init();
40 +
41 +       sprintf(tsk->comm, "kdelext3-%s", kdevname(sb->s_dev));
42 +       sigfillset(&tsk->blocked);
43 +
44 +       /*tsk->flags |= PF_KERNTHREAD;*/
45 +
46 +       INIT_LIST_HEAD(&sbi->s_delete_list);
47 +       wake_up(&sbi->s_delete_waiter_queue);
48 +       ext3_debug("delete thread on %s started\n", kdevname(sb->s_dev));
49 +
50 +       /* main loop */
51 +       for (;;) {
52 +               wait_event_interruptible(sbi->s_delete_thread_queue,
53 +                                        !list_empty(&sbi->s_delete_list) ||
54 +                                        !test_opt(sb, ASYNCDEL));
55 +               ext3_debug("%s woken up: %lu inodes, %lu blocks\n",
56 +                          tsk->comm,sbi->s_delete_inodes,sbi->s_delete_blocks);
57 +
58 +               spin_lock(&sbi->s_delete_lock);
59 +               if (list_empty(&sbi->s_delete_list)) {
60 +                       clear_opt(sbi->s_mount_opt, ASYNCDEL);
61 +                       memset(&sbi->s_delete_list, 0,
62 +                              sizeof(sbi->s_delete_list));
63 +                       spin_unlock(&sbi->s_delete_lock);
64 +                       ext3_debug("delete thread on %s exiting\n",
65 +                                  kdevname(sb->s_dev));
66 +                       wake_up(&sbi->s_delete_waiter_queue);
67 +                       break;
68 +               }
69 +
70 +               while (!list_empty(&sbi->s_delete_list)) {
71 +                       inode = list_entry(sbi->s_delete_list.next,
72 +                                              struct inode, i_devices);
73 +                       blocks = inode->i_blocks >> (inode->i_blkbits - 9);
74 +
75 +                       list_del_init(&inode->i_devices);
76 +                       spin_unlock(&sbi->s_delete_lock);
77 +                       ext3_debug("%s delete ino %lu blk %lu\n",
78 +                                  tsk->comm, inode->i_ino, blocks);
79 +
80 +                       J_ASSERT(inode->i_nlink == 1);
81 +                       inode->i_nlink = 0;
82 +                       iput(inode);
83 +
84 +                       spin_lock(&sbi->s_delete_lock);
85 +                       sbi->s_delete_blocks -= blocks;
86 +                       sbi->s_delete_inodes--;
87 +               }
88 +               if (sbi->s_delete_blocks != 0 || sbi->s_delete_inodes != 0) {
89 +                       ext3_warning(sb, __FUNCTION__,
90 +                                    "%lu blocks, %lu inodes on list?\n",
91 +                                    sbi->s_delete_blocks,sbi->s_delete_inodes);
92 +                       sbi->s_delete_blocks = 0;
93 +                       sbi->s_delete_inodes = 0;
94 +               }
95 +               spin_unlock(&sbi->s_delete_lock);
96 +               wake_up(&sbi->s_delete_waiter_queue);
97 +       }
98 +
99 +       return 0;
100 +}
101 +
102 +static void ext3_start_delete_thread(struct super_block *sb)
103 +{
104 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
105 +       int rc;
106 +
107 +       spin_lock_init(&sbi->s_delete_lock);
108 +       init_waitqueue_head(&sbi->s_delete_thread_queue);
109 +       init_waitqueue_head(&sbi->s_delete_waiter_queue);
110 +
111 +       if (!test_opt(sb, ASYNCDEL))
112 +               return;
113 +
114 +       rc = kernel_thread(ext3_delete_thread, sb, CLONE_VM | CLONE_FILES);
115 +       if (rc < 0)
116 +               printk(KERN_ERR "EXT3-fs: cannot start delete thread: rc %d\n",
117 +                      rc);
118 +       else
119 +               wait_event(sbi->s_delete_waiter_queue, sbi->s_delete_list.next);
120 +}
121 +
122 +static void ext3_stop_delete_thread(struct ext3_sb_info *sbi)
123 +{
124 +       if (sbi->s_delete_list.next == 0)       /* thread never started */
125 +               return;
126 +
127 +       clear_opt(sbi->s_mount_opt, ASYNCDEL);
128 +       wake_up(&sbi->s_delete_thread_queue);
129 +       wait_event(sbi->s_delete_waiter_queue, sbi->s_delete_list.next == 0);
130 +}
131 +#else
132 +#define ext3_start_delete_thread(sbi) do {} while(0)
133 +#define ext3_stop_delete_thread(sbi) do {} while(0)
134 +#endif /* EXT3_DELETE_THREAD */
135 +
136  void ext3_put_super (struct super_block * sb)
137  {
138         struct ext3_sb_info *sbi = EXT3_SB(sb);
139 @@ -527,6 +647,13 @@
140                         clear_opt (*mount_options, XATTR_USER);
141                 else
142  #endif
143 +#ifdef EXT3_DELETE_THREAD
144 +               if (!strcmp(this_char, "asyncdel"))
145 +                       set_opt(*mount_options, ASYNCDEL);
146 +               else if (!strcmp(this_char, "noasyncdel"))
147 +                       clear_opt(*mount_options, ASYNCDEL);
148 +               else
149 +#endif
150                 if (!strcmp (this_char, "bsddf"))
151                         clear_opt (*mount_options, MINIX_DF);
152                 else if (!strcmp (this_char, "nouid32")) {
153 @@ -1227,6 +1354,7 @@
154         }
155  
156         ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
157 +       ext3_start_delete_thread(sb);
158         /*
159          * akpm: core read_super() calls in here with the superblock locked.
160          * That deadlocks, because orphan cleanup needs to lock the superblock
161 @@ -1618,7 +1746,12 @@
162  static int ext3_sync_fs(struct super_block *sb)
163  {
164         tid_t target;
165 -       
166 +
167 +       if (atomic_read(&sb->s_active) == 0) {
168 +               /* fs is being umounted: time to stop delete thread */
169 +               ext3_stop_delete_thread(EXT3_SB(sb));
170 +       }
171 +
172         sb->s_dirt = 0;
173         target = log_start_commit(EXT3_SB(sb)->s_journal, NULL);
174         log_wait_commit(EXT3_SB(sb)->s_journal, target);
175 @@ -1682,6 +1815,9 @@
176         if (!parse_options(data, &tmp, sbi, &tmp, 1))
177                 return -EINVAL;
178  
179 +       if (!test_opt(sb, ASYNCDEL) || (*flags & MS_RDONLY))
180 +               ext3_stop_delete_thread(sbi);
181 +
182         if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
183                 ext3_abort(sb, __FUNCTION__, "Abort forced by user");
184  
185 Index: linux-2.4.24-vanilla/fs/ext3/inode.c
186 ===================================================================
187 --- linux-2.4.24-vanilla.orig/fs/ext3/inode.c   2004-01-10 17:49:11.000000000 +0300
188 +++ linux-2.4.24-vanilla/fs/ext3/inode.c        2004-01-11 22:26:24.000000000 +0300
189 @@ -2551,6 +2551,118 @@
190         return err;
191  }
192  
193 +#ifdef EXT3_DELETE_THREAD
194 +/* Move blocks from to-be-truncated inode over to a new inode, and delete
195 + * that one from the delete thread instead.  This avoids a lot of latency
196 + * when truncating large files.
197 + *
198 + * If we have any problem deferring the truncate, just truncate it right away.
199 + * If we defer it, we also mark how many blocks it would free, so that we
200 + * can keep the statfs data correct, and we know if we should sleep on the
201 + * delete thread when we run out of space.
202 + */
203 +void ext3_truncate_thread(struct inode *old_inode)
204 +{
205 +       struct ext3_sb_info *sbi = EXT3_SB(old_inode->i_sb);
206 +       struct ext3_inode_info *nei, *oei = EXT3_I(old_inode);
207 +       struct inode *new_inode;
208 +       handle_t *handle;
209 +       unsigned long blocks = old_inode->i_blocks >> (old_inode->i_blkbits-9);
210 +
211 +       if (!test_opt(old_inode->i_sb, ASYNCDEL) || !sbi->s_delete_list.next)
212 +               goto out_truncate;
213 +
214 +       /* XXX This is a temporary limitation for code simplicity.
215 +        *     We could truncate to arbitrary sizes at some later time.
216 +        */
217 +       if (old_inode->i_size != 0)
218 +               goto out_truncate;
219 +
220 +       /* We may want to truncate the inode immediately and not defer it */
221 +       if (IS_SYNC(old_inode) || blocks <= EXT3_NDIR_BLOCKS ||
222 +           old_inode->i_size > oei->i_disksize)
223 +               goto out_truncate;
224 +
225 +       /* We can't use the delete thread as-is during real orphan recovery,
226 +        * as we add to the orphan list here, causing ext3_orphan_cleanup()
227 +        * to loop endlessly.  It would be nice to do so, but needs work.
228 +        */
229 +       if (oei->i_state & EXT3_STATE_DELETE ||
230 +           sbi->s_mount_state & EXT3_ORPHAN_FS) {
231 +               ext3_debug("doing deferred inode %lu delete (%lu blocks)\n",
232 +                          old_inode->i_ino, blocks);
233 +               goto out_truncate;
234 +       }
235 +
236 +       ext3_discard_prealloc(old_inode);
237 +
238 +       /* old_inode   = 1
239 +        * new_inode   = sb + GDT + ibitmap
240 +        * orphan list = 1 inode/superblock for add, 2 inodes for del
241 +        * quota files = 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
242 +        */
243 +       handle = ext3_journal_start(old_inode, 7);
244 +       if (IS_ERR(handle))
245 +               goto out_truncate;
246 +
247 +       new_inode = ext3_new_inode(handle, old_inode, old_inode->i_mode);
248 +       if (IS_ERR(new_inode)) {
249 +               ext3_debug("truncate inode %lu directly (no new inodes)\n",
250 +                          old_inode->i_ino);
251 +               goto out_journal;
252 +       }
253 +
254 +       nei = EXT3_I(new_inode);
255 +
256 +       down_write(&oei->truncate_sem);
257 +       new_inode->i_size = old_inode->i_size;
258 +       new_inode->i_blocks = old_inode->i_blocks;
259 +       new_inode->i_uid = old_inode->i_uid;
260 +       new_inode->i_gid = old_inode->i_gid;
261 +       new_inode->i_nlink = 0;
262 +
263 +       /* FIXME when we do arbitrary truncates */
264 +       old_inode->i_blocks = oei->i_file_acl ? old_inode->i_blksize / 512 : 0;
265 +       old_inode->i_mtime = old_inode->i_ctime = CURRENT_TIME;
266 +
267 +       memcpy(nei->i_data, oei->i_data, sizeof(nei->i_data));
268 +       memset(oei->i_data, 0, sizeof(oei->i_data));
269 +
270 +       nei->i_disksize = oei->i_disksize;
271 +       nei->i_state |= EXT3_STATE_DELETE;
272 +       up_write(&oei->truncate_sem);
273 +
274 +       if (ext3_orphan_add(handle, new_inode) < 0)
275 +               goto out_journal;
276 +
277 +       if (ext3_orphan_del(handle, old_inode) < 0) {
278 +               ext3_orphan_del(handle, new_inode);
279 +               iput(new_inode);
280 +               goto out_journal;
281 +       }
282 +
283 +       ext3_journal_stop(handle, old_inode);
284 +
285 +       spin_lock(&sbi->s_delete_lock);
286 +       J_ASSERT(list_empty(&new_inode->i_devices));
287 +       list_add_tail(&new_inode->i_devices, &sbi->s_delete_list);
288 +       sbi->s_delete_blocks += blocks;
289 +       sbi->s_delete_inodes++;
290 +       spin_unlock(&sbi->s_delete_lock);
291 +
292 +       ext3_debug("delete inode %lu (%lu blocks) by thread\n",
293 +                  new_inode->i_ino, blocks);
294 +
295 +       wake_up(&sbi->s_delete_thread_queue);
296 +       return;
297 +
298 +out_journal:
299 +       ext3_journal_stop(handle, old_inode);
300 +out_truncate:
301 +       ext3_truncate(old_inode);
302 +}
303 +#endif /* EXT3_DELETE_THREAD */
304 +
305  /* 
306   * On success, We end up with an outstanding reference count against
307   * iloc->bh.  This _must_ be cleaned up later. 
308 Index: linux-2.4.24-vanilla/fs/ext3/file.c
309 ===================================================================
310 --- linux-2.4.24-vanilla.orig/fs/ext3/file.c    2004-01-10 17:49:10.000000000 +0300
311 +++ linux-2.4.24-vanilla/fs/ext3/file.c 2004-01-11 22:26:24.000000000 +0300
312 @@ -126,7 +126,11 @@
313  };
314  
315  struct inode_operations ext3_file_inode_operations = {
316 +#ifdef EXT3_DELETE_THREAD
317 +       truncate:       ext3_truncate_thread,   /* BKL held */
318 +#else
319         truncate:       ext3_truncate,          /* BKL held */
320 +#endif
321         setattr:        ext3_setattr,           /* BKL held */
322         setxattr:       ext3_setxattr,          /* BKL held */
323         getxattr:       ext3_getxattr,          /* BKL held */
324 Index: linux-2.4.24-vanilla/fs/ext3/namei.c
325 ===================================================================
326 --- linux-2.4.24-vanilla.orig/fs/ext3/namei.c   2004-01-10 17:49:10.000000000 +0300
327 +++ linux-2.4.24-vanilla/fs/ext3/namei.c        2004-01-11 22:26:24.000000000 +0300
328 @@ -1936,6 +1936,36 @@
329         return retval;
330  }
331  
332 +static int ext3_try_to_delay_deletion(struct inode *inode)
333 +{
334 +       struct ext3_sb_info *sbi = EXT3_SB(inode->i_sb);
335 +       struct ext3_inode_info *ei = EXT3_I(inode);
336 +       unsigned long blocks;
337 +       
338 +       if (!test_opt(inode->i_sb, ASYNCDEL))
339 +               return 0;
340 +
341 +       /* We may want to delete the inode immediately and not defer it */
342 +       blocks = inode->i_blocks >> (inode->i_blkbits - 9);
343 +       if (IS_SYNC(inode) || blocks <= EXT3_NDIR_BLOCKS)
344 +               return 0;
345 +
346 +       inode->i_nlink = 1;
347 +       atomic_inc(&inode->i_count);
348 +       ei->i_state |= EXT3_STATE_DELETE;
349 +
350 +       spin_lock(&sbi->s_delete_lock);
351 +       J_ASSERT(list_empty(&inode->i_devices));
352 +       list_add_tail(&inode->i_devices, &sbi->s_delete_list);
353 +       sbi->s_delete_blocks += blocks;
354 +       sbi->s_delete_inodes++;
355 +       spin_unlock(&sbi->s_delete_lock);
356 +       
357 +       wake_up(&sbi->s_delete_thread_queue);
358 +
359 +       return 0;
360 +}
361 +
362  static int ext3_unlink(struct inode * dir, struct dentry *dentry)
363  {
364         int retval;
365 @@ -1977,8 +2007,10 @@
366         ext3_update_dx_flag(dir);
367         ext3_mark_inode_dirty(handle, dir);
368         inode->i_nlink--;
369 -       if (!inode->i_nlink)
370 +       if (!inode->i_nlink) {
371 +               ext3_try_to_delay_deletion(inode);
372                 ext3_orphan_add(handle, inode);
373 +       }
374         inode->i_ctime = dir->i_ctime;
375         ext3_mark_inode_dirty(handle, inode);
376         retval = 0;
377 Index: linux-2.4.24-vanilla/include/linux/ext3_fs.h
378 ===================================================================
379 --- linux-2.4.24-vanilla.orig/include/linux/ext3_fs.h   2004-01-10 17:49:11.000000000 +0300
380 +++ linux-2.4.24-vanilla/include/linux/ext3_fs.h        2004-01-11 22:26:24.000000000 +0300
381 @@ -193,6 +193,7 @@
382   */
383  #define EXT3_STATE_JDATA               0x00000001 /* journaled data exists */
384  #define EXT3_STATE_NEW                 0x00000002 /* inode is newly created */
385 +#define EXT3_STATE_DELETE              0x00000010 /* deferred delete inode */
386  
387  /*
388   * ioctl commands
389 @@ -320,6 +321,7 @@
390  #define EXT3_MOUNT_UPDATE_JOURNAL      0x1000  /* Update the journal format */
391  #define EXT3_MOUNT_NO_UID32            0x2000  /* Disable 32-bit UIDs */
392  #define EXT3_MOUNT_XATTR_USER          0x4000  /* Extended user attributes */
393 +#define EXT3_MOUNT_ASYNCDEL            0x20000 /* Delayed deletion */
394  
395  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
396  #ifndef _LINUX_EXT2_FS_H
397 @@ -697,6 +699,9 @@
398  extern void ext3_dirty_inode(struct inode *);
399  extern int ext3_change_inode_journal_flag(struct inode *, int);
400  extern void ext3_truncate (struct inode *);
401 +#ifdef EXT3_DELETE_THREAD
402 +extern void ext3_truncate_thread(struct inode *inode);
403 +#endif
404  extern void ext3_set_inode_flags(struct inode *);
405  
406  /* ioctl.c */
407 Index: linux-2.4.24-vanilla/include/linux/ext3_fs_sb.h
408 ===================================================================
409 --- linux-2.4.24-vanilla.orig/include/linux/ext3_fs_sb.h        2004-01-10 17:49:10.000000000 +0300
410 +++ linux-2.4.24-vanilla/include/linux/ext3_fs_sb.h     2004-01-11 22:28:44.000000000 +0300
411 @@ -29,6 +29,8 @@
412  
413  #define EXT3_MAX_GROUP_LOADED  8
414  
415 +#define EXT3_DELETE_THREAD
416 +
417  /*
418   * third extended-fs super-block data in memory
419   */
420 @@ -76,6 +78,14 @@
421         struct timer_list turn_ro_timer;        /* For turning read-only (crash simulation) */
422         wait_queue_head_t ro_wait_queue;        /* For people waiting for the fs to go read-only */
423  #endif
424 +#ifdef EXT3_DELETE_THREAD
425 +       spinlock_t s_delete_lock;
426 +       struct list_head s_delete_list;
427 +       unsigned long s_delete_blocks;
428 +       unsigned long s_delete_inodes;
429 +       wait_queue_head_t s_delete_thread_queue;
430 +       wait_queue_head_t s_delete_waiter_queue;
431 +#endif
432  };
433  
434  #endif /* _LINUX_EXT3_FS_SB */