blob: 27507201f9e7138de8b02543011569ada0ceba48 [file] [log] [blame]
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07001/*
2 * V9FS FID Management
3 *
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -08004 * Copyright (C) 2005, 2006 by Eric Van Hensbergen <ericvh@gmail.com>
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07005 *
6 * This program is free software; you can redistribute it and/or modify
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -08007 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07009 *
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 Hensbergen3ed84912005-09-09 13:04:24 -070023#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040026#include <linux/sched.h>
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070027#include <linux/idr.h>
28
29#include "debug.h"
30#include "v9fs.h"
31#include "9p.h"
32#include "v9fs_vfs.h"
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070033#include "fid.h"
34
35/**
36 * v9fs_fid_insert - add a fid to a dentry
37 * @fid: fid to add
38 * @dentry: dentry that it is being added to
39 *
40 */
41
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080042int v9fs_fid_insert(struct v9fs_fid *fid, struct dentry *dentry)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070043{
44 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
45 dprintk(DEBUG_9P, "fid %d (%p) dentry %s (%p)\n", fid->fid, fid,
46 dentry->d_iname, dentry);
47 if (dentry->d_fsdata == NULL) {
48 dentry->d_fsdata =
49 kmalloc(sizeof(struct list_head), GFP_KERNEL);
50 if (dentry->d_fsdata == NULL) {
51 dprintk(DEBUG_ERROR, "Out of memory\n");
52 return -ENOMEM;
53 }
54 fid_list = (struct list_head *)dentry->d_fsdata;
55 INIT_LIST_HEAD(fid_list); /* Initialize list head */
56 }
57
58 fid->uid = current->uid;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070059 list_add(&fid->list, fid_list);
60 return 0;
61}
62
63/**
64 * v9fs_fid_create - allocate a FID structure
65 * @dentry - dentry to link newly created fid to
66 *
67 */
68
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080069struct v9fs_fid *v9fs_fid_create(struct v9fs_session_info *v9ses, int fid)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070070{
71 struct v9fs_fid *new;
72
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080073 dprintk(DEBUG_9P, "fid create fid %d\n", fid);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070074 new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
75 if (new == NULL) {
76 dprintk(DEBUG_ERROR, "Out of Memory\n");
77 return ERR_PTR(-ENOMEM);
78 }
79
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070080 new->fid = fid;
81 new->v9ses = v9ses;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070082 new->fidopen = 0;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070083 new->fidclunked = 0;
84 new->iounit = 0;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070085 new->rdir_pos = 0;
86 new->rdir_fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080087 INIT_LIST_HEAD(&new->list);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070088
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -080089 return new;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070090}
91
92/**
93 * v9fs_fid_destroy - deallocate a FID structure
94 * @fid: fid to destroy
95 *
96 */
97
98void v9fs_fid_destroy(struct v9fs_fid *fid)
99{
100 list_del(&fid->list);
101 kfree(fid);
102}
103
104/**
105 * v9fs_fid_lookup - retrieve the right fid from a particular dentry
106 * @dentry: dentry to look for fid in
107 * @type: intent of lookup (operation or traversal)
108 *
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -0800109 * find a fid in the dentry
110 *
111 * TODO: only match fids that have the same uid as current user
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700112 *
113 */
114
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700115struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700116{
117 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700118 struct v9fs_fid *return_fid = NULL;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700119
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700120 dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700121
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800122 if (fid_list)
123 return_fid = list_entry(fid_list->next, struct v9fs_fid, list);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700124
125 if (!return_fid) {
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -0800126 dprintk(DEBUG_ERROR, "Couldn't find a fid in dentry\n");
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700127 }
128
129 return return_fid;
130}