Whamcloud - gitweb
af7341e01d41b3165c2902da60e59c8839a3d991
[fs/lustre-release.git] / lustre / obdfs / flushd.c
1 /*
2  * OBDFS Super operations - also used for Lustre file system
3  *
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
7  * Copryright (C) 1999 Seagate Technology Inc.
8  *
9  */
10 #define __NO_VERSION__
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/fs.h>
14 #include <linux/malloc.h>
15 #include <linux/locks.h>
16 #include <linux/errno.h>
17 #include <linux/swap.h>
18 #include <linux/smp_lock.h>
19 #include <linux/vmalloc.h>
20 #include <linux/blkdev.h>
21 #include <linux/sysrq.h>
22 #include <linux/file.h>
23 #include <linux/init.h>
24 #include <linux/quotaops.h>
25 #include <linux/iobuf.h>
26 #include <linux/highmem.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30 #include <asm/bitops.h>
31 #include <asm/mmu_context.h>
32
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obdfs.h>
36
37
38 struct {
39         int nfract;  /* Percentage of buffer cache dirty to 
40                         activate bdflush */
41         int ndirty;  /* Maximum number of dirty blocks to write out per
42                         wake-cycle */
43         int nrefill; /* Number of clean buffers to try to obtain
44                                 each time we call refill */
45         int nref_dirt; /* Dirty buffer threshold for activating bdflush
46                           when trying to refill buffers. */
47         int interval; /* jiffies delay between kupdate flushes */
48         int age_buffer;  /* Time for normal buffer to age before we flush it */
49         int age_super;  /* Time for superblock to age before we flush it */
50 /* } pupd_prm = {40, 500, 64, 256, 5*HZ, 30*HZ, 5*HZ };  */
51 } pupd_prm = {40, 500, 64, 256, 10*HZ, 30*HZ, 5*HZ }; 
52
53 /* Called with the superblock list lock */
54 static int obdfs_enqueue_pages(struct inode *inode, struct obdo **obdo,
55                                int nr_slots, struct page **pages, char **bufs,
56                                obd_size *counts, obd_off *offsets,
57                                obd_flag *flag, int check_time)
58 {
59         struct list_head *page_list = obdfs_iplist(inode);
60         struct list_head *tmp;
61         int num = 0;
62
63         ENTRY;
64         OIDEBUG(inode);
65
66         *obdo = obdo_fromid(IID(inode), inode->i_ino, OBD_MD_FLNOTOBD);
67         if ( IS_ERR(*obdo) ) {
68                 EXIT;
69                 return PTR_ERR(*obdo);
70         }
71
72         obdfs_from_inode(*obdo, inode); /* FIXME revisit fromid & from_inode */
73         *flag = OBD_BRW_CREATE;
74
75         tmp = page_list;
76         while ( ((tmp = tmp->next) != page_list) && (num < nr_slots) ) {
77                 struct obdfs_pgrq *req;
78                 struct page *page;
79                 
80                 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
81                 page = req->rq_page;
82
83                 
84                 if (check_time && 
85                     (jiffies - req->rq_jiffies) < pupd_prm.age_buffer)
86                         continue;
87
88                 /* Remove request from list before write to avoid conflict.
89                  * Note that obdfs_pgrq_del() also deletes the request.
90                  */
91                 obdfs_pgrq_del(req);
92                 if ( !page ) {
93                         CDEBUG(D_INODE, "no page \n");
94                         continue;
95                 }
96
97                 bufs[num] = (char *)page_address(page);
98                 pages[num] = page;
99                 counts[num] = PAGE_SIZE;
100                 offsets[num] = ((obd_off)page->index) << PAGE_SHIFT;
101                 CDEBUG(D_INODE, "ENQ inode %ld, page %p addr %p to vector\n", 
102                        inode->i_ino, page, (char *)page_address(page));
103                 num++;
104         }
105
106         if (!list_empty(page_list))
107                 CDEBUG(D_INODE, "inode %ld list not empty\n", inode->i_ino);
108         CDEBUG(D_INODE, "added %d page(s) to vector\n", num);
109
110         EXIT;
111         return num;  
112 } /* obdfs_enqueue_pages */
113
114 /* dequeue requests for a dying inode */
115 void obdfs_dequeue_reqs(struct inode *inode)
116 {
117
118         struct list_head *tmp;
119
120         obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
121         tmp = obdfs_islist(inode);
122         if ( list_empty(tmp) ) {
123                 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
124                 EXIT;
125                 return;
126         }
127
128         /* take it out of the super list */
129         list_del(tmp);
130         INIT_LIST_HEAD(obdfs_islist(inode));
131
132         tmp = obdfs_iplist(inode);
133         while ( (tmp = tmp->next) != obdfs_iplist(inode) ) {
134                 struct obdfs_pgrq *req;
135                 struct page *page;
136                 
137                 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
138                 page = req->rq_page;
139                 /* take it out of the list and free */
140                 obdfs_pgrq_del(req);
141                 /* now put the page away */
142                 put_page(page);
143         }
144         iput(inode);
145         obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
146 } /* obdfs_dequeue_reqs */
147
148 /* Remove writeback requests for the superblock */
149 int obdfs_flush_reqs(struct list_head *inode_list, int check_time)
150 {
151         struct list_head *tmp;
152         int               total_io = 0;
153         obd_count         num_io = 0;
154         obd_count         num_obdos = 0;
155         struct inode     *inodes[MAX_IOVEC];    /* write data back to these */
156         struct page      *pages[MAX_IOVEC];     /* call put_page on these */
157         struct obdo      *obdos[MAX_IOVEC];
158         char             *bufs[MAX_IOVEC];
159         obd_size          counts[MAX_IOVEC];
160         obd_off           offsets[MAX_IOVEC];
161         obd_flag          flags[MAX_IOVEC];
162         obd_count         bufs_per_obdo[MAX_IOVEC];
163         int               err = 0;
164         struct obdfs_sb_info *sbi;
165
166
167         ENTRY;
168
169         if (!inode_list) {
170                 CDEBUG(D_INODE, "no list\n");
171                 EXIT;
172                 return 0;
173         }
174
175         sbi = list_entry(inode_list, struct obdfs_sb_info, osi_inodes);
176
177         obd_down(&sbi->osi_list_mutex);
178         if ( list_empty(inode_list)) {
179                 CDEBUG(D_INODE, "list empty\n");
180                 obd_up(&sbi->osi_list_mutex);
181                 EXIT;
182                 return 0;
183         }
184
185         /* add each inode's dirty pages to a write vector, and write it */
186  again:
187         tmp = inode_list;
188         while ( (tmp = tmp->next) != inode_list && 
189                 total_io < pupd_prm.ndirty) {
190                 struct obdfs_inode_info *ii;
191                 struct inode *inode;
192                 int res;
193
194                 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
195                 inode = list_entry(ii, struct inode, u);
196                 inodes[num_obdos] = inode;
197                 CDEBUG(D_INODE, "checking inode %ld pages\n", inode->i_ino);
198
199                 res = 1;
200
201                 /* Loop on this inode until we can't get more pages from it
202                  * (either no more pages, or the pages aren't old enough).
203                  * Make sure we reference "inode" and not "inodes[num_obdos]",
204                  * as num_obdos will change after the loop is run.
205                  */
206                 while (!list_empty(obdfs_iplist(inode)) && res &&
207                        total_io < pupd_prm.ndirty ) {
208                         res = obdfs_enqueue_pages(inode, &obdos[num_obdos],
209                                                   MAX_IOVEC - num_io,
210                                                   &pages[num_io], &bufs[num_io],
211                                                   &counts[num_io],
212                                                   &offsets[num_io],
213                                                   &flags[num_obdos],
214                                                   check_time);
215                         CDEBUG(D_INODE, "FLUSHED inode %ld, pages flushed: %d\n", 
216                                inode->i_ino, res);
217                         if ( res < 0 ) {
218                                 obd_up(&sbi->osi_list_mutex);
219                                 err = res;
220                                 goto ERR;
221                         }
222                         
223                         num_io += res;
224                         total_io += res;
225                         bufs_per_obdo[num_obdos] = res;
226                         num_obdos++;
227
228                         if ( num_io == MAX_IOVEC ) {
229                                 obd_up(&sbi->osi_list_mutex);
230                                 err = obdfs_do_vec_wr(inodes, num_io, num_obdos,
231                                                       obdos, bufs_per_obdo,
232                                                       pages, bufs, counts,
233                                                       offsets, flags);
234                                 if ( err ) {
235                                         EXIT;
236                                         goto ERR;
237                                 }
238                                 inodes[0] = inode;
239                                 num_io = 0;
240                                 num_obdos = 0;
241                                 obd_down(&sbi->osi_list_mutex);
242                                 goto again;
243                         }
244                 }
245         }
246
247         obd_up(&sbi->osi_list_mutex);
248
249         /* flush any remaining I/Os */
250         if ( num_io ) {
251                 err = obdfs_do_vec_wr(inodes, num_io, num_obdos, obdos,
252                                       bufs_per_obdo, pages, bufs, counts,
253                                       offsets, flags);
254         }
255
256         /* Remove inode from superblock dirty list when no more pages.
257          * Make sure we don't point at the current inode with tmp
258          * when we re-init the list on the inode, or we will loop.
259          */
260         obd_down(&sbi->osi_list_mutex);
261         tmp = inode_list;
262         while ( (tmp = tmp->next) != inode_list ) {
263                 struct obdfs_inode_info *ii;
264                 struct inode *inode;
265
266                 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
267                 inode = list_entry(ii, struct inode, u);
268                 CDEBUG(D_INODE, "checking inode %ld empty\n", inode->i_ino);
269                 if (list_empty(obdfs_iplist(inode))) {
270                         CDEBUG(D_INODE, "remove inode %ld from dirty list\n",
271                                inode->i_ino);
272                         tmp = tmp->prev;
273                         list_del(obdfs_islist(inode));
274                         iput(inode);
275                         INIT_LIST_HEAD(obdfs_islist(inode));
276                 }
277         }
278         obd_up(&sbi->osi_list_mutex);
279
280         CDEBUG(D_INODE, "flushed %d pages in total\n", total_io);
281         EXIT;
282 ERR:
283         return err;
284 } /* obdfs_remove_pages_from_cache */
285
286
287 void obdfs_flush_dirty_pages(int check_time)
288 {
289         struct list_head *sl;
290
291         ENTRY;
292         sl = &obdfs_super_list;
293         while ( (sl = sl->next) != &obdfs_super_list ) {
294                 struct obdfs_sb_info *sbi = 
295                         list_entry(sl, struct obdfs_sb_info, osi_list);
296
297                 /* walk write requests here, use the sb, check the time */
298                 obdfs_flush_reqs(&sbi->osi_inodes, check_time);
299         }
300         EXIT;
301 }
302
303
304 static struct task_struct *pupdated;
305
306 static int pupdate(void *unused) 
307 {
308         struct task_struct * tsk = current;
309         int interval;
310         
311         pupdated = current;
312
313         exit_files(current);
314         exit_mm(current);
315
316         tsk->session = 1;
317         tsk->pgrp = 1;
318         sprintf(tsk->comm, "pupdated");
319         pupdated = current;
320
321         MOD_INC_USE_COUNT;      /* XXX until send_sig works */
322         printk("pupdated activated...\n");
323
324         /* sigstop and sigcont will stop and wakeup pupdate */
325         spin_lock_irq(&tsk->sigmask_lock);
326         sigfillset(&tsk->blocked);
327         siginitsetinv(&tsk->blocked, sigmask(SIGTERM));
328         recalc_sigpending(tsk);
329         spin_unlock_irq(&tsk->sigmask_lock);
330
331         for (;;) {
332                 /* update interval */
333                 interval = pupd_prm.interval;
334                 if (interval)
335                 {
336                         tsk->state = TASK_INTERRUPTIBLE;
337                         schedule_timeout(interval);
338                 }
339                 else
340                 {
341                 stop_pupdate:
342                         tsk->state = TASK_STOPPED;
343                         MOD_DEC_USE_COUNT; /* XXX until send_sig works */
344                         printk("pupdated stopped...\n");
345                         return 0;
346                 }
347                 /* check for sigstop */
348                 if (signal_pending(tsk))
349                 {
350                         int stopped = 0;
351                         spin_lock_irq(&tsk->sigmask_lock);
352                         if (sigismember(&tsk->signal, SIGTERM))
353                         {
354                                 sigdelset(&tsk->signal, SIGTERM);
355                                 stopped = 1;
356                         }
357                         recalc_sigpending(tsk);
358                         spin_unlock_irq(&tsk->sigmask_lock);
359                         if (stopped)
360                                 goto stop_pupdate;
361                 }
362                 /* asynchronous setattr etc for the future ...
363                 flush_inodes();
364                  */
365                 /* we don't currently check the time on the pages
366                 obdfs_flush_dirty_pages(1); 
367                  */
368                 obdfs_flush_dirty_pages(0); 
369         }
370 }
371
372
373 int obdfs_flushd_init(void)
374 {
375         /*
376         kernel_thread(bdflush, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
377          */
378         kernel_thread(pupdate, NULL, 0);
379         printk("flushd inited\n");
380         return 0;
381 }
382
383 int obdfs_flushd_cleanup(void)
384 {
385         ENTRY;
386         /* deliver a signal to pupdated to shut it down
387            XXX need to kill it from user space for now XXX
388         if (pupdated) {
389                 send_sig_info(SIGTERM, 1, pupdated);
390         }
391          */
392
393         EXIT;
394         /* not reached */
395         return 0;
396
397 }