aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-08-03 17:36:16 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-08-03 17:36:16 -0400
commitb8b3e9058f0f1d5c5a81c7d3d46f373d5f59a82c (patch)
tree1033085760392dc7e24a79bbeb276ce1a34314d0 /fs
parent51102ee5b8853d230e534cbcd0d888f0134738a3 (diff)
parent327aec03ac4c7bbf5e41ff03ac3a84c424589f27 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh/v9fs
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh/v9fs: (22 commits) 9p: fix sparse warnings in new xattr code fs/9p: remove sparse warning in vfs_inode fs/9p: destroy fid on failed remove fs/9p: Prevent parallel rename when doing fid_lookup fs/9p: Add support user. xattr net/9p: Implement TXATTRCREATE 9p call net/9p: Implement attrwalk 9p call 9p: Implement LOPEN fs/9p: This patch implements TLCREATE for 9p2000.L protocol. 9p: Implement TMKDIR 9p: Implement TMKNOD 9p: Define and implement TSYMLINK for 9P2000.L 9p: Define and implement TLINK for 9P2000.L 9p: Define and implement TLINK for 9P2000.L 9p: Implement client side of setattr for 9P2000.L protocol. 9p: getattr client implementation for 9P2000.L protocol. fs/9p: Pass the correct user credentials during attach net/9p: Handle the server returned error properly 9p: readdir implementation for 9p2000.L 9p: Make use of iounit for read/write ...
Diffstat (limited to 'fs')
-rw-r--r--fs/9p/Makefile4
-rw-r--r--fs/9p/fid.c111
-rw-r--r--fs/9p/v9fs.c3
-rw-r--r--fs/9p/v9fs.h1
-rw-r--r--fs/9p/v9fs_vfs.h1
-rw-r--r--fs/9p/vfs_dir.c134
-rw-r--r--fs/9p/vfs_file.c26
-rw-r--r--fs/9p/vfs_inode.c757
-rw-r--r--fs/9p/vfs_super.c50
-rw-r--r--fs/9p/xattr.c160
-rw-r--r--fs/9p/xattr.h27
-rw-r--r--fs/9p/xattr_user.c80
12 files changed, 1242 insertions, 112 deletions
diff --git a/fs/9p/Makefile b/fs/9p/Makefile
index 1a940ec7af61..91fba025fcbe 100644
--- a/fs/9p/Makefile
+++ b/fs/9p/Makefile
@@ -8,6 +8,8 @@ obj-$(CONFIG_9P_FS) := 9p.o
8 vfs_dir.o \ 8 vfs_dir.o \
9 vfs_dentry.o \ 9 vfs_dentry.o \
10 v9fs.o \ 10 v9fs.o \
11 fid.o 11 fid.o \
12 xattr.o \
13 xattr_user.o
12 14
139p-$(CONFIG_9P_FSCACHE) += cache.o 159p-$(CONFIG_9P_FSCACHE) += cache.o
diff --git a/fs/9p/fid.c b/fs/9p/fid.c
index 7317b39b2815..358563689064 100644
--- a/fs/9p/fid.c
+++ b/fs/9p/fid.c
@@ -97,6 +97,34 @@ static struct p9_fid *v9fs_fid_find(struct dentry *dentry, u32 uid, int any)
97 return ret; 97 return ret;
98} 98}
99 99
100/*
101 * We need to hold v9ses->rename_sem as long as we hold references
102 * to returned path array. Array element contain pointers to
103 * dentry names.
104 */
105static int build_path_from_dentry(struct v9fs_session_info *v9ses,
106 struct dentry *dentry, char ***names)
107{
108 int n = 0, i;
109 char **wnames;
110 struct dentry *ds;
111
112 for (ds = dentry; !IS_ROOT(ds); ds = ds->d_parent)
113 n++;
114
115 wnames = kmalloc(sizeof(char *) * n, GFP_KERNEL);
116 if (!wnames)
117 goto err_out;
118
119 for (ds = dentry, i = (n-1); i >= 0; i--, ds = ds->d_parent)
120 wnames[i] = (char *)ds->d_name.name;
121
122 *names = wnames;
123 return n;
124err_out:
125 return -ENOMEM;
126}
127
100/** 128/**
101 * v9fs_fid_lookup - lookup for a fid, try to walk if not found 129 * v9fs_fid_lookup - lookup for a fid, try to walk if not found
102 * @dentry: dentry to look for fid in 130 * @dentry: dentry to look for fid in
@@ -112,7 +140,7 @@ struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
112 int i, n, l, clone, any, access; 140 int i, n, l, clone, any, access;
113 u32 uid; 141 u32 uid;
114 struct p9_fid *fid, *old_fid = NULL; 142 struct p9_fid *fid, *old_fid = NULL;
115 struct dentry *d, *ds; 143 struct dentry *ds;
116 struct v9fs_session_info *v9ses; 144 struct v9fs_session_info *v9ses;
117 char **wnames, *uname; 145 char **wnames, *uname;
118 146
@@ -139,49 +167,62 @@ struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
139 fid = v9fs_fid_find(dentry, uid, any); 167 fid = v9fs_fid_find(dentry, uid, any);
140 if (fid) 168 if (fid)
141 return fid; 169 return fid;
142 170 /*
171 * we don't have a matching fid. To do a TWALK we need
172 * parent fid. We need to prevent rename when we want to
173 * look at the parent.
174 */
175 down_read(&v9ses->rename_sem);
143 ds = dentry->d_parent; 176 ds = dentry->d_parent;
144 fid = v9fs_fid_find(ds, uid, any); 177 fid = v9fs_fid_find(ds, uid, any);
145 if (!fid) { /* walk from the root */ 178 if (fid) {
146 n = 0; 179 /* Found the parent fid do a lookup with that */
147 for (ds = dentry; !IS_ROOT(ds); ds = ds->d_parent) 180 fid = p9_client_walk(fid, 1, (char **)&dentry->d_name.name, 1);
148 n++; 181 goto fid_out;
182 }
183 up_read(&v9ses->rename_sem);
149 184
150 fid = v9fs_fid_find(ds, uid, any); 185 /* start from the root and try to do a lookup */
151 if (!fid) { /* the user is not attached to the fs yet */ 186 fid = v9fs_fid_find(dentry->d_sb->s_root, uid, any);
152 if (access == V9FS_ACCESS_SINGLE) 187 if (!fid) {
153 return ERR_PTR(-EPERM); 188 /* the user is not attached to the fs yet */
189 if (access == V9FS_ACCESS_SINGLE)
190 return ERR_PTR(-EPERM);
154 191
155 if (v9fs_proto_dotu(v9ses)) 192 if (v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses))
156 uname = NULL; 193 uname = NULL;
157 else 194 else
158 uname = v9ses->uname; 195 uname = v9ses->uname;
159 196
160 fid = p9_client_attach(v9ses->clnt, NULL, uname, uid, 197 fid = p9_client_attach(v9ses->clnt, NULL, uname, uid,
161 v9ses->aname); 198 v9ses->aname);
162 199 if (IS_ERR(fid))
163 if (IS_ERR(fid)) 200 return fid;
164 return fid;
165
166 v9fs_fid_add(ds, fid);
167 }
168 } else /* walk from the parent */
169 n = 1;
170 201
171 if (ds == dentry) 202 v9fs_fid_add(dentry->d_sb->s_root, fid);
203 }
204 /* If we are root ourself just return that */
205 if (dentry->d_sb->s_root == dentry)
172 return fid; 206 return fid;
173 207 /*
174 wnames = kmalloc(sizeof(char *) * n, GFP_KERNEL); 208 * Do a multipath walk with attached root.
175 if (!wnames) 209 * When walking parent we need to make sure we
176 return ERR_PTR(-ENOMEM); 210 * don't have a parallel rename happening
177 211 */
178 for (d = dentry, i = (n-1); i >= 0; i--, d = d->d_parent) 212 down_read(&v9ses->rename_sem);
179 wnames[i] = (char *) d->d_name.name; 213 n = build_path_from_dentry(v9ses, dentry, &wnames);
180 214 if (n < 0) {
215 fid = ERR_PTR(n);
216 goto err_out;
217 }
181 clone = 1; 218 clone = 1;
182 i = 0; 219 i = 0;
183 while (i < n) { 220 while (i < n) {
184 l = min(n - i, P9_MAXWELEM); 221 l = min(n - i, P9_MAXWELEM);
222 /*
223 * We need to hold rename lock when doing a multipath
224 * walk to ensure none of the patch component change
225 */
185 fid = p9_client_walk(fid, l, &wnames[i], clone); 226 fid = p9_client_walk(fid, l, &wnames[i], clone);
186 if (IS_ERR(fid)) { 227 if (IS_ERR(fid)) {
187 if (old_fid) { 228 if (old_fid) {
@@ -193,15 +234,17 @@ struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
193 p9_client_clunk(old_fid); 234 p9_client_clunk(old_fid);
194 } 235 }
195 kfree(wnames); 236 kfree(wnames);
196 return fid; 237 goto err_out;
197 } 238 }
198 old_fid = fid; 239 old_fid = fid;
199 i += l; 240 i += l;
200 clone = 0; 241 clone = 0;
201 } 242 }
202
203 kfree(wnames); 243 kfree(wnames);
244fid_out:
204 v9fs_fid_add(dentry, fid); 245 v9fs_fid_add(dentry, fid);
246err_out:
247 up_read(&v9ses->rename_sem);
205 return fid; 248 return fid;
206} 249}
207 250
diff --git a/fs/9p/v9fs.c b/fs/9p/v9fs.c
index f8b86e92cd66..38dc0e067599 100644
--- a/fs/9p/v9fs.c
+++ b/fs/9p/v9fs.c
@@ -237,6 +237,7 @@ struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
237 __putname(v9ses->uname); 237 __putname(v9ses->uname);
238 return ERR_PTR(-ENOMEM); 238 return ERR_PTR(-ENOMEM);
239 } 239 }
240 init_rwsem(&v9ses->rename_sem);
240 241
241 rc = bdi_setup_and_register(&v9ses->bdi, "9p", BDI_CAP_MAP_COPY); 242 rc = bdi_setup_and_register(&v9ses->bdi, "9p", BDI_CAP_MAP_COPY);
242 if (rc) { 243 if (rc) {
@@ -278,7 +279,7 @@ struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
278 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ; 279 v9ses->maxdata = v9ses->clnt->msize - P9_IOHDRSZ;
279 280
280 /* for legacy mode, fall back to V9FS_ACCESS_ANY */ 281 /* for legacy mode, fall back to V9FS_ACCESS_ANY */
281 if (!v9fs_proto_dotu(v9ses) && 282 if (!(v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses)) &&
282 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) { 283 ((v9ses->flags&V9FS_ACCESS_MASK) == V9FS_ACCESS_USER)) {
283 284
284 v9ses->flags &= ~V9FS_ACCESS_MASK; 285 v9ses->flags &= ~V9FS_ACCESS_MASK;
diff --git a/fs/9p/v9fs.h b/fs/9p/v9fs.h
index bec4d0bcb458..4c963c9fc41f 100644
--- a/fs/9p/v9fs.h
+++ b/fs/9p/v9fs.h
@@ -104,6 +104,7 @@ struct v9fs_session_info {
104 struct p9_client *clnt; /* 9p client */ 104 struct p9_client *clnt; /* 9p client */
105 struct list_head slist; /* list of sessions registered with v9fs */ 105 struct list_head slist; /* list of sessions registered with v9fs */
106 struct backing_dev_info bdi; 106 struct backing_dev_info bdi;
107 struct rw_semaphore rename_sem;
107}; 108};
108 109
109struct p9_fid *v9fs_session_init(struct v9fs_session_info *, const char *, 110struct p9_fid *v9fs_session_init(struct v9fs_session_info *, const char *,
diff --git a/fs/9p/v9fs_vfs.h b/fs/9p/v9fs_vfs.h
index 32ef4009d030..f47c6bbb01b3 100644
--- a/fs/9p/v9fs_vfs.h
+++ b/fs/9p/v9fs_vfs.h
@@ -55,6 +55,7 @@ struct inode *v9fs_get_inode(struct super_block *sb, int mode);
55void v9fs_clear_inode(struct inode *inode); 55void v9fs_clear_inode(struct inode *inode);
56ino_t v9fs_qid2ino(struct p9_qid *qid); 56ino_t v9fs_qid2ino(struct p9_qid *qid);
57void v9fs_stat2inode(struct p9_wstat *, struct inode *, struct super_block *); 57void v9fs_stat2inode(struct p9_wstat *, struct inode *, struct super_block *);
58void v9fs_stat2inode_dotl(struct p9_stat_dotl *, struct inode *);
58int v9fs_dir_release(struct inode *inode, struct file *filp); 59int v9fs_dir_release(struct inode *inode, struct file *filp);
59int v9fs_file_open(struct inode *inode, struct file *file); 60int v9fs_file_open(struct inode *inode, struct file *file);
60void v9fs_inode2stat(struct inode *inode, struct p9_wstat *stat); 61void v9fs_inode2stat(struct inode *inode, struct p9_wstat *stat);
diff --git a/fs/9p/vfs_dir.c b/fs/9p/vfs_dir.c
index 36d961f342af..16c8a2a98c1b 100644
--- a/fs/9p/vfs_dir.c
+++ b/fs/9p/vfs_dir.c
@@ -87,29 +87,19 @@ static void p9stat_init(struct p9_wstat *stbuf)
87} 87}
88 88
89/** 89/**
90 * v9fs_dir_readdir - read a directory 90 * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
91 * @filp: opened file structure 91 * @filp: opened file structure
92 * @dirent: directory structure ??? 92 * @buflen: Length in bytes of buffer to allocate
93 * @filldir: function to populate directory structure ???
94 * 93 *
95 */ 94 */
96 95
97static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir) 96static int v9fs_alloc_rdir_buf(struct file *filp, int buflen)
98{ 97{
99 int over;
100 struct p9_wstat st;
101 int err = 0;
102 struct p9_fid *fid;
103 int buflen;
104 int reclen = 0;
105 struct p9_rdir *rdir; 98 struct p9_rdir *rdir;
99 struct p9_fid *fid;
100 int err = 0;
106 101
107 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
108 fid = filp->private_data; 102 fid = filp->private_data;
109
110 buflen = fid->clnt->msize - P9_IOHDRSZ;
111
112 /* allocate rdir on demand */
113 if (!fid->rdir) { 103 if (!fid->rdir) {
114 rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL); 104 rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
115 105
@@ -128,6 +118,36 @@ static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
128 spin_unlock(&filp->f_dentry->d_lock); 118 spin_unlock(&filp->f_dentry->d_lock);
129 kfree(rdir); 119 kfree(rdir);
130 } 120 }
121exit:
122 return err;
123}
124
125/**
126 * v9fs_dir_readdir - read a directory
127 * @filp: opened file structure
128 * @dirent: directory structure ???
129 * @filldir: function to populate directory structure ???
130 *
131 */
132
133static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
134{
135 int over;
136 struct p9_wstat st;
137 int err = 0;
138 struct p9_fid *fid;
139 int buflen;
140 int reclen = 0;
141 struct p9_rdir *rdir;
142
143 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
144 fid = filp->private_data;
145
146 buflen = fid->clnt->msize - P9_IOHDRSZ;
147
148 err = v9fs_alloc_rdir_buf(filp, buflen);
149 if (err)
150 goto exit;
131 rdir = (struct p9_rdir *) fid->rdir; 151 rdir = (struct p9_rdir *) fid->rdir;
132 152
133 err = mutex_lock_interruptible(&rdir->mutex); 153 err = mutex_lock_interruptible(&rdir->mutex);
@@ -176,6 +196,88 @@ exit:
176 return err; 196 return err;
177} 197}
178 198
199/**
200 * v9fs_dir_readdir_dotl - read a directory
201 * @filp: opened file structure
202 * @dirent: buffer to fill dirent structures
203 * @filldir: function to populate dirent structures
204 *
205 */
206static int v9fs_dir_readdir_dotl(struct file *filp, void *dirent,
207 filldir_t filldir)
208{
209 int over;
210 int err = 0;
211 struct p9_fid *fid;
212 int buflen;
213 struct p9_rdir *rdir;
214 struct p9_dirent curdirent;
215 u64 oldoffset = 0;
216
217 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
218 fid = filp->private_data;
219
220 buflen = fid->clnt->msize - P9_READDIRHDRSZ;
221
222 err = v9fs_alloc_rdir_buf(filp, buflen);
223 if (err)
224 goto exit;
225 rdir = (struct p9_rdir *) fid->rdir;
226
227 err = mutex_lock_interruptible(&rdir->mutex);
228 if (err)
229 return err;
230
231 while (err == 0) {
232 if (rdir->tail == rdir->head) {
233 err = p9_client_readdir(fid, rdir->buf, buflen,
234 filp->f_pos);
235 if (err <= 0)
236 goto unlock_and_exit;
237
238 rdir->head = 0;
239 rdir->tail = err;
240 }
241
242 while (rdir->head < rdir->tail) {
243
244 err = p9dirent_read(rdir->buf + rdir->head,
245 buflen - rdir->head, &curdirent,
246 fid->clnt->proto_version);
247 if (err < 0) {
248 P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
249 err = -EIO;
250 goto unlock_and_exit;
251 }
252
253 /* d_off in dirent structure tracks the offset into
254 * the next dirent in the dir. However, filldir()
255 * expects offset into the current dirent. Hence
256 * while calling filldir send the offset from the
257 * previous dirent structure.
258 */
259 over = filldir(dirent, curdirent.d_name,
260 strlen(curdirent.d_name),
261 oldoffset, v9fs_qid2ino(&curdirent.qid),
262 curdirent.d_type);
263 oldoffset = curdirent.d_off;
264
265 if (over) {
266 err = 0;
267 goto unlock_and_exit;
268 }
269
270 filp->f_pos = curdirent.d_off;
271 rdir->head += err;
272 }
273 }
274
275unlock_and_exit:
276 mutex_unlock(&rdir->mutex);
277exit:
278 return err;
279}
280
179 281
180/** 282/**
181 * v9fs_dir_release - close a directory 283 * v9fs_dir_release - close a directory
@@ -207,7 +309,7 @@ const struct file_operations v9fs_dir_operations = {
207const struct file_operations v9fs_dir_operations_dotl = { 309const struct file_operations v9fs_dir_operations_dotl = {
208 .read = generic_read_dir, 310 .read = generic_read_dir,
209 .llseek = generic_file_llseek, 311 .llseek = generic_file_llseek,
210 .readdir = v9fs_dir_readdir, 312 .readdir = v9fs_dir_readdir_dotl,
211 .open = v9fs_file_open, 313 .open = v9fs_file_open,
212 .release = v9fs_dir_release, 314 .release = v9fs_dir_release,
213}; 315};
diff --git a/fs/9p/vfs_file.c b/fs/9p/vfs_file.c
index 2bedc6c94fc2..e97c92bd6f16 100644
--- a/fs/9p/vfs_file.c
+++ b/fs/9p/vfs_file.c
@@ -59,9 +59,13 @@ int v9fs_file_open(struct inode *inode, struct file *file)
59 struct p9_fid *fid; 59 struct p9_fid *fid;
60 int omode; 60 int omode;
61 61
62 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p \n", inode, file); 62 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
63 v9ses = v9fs_inode2v9ses(inode); 63 v9ses = v9fs_inode2v9ses(inode);
64 omode = v9fs_uflags2omode(file->f_flags, v9fs_proto_dotu(v9ses)); 64 if (v9fs_proto_dotl(v9ses))
65 omode = file->f_flags;
66 else
67 omode = v9fs_uflags2omode(file->f_flags,
68 v9fs_proto_dotu(v9ses));
65 fid = file->private_data; 69 fid = file->private_data;
66 if (!fid) { 70 if (!fid) {
67 fid = v9fs_fid_clone(file->f_path.dentry); 71 fid = v9fs_fid_clone(file->f_path.dentry);
@@ -73,11 +77,12 @@ int v9fs_file_open(struct inode *inode, struct file *file)
73 p9_client_clunk(fid); 77 p9_client_clunk(fid);
74 return err; 78 return err;
75 } 79 }
76 if (omode & P9_OTRUNC) { 80 if (file->f_flags & O_TRUNC) {
77 i_size_write(inode, 0); 81 i_size_write(inode, 0);
78 inode->i_blocks = 0; 82 inode->i_blocks = 0;
79 } 83 }
80 if ((file->f_flags & O_APPEND) && (!v9fs_proto_dotu(v9ses))) 84 if ((file->f_flags & O_APPEND) &&
85 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
81 generic_file_llseek(file, 0, SEEK_END); 86 generic_file_llseek(file, 0, SEEK_END);
82 } 87 }
83 88
@@ -139,7 +144,7 @@ ssize_t
139v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count, 144v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
140 u64 offset) 145 u64 offset)
141{ 146{
142 int n, total; 147 int n, total, size;
143 struct p9_fid *fid = filp->private_data; 148 struct p9_fid *fid = filp->private_data;
144 149
145 P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid, 150 P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
@@ -147,6 +152,7 @@ v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
147 152
148 n = 0; 153 n = 0;
149 total = 0; 154 total = 0;
155 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
150 do { 156 do {
151 n = p9_client_read(fid, data, udata, offset, count); 157 n = p9_client_read(fid, data, udata, offset, count);
152 if (n <= 0) 158 if (n <= 0)
@@ -160,7 +166,7 @@ v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
160 offset += n; 166 offset += n;
161 count -= n; 167 count -= n;
162 total += n; 168 total += n;
163 } while (count > 0 && n == (fid->clnt->msize - P9_IOHDRSZ)); 169 } while (count > 0 && n == size);
164 170
165 if (n < 0) 171 if (n < 0)
166 total = n; 172 total = n;
@@ -183,11 +189,13 @@ v9fs_file_read(struct file *filp, char __user *udata, size_t count,
183{ 189{
184 int ret; 190 int ret;
185 struct p9_fid *fid; 191 struct p9_fid *fid;
192 size_t size;
186 193
187 P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset); 194 P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
188 fid = filp->private_data; 195 fid = filp->private_data;
189 196
190 if (count > (fid->clnt->msize - P9_IOHDRSZ)) 197 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
198 if (count > size)
191 ret = v9fs_file_readn(filp, NULL, udata, count, *offset); 199 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
192 else 200 else
193 ret = p9_client_read(fid, NULL, udata, *offset, count); 201 ret = p9_client_read(fid, NULL, udata, *offset, count);
@@ -224,9 +232,7 @@ v9fs_file_write(struct file *filp, const char __user * data,
224 fid = filp->private_data; 232 fid = filp->private_data;
225 clnt = fid->clnt; 233 clnt = fid->clnt;
226 234
227 rsize = fid->iounit; 235 rsize = fid->iounit ? fid->iounit : clnt->msize - P9_IOHDRSZ;
228 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
229 rsize = clnt->msize - P9_IOHDRSZ;
230 236
231 do { 237 do {
232 if (count < rsize) 238 if (count < rsize)
diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
index 4331b3b5ee1c..6e94f3247cec 100644
--- a/fs/9p/vfs_inode.c
+++ b/fs/9p/vfs_inode.c
@@ -35,6 +35,7 @@
35#include <linux/idr.h> 35#include <linux/idr.h>
36#include <linux/sched.h> 36#include <linux/sched.h>
37#include <linux/slab.h> 37#include <linux/slab.h>
38#include <linux/xattr.h>
38#include <net/9p/9p.h> 39#include <net/9p/9p.h>
39#include <net/9p/client.h> 40#include <net/9p/client.h>
40 41
@@ -42,6 +43,7 @@
42#include "v9fs_vfs.h" 43#include "v9fs_vfs.h"
43#include "fid.h" 44#include "fid.h"
44#include "cache.h" 45#include "cache.h"
46#include "xattr.h"
45 47
46static const struct inode_operations v9fs_dir_inode_operations; 48static const struct inode_operations v9fs_dir_inode_operations;
47static const struct inode_operations v9fs_dir_inode_operations_dotu; 49static const struct inode_operations v9fs_dir_inode_operations_dotu;
@@ -236,6 +238,41 @@ void v9fs_destroy_inode(struct inode *inode)
236#endif 238#endif
237 239
238/** 240/**
241 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
242 * new file system object. This checks the S_ISGID to determine the owning
243 * group of the new file system object.
244 */
245
246static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
247{
248 BUG_ON(dir_inode == NULL);
249
250 if (dir_inode->i_mode & S_ISGID) {
251 /* set_gid bit is set.*/
252 return dir_inode->i_gid;
253 }
254 return current_fsgid();
255}
256
257/**
258 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
259 * dir inode.
260 *
261 */
262
263static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
264{
265 struct dentry *dentry;
266
267 spin_lock(&dcache_lock);
268 /* Directory should have only one entry. */
269 BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
270 dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
271 spin_unlock(&dcache_lock);
272 return dentry;
273}
274
275/**
239 * v9fs_get_inode - helper function to setup an inode 276 * v9fs_get_inode - helper function to setup an inode
240 * @sb: superblock 277 * @sb: superblock
241 * @mode: mode to setup inode with 278 * @mode: mode to setup inode with
@@ -267,7 +304,13 @@ struct inode *v9fs_get_inode(struct super_block *sb, int mode)
267 case S_IFBLK: 304 case S_IFBLK:
268 case S_IFCHR: 305 case S_IFCHR:
269 case S_IFSOCK: 306 case S_IFSOCK:
270 if (!v9fs_proto_dotu(v9ses)) { 307 if (v9fs_proto_dotl(v9ses)) {
308 inode->i_op = &v9fs_file_inode_operations_dotl;
309 inode->i_fop = &v9fs_file_operations_dotl;
310 } else if (v9fs_proto_dotu(v9ses)) {
311 inode->i_op = &v9fs_file_inode_operations;
312 inode->i_fop = &v9fs_file_operations;
313 } else {
271 P9_DPRINTK(P9_DEBUG_ERROR, 314 P9_DPRINTK(P9_DEBUG_ERROR,
272 "special files without extended mode\n"); 315 "special files without extended mode\n");
273 err = -EINVAL; 316 err = -EINVAL;
@@ -396,23 +439,14 @@ void v9fs_clear_inode(struct inode *inode)
396#endif 439#endif
397} 440}
398 441
399/**
400 * v9fs_inode_from_fid - populate an inode by issuing a attribute request
401 * @v9ses: session information
402 * @fid: fid to issue attribute request for
403 * @sb: superblock on which to create inode
404 *
405 */
406
407static struct inode * 442static struct inode *
408v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid, 443v9fs_inode(struct v9fs_session_info *v9ses, struct p9_fid *fid,
409 struct super_block *sb) 444 struct super_block *sb)
410{ 445{
411 int err, umode; 446 int err, umode;
412 struct inode *ret; 447 struct inode *ret = NULL;
413 struct p9_wstat *st; 448 struct p9_wstat *st;
414 449
415 ret = NULL;
416 st = p9_client_stat(fid); 450 st = p9_client_stat(fid);
417 if (IS_ERR(st)) 451 if (IS_ERR(st))
418 return ERR_CAST(st); 452 return ERR_CAST(st);
@@ -433,15 +467,62 @@ v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
433#endif 467#endif
434 p9stat_free(st); 468 p9stat_free(st);
435 kfree(st); 469 kfree(st);
436
437 return ret; 470 return ret;
438
439error: 471error:
440 p9stat_free(st); 472 p9stat_free(st);
441 kfree(st); 473 kfree(st);
442 return ERR_PTR(err); 474 return ERR_PTR(err);
443} 475}
444 476
477static struct inode *
478v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
479 struct super_block *sb)
480{
481 struct inode *ret = NULL;
482 int err;
483 struct p9_stat_dotl *st;
484
485 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
486 if (IS_ERR(st))
487 return ERR_CAST(st);
488
489 ret = v9fs_get_inode(sb, st->st_mode);
490 if (IS_ERR(ret)) {
491 err = PTR_ERR(ret);
492 goto error;
493 }
494
495 v9fs_stat2inode_dotl(st, ret);
496 ret->i_ino = v9fs_qid2ino(&st->qid);
497#ifdef CONFIG_9P_FSCACHE
498 v9fs_vcookie_set_qid(ret, &st->qid);
499 v9fs_cache_inode_get_cookie(ret);
500#endif
501 kfree(st);
502 return ret;
503error:
504 kfree(st);
505 return ERR_PTR(err);
506}
507
508/**
509 * v9fs_inode_from_fid - Helper routine to populate an inode by
510 * issuing a attribute request
511 * @v9ses: session information
512 * @fid: fid to issue attribute request for
513 * @sb: superblock on which to create inode
514 *
515 */
516static inline struct inode *
517v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
518 struct super_block *sb)
519{
520 if (v9fs_proto_dotl(v9ses))
521 return v9fs_inode_dotl(v9ses, fid, sb);
522 else
523 return v9fs_inode(v9ses, fid, sb);
524}
525
445/** 526/**
446 * v9fs_remove - helper function to remove files and directories 527 * v9fs_remove - helper function to remove files and directories
447 * @dir: directory inode that is being deleted 528 * @dir: directory inode that is being deleted
@@ -563,6 +644,118 @@ error:
563} 644}
564 645
565/** 646/**
647 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
648 * @dir: directory inode that is being created
649 * @dentry: dentry that is being deleted
650 * @mode: create permissions
651 * @nd: path information
652 *
653 */
654
655static int
656v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int mode,
657 struct nameidata *nd)
658{
659 int err = 0;
660 char *name = NULL;
661 gid_t gid;
662 int flags;
663 struct v9fs_session_info *v9ses;
664 struct p9_fid *fid = NULL;
665 struct p9_fid *dfid, *ofid;
666 struct file *filp;
667 struct p9_qid qid;
668 struct inode *inode;
669
670 v9ses = v9fs_inode2v9ses(dir);
671 if (nd && nd->flags & LOOKUP_OPEN)
672 flags = nd->intent.open.flags - 1;
673 else
674 flags = O_RDWR;
675
676 name = (char *) dentry->d_name.name;
677 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
678 "mode:0x%x\n", name, flags, mode);
679
680 dfid = v9fs_fid_lookup(dentry->d_parent);
681 if (IS_ERR(dfid)) {
682 err = PTR_ERR(dfid);
683 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
684 return err;
685 }
686
687 /* clone a fid to use for creation */
688 ofid = p9_client_walk(dfid, 0, NULL, 1);
689 if (IS_ERR(ofid)) {
690 err = PTR_ERR(ofid);
691 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
692 return err;
693 }
694
695 gid = v9fs_get_fsgid_for_create(dir);
696 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
697 if (err < 0) {
698 P9_DPRINTK(P9_DEBUG_VFS,
699 "p9_client_open_dotl failed in creat %d\n",
700 err);
701 goto error;
702 }
703
704 /* No need to populate the inode if we are not opening the file AND
705 * not in cached mode.
706 */
707 if (!v9ses->cache && !(nd && nd->flags & LOOKUP_OPEN)) {
708 /* Not in cached mode. No need to populate inode with stat */
709 dentry->d_op = &v9fs_dentry_operations;
710 p9_client_clunk(ofid);
711 d_instantiate(dentry, NULL);
712 return 0;
713 }
714
715 /* Now walk from the parent so we can get an unopened fid. */
716 fid = p9_client_walk(dfid, 1, &name, 1);
717 if (IS_ERR(fid)) {
718 err = PTR_ERR(fid);
719 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
720 fid = NULL;
721 goto error;
722 }
723
724 /* instantiate inode and assign the unopened fid to dentry */
725 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
726 if (IS_ERR(inode)) {
727 err = PTR_ERR(inode);
728 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
729 goto error;
730 }
731 dentry->d_op = &v9fs_cached_dentry_operations;
732 d_instantiate(dentry, inode);
733 err = v9fs_fid_add(dentry, fid);
734 if (err < 0)
735 goto error;
736
737 /* if we are opening a file, assign the open fid to the file */
738 if (nd && nd->flags & LOOKUP_OPEN) {
739 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
740 if (IS_ERR(filp)) {
741 p9_client_clunk(ofid);
742 return PTR_ERR(filp);
743 }
744 filp->private_data = ofid;
745 } else
746 p9_client_clunk(ofid);
747
748 return 0;
749
750error:
751 if (ofid)
752 p9_client_clunk(ofid);
753 if (fid)
754 p9_client_clunk(fid);
755 return err;
756}
757
758/**
566 * v9fs_vfs_create - VFS hook to create files 759 * v9fs_vfs_create - VFS hook to create files
567 * @dir: directory inode that is being created 760 * @dir: directory inode that is being created
568 * @dentry: dentry that is being deleted 761 * @dentry: dentry that is being deleted
@@ -652,6 +845,83 @@ static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
652 return err; 845 return err;
653} 846}
654 847
848
849/**
850 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
851 * @dir: inode that is being unlinked
852 * @dentry: dentry that is being unlinked
853 * @mode: mode for new directory
854 *
855 */
856
857static int v9fs_vfs_mkdir_dotl(struct inode *dir, struct dentry *dentry,
858 int mode)
859{
860 int err;
861 struct v9fs_session_info *v9ses;
862 struct p9_fid *fid = NULL, *dfid = NULL;
863 gid_t gid;
864 char *name;
865 struct inode *inode;
866 struct p9_qid qid;
867 struct dentry *dir_dentry;
868
869 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
870 err = 0;
871 v9ses = v9fs_inode2v9ses(dir);
872
873 mode |= S_IFDIR;
874 dir_dentry = v9fs_dentry_from_dir_inode(dir);
875 dfid = v9fs_fid_lookup(dir_dentry);
876 if (IS_ERR(dfid)) {
877 err = PTR_ERR(dfid);
878 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
879 dfid = NULL;
880 goto error;
881 }
882
883 gid = v9fs_get_fsgid_for_create(dir);
884 if (gid < 0) {
885 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
886 goto error;
887 }
888
889 name = (char *) dentry->d_name.name;
890 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
891 if (err < 0)
892 goto error;
893
894 /* instantiate inode and assign the unopened fid to the dentry */
895 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
896 fid = p9_client_walk(dfid, 1, &name, 1);
897 if (IS_ERR(fid)) {
898 err = PTR_ERR(fid);
899 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
900 err);
901 fid = NULL;
902 goto error;
903 }
904
905 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
906 if (IS_ERR(inode)) {
907 err = PTR_ERR(inode);
908 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
909 err);
910 goto error;
911 }
912 dentry->d_op = &v9fs_cached_dentry_operations;
913 d_instantiate(dentry, inode);
914 err = v9fs_fid_add(dentry, fid);
915 if (err < 0)
916 goto error;
917 fid = NULL;
918 }
919error:
920 if (fid)
921 p9_client_clunk(fid);
922 return err;
923}
924
655/** 925/**
656 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode 926 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
657 * @dir: inode that is being walked from 927 * @dir: inode that is being walked from
@@ -678,6 +948,7 @@ static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
678 948
679 sb = dir->i_sb; 949 sb = dir->i_sb;
680 v9ses = v9fs_inode2v9ses(dir); 950 v9ses = v9fs_inode2v9ses(dir);
951 /* We can walk d_parent because we hold the dir->i_mutex */
681 dfid = v9fs_fid_lookup(dentry->d_parent); 952 dfid = v9fs_fid_lookup(dentry->d_parent);
682 if (IS_ERR(dfid)) 953 if (IS_ERR(dfid))
683 return ERR_CAST(dfid); 954 return ERR_CAST(dfid);
@@ -785,27 +1056,33 @@ v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
785 goto clunk_olddir; 1056 goto clunk_olddir;
786 } 1057 }
787 1058
1059 down_write(&v9ses->rename_sem);
788 if (v9fs_proto_dotl(v9ses)) { 1060 if (v9fs_proto_dotl(v9ses)) {
789 retval = p9_client_rename(oldfid, newdirfid, 1061 retval = p9_client_rename(oldfid, newdirfid,
790 (char *) new_dentry->d_name.name); 1062 (char *) new_dentry->d_name.name);
791 if (retval != -ENOSYS) 1063 if (retval != -ENOSYS)
792 goto clunk_newdir; 1064 goto clunk_newdir;
793 } 1065 }
1066 if (old_dentry->d_parent != new_dentry->d_parent) {
1067 /*
1068 * 9P .u can only handle file rename in the same directory
1069 */
794 1070
795 /* 9P can only handle file rename in the same directory */
796 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
797 P9_DPRINTK(P9_DEBUG_ERROR, 1071 P9_DPRINTK(P9_DEBUG_ERROR,
798 "old dir and new dir are different\n"); 1072 "old dir and new dir are different\n");
799 retval = -EXDEV; 1073 retval = -EXDEV;
800 goto clunk_newdir; 1074 goto clunk_newdir;
801 } 1075 }
802
803 v9fs_blank_wstat(&wstat); 1076 v9fs_blank_wstat(&wstat);
804 wstat.muid = v9ses->uname; 1077 wstat.muid = v9ses->uname;
805 wstat.name = (char *) new_dentry->d_name.name; 1078 wstat.name = (char *) new_dentry->d_name.name;
806 retval = p9_client_wstat(oldfid, &wstat); 1079 retval = p9_client_wstat(oldfid, &wstat);
807 1080
808clunk_newdir: 1081clunk_newdir:
1082 if (!retval)
1083 /* successful rename */
1084 d_move(old_dentry, new_dentry);
1085 up_write(&v9ses->rename_sem);
809 p9_client_clunk(newdirfid); 1086 p9_client_clunk(newdirfid);
810 1087
811clunk_olddir: 1088clunk_olddir:
@@ -853,6 +1130,42 @@ v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
853 return 0; 1130 return 0;
854} 1131}
855 1132
1133static int
1134v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
1135 struct kstat *stat)
1136{
1137 int err;
1138 struct v9fs_session_info *v9ses;
1139 struct p9_fid *fid;
1140 struct p9_stat_dotl *st;
1141
1142 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1143 err = -EPERM;
1144 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1145 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
1146 return simple_getattr(mnt, dentry, stat);
1147
1148 fid = v9fs_fid_lookup(dentry);
1149 if (IS_ERR(fid))
1150 return PTR_ERR(fid);
1151
1152 /* Ask for all the fields in stat structure. Server will return
1153 * whatever it supports
1154 */
1155
1156 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
1157 if (IS_ERR(st))
1158 return PTR_ERR(st);
1159
1160 v9fs_stat2inode_dotl(st, dentry->d_inode);
1161 generic_fillattr(dentry->d_inode, stat);
1162 /* Change block size to what the server returned */
1163 stat->blksize = st->st_blksize;
1164
1165 kfree(st);
1166 return 0;
1167}
1168
856/** 1169/**
857 * v9fs_vfs_setattr - set file metadata 1170 * v9fs_vfs_setattr - set file metadata
858 * @dentry: file whose metadata to set 1171 * @dentry: file whose metadata to set
@@ -903,6 +1216,49 @@ static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
903} 1216}
904 1217
905/** 1218/**
1219 * v9fs_vfs_setattr_dotl - set file metadata
1220 * @dentry: file whose metadata to set
1221 * @iattr: metadata assignment structure
1222 *
1223 */
1224
1225static int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
1226{
1227 int retval;
1228 struct v9fs_session_info *v9ses;
1229 struct p9_fid *fid;
1230 struct p9_iattr_dotl p9attr;
1231
1232 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1233
1234 retval = inode_change_ok(dentry->d_inode, iattr);
1235 if (retval)
1236 return retval;
1237
1238 p9attr.valid = iattr->ia_valid;
1239 p9attr.mode = iattr->ia_mode;
1240 p9attr.uid = iattr->ia_uid;
1241 p9attr.gid = iattr->ia_gid;
1242 p9attr.size = iattr->ia_size;
1243 p9attr.atime_sec = iattr->ia_atime.tv_sec;
1244 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
1245 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
1246 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
1247
1248 retval = -EPERM;
1249 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1250 fid = v9fs_fid_lookup(dentry);
1251 if (IS_ERR(fid))
1252 return PTR_ERR(fid);
1253
1254 retval = p9_client_setattr(fid, &p9attr);
1255 if (retval >= 0)
1256 retval = inode_setattr(dentry->d_inode, iattr);
1257
1258 return retval;
1259}
1260
1261/**
906 * v9fs_stat2inode - populate an inode structure with mistat info 1262 * v9fs_stat2inode - populate an inode structure with mistat info
907 * @stat: Plan 9 metadata (mistat) structure 1263 * @stat: Plan 9 metadata (mistat) structure
908 * @inode: inode to populate 1264 * @inode: inode to populate
@@ -980,6 +1336,77 @@ v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
980} 1336}
981 1337
982/** 1338/**
1339 * v9fs_stat2inode_dotl - populate an inode structure with stat info
1340 * @stat: stat structure
1341 * @inode: inode to populate
1342 * @sb: superblock of filesystem
1343 *
1344 */
1345
1346void
1347v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
1348{
1349
1350 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
1351 inode->i_atime.tv_sec = stat->st_atime_sec;
1352 inode->i_atime.tv_nsec = stat->st_atime_nsec;
1353 inode->i_mtime.tv_sec = stat->st_mtime_sec;
1354 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
1355 inode->i_ctime.tv_sec = stat->st_ctime_sec;
1356 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
1357 inode->i_uid = stat->st_uid;
1358 inode->i_gid = stat->st_gid;
1359 inode->i_nlink = stat->st_nlink;
1360 inode->i_mode = stat->st_mode;
1361 inode->i_rdev = new_decode_dev(stat->st_rdev);
1362
1363 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
1364 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1365
1366 i_size_write(inode, stat->st_size);
1367 inode->i_blocks = stat->st_blocks;
1368 } else {
1369 if (stat->st_result_mask & P9_STATS_ATIME) {
1370 inode->i_atime.tv_sec = stat->st_atime_sec;
1371 inode->i_atime.tv_nsec = stat->st_atime_nsec;
1372 }
1373 if (stat->st_result_mask & P9_STATS_MTIME) {
1374 inode->i_mtime.tv_sec = stat->st_mtime_sec;
1375 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
1376 }
1377 if (stat->st_result_mask & P9_STATS_CTIME) {
1378 inode->i_ctime.tv_sec = stat->st_ctime_sec;
1379 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
1380 }
1381 if (stat->st_result_mask & P9_STATS_UID)
1382 inode->i_uid = stat->st_uid;
1383 if (stat->st_result_mask & P9_STATS_GID)
1384 inode->i_gid = stat->st_gid;
1385 if (stat->st_result_mask & P9_STATS_NLINK)
1386 inode->i_nlink = stat->st_nlink;
1387 if (stat->st_result_mask & P9_STATS_MODE) {
1388 inode->i_mode = stat->st_mode;
1389 if ((S_ISBLK(inode->i_mode)) ||
1390 (S_ISCHR(inode->i_mode)))
1391 init_special_inode(inode, inode->i_mode,
1392 inode->i_rdev);
1393 }
1394 if (stat->st_result_mask & P9_STATS_RDEV)
1395 inode->i_rdev = new_decode_dev(stat->st_rdev);
1396 if (stat->st_result_mask & P9_STATS_SIZE)
1397 i_size_write(inode, stat->st_size);
1398 if (stat->st_result_mask & P9_STATS_BLOCKS)
1399 inode->i_blocks = stat->st_blocks;
1400 }
1401 if (stat->st_result_mask & P9_STATS_GEN)
1402 inode->i_generation = stat->st_gen;
1403
1404 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
1405 * because the inode structure does not have fields for them.
1406 */
1407}
1408
1409/**
983 * v9fs_qid2ino - convert qid into inode number 1410 * v9fs_qid2ino - convert qid into inode number
984 * @qid: qid to hash 1411 * @qid: qid to hash
985 * 1412 *
@@ -1022,7 +1449,7 @@ static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1022 if (IS_ERR(fid)) 1449 if (IS_ERR(fid))
1023 return PTR_ERR(fid); 1450 return PTR_ERR(fid);
1024 1451
1025 if (!v9fs_proto_dotu(v9ses)) 1452 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses))
1026 return -EBADF; 1453 return -EBADF;
1027 1454
1028 st = p9_client_stat(fid); 1455 st = p9_client_stat(fid);
@@ -1128,6 +1555,99 @@ static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1128} 1555}
1129 1556
1130/** 1557/**
1558 * v9fs_vfs_symlink_dotl - helper function to create symlinks
1559 * @dir: directory inode containing symlink
1560 * @dentry: dentry for symlink
1561 * @symname: symlink data
1562 *
1563 * See Also: 9P2000.L RFC for more information
1564 *
1565 */
1566
1567static int
1568v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
1569 const char *symname)
1570{
1571 struct v9fs_session_info *v9ses;
1572 struct p9_fid *dfid;
1573 struct p9_fid *fid = NULL;
1574 struct inode *inode;
1575 struct p9_qid qid;
1576 char *name;
1577 int err;
1578 gid_t gid;
1579
1580 name = (char *) dentry->d_name.name;
1581 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
1582 dir->i_ino, name, symname);
1583 v9ses = v9fs_inode2v9ses(dir);
1584
1585 dfid = v9fs_fid_lookup(dentry->d_parent);
1586 if (IS_ERR(dfid)) {
1587 err = PTR_ERR(dfid);
1588 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
1589 return err;
1590 }
1591
1592 gid = v9fs_get_fsgid_for_create(dir);
1593
1594 if (gid < 0) {
1595 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_egid failed %d\n", gid);
1596 goto error;
1597 }
1598
1599 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
1600 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
1601
1602 if (err < 0) {
1603 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
1604 goto error;
1605 }
1606
1607 if (v9ses->cache) {
1608 /* Now walk from the parent so we can get an unopened fid. */
1609 fid = p9_client_walk(dfid, 1, &name, 1);
1610 if (IS_ERR(fid)) {
1611 err = PTR_ERR(fid);
1612 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
1613 err);
1614 fid = NULL;
1615 goto error;
1616 }
1617
1618 /* instantiate inode and assign the unopened fid to dentry */
1619 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
1620 if (IS_ERR(inode)) {
1621 err = PTR_ERR(inode);
1622 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
1623 err);
1624 goto error;
1625 }
1626 dentry->d_op = &v9fs_cached_dentry_operations;
1627 d_instantiate(dentry, inode);
1628 err = v9fs_fid_add(dentry, fid);
1629 if (err < 0)
1630 goto error;
1631 fid = NULL;
1632 } else {
1633 /* Not in cached mode. No need to populate inode with stat */
1634 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
1635 if (IS_ERR(inode)) {
1636 err = PTR_ERR(inode);
1637 goto error;
1638 }
1639 dentry->d_op = &v9fs_dentry_operations;
1640 d_instantiate(dentry, inode);
1641 }
1642
1643error:
1644 if (fid)
1645 p9_client_clunk(fid);
1646
1647 return err;
1648}
1649
1650/**
1131 * v9fs_vfs_symlink - helper function to create symlinks 1651 * v9fs_vfs_symlink - helper function to create symlinks
1132 * @dir: directory inode containing symlink 1652 * @dir: directory inode containing symlink
1133 * @dentry: dentry for symlink 1653 * @dentry: dentry for symlink
@@ -1186,6 +1706,76 @@ clunk_fid:
1186} 1706}
1187 1707
1188/** 1708/**
1709 * v9fs_vfs_link_dotl - create a hardlink for dotl
1710 * @old_dentry: dentry for file to link to
1711 * @dir: inode destination for new link
1712 * @dentry: dentry for link
1713 *
1714 */
1715
1716static int
1717v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
1718 struct dentry *dentry)
1719{
1720 int err;
1721 struct p9_fid *dfid, *oldfid;
1722 char *name;
1723 struct v9fs_session_info *v9ses;
1724 struct dentry *dir_dentry;
1725
1726 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
1727 dir->i_ino, old_dentry->d_name.name,
1728 dentry->d_name.name);
1729
1730 v9ses = v9fs_inode2v9ses(dir);
1731 dir_dentry = v9fs_dentry_from_dir_inode(dir);
1732 dfid = v9fs_fid_lookup(dir_dentry);
1733 if (IS_ERR(dfid))
1734 return PTR_ERR(dfid);
1735
1736 oldfid = v9fs_fid_lookup(old_dentry);
1737 if (IS_ERR(oldfid))
1738 return PTR_ERR(oldfid);
1739
1740 name = (char *) dentry->d_name.name;
1741
1742 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
1743
1744 if (err < 0) {
1745 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
1746 return err;
1747 }
1748
1749 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1750 /* Get the latest stat info from server. */
1751 struct p9_fid *fid;
1752 struct p9_stat_dotl *st;
1753
1754 fid = v9fs_fid_lookup(old_dentry);
1755 if (IS_ERR(fid))
1756 return PTR_ERR(fid);
1757
1758 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
1759 if (IS_ERR(st))
1760 return PTR_ERR(st);
1761
1762 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
1763
1764 kfree(st);
1765 } else {
1766 /* Caching disabled. No need to get upto date stat info.
1767 * This dentry will be released immediately. So, just i_count++
1768 */
1769 atomic_inc(&old_dentry->d_inode->i_count);
1770 }
1771
1772 dentry->d_op = old_dentry->d_op;
1773 d_instantiate(dentry, old_dentry->d_inode);
1774
1775 return err;
1776}
1777
1778/**
1189 * v9fs_vfs_mknod - create a special file 1779 * v9fs_vfs_mknod - create a special file
1190 * @dir: inode destination for new link 1780 * @dir: inode destination for new link
1191 * @dentry: dentry for file 1781 * @dentry: dentry for file
@@ -1230,6 +1820,100 @@ v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1230 return retval; 1820 return retval;
1231} 1821}
1232 1822
1823/**
1824 * v9fs_vfs_mknod_dotl - create a special file
1825 * @dir: inode destination for new link
1826 * @dentry: dentry for file
1827 * @mode: mode for creation
1828 * @rdev: device associated with special file
1829 *
1830 */
1831static int
1832v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int mode,
1833 dev_t rdev)
1834{
1835 int err;
1836 char *name;
1837 struct v9fs_session_info *v9ses;
1838 struct p9_fid *fid = NULL, *dfid = NULL;
1839 struct inode *inode;
1840 gid_t gid;
1841 struct p9_qid qid;
1842 struct dentry *dir_dentry;
1843
1844 P9_DPRINTK(P9_DEBUG_VFS,
1845 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1846 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1847
1848 if (!new_valid_dev(rdev))
1849 return -EINVAL;
1850
1851 v9ses = v9fs_inode2v9ses(dir);
1852 dir_dentry = v9fs_dentry_from_dir_inode(dir);
1853 dfid = v9fs_fid_lookup(dir_dentry);
1854 if (IS_ERR(dfid)) {
1855 err = PTR_ERR(dfid);
1856 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
1857 dfid = NULL;
1858 goto error;
1859 }
1860
1861 gid = v9fs_get_fsgid_for_create(dir);
1862 if (gid < 0) {
1863 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
1864 goto error;
1865 }
1866
1867 name = (char *) dentry->d_name.name;
1868
1869 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
1870 if (err < 0)
1871 goto error;
1872
1873 /* instantiate inode and assign the unopened fid to the dentry */
1874 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1875 fid = p9_client_walk(dfid, 1, &name, 1);
1876 if (IS_ERR(fid)) {
1877 err = PTR_ERR(fid);
1878 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
1879 err);
1880 fid = NULL;
1881 goto error;
1882 }
1883
1884 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
1885 if (IS_ERR(inode)) {
1886 err = PTR_ERR(inode);
1887 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
1888 err);
1889 goto error;
1890 }
1891 dentry->d_op = &v9fs_cached_dentry_operations;
1892 d_instantiate(dentry, inode);
1893 err = v9fs_fid_add(dentry, fid);
1894 if (err < 0)
1895 goto error;
1896 fid = NULL;
1897 } else {
1898 /*
1899 * Not in cached mode. No need to populate inode with stat.
1900 * socket syscall returns a fd, so we need instantiate
1901 */
1902 inode = v9fs_get_inode(dir->i_sb, mode);
1903 if (IS_ERR(inode)) {
1904 err = PTR_ERR(inode);
1905 goto error;
1906 }
1907 dentry->d_op = &v9fs_dentry_operations;
1908 d_instantiate(dentry, inode);
1909 }
1910
1911error:
1912 if (fid)
1913 p9_client_clunk(fid);
1914 return err;
1915}
1916
1233static const struct inode_operations v9fs_dir_inode_operations_dotu = { 1917static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1234 .create = v9fs_vfs_create, 1918 .create = v9fs_vfs_create,
1235 .lookup = v9fs_vfs_lookup, 1919 .lookup = v9fs_vfs_lookup,
@@ -1238,24 +1922,29 @@ static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1238 .unlink = v9fs_vfs_unlink, 1922 .unlink = v9fs_vfs_unlink,
1239 .mkdir = v9fs_vfs_mkdir, 1923 .mkdir = v9fs_vfs_mkdir,
1240 .rmdir = v9fs_vfs_rmdir, 1924 .rmdir = v9fs_vfs_rmdir,
1241 .mknod = v9fs_vfs_mknod, 1925 .mknod = v9fs_vfs_mknod_dotl,
1242 .rename = v9fs_vfs_rename, 1926 .rename = v9fs_vfs_rename,
1243 .getattr = v9fs_vfs_getattr, 1927 .getattr = v9fs_vfs_getattr,
1244 .setattr = v9fs_vfs_setattr, 1928 .setattr = v9fs_vfs_setattr,
1245}; 1929};
1246 1930
1247static const struct inode_operations v9fs_dir_inode_operations_dotl = { 1931static const struct inode_operations v9fs_dir_inode_operations_dotl = {
1248 .create = v9fs_vfs_create, 1932 .create = v9fs_vfs_create_dotl,
1249 .lookup = v9fs_vfs_lookup, 1933 .lookup = v9fs_vfs_lookup,
1250 .symlink = v9fs_vfs_symlink, 1934 .link = v9fs_vfs_link_dotl,
1251 .link = v9fs_vfs_link, 1935 .symlink = v9fs_vfs_symlink_dotl,
1252 .unlink = v9fs_vfs_unlink, 1936 .unlink = v9fs_vfs_unlink,
1253 .mkdir = v9fs_vfs_mkdir, 1937 .mkdir = v9fs_vfs_mkdir_dotl,
1254 .rmdir = v9fs_vfs_rmdir, 1938 .rmdir = v9fs_vfs_rmdir,
1255 .mknod = v9fs_vfs_mknod, 1939 .mknod = v9fs_vfs_mknod_dotl,
1256 .rename = v9fs_vfs_rename, 1940 .rename = v9fs_vfs_rename,
1257 .getattr = v9fs_vfs_getattr, 1941 .getattr = v9fs_vfs_getattr_dotl,
1258 .setattr = v9fs_vfs_setattr, 1942 .setattr = v9fs_vfs_setattr_dotl,
1943 .setxattr = generic_setxattr,
1944 .getxattr = generic_getxattr,
1945 .removexattr = generic_removexattr,
1946 .listxattr = v9fs_listxattr,
1947
1259}; 1948};
1260 1949
1261static const struct inode_operations v9fs_dir_inode_operations = { 1950static const struct inode_operations v9fs_dir_inode_operations = {
@@ -1276,8 +1965,12 @@ static const struct inode_operations v9fs_file_inode_operations = {
1276}; 1965};
1277 1966
1278static const struct inode_operations v9fs_file_inode_operations_dotl = { 1967static const struct inode_operations v9fs_file_inode_operations_dotl = {
1279 .getattr = v9fs_vfs_getattr, 1968 .getattr = v9fs_vfs_getattr_dotl,
1280 .setattr = v9fs_vfs_setattr, 1969 .setattr = v9fs_vfs_setattr_dotl,
1970 .setxattr = generic_setxattr,
1971 .getxattr = generic_getxattr,
1972 .removexattr = generic_removexattr,
1973 .listxattr = v9fs_listxattr,
1281}; 1974};
1282 1975
1283static const struct inode_operations v9fs_symlink_inode_operations = { 1976static const struct inode_operations v9fs_symlink_inode_operations = {
@@ -1292,6 +1985,10 @@ static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
1292 .readlink = generic_readlink, 1985 .readlink = generic_readlink,
1293 .follow_link = v9fs_vfs_follow_link, 1986 .follow_link = v9fs_vfs_follow_link,
1294 .put_link = v9fs_vfs_put_link, 1987 .put_link = v9fs_vfs_put_link,
1295 .getattr = v9fs_vfs_getattr, 1988 .getattr = v9fs_vfs_getattr_dotl,
1296 .setattr = v9fs_vfs_setattr, 1989 .setattr = v9fs_vfs_setattr_dotl,
1990 .setxattr = generic_setxattr,
1991 .getxattr = generic_getxattr,
1992 .removexattr = generic_removexattr,
1993 .listxattr = v9fs_listxattr,
1297}; 1994};
diff --git a/fs/9p/vfs_super.c b/fs/9p/vfs_super.c
index be74d020436e..4b9ede0b41b7 100644
--- a/fs/9p/vfs_super.c
+++ b/fs/9p/vfs_super.c
@@ -45,6 +45,7 @@
45#include "v9fs.h" 45#include "v9fs.h"
46#include "v9fs_vfs.h" 46#include "v9fs_vfs.h"
47#include "fid.h" 47#include "fid.h"
48#include "xattr.h"
48 49
49static const struct super_operations v9fs_super_ops, v9fs_super_ops_dotl; 50static const struct super_operations v9fs_super_ops, v9fs_super_ops_dotl;
50 51
@@ -77,9 +78,10 @@ v9fs_fill_super(struct super_block *sb, struct v9fs_session_info *v9ses,
77 sb->s_blocksize_bits = fls(v9ses->maxdata - 1); 78 sb->s_blocksize_bits = fls(v9ses->maxdata - 1);
78 sb->s_blocksize = 1 << sb->s_blocksize_bits; 79 sb->s_blocksize = 1 << sb->s_blocksize_bits;
79 sb->s_magic = V9FS_MAGIC; 80 sb->s_magic = V9FS_MAGIC;
80 if (v9fs_proto_dotl(v9ses)) 81 if (v9fs_proto_dotl(v9ses)) {
81 sb->s_op = &v9fs_super_ops_dotl; 82 sb->s_op = &v9fs_super_ops_dotl;
82 else 83 sb->s_xattr = v9fs_xattr_handlers;
84 } else
83 sb->s_op = &v9fs_super_ops; 85 sb->s_op = &v9fs_super_ops;
84 sb->s_bdi = &v9ses->bdi; 86 sb->s_bdi = &v9ses->bdi;
85 87
@@ -107,7 +109,6 @@ static int v9fs_get_sb(struct file_system_type *fs_type, int flags,
107 struct inode *inode = NULL; 109 struct inode *inode = NULL;
108 struct dentry *root = NULL; 110 struct dentry *root = NULL;
109 struct v9fs_session_info *v9ses = NULL; 111 struct v9fs_session_info *v9ses = NULL;
110 struct p9_wstat *st = NULL;
111 int mode = S_IRWXUGO | S_ISVTX; 112 int mode = S_IRWXUGO | S_ISVTX;
112 struct p9_fid *fid; 113 struct p9_fid *fid;
113 int retval = 0; 114 int retval = 0;
@@ -124,16 +125,10 @@ static int v9fs_get_sb(struct file_system_type *fs_type, int flags,
124 goto close_session; 125 goto close_session;
125 } 126 }
126 127
127 st = p9_client_stat(fid);
128 if (IS_ERR(st)) {
129 retval = PTR_ERR(st);
130 goto clunk_fid;
131 }
132
133 sb = sget(fs_type, NULL, v9fs_set_super, v9ses); 128 sb = sget(fs_type, NULL, v9fs_set_super, v9ses);
134 if (IS_ERR(sb)) { 129 if (IS_ERR(sb)) {
135 retval = PTR_ERR(sb); 130 retval = PTR_ERR(sb);
136 goto free_stat; 131 goto clunk_fid;
137 } 132 }
138 v9fs_fill_super(sb, v9ses, flags, data); 133 v9fs_fill_super(sb, v9ses, flags, data);
139 134
@@ -151,22 +146,38 @@ static int v9fs_get_sb(struct file_system_type *fs_type, int flags,
151 } 146 }
152 147
153 sb->s_root = root; 148 sb->s_root = root;
154 root->d_inode->i_ino = v9fs_qid2ino(&st->qid);
155 149
156 v9fs_stat2inode(st, root->d_inode, sb); 150 if (v9fs_proto_dotl(v9ses)) {
151 struct p9_stat_dotl *st = NULL;
152 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
153 if (IS_ERR(st)) {
154 retval = PTR_ERR(st);
155 goto clunk_fid;
156 }
157
158 v9fs_stat2inode_dotl(st, root->d_inode);
159 kfree(st);
160 } else {
161 struct p9_wstat *st = NULL;
162 st = p9_client_stat(fid);
163 if (IS_ERR(st)) {
164 retval = PTR_ERR(st);
165 goto clunk_fid;
166 }
167
168 root->d_inode->i_ino = v9fs_qid2ino(&st->qid);
169 v9fs_stat2inode(st, root->d_inode, sb);
170
171 p9stat_free(st);
172 kfree(st);
173 }
157 174
158 v9fs_fid_add(root, fid); 175 v9fs_fid_add(root, fid);
159 p9stat_free(st);
160 kfree(st);
161 176
162P9_DPRINTK(P9_DEBUG_VFS, " simple set mount, return 0\n"); 177P9_DPRINTK(P9_DEBUG_VFS, " simple set mount, return 0\n");
163 simple_set_mnt(mnt, sb); 178 simple_set_mnt(mnt, sb);
164 return 0; 179 return 0;
165 180
166free_stat:
167 p9stat_free(st);
168 kfree(st);
169
170clunk_fid: 181clunk_fid:
171 p9_client_clunk(fid); 182 p9_client_clunk(fid);
172 183
@@ -176,8 +187,6 @@ close_session:
176 return retval; 187 return retval;
177 188
178release_sb: 189release_sb:
179 p9stat_free(st);
180 kfree(st);
181 deactivate_locked_super(sb); 190 deactivate_locked_super(sb);
182 return retval; 191 return retval;
183} 192}
@@ -278,4 +287,5 @@ struct file_system_type v9fs_fs_type = {
278 .get_sb = v9fs_get_sb, 287 .get_sb = v9fs_get_sb,
279 .kill_sb = v9fs_kill_super, 288 .kill_sb = v9fs_kill_super,
280 .owner = THIS_MODULE, 289 .owner = THIS_MODULE,
290 .fs_flags = FS_RENAME_DOES_D_MOVE,
281}; 291};
diff --git a/fs/9p/xattr.c b/fs/9p/xattr.c
new file mode 100644
index 000000000000..f88e5c2dc873
--- /dev/null
+++ b/fs/9p/xattr.c
@@ -0,0 +1,160 @@
1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/fs.h>
17#include <linux/sched.h>
18#include <net/9p/9p.h>
19#include <net/9p/client.h>
20
21#include "fid.h"
22#include "xattr.h"
23
24/*
25 * v9fs_xattr_get()
26 *
27 * Copy an extended attribute into the buffer
28 * provided, or compute the buffer size required.
29 * Buffer is NULL to compute the size of the buffer required.
30 *
31 * Returns a negative error number on failure, or the number of bytes
32 * used / required on success.
33 */
34ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
35 void *buffer, size_t buffer_size)
36{
37 ssize_t retval;
38 int msize, read_count;
39 u64 offset = 0, attr_size;
40 struct p9_fid *fid, *attr_fid;
41
42 P9_DPRINTK(P9_DEBUG_VFS, "%s: name = %s value_len = %zu\n",
43 __func__, name, buffer_size);
44
45 fid = v9fs_fid_lookup(dentry);
46 if (IS_ERR(fid))
47 return PTR_ERR(fid);
48
49 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
50 if (IS_ERR(attr_fid)) {
51 retval = PTR_ERR(attr_fid);
52 P9_DPRINTK(P9_DEBUG_VFS,
53 "p9_client_attrwalk failed %zd\n", retval);
54 attr_fid = NULL;
55 goto error;
56 }
57 if (!buffer_size) {
58 /* request to get the attr_size */
59 retval = attr_size;
60 goto error;
61 }
62 if (attr_size > buffer_size) {
63 retval = -ERANGE;
64 goto error;
65 }
66 msize = attr_fid->clnt->msize;
67 while (attr_size) {
68 if (attr_size > (msize - P9_IOHDRSZ))
69 read_count = msize - P9_IOHDRSZ;
70 else
71 read_count = attr_size;
72 read_count = p9_client_read(attr_fid, ((char *)buffer)+offset,
73 NULL, offset, read_count);
74 if (read_count < 0) {
75 /* error in xattr read */
76 retval = read_count;
77 goto error;
78 }
79 offset += read_count;
80 attr_size -= read_count;
81 }
82 /* Total read xattr bytes */
83 retval = offset;
84error:
85 if (attr_fid)
86 p9_client_clunk(attr_fid);
87 return retval;
88
89}
90
91/*
92 * v9fs_xattr_set()
93 *
94 * Create, replace or remove an extended attribute for this inode. Buffer
95 * is NULL to remove an existing extended attribute, and non-NULL to
96 * either replace an existing extended attribute, or create a new extended
97 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
98 * specify that an extended attribute must exist and must not exist
99 * previous to the call, respectively.
100 *
101 * Returns 0, or a negative error number on failure.
102 */
103int v9fs_xattr_set(struct dentry *dentry, const char *name,
104 const void *value, size_t value_len, int flags)
105{
106 u64 offset = 0;
107 int retval, msize, write_count;
108 struct p9_fid *fid = NULL;
109
110 P9_DPRINTK(P9_DEBUG_VFS, "%s: name = %s value_len = %zu flags = %d\n",
111 __func__, name, value_len, flags);
112
113 fid = v9fs_fid_clone(dentry);
114 if (IS_ERR(fid)) {
115 retval = PTR_ERR(fid);
116 fid = NULL;
117 goto error;
118 }
119 /*
120 * On success fid points to xattr
121 */
122 retval = p9_client_xattrcreate(fid, name, value_len, flags);
123 if (retval < 0) {
124 P9_DPRINTK(P9_DEBUG_VFS,
125 "p9_client_xattrcreate failed %d\n", retval);
126 goto error;
127 }
128 msize = fid->clnt->msize;;
129 while (value_len) {
130 if (value_len > (msize - P9_IOHDRSZ))
131 write_count = msize - P9_IOHDRSZ;
132 else
133 write_count = value_len;
134 write_count = p9_client_write(fid, ((char *)value)+offset,
135 NULL, offset, write_count);
136 if (write_count < 0) {
137 /* error in xattr write */
138 retval = write_count;
139 goto error;
140 }
141 offset += write_count;
142 value_len -= write_count;
143 }
144 /* Total read xattr bytes */
145 retval = offset;
146error:
147 if (fid)
148 retval = p9_client_clunk(fid);
149 return retval;
150}
151
152ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
153{
154 return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
155}
156
157const struct xattr_handler *v9fs_xattr_handlers[] = {
158 &v9fs_xattr_user_handler,
159 NULL
160};
diff --git a/fs/9p/xattr.h b/fs/9p/xattr.h
new file mode 100644
index 000000000000..9ddf672ae5c4
--- /dev/null
+++ b/fs/9p/xattr.h
@@ -0,0 +1,27 @@
1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14#ifndef FS_9P_XATTR_H
15#define FS_9P_XATTR_H
16
17#include <linux/xattr.h>
18
19extern const struct xattr_handler *v9fs_xattr_handlers[];
20extern struct xattr_handler v9fs_xattr_user_handler;
21
22extern ssize_t v9fs_xattr_get(struct dentry *, const char *,
23 void *, size_t);
24extern int v9fs_xattr_set(struct dentry *, const char *,
25 const void *, size_t, int);
26extern ssize_t v9fs_listxattr(struct dentry *, char *, size_t);
27#endif /* FS_9P_XATTR_H */
diff --git a/fs/9p/xattr_user.c b/fs/9p/xattr_user.c
new file mode 100644
index 000000000000..d0b701b72080
--- /dev/null
+++ b/fs/9p/xattr_user.c
@@ -0,0 +1,80 @@
1/*
2 * Copyright IBM Corporation, 2010
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14
15
16#include <linux/module.h>
17#include <linux/string.h>
18#include <linux/fs.h>
19#include <linux/slab.h>
20#include "xattr.h"
21
22static int v9fs_xattr_user_get(struct dentry *dentry, const char *name,
23 void *buffer, size_t size, int type)
24{
25 int retval;
26 char *full_name;
27 size_t name_len;
28 size_t prefix_len = XATTR_USER_PREFIX_LEN;
29
30 if (name == NULL)
31 return -EINVAL;
32
33 if (strcmp(name, "") == 0)
34 return -EINVAL;
35
36 name_len = strlen(name);
37 full_name = kmalloc(prefix_len + name_len + 1 , GFP_KERNEL);
38 if (!full_name)
39 return -ENOMEM;
40 memcpy(full_name, XATTR_USER_PREFIX, prefix_len);
41 memcpy(full_name+prefix_len, name, name_len);
42 full_name[prefix_len + name_len] = '\0';
43
44 retval = v9fs_xattr_get(dentry, full_name, buffer, size);
45 kfree(full_name);
46 return retval;
47}
48
49static int v9fs_xattr_user_set(struct dentry *dentry, const char *name,
50 const void *value, size_t size, int flags, int type)
51{
52 int retval;
53 char *full_name;
54 size_t name_len;
55 size_t prefix_len = XATTR_USER_PREFIX_LEN;
56
57 if (name == NULL)
58 return -EINVAL;
59
60 if (strcmp(name, "") == 0)
61 return -EINVAL;
62
63 name_len = strlen(name);
64 full_name = kmalloc(prefix_len + name_len + 1 , GFP_KERNEL);
65 if (!full_name)
66 return -ENOMEM;
67 memcpy(full_name, XATTR_USER_PREFIX, prefix_len);
68 memcpy(full_name + prefix_len, name, name_len);
69 full_name[prefix_len + name_len] = '\0';
70
71 retval = v9fs_xattr_set(dentry, full_name, value, size, flags);
72 kfree(full_name);
73 return retval;
74}
75
76struct xattr_handler v9fs_xattr_user_handler = {
77 .prefix = XATTR_USER_PREFIX,
78 .get = v9fs_xattr_user_get,
79 .set = v9fs_xattr_user_set,
80};