]>
Commit | Line | Data |
---|---|---|
3ed8491c EVH |
1 | /* |
2 | * V9FS FID Management | |
3 | * | |
4 | * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/config.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/errno.h> | |
27 | #include <linux/fs.h> | |
28 | #include <linux/idr.h> | |
29 | ||
30 | #include "debug.h" | |
31 | #include "v9fs.h" | |
32 | #include "9p.h" | |
33 | #include "v9fs_vfs.h" | |
34 | #include "transport.h" | |
35 | #include "mux.h" | |
36 | #include "conv.h" | |
37 | #include "fid.h" | |
38 | ||
39 | /** | |
40 | * v9fs_fid_insert - add a fid to a dentry | |
41 | * @fid: fid to add | |
42 | * @dentry: dentry that it is being added to | |
43 | * | |
44 | */ | |
45 | ||
46 | static int v9fs_fid_insert(struct v9fs_fid *fid, struct dentry *dentry) | |
47 | { | |
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) { | |
52 | dentry->d_fsdata = | |
53 | kmalloc(sizeof(struct list_head), GFP_KERNEL); | |
54 | if (dentry->d_fsdata == NULL) { | |
55 | dprintk(DEBUG_ERROR, "Out of memory\n"); | |
56 | return -ENOMEM; | |
57 | } | |
58 | fid_list = (struct list_head *)dentry->d_fsdata; | |
59 | INIT_LIST_HEAD(fid_list); /* Initialize list head */ | |
60 | } | |
61 | ||
62 | fid->uid = current->uid; | |
63 | fid->pid = current->pid; | |
64 | list_add(&fid->list, fid_list); | |
65 | return 0; | |
66 | } | |
67 | ||
68 | /** | |
69 | * v9fs_fid_create - allocate a FID structure | |
70 | * @dentry - dentry to link newly created fid to | |
71 | * | |
72 | */ | |
73 | ||
0b8dd177 LI |
74 | struct v9fs_fid *v9fs_fid_create(struct dentry *dentry, |
75 | struct v9fs_session_info *v9ses, int fid, int create) | |
3ed8491c EVH |
76 | { |
77 | struct v9fs_fid *new; | |
78 | ||
0b8dd177 LI |
79 | dprintk(DEBUG_9P, "fid create dentry %p, fid %d, create %d\n", |
80 | dentry, fid, create); | |
81 | ||
3ed8491c EVH |
82 | new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL); |
83 | if (new == NULL) { | |
84 | dprintk(DEBUG_ERROR, "Out of Memory\n"); | |
85 | return ERR_PTR(-ENOMEM); | |
86 | } | |
87 | ||
0b8dd177 LI |
88 | new->fid = fid; |
89 | new->v9ses = v9ses; | |
3ed8491c | 90 | new->fidopen = 0; |
0b8dd177 | 91 | new->fidcreate = create; |
3ed8491c EVH |
92 | new->fidclunked = 0; |
93 | new->iounit = 0; | |
0b8dd177 LI |
94 | new->rdir_pos = 0; |
95 | new->rdir_fcall = NULL; | |
3ed8491c EVH |
96 | |
97 | if (v9fs_fid_insert(new, dentry) == 0) | |
98 | return new; | |
99 | else { | |
100 | dprintk(DEBUG_ERROR, "Problems inserting to dentry\n"); | |
101 | kfree(new); | |
102 | return NULL; | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * v9fs_fid_destroy - deallocate a FID structure | |
108 | * @fid: fid to destroy | |
109 | * | |
110 | */ | |
111 | ||
112 | void v9fs_fid_destroy(struct v9fs_fid *fid) | |
113 | { | |
114 | list_del(&fid->list); | |
115 | kfree(fid); | |
116 | } | |
117 | ||
0b8dd177 LI |
118 | /** |
119 | * v9fs_fid_walk_up - walks from the process current directory | |
120 | * up to the specified dentry. | |
121 | */ | |
122 | static struct v9fs_fid *v9fs_fid_walk_up(struct dentry *dentry) | |
123 | { | |
124 | int fidnum, cfidnum, err; | |
125 | struct v9fs_fid *cfid; | |
126 | struct dentry *cde; | |
127 | struct v9fs_session_info *v9ses; | |
128 | ||
129 | v9ses = v9fs_inode2v9ses(current->fs->pwd->d_inode); | |
130 | cfid = v9fs_fid_lookup(current->fs->pwd); | |
131 | if (cfid == NULL) { | |
132 | dprintk(DEBUG_ERROR, "process cwd doesn't have a fid\n"); | |
133 | return ERR_PTR(-ENOENT); | |
134 | } | |
135 | ||
136 | cfidnum = cfid->fid; | |
137 | cde = current->fs->pwd; | |
138 | /* TODO: take advantage of multiwalk */ | |
139 | ||
140 | fidnum = v9fs_get_idpool(&v9ses->fidpool); | |
141 | if (fidnum < 0) { | |
142 | dprintk(DEBUG_ERROR, "could not get a new fid num\n"); | |
143 | err = -ENOENT; | |
144 | goto clunk_fid; | |
145 | } | |
146 | ||
147 | while (cde != dentry) { | |
148 | if (cde == cde->d_parent) { | |
149 | dprintk(DEBUG_ERROR, "can't find dentry\n"); | |
150 | err = -ENOENT; | |
151 | goto clunk_fid; | |
152 | } | |
153 | ||
154 | err = v9fs_t_walk(v9ses, cfidnum, fidnum, "..", NULL); | |
155 | if (err < 0) { | |
156 | dprintk(DEBUG_ERROR, "problem walking to parent\n"); | |
157 | goto clunk_fid; | |
158 | } | |
159 | ||
160 | cfidnum = fidnum; | |
161 | cde = cde->d_parent; | |
162 | } | |
163 | ||
164 | return v9fs_fid_create(dentry, v9ses, fidnum, 0); | |
165 | ||
166 | clunk_fid: | |
167 | v9fs_t_clunk(v9ses, fidnum, NULL); | |
168 | return ERR_PTR(err); | |
169 | } | |
170 | ||
3ed8491c EVH |
171 | /** |
172 | * v9fs_fid_lookup - retrieve the right fid from a particular dentry | |
173 | * @dentry: dentry to look for fid in | |
174 | * @type: intent of lookup (operation or traversal) | |
175 | * | |
176 | * search list of fids associated with a dentry for a fid with a matching | |
177 | * thread id or uid. If that fails, look up the dentry's parents to see if you | |
178 | * can find a matching fid. | |
179 | * | |
180 | */ | |
181 | ||
0b8dd177 | 182 | struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry) |
3ed8491c EVH |
183 | { |
184 | struct list_head *fid_list = (struct list_head *)dentry->d_fsdata; | |
185 | struct v9fs_fid *current_fid = NULL; | |
186 | struct v9fs_fid *temp = NULL; | |
187 | struct v9fs_fid *return_fid = NULL; | |
3ed8491c | 188 | |
0b8dd177 | 189 | dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry); |
3ed8491c | 190 | |
0b8dd177 | 191 | if (fid_list) { |
3ed8491c | 192 | list_for_each_entry_safe(current_fid, temp, fid_list, list) { |
0b8dd177 LI |
193 | if (!current_fid->fidcreate) { |
194 | return_fid = current_fid; | |
195 | break; | |
3ed8491c EVH |
196 | } |
197 | } | |
0b8dd177 LI |
198 | |
199 | if (!return_fid) | |
200 | return_fid = current_fid; | |
3ed8491c EVH |
201 | } |
202 | ||
203 | /* we are at the root but didn't match */ | |
204 | if ((!return_fid) && (dentry->d_parent == dentry)) { | |
205 | /* TODO: clone attach with new uid */ | |
206 | return_fid = current_fid; | |
207 | } | |
208 | ||
209 | if (!return_fid) { | |
210 | struct dentry *par = current->fs->pwd->d_parent; | |
211 | int count = 1; | |
212 | while (par != NULL) { | |
213 | if (par == dentry) | |
214 | break; | |
215 | count++; | |
216 | if (par == par->d_parent) { | |
217 | dprintk(DEBUG_ERROR, | |
218 | "got to root without finding dentry\n"); | |
219 | break; | |
220 | } | |
221 | par = par->d_parent; | |
222 | } | |
223 | ||
224 | /* XXX - there may be some duplication we can get rid of */ | |
225 | if (par == dentry) { | |
0b8dd177 LI |
226 | return_fid = v9fs_fid_walk_up(dentry); |
227 | if (IS_ERR(return_fid)) | |
228 | return_fid = NULL; | |
229 | } | |
230 | } | |
3ed8491c | 231 | |
0b8dd177 LI |
232 | return return_fid; |
233 | } | |
3ed8491c | 234 | |
0b8dd177 LI |
235 | struct v9fs_fid *v9fs_fid_get_created(struct dentry *dentry) |
236 | { | |
237 | struct list_head *fid_list; | |
238 | struct v9fs_fid *fid, *ftmp, *ret; | |
239 | ||
240 | dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry); | |
241 | fid_list = (struct list_head *)dentry->d_fsdata; | |
242 | ret = NULL; | |
243 | if (fid_list) { | |
244 | list_for_each_entry_safe(fid, ftmp, fid_list, list) { | |
245 | if (fid->fidcreate && fid->pid == current->pid) { | |
246 | list_del(&fid->list); | |
247 | ret = fid; | |
248 | break; | |
3ed8491c EVH |
249 | } |
250 | } | |
251 | } | |
252 | ||
0b8dd177 LI |
253 | dprintk(DEBUG_9P, "return %p\n", ret); |
254 | return ret; | |
3ed8491c | 255 | } |