Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
0003  *
0004  * This software may be freely redistributed under the terms of the
0005  * GNU General Public License.
0006  *
0007  * You should have received a copy of the GNU General Public License
0008  * along with this program; if not, write to the Free Software
0009  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0010  *
0011  * Authors: David Woodhouse <dwmw2@infradead.org>
0012  *          David Howells <dhowells@redhat.com>
0013  *
0014  */
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/init.h>
0019 #include <linux/circ_buf.h>
0020 #include <linux/sched.h>
0021 #include "internal.h"
0022 
0023 /*
0024  * Handle invalidation of an mmap'd file.  We invalidate all the PTEs referring
0025  * to the pages in this file's pagecache, forcing the kernel to go through
0026  * ->fault() or ->page_mkwrite() - at which point we can handle invalidation
0027  * more fully.
0028  */
0029 void afs_invalidate_mmap_work(struct work_struct *work)
0030 {
0031     struct afs_vnode *vnode = container_of(work, struct afs_vnode, cb_work);
0032 
0033     unmap_mapping_pages(vnode->netfs.inode.i_mapping, 0, 0, false);
0034 }
0035 
0036 void afs_server_init_callback_work(struct work_struct *work)
0037 {
0038     struct afs_server *server = container_of(work, struct afs_server, initcb_work);
0039     struct afs_vnode *vnode;
0040     struct afs_cell *cell = server->cell;
0041 
0042     down_read(&cell->fs_open_mmaps_lock);
0043 
0044     list_for_each_entry(vnode, &cell->fs_open_mmaps, cb_mmap_link) {
0045         if (vnode->cb_server == server) {
0046             clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
0047             queue_work(system_unbound_wq, &vnode->cb_work);
0048         }
0049     }
0050 
0051     up_read(&cell->fs_open_mmaps_lock);
0052 }
0053 
0054 /*
0055  * Allow the fileserver to request callback state (re-)initialisation.
0056  * Unfortunately, UUIDs are not guaranteed unique.
0057  */
0058 void afs_init_callback_state(struct afs_server *server)
0059 {
0060     rcu_read_lock();
0061     do {
0062         server->cb_s_break++;
0063         atomic_inc(&server->cell->fs_s_break);
0064         if (!list_empty(&server->cell->fs_open_mmaps))
0065             queue_work(system_unbound_wq, &server->initcb_work);
0066 
0067     } while ((server = rcu_dereference(server->uuid_next)));
0068     rcu_read_unlock();
0069 }
0070 
0071 /*
0072  * actually break a callback
0073  */
0074 void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
0075 {
0076     _enter("");
0077 
0078     clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
0079     if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
0080         vnode->cb_break++;
0081         vnode->cb_v_break = vnode->volume->cb_v_break;
0082         afs_clear_permits(vnode);
0083 
0084         if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
0085             afs_lock_may_be_available(vnode);
0086 
0087         if (reason != afs_cb_break_for_deleted &&
0088             vnode->status.type == AFS_FTYPE_FILE &&
0089             atomic_read(&vnode->cb_nr_mmap))
0090             queue_work(system_unbound_wq, &vnode->cb_work);
0091 
0092         trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
0093     } else {
0094         trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
0095     }
0096 }
0097 
0098 void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
0099 {
0100     write_seqlock(&vnode->cb_lock);
0101     __afs_break_callback(vnode, reason);
0102     write_sequnlock(&vnode->cb_lock);
0103 }
0104 
0105 /*
0106  * Look up a volume by volume ID under RCU conditions.
0107  */
0108 static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
0109                         afs_volid_t vid)
0110 {
0111     struct afs_volume *volume = NULL;
0112     struct rb_node *p;
0113     int seq = 0;
0114 
0115     do {
0116         /* Unfortunately, rbtree walking doesn't give reliable results
0117          * under just the RCU read lock, so we have to check for
0118          * changes.
0119          */
0120         read_seqbegin_or_lock(&cell->volume_lock, &seq);
0121 
0122         p = rcu_dereference_raw(cell->volumes.rb_node);
0123         while (p) {
0124             volume = rb_entry(p, struct afs_volume, cell_node);
0125 
0126             if (volume->vid < vid)
0127                 p = rcu_dereference_raw(p->rb_left);
0128             else if (volume->vid > vid)
0129                 p = rcu_dereference_raw(p->rb_right);
0130             else
0131                 break;
0132             volume = NULL;
0133         }
0134 
0135     } while (need_seqretry(&cell->volume_lock, seq));
0136 
0137     done_seqretry(&cell->volume_lock, seq);
0138     return volume;
0139 }
0140 
0141 /*
0142  * allow the fileserver to explicitly break one callback
0143  * - happens when
0144  *   - the backing file is changed
0145  *   - a lock is released
0146  */
0147 static void afs_break_one_callback(struct afs_volume *volume,
0148                    struct afs_fid *fid)
0149 {
0150     struct super_block *sb;
0151     struct afs_vnode *vnode;
0152     struct inode *inode;
0153 
0154     if (fid->vnode == 0 && fid->unique == 0) {
0155         /* The callback break applies to an entire volume. */
0156         write_lock(&volume->cb_v_break_lock);
0157         volume->cb_v_break++;
0158         trace_afs_cb_break(fid, volume->cb_v_break,
0159                    afs_cb_break_for_volume_callback, false);
0160         write_unlock(&volume->cb_v_break_lock);
0161         return;
0162     }
0163 
0164     /* See if we can find a matching inode - even an I_NEW inode needs to
0165      * be marked as it can have its callback broken before we finish
0166      * setting up the local inode.
0167      */
0168     sb = rcu_dereference(volume->sb);
0169     if (!sb)
0170         return;
0171 
0172     inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
0173     if (inode) {
0174         vnode = AFS_FS_I(inode);
0175         afs_break_callback(vnode, afs_cb_break_for_callback);
0176     } else {
0177         trace_afs_cb_miss(fid, afs_cb_break_for_callback);
0178     }
0179 }
0180 
0181 static void afs_break_some_callbacks(struct afs_server *server,
0182                      struct afs_callback_break *cbb,
0183                      size_t *_count)
0184 {
0185     struct afs_callback_break *residue = cbb;
0186     struct afs_volume *volume;
0187     afs_volid_t vid = cbb->fid.vid;
0188     size_t i;
0189 
0190     volume = afs_lookup_volume_rcu(server->cell, vid);
0191 
0192     /* TODO: Find all matching volumes if we couldn't match the server and
0193      * break them anyway.
0194      */
0195 
0196     for (i = *_count; i > 0; cbb++, i--) {
0197         if (cbb->fid.vid == vid) {
0198             _debug("- Fid { vl=%08llx n=%llu u=%u }",
0199                    cbb->fid.vid,
0200                    cbb->fid.vnode,
0201                    cbb->fid.unique);
0202             --*_count;
0203             if (volume)
0204                 afs_break_one_callback(volume, &cbb->fid);
0205         } else {
0206             *residue++ = *cbb;
0207         }
0208     }
0209 }
0210 
0211 /*
0212  * allow the fileserver to break callback promises
0213  */
0214 void afs_break_callbacks(struct afs_server *server, size_t count,
0215              struct afs_callback_break *callbacks)
0216 {
0217     _enter("%p,%zu,", server, count);
0218 
0219     ASSERT(server != NULL);
0220 
0221     rcu_read_lock();
0222 
0223     while (count > 0)
0224         afs_break_some_callbacks(server, callbacks, &count);
0225 
0226     rcu_read_unlock();
0227     return;
0228 }