4 * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to:
18 * Free Software Foundation
19 * 51 Franklin Street, Fifth Floor
20 * Boston, MA 02111-1301 USA
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/errno.h>
28 #include <linux/idr.h>
34 #include "transport.h"
40 * v9fs_fid_insert - add a fid to a dentry
42 * @dentry: dentry that it is being added to
46 static int v9fs_fid_insert(struct v9fs_fid *fid, struct dentry *dentry)
48 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
49 dprintk(DEBUG_9P, "fid %d (%p) dentry %s (%p)\n", fid->fid, fid,
50 dentry->d_iname, dentry);
51 if (dentry->d_fsdata == NULL) {
53 kmalloc(sizeof(struct list_head), GFP_KERNEL);
54 if (dentry->d_fsdata == NULL) {
55 dprintk(DEBUG_ERROR, "Out of memory\n");
58 fid_list = (struct list_head *)dentry->d_fsdata;
59 INIT_LIST_HEAD(fid_list); /* Initialize list head */
62 fid->uid = current->uid;
63 fid->pid = current->pid;
64 list_add(&fid->list, fid_list);
69 * v9fs_fid_create - allocate a FID structure
70 * @dentry - dentry to link newly created fid to
74 struct v9fs_fid *v9fs_fid_create(struct dentry *dentry)
78 new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
80 dprintk(DEBUG_ERROR, "Out of Memory\n");
81 return ERR_PTR(-ENOMEM);
90 if (v9fs_fid_insert(new, dentry) == 0)
93 dprintk(DEBUG_ERROR, "Problems inserting to dentry\n");
100 * v9fs_fid_destroy - deallocate a FID structure
101 * @fid: fid to destroy
105 void v9fs_fid_destroy(struct v9fs_fid *fid)
107 list_del(&fid->list);
112 * v9fs_fid_lookup - retrieve the right fid from a particular dentry
113 * @dentry: dentry to look for fid in
114 * @type: intent of lookup (operation or traversal)
116 * search list of fids associated with a dentry for a fid with a matching
117 * thread id or uid. If that fails, look up the dentry's parents to see if you
118 * can find a matching fid.
122 struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry, int type)
124 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
125 struct v9fs_fid *current_fid = NULL;
126 struct v9fs_fid *temp = NULL;
127 struct v9fs_fid *return_fid = NULL;
128 int found_parent = 0;
131 dprintk(DEBUG_9P, " dentry: %s (%p) type %d\n", dentry->d_iname, dentry,
134 if (fid_list && !list_empty(fid_list)) {
135 list_for_each_entry_safe(current_fid, temp, fid_list, list) {
136 if (current_fid->uid == current->uid) {
137 if (return_fid == NULL) {
139 || (!current_fid->fidopen)) {
140 return_fid = current_fid;
145 if (current_fid->pid == current->real_parent->pid) {
146 if ((return_fid == NULL) || (found_parent)
149 || (!current_fid->fidopen)) {
150 return_fid = current_fid;
156 if (current_fid->pid == current->pid) {
157 if ((type == FID_OP) ||
158 (!current_fid->fidopen)) {
159 return_fid = current_fid;
167 /* we are at the root but didn't match */
168 if ((!return_fid) && (dentry->d_parent == dentry)) {
169 /* TODO: clone attach with new uid */
170 return_fid = current_fid;
174 struct dentry *par = current->fs->pwd->d_parent;
176 while (par != NULL) {
180 if (par == par->d_parent) {
182 "got to root without finding dentry\n");
188 /* XXX - there may be some duplication we can get rid of */
190 /* we need to fid_lookup the starting point */
194 struct v9fs_session_info *v9ses =
195 v9fs_inode2v9ses(current->fs->pwd->d_inode);
198 v9fs_fid_lookup(current->fs->pwd, FID_WALK);
199 if (current_fid == NULL) {
201 "process cwd doesn't have a fid\n");
204 oldfid = current_fid->fid;
205 par = current->fs->pwd;
206 /* TODO: take advantage of multiwalk */
208 fidnum = v9fs_get_idpool(&v9ses->fidpool);
211 "could not get a new fid num\n");
215 while (par != dentry) {
217 v9fs_t_walk(v9ses, oldfid, fidnum, "..",
221 "problem walking to parent\n");
226 if (par == par->d_parent) {
228 "can't find dentry\n");
234 return_fid = v9fs_fid_create(dentry);
235 return_fid->fid = fidnum;