2 * OBDFS Super operations - also used for Lustre file system
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
7 * Copryright (C) 1999 Seagate Technology Inc.
10 #define __NO_VERSION__
12 #include <linux/locks.h>
13 #include <linux/swap.h>
15 #include <linux/obd_support.h>
16 #include <linux/obd_class.h>
17 #include <linux/obdfs.h>
20 /* XXX temporary until the real function is available from kernel
21 * XXX set this to memory size in pages for max page cache size
23 #define nr_free_buffer_pages() 32768
26 int nfract; /* Percentage of buffer cache dirty to
28 int ndirty; /* Maximum number of dirty blocks to write out per
30 int nrefill; /* Number of clean buffers to try to obtain
31 each time we call refill */
32 int nref_dirt; /* Dirty buffer threshold for activating bdflush
33 when trying to refill buffers. */
34 int interval; /* jiffies delay between pupdate flushes */
35 int age_buffer; /* Time for normal buffer to age before we flush it */
36 int age_super; /* Time for superblock to age before we flush it */
37 } pupd_prm = {40, 1024, 64, 256, 1*HZ, 30*HZ, 5*HZ };
39 /* Called with the superblock list lock held */
40 static int obdfs_enqueue_pages(struct inode *inode, struct obdo **obdo,
41 int nr_slots, struct page **pages, char **bufs,
42 obd_size *counts, obd_off *offsets,
43 obd_flag *flag, unsigned long check_time)
45 struct list_head *page_list = obdfs_iplist(inode);
46 struct list_head *tmp;
52 /* Traverse list in reverse order, so we do FIFO, not LIFO order */
53 while ( (tmp = tmp->prev) != page_list && num < nr_slots ) {
54 struct obdfs_pgrq *req;
57 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
61 if (req->rq_jiffies > check_time)
62 break; /* pages are in chronological order */
64 /* Only allocate the obdo if we will actually do I/O here */
67 *obdo = obdo_fromid(IID(inode), inode->i_ino,
69 if ( IS_ERR(*obdo) ) {
70 int err = PTR_ERR(*obdo);
77 /* FIXME revisit fromid & from_inode */
78 obdfs_from_inode(*obdo, inode);
79 *flag = OBD_BRW_CREATE;
82 /* Remove request from list before write to avoid conflict.
83 * Note that obdfs_pgrq_del() also deletes the request.
87 CDEBUG(D_CACHE, "no page \n");
91 bufs[num] = (char *)page_address(page);
93 counts[num] = PAGE_SIZE;
94 offsets[num] = ((obd_off)page->index) << PAGE_SHIFT;
95 CDEBUG(D_INFO, "ENQ inode %ld, page %p addr %p to vector\n",
96 inode->i_ino, page, (char *)page_address(page));
100 if (!list_empty(page_list))
101 CDEBUG(D_INFO, "inode %ld list not empty\n", inode->i_ino);
102 CDEBUG(D_INFO, "added %d page(s) to vector\n", num);
106 } /* obdfs_enqueue_pages */
108 /* Dequeue cached pages for a dying inode without writing them to disk. */
109 void obdfs_dequeue_pages(struct inode *inode)
111 struct list_head *tmp;
114 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
115 tmp = obdfs_islist(inode);
116 if ( list_empty(tmp) ) {
117 CDEBUG(D_INFO, "no dirty pages for inode %ld\n", inode->i_ino);
118 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
123 /* take it out of the super list */
125 INIT_LIST_HEAD(obdfs_islist(inode));
127 tmp = obdfs_iplist(inode);
128 while ( (tmp = tmp->prev) != obdfs_iplist(inode) ) {
129 struct obdfs_pgrq *req;
132 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
134 /* take it out of the list and free */
136 /* now put the page away */
140 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
142 /* decrement inode reference for page cache */
143 atomic_dec(&inode->i_count);
147 /* Remove writeback requests for the superblock */
148 int obdfs_flush_reqs(struct list_head *inode_list, unsigned long check_time)
150 struct list_head *tmp;
151 unsigned long max_io, total_io = 0;
154 struct inode *inodes[MAX_IOVEC]; /* write data back to these */
155 struct page *pages[MAX_IOVEC]; /* call put_page on these */
156 struct obdo *obdos[MAX_IOVEC];
157 char *bufs[MAX_IOVEC];
158 obd_size counts[MAX_IOVEC];
159 obd_off offsets[MAX_IOVEC];
160 obd_flag flags[MAX_IOVEC];
161 obd_count bufs_per_obdo[MAX_IOVEC];
163 struct obdfs_sb_info *sbi;
167 CDEBUG(D_INODE, "no list\n");
172 sbi = list_entry(inode_list, struct obdfs_sb_info, osi_inodes);
174 obd_down(&sbi->osi_list_mutex);
175 if ( list_empty(inode_list) ) {
176 CDEBUG(D_INFO, "list empty\n");
177 obd_up(&sbi->osi_list_mutex);
182 /* If we are forcing a write, write out all dirty pages */
183 max_io = check_time == ~0UL ? 1<<31 : pupd_prm.ndirty;
184 CDEBUG(D_INFO, "max_io = %lu\n", max_io);
186 /* Add each inode's dirty pages to a write vector, and write it.
187 * Traverse list in reverse order, so we do FIFO, not LIFO order
193 while ( (tmp = tmp->prev) != inode_list && total_io < max_io) {
194 struct obdfs_inode_info *ii;
198 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
199 inode = list_entry(ii, struct inode, u);
200 inodes[num_obdos] = inode;
201 obdos[num_obdos] = NULL;
202 CDEBUG(D_INFO, "checking inode %ld pages\n", inode->i_ino);
204 /* Make sure we reference "inode" and not "inodes[num_obdos]",
205 * as num_obdos will change after the loop is run.
207 if (!list_empty(obdfs_iplist(inode))) {
208 res = obdfs_enqueue_pages(inode, &obdos[num_obdos],
210 &pages[num_io], &bufs[num_io],
215 CDEBUG(D_INFO, "FLUSH inode %ld, pages flushed: %d\n",
219 "fatal: unable to enqueue inode %ld (err %d)\n",
221 /* XXX Move bad inode to end of list so we can
222 * continue with flushing list. This is a
223 * temporary measure to avoid machine lockups.
224 * Maybe if we have -ENOENT, simply discard.
227 list_add(tmp, inode_list);
237 bufs_per_obdo[num_obdos] = res;
240 if ( num_io == MAX_IOVEC ) {
241 obd_up(&sbi->osi_list_mutex);
242 err = obdfs_do_vec_wr(inodes, num_io, num_obdos,
243 obdos, bufs_per_obdo,
248 "fatal: do_vec_wr err=%d\n",
253 obd_down(&sbi->osi_list_mutex);
260 obd_up(&sbi->osi_list_mutex);
262 /* flush any remaining I/Os */
264 err = obdfs_do_vec_wr(inodes, num_io, num_obdos, obdos,
265 bufs_per_obdo, pages, bufs, counts,
268 CDEBUG(D_INODE, "fatal: unable to do vec_wr (err %d)\n", err);
273 /* Remove inode from superblock dirty list when no more pages.
274 * Make sure we don't point at the current inode with tmp
275 * when we re-init the list on the inode, or we will loop.
277 obd_down(&sbi->osi_list_mutex);
279 while ( (tmp = tmp->prev) != inode_list ) {
280 struct obdfs_inode_info *ii;
283 ii = list_entry(tmp, struct obdfs_inode_info, oi_inodes);
284 inode = list_entry(ii, struct inode, u);
285 CDEBUG(D_INFO, "checking inode %ld empty\n", inode->i_ino);
286 if (list_empty(obdfs_iplist(inode))) {
287 CDEBUG(D_INFO, "remove inode %ld from dirty list\n",
290 list_del(obdfs_islist(inode));
291 /* decrement inode reference for page cache */
292 atomic_dec(&inode->i_count);
293 INIT_LIST_HEAD(obdfs_islist(inode));
296 obd_up(&sbi->osi_list_mutex);
298 CDEBUG(D_INFO, "flushed %ld pages in total\n", total_io);
301 return err ? err : total_io;
302 } /* obdfs_flush_reqs */
305 /* Walk all of the superblocks and write out blocks which are too old.
306 * Return the maximum number of blocks written for a single filesystem.
308 int obdfs_flush_dirty_pages(unsigned long check_time)
310 struct list_head *sl;
314 sl = &obdfs_super_list;
315 while ( (sl = sl->prev) != &obdfs_super_list ) {
316 struct obdfs_sb_info *sbi =
317 list_entry(sl, struct obdfs_sb_info, osi_list);
320 /* walk write requests here, use the sb, check the time */
321 ret = obdfs_flush_reqs(&sbi->osi_inodes, check_time);
322 /* XXX handle error? What to do with it? */
324 max = ret > max ? ret : max;
328 } /* obdfs_flush_dirty_pages */
331 static struct task_struct *pupdated;
333 static int pupdate(void *unused)
335 int interval = pupd_prm.interval;
336 long age = pupd_prm.age_buffer;
343 pupdated->session = 1;
345 strcpy(pupdated->comm, "pupdated");
347 printk("pupdated activated...\n");
349 spin_lock_irq(&pupdated->sigmask_lock);
350 sigfillset(&pupdated->blocked);
351 siginitsetinv(&pupdated->blocked, sigmask(SIGTERM));
352 recalc_sigpending(pupdated);
353 spin_unlock_irq(&pupdated->sigmask_lock);
358 /* update interval */
360 set_task_state(pupdated, TASK_INTERRUPTIBLE);
361 schedule_timeout(interval);
363 if (signal_pending(pupdated))
366 spin_lock_irq(&pupdated->sigmask_lock);
367 if (sigismember(&pupdated->pending.signal, SIGTERM))
369 sigdelset(&pupdated->pending.signal, SIGTERM);
372 recalc_sigpending(pupdated);
373 spin_unlock_irq(&pupdated->sigmask_lock);
375 printk("pupdated stopped...\n");
376 set_task_state(pupdated, TASK_STOPPED);
381 /* asynchronous setattr etc for the future ...
382 obdfs_flush_dirty_inodes(jiffies - pupd_prm.age_super);
384 dirty_limit = nr_free_buffer_pages() * pupd_prm.nfract / 100;
386 if (obdfs_cache_count > dirty_limit) {
388 if ( wrote < pupd_prm.ndirty )
390 CDEBUG(D_CACHE, "wrote %d, age %ld, interval %d\n",
391 wrote, age, interval);
393 if ( wrote < pupd_prm.ndirty >> 1 &&
394 obdfs_cache_count < dirty_limit / 2) {
395 interval = pupd_prm.interval;
396 age = pupd_prm.age_buffer;
398 "wrote %d, age %ld, interval %d\n",
399 wrote, age, interval);
400 } else if (obdfs_cache_count > dirty_limit / 2) {
402 if ( wrote < pupd_prm.ndirty )
405 "wrote %d, age %ld, interval %d\n",
406 wrote, age, interval);
410 wrote = obdfs_flush_dirty_pages(jiffies - age);
413 "dirty_limit %ld, cache_count %ld, wrote %d\n",
414 dirty_limit, obdfs_cache_count, wrote);
419 int obdfs_flushd_init(void)
422 kernel_thread(bdflush, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
424 kernel_thread(pupdate, NULL, 0);
425 CDEBUG(D_PSDEV, __FUNCTION__ ": flushd inited\n");
429 int obdfs_flushd_cleanup(void)
433 if (pupdated) /* for debugging purposes only */
434 CDEBUG(D_CACHE, "pupdated->state = %lx\n", pupdated->state);
436 /* deliver a signal to pupdated to shut it down */
437 if (pupdated && (pupdated->state == TASK_RUNNING ||
438 pupdated->state == TASK_INTERRUPTIBLE )) {
439 unsigned long timeout = HZ/20;
440 unsigned long count = 0;
441 send_sig_info(SIGTERM, (struct siginfo *)1, pupdated);
443 if ((count % 2*HZ) == timeout)
444 printk(KERN_INFO "wait for pupdated to stop\n");
446 set_current_state(TASK_INTERRUPTIBLE);
447 schedule_timeout(timeout);