1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/obdfilter/filter_io.c
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
7 * Author: Peter Braam <braam@clusterfs.com>
8 * Author: Andreas Dilger <adilger@clusterfs.com>
9 * Author: Phil Schwan <phil@clusterfs.com>
11 * This file is part of the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
14 * You may have signed or agreed to another license before downloading
15 * this software. If so, you are bound by the terms and conditions
16 * of that agreement, and the following does not apply to you. See the
17 * LICENSE file included with this distribution for more information.
19 * If you did not agree to a different license, then this copy of Lustre
20 * is open source software; you can redistribute it and/or modify it
21 * under the terms of version 2 of the GNU General Public License as
22 * published by the Free Software Foundation.
24 * In either case, Lustre is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * license text for more details.
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/pagemap.h> // XXX kill me soon
33 #include <linux/version.h>
34 #include <linux/buffer_head.h>
36 #define DEBUG_SUBSYSTEM S_FILTER
38 #include <linux/obd_class.h>
39 #include <linux/lustre_fsfilt.h>
40 #include <linux/lustre_quota.h>
41 #include "filter_internal.h"
43 /* 512byte block min */
44 #define MAX_BLOCKS_PER_PAGE (PAGE_SIZE / 512)
46 atomic_t dr_numreqs; /* number of reqs being processed */
47 struct bio *dr_bios; /* list of completed bios */
48 wait_queue_head_t dr_wait;
52 struct page **dr_pages;
53 unsigned long *dr_blocks;
55 unsigned long dr_start_time; /* jiffies */
56 unsigned int dr_ignore_quota:1;
57 struct filter_obd *dr_filter;
60 static void record_start_io(struct filter_iobuf *iobuf, int rw, int size)
62 struct filter_obd *filter = iobuf->dr_filter;
65 atomic_inc(&iobuf->dr_numreqs);
67 if (rw == OBD_BRW_READ) {
68 lprocfs_oh_tally(&filter->fo_read_rpc_hist,
69 filter->fo_r_in_flight);
70 lprocfs_oh_tally_log2(&filter->fo_r_disk_iosize, size);
72 lprocfs_oh_tally(&filter->fo_write_rpc_hist,
73 filter->fo_w_in_flight);
74 lprocfs_oh_tally_log2(&filter->fo_w_disk_iosize, size);
76 spin_lock_irqsave(&filter->fo_stats_lock, flags);
77 if (rw == OBD_BRW_READ)
78 filter->fo_r_in_flight++;
80 filter->fo_w_in_flight++;
81 spin_unlock_irqrestore(&filter->fo_stats_lock, flags);
82 iobuf->dr_start_time = jiffies;
85 static void record_finish_io(struct filter_iobuf *iobuf, int rw, int rc)
87 struct filter_obd *filter = iobuf->dr_filter;
88 unsigned long flags, stop_time = jiffies;
90 spin_lock_irqsave(&filter->fo_stats_lock, flags);
91 if (rw == OBD_BRW_READ)
92 filter->fo_r_in_flight--;
94 filter->fo_w_in_flight--;
95 spin_unlock_irqrestore(&filter->fo_stats_lock, flags);
97 if (atomic_dec_and_test(&iobuf->dr_numreqs))
98 wake_up(&iobuf->dr_wait);
103 if (rw == OBD_BRW_READ) {
104 lprocfs_oh_tally_log2(&filter->fo_r_io_time,
105 stop_time - iobuf->dr_start_time);
107 lprocfs_oh_tally_log2(&filter->fo_w_io_time,
108 stop_time - iobuf->dr_start_time);
112 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
114 struct filter_iobuf *iobuf = bio->bi_private;
118 CWARN("gets called against non-complete bio 0x%p: %d/%d/%d\n",
119 bio, bio->bi_size, done, error);
124 CERROR("***** bio->bi_private is NULL! This should never "
125 "happen. Normally, I would crash here, but instead I "
126 "will dump the bio contents to the console. Please "
127 "report this to CFS, along with any interesting "
128 "messages leading up to this point (like SCSI errors, "
129 "perhaps). Because bi_private is NULL, I can't wake up "
130 "the thread that initiated this I/O -- so you will "
131 "probably have to reboot this node.\n");
132 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
133 "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
134 "bi_private: %p\n", bio->bi_next, bio->bi_flags,
135 bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
136 bio->bi_end_io, atomic_read(&bio->bi_cnt),
141 spin_lock_irqsave(&iobuf->dr_lock, flags);
142 bio->bi_private = iobuf->dr_bios;
143 iobuf->dr_bios = bio;
144 if (iobuf->dr_error == 0)
145 iobuf->dr_error = error;
146 spin_unlock_irqrestore(&iobuf->dr_lock, flags);
148 record_finish_io(iobuf, test_bit(BIO_RW, &bio->bi_rw) ?
149 OBD_BRW_WRITE : OBD_BRW_READ, error);
154 static int can_be_merged(struct bio *bio, sector_t sector)
161 size = bio->bi_size >> 9;
162 return bio->bi_sector + size == sector ? 1 : 0;
165 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
166 int rw, int num_pages)
168 struct filter_iobuf *iobuf;
170 LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
172 OBD_ALLOC(iobuf, sizeof(*iobuf));
176 OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
177 if (iobuf->dr_pages == NULL)
180 OBD_ALLOC(iobuf->dr_blocks,
181 MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
182 if (iobuf->dr_blocks == NULL)
185 iobuf->dr_filter = filter;
186 iobuf->dr_bios = NULL;
187 init_waitqueue_head(&iobuf->dr_wait);
188 atomic_set(&iobuf->dr_numreqs, 0);
189 spin_lock_init(&iobuf->dr_lock);
190 iobuf->dr_max_pages = num_pages;
191 iobuf->dr_npages = 0;
196 OBD_FREE(iobuf->dr_pages,
197 num_pages * sizeof(*iobuf->dr_pages));
199 OBD_FREE(iobuf, sizeof(*iobuf));
201 RETURN(ERR_PTR(-ENOMEM));
204 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
207 while (iobuf->dr_bios) {
208 struct bio *bio = iobuf->dr_bios;
209 iobuf->dr_bios = bio->bi_private;
212 iobuf->dr_npages = 0;
213 atomic_set(&iobuf->dr_numreqs, 0);
216 void filter_free_iobuf(struct filter_iobuf *iobuf)
218 int num_pages = iobuf->dr_max_pages;
220 filter_clear_iobuf(iobuf);
222 OBD_FREE(iobuf->dr_blocks,
223 MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
224 OBD_FREE(iobuf->dr_pages,
225 num_pages * sizeof(*iobuf->dr_pages));
229 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
230 struct obd_trans_info *oti)
232 int thread_id = oti ? oti->oti_thread_id : -1;
234 if (unlikely(thread_id < 0)) {
235 filter_free_iobuf(iobuf);
239 LASSERTF(filter->fo_iobuf_pool[thread_id] == iobuf,
240 "iobuf mismatch for thread %d: pool %p iobuf %p\n",
241 thread_id, filter->fo_iobuf_pool[thread_id], iobuf);
242 filter_clear_iobuf(iobuf);
245 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
246 struct inode *inode, struct page *page)
248 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
249 iobuf->dr_pages[iobuf->dr_npages++] = page;
254 int filter_do_bio(struct obd_device *obd, struct inode *inode,
255 struct filter_iobuf *iobuf, int rw)
257 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
258 struct page **pages = iobuf->dr_pages;
259 int npages = iobuf->dr_npages;
260 unsigned long *blocks = iobuf->dr_blocks;
261 int total_blocks = npages * blocks_per_page;
262 int sector_bits = inode->i_sb->s_blocksize_bits - 9;
263 unsigned int blocksize = inode->i_sb->s_blocksize;
264 struct bio *bio = NULL;
266 unsigned int page_offset;
275 LASSERT(iobuf->dr_npages == npages);
276 LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
278 for (page_idx = 0, block_idx = 0;
280 page_idx++, block_idx += blocks_per_page) {
282 page = pages[page_idx];
283 LASSERT (block_idx + blocks_per_page <= total_blocks);
285 for (i = 0, page_offset = 0;
287 i += nblocks, page_offset += blocksize * nblocks) {
291 if (blocks[block_idx + i] == 0) { /* hole */
292 LASSERT(rw == OBD_BRW_READ);
293 memset(kmap(page) + page_offset, 0, blocksize);
298 sector = blocks[block_idx + i] << sector_bits;
300 /* Additional contiguous file blocks? */
301 while (i + nblocks < blocks_per_page &&
302 (sector + nblocks*(blocksize>>9)) ==
303 (blocks[block_idx + i + nblocks] << sector_bits))
307 can_be_merged(bio, sector) &&
308 bio_add_page(bio, page,
309 blocksize * nblocks, page_offset) != 0)
310 continue; /* added this frag OK */
314 bdev_get_queue(bio->bi_bdev);
316 /* Dang! I have to fragment this I/O */
317 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
318 "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
320 bio->bi_vcnt, bio->bi_max_vecs,
321 bio->bi_size >> 9, q->max_sectors,
322 bio_phys_segments(q, bio),
323 q->max_phys_segments,
324 bio_hw_segments(q, bio),
327 record_start_io(iobuf, rw, bio->bi_size);
328 rc = fsfilt_send_bio(rw, obd, inode, bio);
330 CERROR("Can't send bio: %d\n", rc);
331 record_finish_io(iobuf, rw, rc);
336 /* allocate new bio */
337 bio = bio_alloc(GFP_NOIO,
338 (npages - page_idx) * blocks_per_page);
340 CERROR ("Can't allocate bio\n");
345 bio->bi_bdev = inode->i_sb->s_bdev;
346 bio->bi_sector = sector;
347 bio->bi_end_io = dio_complete_routine;
348 bio->bi_private = iobuf;
350 rc = bio_add_page(bio, page,
351 blocksize * nblocks, page_offset);
357 record_start_io(iobuf, rw, bio->bi_size);
358 rc = fsfilt_send_bio(rw, obd, inode, bio);
362 CERROR("Can't send bio: %d\n", rc);
363 record_finish_io(iobuf, rw, rc);
368 wait_event(iobuf->dr_wait, atomic_read(&iobuf->dr_numreqs) == 0);
371 rc = iobuf->dr_error;
375 /* These are our hacks to keep our directio/bh IO coherent with ext3's
376 * page cache use. Most notably ext3 reads file data into the page
377 * cache when it is zeroing the tail of partial-block truncates and
378 * leaves it there, sometimes generating io from it at later truncates.
379 * This removes the partial page and its buffers from the page cache,
380 * so it should only ever cause a wait in rare cases, as otherwise we
381 * always do full-page IO to the OST.
383 * The call to truncate_complete_page() will call journal_invalidatepage()
384 * to free the buffers and drop the page from cache. The buffers should
385 * not be dirty, because we already called fdatasync/fdatawait on them.
387 static int filter_clear_page_cache(struct inode *inode,
388 struct filter_iobuf *iobuf)
393 /* This is nearly generic_osync_inode, without the waiting on the inode
394 rc = generic_osync_inode(inode, inode->i_mapping,
395 OSYNC_DATA|OSYNC_METADATA);
397 rc = filemap_fdatawrite(inode->i_mapping);
398 rc2 = sync_mapping_buffers(inode->i_mapping);
401 rc2 = filemap_fdatawait(inode->i_mapping);
407 /* be careful to call this after fsync_inode_data_buffers has waited
408 * for IO to complete before we evict it from the cache */
409 for (i = 0; i < iobuf->dr_npages; i++) {
410 page = find_lock_page(inode->i_mapping,
411 iobuf->dr_pages[i]->index);
414 if (page->mapping != NULL) {
415 wait_on_page_writeback(page);
416 ll_truncate_complete_page(page);
420 page_cache_release(page);
426 /* Must be called with i_sem taken for writes; this will drop it */
427 int filter_direct_io(int rw, struct dentry *dchild, struct filter_iobuf *iobuf,
428 struct obd_export *exp, struct iattr *attr,
429 struct obd_trans_info *oti, void **wait_handle)
431 struct obd_device *obd = exp->exp_obd;
432 struct inode *inode = dchild->d_inode;
433 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
435 struct semaphore *sem;
438 LASSERTF(iobuf->dr_npages <= iobuf->dr_max_pages, "%d,%d\n",
439 iobuf->dr_npages, iobuf->dr_max_pages);
440 LASSERT(iobuf->dr_npages <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
442 if (rw == OBD_BRW_READ) {
443 if (iobuf->dr_npages == 0)
448 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
449 LASSERT(iobuf->dr_npages > 0);
451 sem = &obd->u.filter.fo_alloc_lock;
453 lquota_enforce(quota_interface, obd, iobuf->dr_ignore_quota);
456 rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
457 iobuf->dr_npages, iobuf->dr_blocks,
458 obdfilter_created_scratchpad, create, sem);
461 LASSERT(rw == OBD_BRW_WRITE &&
462 !cap_raised(current->cap_effective, CAP_SYS_RESOURCE));
464 /* Unfortunately, if quota master is too busy to handle the
465 * pre-dqacq in time or this user has exceeded quota limit, we
466 * have to wait for the completion of in flight dqacq/dqrel,
468 if (lquota_acquire(quota_interface, obd, inode->i_uid,
473 if (rw == OBD_BRW_WRITE) {
475 filter_tally_write(&obd->u.filter,
480 if (attr->ia_size > inode->i_size)
481 attr->ia_valid |= ATTR_SIZE;
482 rc = fsfilt_setattr(obd, dchild,
483 oti->oti_handle, attr, 0);
488 rc2 = filter_finish_transno(exp, oti, 0);
490 CERROR("can't close transaction: %d\n", rc2);
495 rc2 =fsfilt_commit_async(obd,inode,oti->oti_handle,wait_handle);
502 rc = filter_clear_page_cache(inode, iobuf);
506 RETURN(filter_do_bio(obd, inode, iobuf, rw));
509 /* See if there are unallocated parts in given file region */
510 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
512 sector_t (*fs_bmap)(struct address_space *, sector_t) =
513 inode->i_mapping->a_ops->bmap;
516 /* We can't know if we are overwriting or not */
520 offset >>= inode->i_blkbits;
521 len >>= inode->i_blkbits;
523 for (j = 0; j <= len; j++)
524 if (fs_bmap(inode->i_mapping, offset + j) == 0)
530 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
531 int objcount, struct obd_ioobj *obj, int niocount,
532 struct niobuf_local *res, struct obd_trans_info *oti,
535 struct niobuf_local *lnb;
536 struct filter_iobuf *iobuf = NULL;
537 struct lvfs_run_ctxt saved;
538 struct fsfilt_objinfo fso;
539 struct iattr iattr = { 0 };
540 struct inode *inode = NULL;
541 unsigned long now = jiffies;
542 int i, err, cleanup_phase = 0;
543 struct obd_device *obd = exp->exp_obd;
546 unsigned int qcids[MAXQUOTAS] = {0, 0};
549 LASSERT(oti != NULL);
550 LASSERT(objcount == 1);
551 LASSERT(current->journal_info == NULL);
556 iobuf = filter_iobuf_get(&obd->u.filter, oti);
559 fso.fso_dentry = res->dentry;
560 fso.fso_bufcnt = obj->ioo_bufcnt;
561 inode = res->dentry->d_inode;
563 iobuf->dr_ignore_quota = 0;
564 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
567 /* If overwriting an existing block, we don't need a grant */
568 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
569 filter_range_is_mapped(inode, lnb->offset, lnb->len))
572 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
573 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
577 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
580 total_size += lnb->len;
582 /* we expect these pages to be in offset order, but we'll
584 this_size = lnb->offset + lnb->len;
585 if (this_size > iattr.ia_size)
586 iattr.ia_size = this_size;
588 /* if one page is a write-back page from client cache, or it's
589 * written by root, then mark the whole io request as ignore
591 if (lnb->flags & (OBD_BRW_FROM_GRANT | OBD_BRW_NOQUOTA))
592 iobuf->dr_ignore_quota = 1;
595 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
599 fsfilt_check_slow(now, obd_timeout, "i_sem");
600 oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
602 if (IS_ERR(oti->oti_handle)) {
604 rc = PTR_ERR(oti->oti_handle);
605 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
606 "error starting transaction: rc = %d\n", rc);
607 oti->oti_handle = NULL;
610 /* have to call fsfilt_commit() from this point on */
612 fsfilt_check_slow(now, obd_timeout, "brw_start");
614 i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
616 /* If the inode still has SUID+SGID bits set (see filter_precreate())
617 * then we will accept the UID+GID if sent by the client for
618 * initializing the ownership of this inode. We only allow this to
619 * happen once (so clear these bits) and later only allow setattr. */
620 if (inode->i_mode & S_ISUID)
622 if (inode->i_mode & S_ISGID)
625 iattr_from_obdo(&iattr, oa, i);
626 if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
627 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
628 (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
630 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
632 iattr.ia_valid |= ATTR_MODE;
633 iattr.ia_mode = inode->i_mode;
634 if (iattr.ia_valid & ATTR_UID)
635 iattr.ia_mode &= ~S_ISUID;
636 if (iattr.ia_valid & ATTR_GID)
637 iattr.ia_mode &= ~S_ISGID;
639 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
642 /* filter_direct_io drops i_sem */
643 rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
646 obdo_from_inode(oa, inode,
647 FILTER_VALID_FLAGS |OBD_MD_FLUID |OBD_MD_FLGID);
649 obdo_from_inode(oa, inode, OBD_MD_FLUID | OBD_MD_FLGID);
651 lquota_getflag(quota_interface, obd, oa);
653 fsfilt_check_slow(now, obd_timeout, "direct_io");
655 err = fsfilt_commit_wait(obd, inode, wait_handle);
659 if (obd_sync_filter && !err)
660 LASSERTF(oti->oti_transno <= obd->obd_last_committed,
661 "oti_transno "LPU64" last_committed "LPU64"\n",
662 oti->oti_transno, obd->obd_last_committed);
664 fsfilt_check_slow(now, obd_timeout, "commitrw commit");
667 filter_grant_commit(exp, niocount, res);
669 switch (cleanup_phase) {
671 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
672 LASSERT(current->journal_info == NULL);
674 filter_iobuf_put(&obd->u.filter, iobuf, oti);
677 * lnb->page automatically returns back into per-thread page
683 /* trigger quota pre-acquire */
684 qcids[USRQUOTA] = oa->o_uid;
685 qcids[GRPQUOTA] = oa->o_gid;
686 err = lquota_adjust(quota_interface, obd, qcids, NULL, rc,
688 CDEBUG(err ? D_ERROR : D_QUOTA,
689 "error filter adjust qunit! (rc:%d)\n", err);