blob: c27f546dd25be595d221fb9175aada8a4318105b [file] [log] [blame]
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07001/*
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"
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070034#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 Ionkov6a3124a2006-03-02 02:54:30 -080043int v9fs_fid_insert(struct v9fs_fid *fid, struct dentry *dentry)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070044{
45 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
46 dprintk(DEBUG_9P, "fid %d (%p) dentry %s (%p)\n", fid->fid, fid,
47 dentry->d_iname, dentry);
48 if (dentry->d_fsdata == NULL) {
49 dentry->d_fsdata =
50 kmalloc(sizeof(struct list_head), GFP_KERNEL);
51 if (dentry->d_fsdata == NULL) {
52 dprintk(DEBUG_ERROR, "Out of memory\n");
53 return -ENOMEM;
54 }
55 fid_list = (struct list_head *)dentry->d_fsdata;
56 INIT_LIST_HEAD(fid_list); /* Initialize list head */
57 }
58
59 fid->uid = current->uid;
60 fid->pid = current->pid;
61 list_add(&fid->list, fid_list);
62 return 0;
63}
64
65/**
66 * v9fs_fid_create - allocate a FID structure
67 * @dentry - dentry to link newly created fid to
68 *
69 */
70
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080071struct v9fs_fid *v9fs_fid_create(struct v9fs_session_info *v9ses, int fid)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070072{
73 struct v9fs_fid *new;
74
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080075 dprintk(DEBUG_9P, "fid create fid %d\n", fid);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070076 new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
77 if (new == NULL) {
78 dprintk(DEBUG_ERROR, "Out of Memory\n");
79 return ERR_PTR(-ENOMEM);
80 }
81
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070082 new->fid = fid;
83 new->v9ses = v9ses;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070084 new->fidopen = 0;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070085 new->fidclunked = 0;
86 new->iounit = 0;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070087 new->rdir_pos = 0;
88 new->rdir_fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080089 INIT_LIST_HEAD(&new->list);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070090
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070091 return new;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070092}
93
94/**
95 * v9fs_fid_destroy - deallocate a FID structure
96 * @fid: fid to destroy
97 *
98 */
99
100void v9fs_fid_destroy(struct v9fs_fid *fid)
101{
102 list_del(&fid->list);
103 kfree(fid);
104}
105
106/**
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700107 * v9fs_fid_walk_up - walks from the process current directory
108 * up to the specified dentry.
109 */
110static struct v9fs_fid *v9fs_fid_walk_up(struct dentry *dentry)
111{
112 int fidnum, cfidnum, err;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800113 struct v9fs_fid *cfid, *fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700114 struct dentry *cde;
115 struct v9fs_session_info *v9ses;
116
117 v9ses = v9fs_inode2v9ses(current->fs->pwd->d_inode);
118 cfid = v9fs_fid_lookup(current->fs->pwd);
119 if (cfid == NULL) {
120 dprintk(DEBUG_ERROR, "process cwd doesn't have a fid\n");
121 return ERR_PTR(-ENOENT);
122 }
123
124 cfidnum = cfid->fid;
125 cde = current->fs->pwd;
126 /* TODO: take advantage of multiwalk */
127
128 fidnum = v9fs_get_idpool(&v9ses->fidpool);
129 if (fidnum < 0) {
130 dprintk(DEBUG_ERROR, "could not get a new fid num\n");
131 err = -ENOENT;
132 goto clunk_fid;
133 }
134
135 while (cde != dentry) {
136 if (cde == cde->d_parent) {
137 dprintk(DEBUG_ERROR, "can't find dentry\n");
138 err = -ENOENT;
139 goto clunk_fid;
140 }
141
142 err = v9fs_t_walk(v9ses, cfidnum, fidnum, "..", NULL);
143 if (err < 0) {
144 dprintk(DEBUG_ERROR, "problem walking to parent\n");
145 goto clunk_fid;
146 }
147
148 cfidnum = fidnum;
149 cde = cde->d_parent;
150 }
151
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800152 fid = v9fs_fid_create(v9ses, fidnum);
153 if (fid) {
154 err = v9fs_fid_insert(fid, dentry);
155 if (err < 0) {
156 kfree(fid);
157 goto clunk_fid;
158 }
159 }
160
161 return fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700162
163clunk_fid:
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800164 v9fs_t_clunk(v9ses, fidnum);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700165 return ERR_PTR(err);
166}
167
168/**
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700169 * v9fs_fid_lookup - retrieve the right fid from a particular dentry
170 * @dentry: dentry to look for fid in
171 * @type: intent of lookup (operation or traversal)
172 *
173 * search list of fids associated with a dentry for a fid with a matching
174 * thread id or uid. If that fails, look up the dentry's parents to see if you
175 * can find a matching fid.
176 *
177 */
178
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700179struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700180{
181 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700182 struct v9fs_fid *return_fid = NULL;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700183
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700184 dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700185
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800186 if (fid_list)
187 return_fid = list_entry(fid_list->next, struct v9fs_fid, list);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700188
189 if (!return_fid) {
190 struct dentry *par = current->fs->pwd->d_parent;
191 int count = 1;
192 while (par != NULL) {
193 if (par == dentry)
194 break;
195 count++;
196 if (par == par->d_parent) {
197 dprintk(DEBUG_ERROR,
198 "got to root without finding dentry\n");
199 break;
200 }
201 par = par->d_parent;
202 }
203
204/* XXX - there may be some duplication we can get rid of */
205 if (par == dentry) {
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700206 return_fid = v9fs_fid_walk_up(dentry);
207 if (IS_ERR(return_fid))
208 return_fid = NULL;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700209 }
210 }
211
212 return return_fid;
213}