blob: c4d13bf904d2319105f5731004555a76f73e88b6 [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
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;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070060 list_add(&fid->list, fid_list);
61 return 0;
62}
63
64/**
65 * v9fs_fid_create - allocate a FID structure
66 * @dentry - dentry to link newly created fid to
67 *
68 */
69
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080070struct v9fs_fid *v9fs_fid_create(struct v9fs_session_info *v9ses, int fid)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070071{
72 struct v9fs_fid *new;
73
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080074 dprintk(DEBUG_9P, "fid create fid %d\n", fid);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070075 new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
76 if (new == NULL) {
77 dprintk(DEBUG_ERROR, "Out of Memory\n");
78 return ERR_PTR(-ENOMEM);
79 }
80
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070081 new->fid = fid;
82 new->v9ses = v9ses;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070083 new->fidopen = 0;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070084 new->fidclunked = 0;
85 new->iounit = 0;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -070086 new->rdir_pos = 0;
87 new->rdir_fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080088 INIT_LIST_HEAD(&new->list);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070089
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -080090 return new;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070091}
92
93/**
94 * v9fs_fid_destroy - deallocate a FID structure
95 * @fid: fid to destroy
96 *
97 */
98
99void v9fs_fid_destroy(struct v9fs_fid *fid)
100{
101 list_del(&fid->list);
102 kfree(fid);
103}
104
105/**
106 * v9fs_fid_lookup - retrieve the right fid from a particular dentry
107 * @dentry: dentry to look for fid in
108 * @type: intent of lookup (operation or traversal)
109 *
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -0800110 * find a fid in the dentry
111 *
112 * TODO: only match fids that have the same uid as current user
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700113 *
114 */
115
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700116struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700117{
118 struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700119 struct v9fs_fid *return_fid = NULL;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700120
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700121 dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700122
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800123 if (fid_list)
124 return_fid = list_entry(fid_list->next, struct v9fs_fid, list);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700125
126 if (!return_fid) {
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -0800127 dprintk(DEBUG_ERROR, "Couldn't find a fid in dentry\n");
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700128 }
129
130 return return_fid;
131}