Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * V9FS FID Management |
| 3 | * |
Eric Van Hensbergen | 46f6dac | 2006-03-02 02:54:33 -0800 | [diff] [blame] | 4 | * Copyright (C) 2005, 2006 by Eric Van Hensbergen <ericvh@gmail.com> |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Eric Van Hensbergen | 42e8c50 | 2006-03-25 03:07:28 -0800 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License version 2 |
| 8 | * as published by the Free Software Foundation. |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to: |
| 17 | * Free Software Foundation |
| 18 | * 51 Franklin Street, Fifth Floor |
| 19 | * Boston, MA 02111-1301 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/fs.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 26 | #include <linux/sched.h> |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 27 | #include <linux/idr.h> |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 28 | #include <asm/semaphore.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 29 | #include <net/9p/9p.h> |
| 30 | #include <net/9p/client.h> |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 31 | |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 32 | #include "v9fs.h" |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 33 | #include "v9fs_vfs.h" |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 34 | #include "fid.h" |
| 35 | |
| 36 | /** |
| 37 | * v9fs_fid_insert - add a fid to a dentry |
| 38 | * @fid: fid to add |
| 39 | * @dentry: dentry that it is being added to |
| 40 | * |
| 41 | */ |
| 42 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 43 | int v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid) |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 44 | { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 45 | struct v9fs_dentry *dent; |
| 46 | |
| 47 | P9_DPRINTK(P9_DEBUG_VFS, "fid %d dentry %s\n", |
| 48 | fid->fid, dentry->d_iname); |
| 49 | |
| 50 | dent = dentry->d_fsdata; |
| 51 | if (!dent) { |
| 52 | dent = kmalloc(sizeof(struct v9fs_dentry), GFP_KERNEL); |
| 53 | if (!dent) |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 54 | return -ENOMEM; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 55 | |
| 56 | spin_lock_init(&dent->lock); |
| 57 | INIT_LIST_HEAD(&dent->fidlist); |
| 58 | dentry->d_fsdata = dent; |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 61 | spin_lock(&dent->lock); |
| 62 | list_add(&fid->dlist, &dent->fidlist); |
| 63 | spin_unlock(&dent->lock); |
| 64 | |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /** |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 69 | * v9fs_fid_lookup - return a locked fid from a dentry |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 70 | * @dentry: dentry to look for fid in |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 71 | * |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 72 | * find a fid in the dentry, obtain its semaphore and return a reference to it. |
| 73 | * code calling lookup is responsible for releasing lock |
Eric Van Hensbergen | 46f6dac | 2006-03-02 02:54:33 -0800 | [diff] [blame] | 74 | * |
| 75 | * TODO: only match fids that have the same uid as current user |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 76 | * |
| 77 | */ |
| 78 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 79 | struct p9_fid *v9fs_fid_lookup(struct dentry *dentry) |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 80 | { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 81 | struct v9fs_dentry *dent; |
| 82 | struct p9_fid *fid; |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 83 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 84 | P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry); |
| 85 | dent = dentry->d_fsdata; |
| 86 | if (dent) |
| 87 | fid = list_entry(dent->fidlist.next, struct p9_fid, dlist); |
| 88 | else |
| 89 | fid = ERR_PTR(-EBADF); |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 90 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 91 | P9_DPRINTK(P9_DEBUG_VFS, " fid: %p\n", fid); |
| 92 | return fid; |
| 93 | } |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 94 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 95 | struct p9_fid *v9fs_fid_lookup_remove(struct dentry *dentry) |
| 96 | { |
| 97 | struct p9_fid *fid; |
| 98 | struct v9fs_dentry *dent; |
| 99 | |
| 100 | dent = dentry->d_fsdata; |
| 101 | fid = v9fs_fid_lookup(dentry); |
| 102 | if (!IS_ERR(fid)) { |
| 103 | spin_lock(&dent->lock); |
| 104 | list_del(&fid->dlist); |
| 105 | spin_unlock(&dent->lock); |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 108 | return fid; |
Eric Van Hensbergen | 3ed8491 | 2005-09-09 13:04:24 -0700 | [diff] [blame] | 109 | } |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 110 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 111 | |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 112 | /** |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 113 | * v9fs_fid_clone - lookup the fid for a dentry, clone a private copy and |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 114 | * release it |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 115 | * @dentry: dentry to look for fid in |
| 116 | * |
| 117 | * find a fid in the dentry and then clone to a new private fid |
| 118 | * |
| 119 | * TODO: only match fids that have the same uid as current user |
| 120 | * |
| 121 | */ |
| 122 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 123 | struct p9_fid *v9fs_fid_clone(struct dentry *dentry) |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 124 | { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 125 | struct p9_fid *ofid, *fid; |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 126 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 127 | P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry); |
| 128 | ofid = v9fs_fid_lookup(dentry); |
| 129 | if (IS_ERR(ofid)) |
| 130 | return ofid; |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 131 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 132 | fid = p9_client_walk(ofid, 0, NULL, 1); |
| 133 | return fid; |
Eric Van Hensbergen | da977b2 | 2007-01-26 00:57:06 -0800 | [diff] [blame] | 134 | } |