blob: f22668afd0d6262dd6d3cd68ee23ed983d7de69b [file] [log] [blame]
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001/*
2 * linux/fs/9p/vfs_inode.c
3 *
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07004 * This file contains vfs inode ops for the 9P2000 protocol.
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07005 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -080010 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/file.h>
30#include <linux/pagemap.h>
31#include <linux/stat.h>
32#include <linux/string.h>
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070033#include <linux/inet.h>
34#include <linux/namei.h>
35#include <linux/idr.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040036#include <linux/sched.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050037#include <net/9p/9p.h>
38#include <net/9p/client.h>
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070039
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070040#include "v9fs.h"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070041#include "v9fs_vfs.h"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070042#include "fid.h"
43
Arjan van de Ven754661f2007-02-12 00:55:38 -080044static const struct inode_operations v9fs_dir_inode_operations;
45static const struct inode_operations v9fs_dir_inode_operations_ext;
46static const struct inode_operations v9fs_file_inode_operations;
47static const struct inode_operations v9fs_symlink_inode_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070048
49/**
50 * unixmode2p9mode - convert unix mode bits to plan 9
51 * @v9ses: v9fs session information
52 * @mode: mode to convert
53 *
54 */
55
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070056static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070057{
58 int res;
59 res = mode & 0777;
60 if (S_ISDIR(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050061 res |= P9_DMDIR;
Latchesar Ionkov24056692007-10-17 14:31:07 -050062 if (v9fs_extended(v9ses)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070063 if (S_ISLNK(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050064 res |= P9_DMSYMLINK;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070065 if (v9ses->nodev == 0) {
66 if (S_ISSOCK(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050067 res |= P9_DMSOCKET;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070068 if (S_ISFIFO(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050069 res |= P9_DMNAMEDPIPE;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070070 if (S_ISBLK(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050071 res |= P9_DMDEVICE;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070072 if (S_ISCHR(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050073 res |= P9_DMDEVICE;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070074 }
75
76 if ((mode & S_ISUID) == S_ISUID)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050077 res |= P9_DMSETUID;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070078 if ((mode & S_ISGID) == S_ISGID)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050079 res |= P9_DMSETGID;
Anthony Liguorid199d652008-02-06 19:25:06 -060080 if ((mode & S_ISVTX) == S_ISVTX)
81 res |= P9_DMSETVTX;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050082 if ((mode & P9_DMLINK))
83 res |= P9_DMLINK;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070084 }
85
86 return res;
87}
88
89/**
90 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
91 * @v9ses: v9fs session information
92 * @mode: mode to convert
93 *
94 */
95
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070096static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070097{
98 int res;
99
100 res = mode & 0777;
101
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500102 if ((mode & P9_DMDIR) == P9_DMDIR)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700103 res |= S_IFDIR;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500104 else if ((mode & P9_DMSYMLINK) && (v9fs_extended(v9ses)))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700105 res |= S_IFLNK;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500106 else if ((mode & P9_DMSOCKET) && (v9fs_extended(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700107 && (v9ses->nodev == 0))
108 res |= S_IFSOCK;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500109 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_extended(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700110 && (v9ses->nodev == 0))
111 res |= S_IFIFO;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500112 else if ((mode & P9_DMDEVICE) && (v9fs_extended(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700113 && (v9ses->nodev == 0))
114 res |= S_IFBLK;
115 else
116 res |= S_IFREG;
117
Latchesar Ionkov24056692007-10-17 14:31:07 -0500118 if (v9fs_extended(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500119 if ((mode & P9_DMSETUID) == P9_DMSETUID)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700120 res |= S_ISUID;
121
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500122 if ((mode & P9_DMSETGID) == P9_DMSETGID)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700123 res |= S_ISGID;
Anthony Liguorid199d652008-02-06 19:25:06 -0600124
125 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
126 res |= S_ISVTX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700127 }
128
129 return res;
130}
131
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600132/**
133 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
134 * @uflags: flags to convert
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500135 * @extended: if .u extensions are active
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600136 */
137
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500138int v9fs_uflags2omode(int uflags, int extended)
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800139{
140 int ret;
141
142 ret = 0;
143 switch (uflags&3) {
144 default:
145 case O_RDONLY:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500146 ret = P9_OREAD;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800147 break;
148
149 case O_WRONLY:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500150 ret = P9_OWRITE;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800151 break;
152
153 case O_RDWR:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500154 ret = P9_ORDWR;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800155 break;
156 }
157
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800158 if (uflags & O_TRUNC)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500159 ret |= P9_OTRUNC;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800160
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500161 if (extended) {
162 if (uflags & O_EXCL)
163 ret |= P9_OEXCL;
164
165 if (uflags & O_APPEND)
166 ret |= P9_OAPPEND;
167 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800168
169 return ret;
170}
171
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700172/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800173 * v9fs_blank_wstat - helper function to setup a 9P stat structure
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800174 * @wstat: structure to initialize
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700175 *
176 */
177
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700178static void
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500179v9fs_blank_wstat(struct p9_wstat *wstat)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700180{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800181 wstat->type = ~0;
182 wstat->dev = ~0;
183 wstat->qid.type = ~0;
184 wstat->qid.version = ~0;
185 *((long long *)&wstat->qid.path) = ~0;
186 wstat->mode = ~0;
187 wstat->atime = ~0;
188 wstat->mtime = ~0;
189 wstat->length = ~0;
190 wstat->name = NULL;
191 wstat->uid = NULL;
192 wstat->gid = NULL;
193 wstat->muid = NULL;
194 wstat->n_uid = ~0;
195 wstat->n_gid = ~0;
196 wstat->n_muid = ~0;
197 wstat->extension = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700198}
199
200/**
201 * v9fs_get_inode - helper function to setup an inode
202 * @sb: superblock
203 * @mode: mode to setup inode with
204 *
205 */
206
207struct inode *v9fs_get_inode(struct super_block *sb, int mode)
208{
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600209 int err;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800210 struct inode *inode;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700211 struct v9fs_session_info *v9ses = sb->s_fs_info;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700212
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500213 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700214
215 inode = new_inode(sb);
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600216 if (!inode) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500217 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600218 return -ENOMEM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700219 }
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600220
221 inode->i_mode = mode;
222 inode->i_uid = current_fsuid();
223 inode->i_gid = current_fsgid();
224 inode->i_blocks = 0;
225 inode->i_rdev = 0;
226 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
227 inode->i_mapping->a_ops = &v9fs_addr_operations;
228
229 switch (mode & S_IFMT) {
230 case S_IFIFO:
231 case S_IFBLK:
232 case S_IFCHR:
233 case S_IFSOCK:
234 if (!v9fs_extended(v9ses)) {
235 P9_DPRINTK(P9_DEBUG_ERROR,
236 "special files without extended mode\n");
237 err = -EINVAL;
238 goto error;
239 }
240 init_special_inode(inode, inode->i_mode, inode->i_rdev);
241 break;
242 case S_IFREG:
243 inode->i_op = &v9fs_file_inode_operations;
244 inode->i_fop = &v9fs_file_operations;
245 break;
246 case S_IFLNK:
247 if (!v9fs_extended(v9ses)) {
248 P9_DPRINTK(P9_DEBUG_ERROR,
249 "extended modes used w/o 9P2000.u\n");
250 err = -EINVAL;
251 goto error;
252 }
253 inode->i_op = &v9fs_symlink_inode_operations;
254 break;
255 case S_IFDIR:
256 inc_nlink(inode);
257 if (v9fs_extended(v9ses))
258 inode->i_op = &v9fs_dir_inode_operations_ext;
259 else
260 inode->i_op = &v9fs_dir_inode_operations;
261 inode->i_fop = &v9fs_dir_operations;
262 break;
263 default:
264 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
265 mode, mode & S_IFMT);
266 err = -EINVAL;
267 goto error;
268 }
269
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700270 return inode;
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600271
272error:
273 iput(inode);
274 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700275}
276
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500277/*
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800278static struct v9fs_fid*
279v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
280{
281 int err;
Mika Kukkonen736c4b82006-12-06 20:36:29 -0800282 int nfid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800283 struct v9fs_fid *ret;
284 struct v9fs_fcall *fcall;
285
286 nfid = v9fs_get_idpool(&v9ses->fidpool);
287 if (nfid < 0) {
288 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800289 return ERR_PTR(-ENOSPC);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800290 }
291
292 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
293 &fcall);
294
295 if (err < 0) {
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700296 if (fcall && fcall->id == RWALK)
297 goto clunk_fid;
298
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800299 PRINT_FCALL_ERROR("walk error", fcall);
300 v9fs_put_idpool(nfid, &v9ses->fidpool);
301 goto error;
302 }
303
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700304 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800305 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800306 ret = v9fs_fid_create(v9ses, nfid);
307 if (!ret) {
308 err = -ENOMEM;
309 goto clunk_fid;
310 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700311
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800312 err = v9fs_fid_insert(ret, dentry);
313 if (err < 0) {
314 v9fs_fid_destroy(ret);
315 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700316 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800317
318 return ret;
319
320clunk_fid:
321 v9fs_t_clunk(v9ses, nfid);
322
323error:
324 kfree(fcall);
325 return ERR_PTR(err);
326}
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500327*/
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800328
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600329/**
330 * v9fs_inode_from_fid - populate an inode by issuing a attribute request
331 * @v9ses: session information
332 * @fid: fid to issue attribute request for
333 * @sb: superblock on which to create inode
334 *
335 */
336
Latchesar Ionkov5174fda2006-03-25 03:07:25 -0800337static struct inode *
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500338v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800339 struct super_block *sb)
340{
341 int err, umode;
342 struct inode *ret;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500343 struct p9_wstat *st;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800344
345 ret = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500346 st = p9_client_stat(fid);
347 if (IS_ERR(st)) {
348 err = PTR_ERR(st);
349 st = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800350 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700351 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800352
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500353 umode = p9mode2unixmode(v9ses, st->mode);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800354 ret = v9fs_get_inode(sb, umode);
355 if (IS_ERR(ret)) {
356 err = PTR_ERR(ret);
357 ret = NULL;
358 goto error;
359 }
360
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500361 v9fs_stat2inode(st, ret, sb);
362 ret->i_ino = v9fs_qid2ino(&st->qid);
363 kfree(st);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800364 return ret;
365
366error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500367 kfree(st);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800368 if (ret)
369 iput(ret);
370
371 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700372}
373
374/**
375 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700376 * @dir: directory inode that is being deleted
377 * @file: dentry that is being deleted
378 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700379 *
380 */
381
382static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
383{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500384 struct inode *file_inode;
385 struct v9fs_session_info *v9ses;
386 struct p9_fid *v9fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700387
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500388 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700389 rmdir);
390
391 file_inode = file->d_inode;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700392 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkovc959df92007-04-23 14:41:11 -0700393 v9fid = v9fs_fid_clone(file);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500394 if (IS_ERR(v9fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800395 return PTR_ERR(v9fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700396
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500397 return p9_client_remove(v9fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700398}
399
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800400static int
401v9fs_open_created(struct inode *inode, struct file *file)
402{
403 return 0;
404}
405
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500406
407/**
408 * v9fs_create - Create a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600409 * @v9ses: session information
410 * @dir: directory that dentry is being created in
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500411 * @dentry: dentry that is being created
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600412 * @extension: 9p2000.u extension string to support devices, etc.
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500413 * @perm: create permissions
414 * @mode: open mode
415 *
416 */
417static struct p9_fid *
418v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
419 struct dentry *dentry, char *extension, u32 perm, u8 mode)
420{
421 int err;
422 char *name;
423 struct p9_fid *dfid, *ofid, *fid;
424 struct inode *inode;
425
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500426 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
427
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500428 err = 0;
429 ofid = NULL;
430 fid = NULL;
431 name = (char *) dentry->d_name.name;
432 dfid = v9fs_fid_clone(dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500433 if (IS_ERR(dfid)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500434 err = PTR_ERR(dfid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500435 P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500436 dfid = NULL;
437 goto error;
438 }
439
440 /* clone a fid to use for creation */
441 ofid = p9_client_walk(dfid, 0, NULL, 1);
442 if (IS_ERR(ofid)) {
443 err = PTR_ERR(ofid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500444 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500445 ofid = NULL;
446 goto error;
447 }
448
449 err = p9_client_fcreate(ofid, name, perm, mode, extension);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500450 if (err < 0) {
451 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500452 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500453 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500454
455 /* now walk from the parent so we can get unopened fid */
456 fid = p9_client_walk(dfid, 1, &name, 0);
457 if (IS_ERR(fid)) {
458 err = PTR_ERR(fid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500459 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500460 fid = NULL;
461 goto error;
462 } else
463 dfid = NULL;
464
465 /* instantiate inode and assign the unopened fid to the dentry */
466 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
467 if (IS_ERR(inode)) {
468 err = PTR_ERR(inode);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500469 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500470 goto error;
471 }
472
Latchesar Ionkovba176742007-10-17 14:31:07 -0500473 if (v9ses->cache)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500474 dentry->d_op = &v9fs_cached_dentry_operations;
475 else
476 dentry->d_op = &v9fs_dentry_operations;
477
478 d_instantiate(dentry, inode);
Abhishek Kulkarni50fb6d22009-07-19 13:41:57 -0600479 err = v9fs_fid_add(dentry, fid);
480 if (err < 0)
481 goto error;
482
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500483 return ofid;
484
485error:
486 if (dfid)
487 p9_client_clunk(dfid);
488
489 if (ofid)
490 p9_client_clunk(ofid);
491
492 if (fid)
493 p9_client_clunk(fid);
494
495 return ERR_PTR(err);
496}
497
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700498/**
499 * v9fs_vfs_create - VFS hook to create files
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600500 * @dir: directory inode that is being created
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700501 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800502 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700503 * @nd: path information
504 *
505 */
506
507static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800508v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700509 struct nameidata *nd)
510{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800511 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500512 u32 perm;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800513 int flags;
514 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500515 struct p9_fid *fid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800516 struct file *filp;
517
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500518 err = 0;
519 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800520 v9ses = v9fs_inode2v9ses(dir);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800521 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800522 if (nd && nd->flags & LOOKUP_OPEN)
523 flags = nd->intent.open.flags - 1;
524 else
525 flags = O_RDWR;
526
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500527 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500528 v9fs_uflags2omode(flags, v9fs_extended(v9ses)));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500529 if (IS_ERR(fid)) {
530 err = PTR_ERR(fid);
531 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800532 goto error;
533 }
534
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500535 /* if we are opening a file, assign the open fid to the file */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800536 if (nd && nd->flags & LOOKUP_OPEN) {
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800537 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
538 if (IS_ERR(filp)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500539 err = PTR_ERR(filp);
540 goto error;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800541 }
542
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500543 filp->private_data = fid;
544 } else
545 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800546
547 return 0;
548
549error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500550 if (fid)
551 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800552
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800553 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700554}
555
556/**
557 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600558 * @dir: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700559 * @dentry: dentry that is being unlinked
560 * @mode: mode for new directory
561 *
562 */
563
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800564static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700565{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800566 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500567 u32 perm;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800568 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500569 struct p9_fid *fid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800570
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500571 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
572 err = 0;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800573 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800574 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500575 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
576 if (IS_ERR(fid)) {
577 err = PTR_ERR(fid);
578 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800579 }
580
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500581 if (fid)
582 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800583
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800584 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700585}
586
587/**
588 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
589 * @dir: inode that is being walked from
590 * @dentry: dentry that is being walked to?
591 * @nameidata: path data
592 *
593 */
594
595static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
596 struct nameidata *nameidata)
597{
598 struct super_block *sb;
599 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500600 struct p9_fid *dfid, *fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700601 struct inode *inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500602 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700603 int result = 0;
604
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500605 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800606 dir, dentry->d_name.name, dentry, nameidata);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700607
608 sb = dir->i_sb;
609 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500610 dfid = v9fs_fid_lookup(dentry->d_parent);
611 if (IS_ERR(dfid))
David Howellse231c2e2008-02-07 00:15:26 -0800612 return ERR_CAST(dfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700613
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500614 name = (char *) dentry->d_name.name;
615 fid = p9_client_walk(dfid, 1, &name, 1);
616 if (IS_ERR(fid)) {
617 result = PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700618 if (result == -ENOENT) {
619 d_add(dentry, NULL);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700620 return NULL;
621 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700622
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500623 return ERR_PTR(result);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700624 }
625
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500626 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
627 if (IS_ERR(inode)) {
628 result = PTR_ERR(inode);
629 inode = NULL;
630 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700631 }
632
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500633 result = v9fs_fid_add(dentry, fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800634 if (result < 0)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500635 goto error;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800636
Latchesar Ionkovba176742007-10-17 14:31:07 -0500637 if ((fid->qid.version) && (v9ses->cache))
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600638 dentry->d_op = &v9fs_cached_dentry_operations;
639 else
640 dentry->d_op = &v9fs_dentry_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700641
642 d_add(dentry, inode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700643 return NULL;
644
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500645error:
Julien Brunel62aa5282008-09-24 16:22:22 -0500646 p9_client_clunk(fid);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800647
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700648 return ERR_PTR(result);
649}
650
651/**
652 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
653 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700654 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700655 *
656 */
657
658static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
659{
660 return v9fs_remove(i, d, 0);
661}
662
663/**
664 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
665 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700666 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700667 *
668 */
669
670static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
671{
672 return v9fs_remove(i, d, 1);
673}
674
675/**
676 * v9fs_vfs_rename - VFS hook to rename an inode
677 * @old_dir: old dir inode
678 * @old_dentry: old dentry
679 * @new_dir: new dir inode
680 * @new_dentry: new dentry
681 *
682 */
683
684static int
685v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
686 struct inode *new_dir, struct dentry *new_dentry)
687{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500688 struct inode *old_inode;
689 struct v9fs_session_info *v9ses;
690 struct p9_fid *oldfid;
691 struct p9_fid *olddirfid;
692 struct p9_fid *newdirfid;
693 struct p9_wstat wstat;
694 int retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700695
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500696 P9_DPRINTK(P9_DEBUG_VFS, "\n");
697 retval = 0;
698 old_inode = old_dentry->d_inode;
699 v9ses = v9fs_inode2v9ses(old_inode);
700 oldfid = v9fs_fid_lookup(old_dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500701 if (IS_ERR(oldfid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800702 return PTR_ERR(oldfid);
703
704 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500705 if (IS_ERR(olddirfid)) {
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800706 retval = PTR_ERR(olddirfid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500707 goto done;
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800708 }
709
710 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500711 if (IS_ERR(newdirfid)) {
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800712 retval = PTR_ERR(newdirfid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500713 goto clunk_olddir;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700714 }
715
716 /* 9P can only handle file rename in the same directory */
717 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500718 P9_DPRINTK(P9_DEBUG_ERROR,
719 "old dir and new dir are different\n");
Eric Van Hensbergen621997c2007-01-26 00:57:04 -0800720 retval = -EXDEV;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500721 goto clunk_newdir;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700722 }
723
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800724 v9fs_blank_wstat(&wstat);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500725 wstat.muid = v9ses->uname;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800726 wstat.name = (char *) new_dentry->d_name.name;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500727 retval = p9_client_wstat(oldfid, &wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700728
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500729clunk_newdir:
Latchesar Ionkov22150c42007-10-23 13:48:33 -0500730 p9_client_clunk(newdirfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700731
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500732clunk_olddir:
Latchesar Ionkov22150c42007-10-23 13:48:33 -0500733 p9_client_clunk(olddirfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700734
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500735done:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700736 return retval;
737}
738
739/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100740 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600741 * @mnt: mount information
742 * @dentry: file to get attributes on
743 * @stat: metadata structure to populate
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700744 *
745 */
746
747static int
748v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
749 struct kstat *stat)
750{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500751 int err;
752 struct v9fs_session_info *v9ses;
753 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500754 struct p9_wstat *st;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700755
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500756 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
757 err = -EPERM;
758 v9ses = v9fs_inode2v9ses(dentry->d_inode);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -0500759 if (v9ses->cache == CACHE_LOOSE)
760 return simple_getattr(mnt, dentry, stat);
761
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500762 fid = v9fs_fid_lookup(dentry);
763 if (IS_ERR(fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800764 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700765
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500766 st = p9_client_stat(fid);
767 if (IS_ERR(st))
768 return PTR_ERR(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700769
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500770 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700771 generic_fillattr(dentry->d_inode, stat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700772
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500773 kfree(st);
774 return 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700775}
776
777/**
778 * v9fs_vfs_setattr - set file metadata
779 * @dentry: file whose metadata to set
780 * @iattr: metadata assignment structure
781 *
782 */
783
784static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
785{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500786 int retval;
787 struct v9fs_session_info *v9ses;
788 struct p9_fid *fid;
789 struct p9_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700790
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500791 P9_DPRINTK(P9_DEBUG_VFS, "\n");
792 retval = -EPERM;
793 v9ses = v9fs_inode2v9ses(dentry->d_inode);
794 fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800795 if(IS_ERR(fid))
796 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700797
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800798 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700799 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800800 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700801
802 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800803 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700804
805 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800806 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700807
808 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800809 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700810
Latchesar Ionkov24056692007-10-17 14:31:07 -0500811 if (v9fs_extended(v9ses)) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800812 if (iattr->ia_valid & ATTR_UID)
813 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700814
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800815 if (iattr->ia_valid & ATTR_GID)
816 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700817 }
818
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500819 retval = p9_client_wstat(fid, &wstat);
820 if (retval >= 0)
821 retval = inode_setattr(dentry->d_inode, iattr);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700822
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500823 return retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700824}
825
826/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800827 * v9fs_stat2inode - populate an inode structure with mistat info
828 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700829 * @inode: inode to populate
830 * @sb: superblock of filesystem
831 *
832 */
833
834void
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500835v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800836 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700837{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800838 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700839 struct v9fs_session_info *v9ses = sb->s_fs_info;
840
841 inode->i_nlink = 1;
842
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800843 inode->i_atime.tv_sec = stat->atime;
844 inode->i_mtime.tv_sec = stat->mtime;
845 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700846
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500847 inode->i_uid = v9ses->dfltuid;
848 inode->i_gid = v9ses->dfltgid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700849
Latchesar Ionkov24056692007-10-17 14:31:07 -0500850 if (v9fs_extended(v9ses)) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800851 inode->i_uid = stat->n_uid;
852 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700853 }
854
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800855 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700856 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
857 char type = 0;
858 int major = -1;
859 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800860
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500861 strncpy(ext, stat->extension, sizeof(ext));
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800862 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700863 switch (type) {
864 case 'c':
865 inode->i_mode &= ~S_IFBLK;
866 inode->i_mode |= S_IFCHR;
867 break;
868 case 'b':
869 break;
870 default:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500871 P9_DPRINTK(P9_DEBUG_ERROR,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500872 "Unknown special type %c %s\n", type,
873 stat->extension);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700874 };
875 inode->i_rdev = MKDEV(major, minor);
Magnus Deininger57c7b4e2008-10-17 12:44:46 -0500876 init_special_inode(inode, inode->i_mode, inode->i_rdev);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700877 } else
878 inode->i_rdev = 0;
879
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800880 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700881
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500882 /* not real number of blocks, but 512 byte ones ... */
883 inode->i_blocks = (inode->i_size + 512 - 1) >> 9;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700884}
885
886/**
887 * v9fs_qid2ino - convert qid into inode number
888 * @qid: qid to hash
889 *
890 * BUG: potential for inode number collisions?
891 */
892
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500893ino_t v9fs_qid2ino(struct p9_qid *qid)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700894{
895 u64 path = qid->path + 2;
896 ino_t i = 0;
897
898 if (sizeof(ino_t) == sizeof(path))
899 memcpy(&i, &path, sizeof(ino_t));
900 else
901 i = (ino_t) (path ^ (path >> 32));
902
903 return i;
904}
905
906/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700907 * v9fs_readlink - read a symlink's location (internal version)
908 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700909 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700910 * @buflen: length of buffer
911 *
912 */
913
914static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
915{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500916 int retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700917
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500918 struct v9fs_session_info *v9ses;
919 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500920 struct p9_wstat *st;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700921
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500922 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
923 retval = -EPERM;
924 v9ses = v9fs_inode2v9ses(dentry->d_inode);
925 fid = v9fs_fid_lookup(dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500926 if (IS_ERR(fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800927 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700928
Latchesar Ionkov24056692007-10-17 14:31:07 -0500929 if (!v9fs_extended(v9ses))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500930 return -EBADF;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700931
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500932 st = p9_client_stat(fid);
933 if (IS_ERR(st))
934 return PTR_ERR(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700935
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500936 if (!(st->mode & P9_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700937 retval = -EINVAL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500938 goto done;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700939 }
940
941 /* copy extension buffer into buffer */
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500942 strncpy(buffer, st->extension, buflen);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700943
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500944 P9_DPRINTK(P9_DEBUG_VFS,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500945 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500946
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700947 retval = buflen;
948
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500949done:
950 kfree(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700951 return retval;
952}
953
954/**
955 * v9fs_vfs_readlink - read a symlink's location
956 * @dentry: dentry for symlink
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600957 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700958 * @buflen: length of buffer
959 *
960 */
961
962static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
963 int buflen)
964{
965 int retval;
966 int ret;
967 char *link = __getname();
968
Florin Malita0710d362006-06-25 05:48:31 -0700969 if (unlikely(!link))
970 return -ENOMEM;
971
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -0700972 if (buflen > PATH_MAX)
973 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700974
Wu Fengguang7dd0cdc2008-12-19 16:47:40 -0600975 P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_name.name,
976 dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700977
978 retval = v9fs_readlink(dentry, link, buflen);
979
980 if (retval > 0) {
981 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500982 P9_DPRINTK(P9_DEBUG_ERROR,
983 "problem copying to user: %d\n", ret);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700984 retval = ret;
985 }
986 }
987
Davi Arnautce44eeb2005-11-07 00:59:36 -0800988 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700989 return retval;
990}
991
992/**
993 * v9fs_vfs_follow_link - follow a symlink path
994 * @dentry: dentry for symlink
995 * @nd: nameidata
996 *
997 */
998
999static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1000{
1001 int len = 0;
1002 char *link = __getname();
1003
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001004 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001005
1006 if (!link)
1007 link = ERR_PTR(-ENOMEM);
1008 else {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001009 len = v9fs_readlink(dentry, link, PATH_MAX);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001010
1011 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -08001012 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001013 link = ERR_PTR(len);
1014 } else
1015 link[len] = 0;
1016 }
1017 nd_set_link(nd, link);
1018
1019 return NULL;
1020}
1021
1022/**
1023 * v9fs_vfs_put_link - release a symlink path
1024 * @dentry: dentry for symlink
1025 * @nd: nameidata
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001026 * @p: unused
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001027 *
1028 */
1029
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001030static void
1031v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001032{
1033 char *s = nd_get_link(nd);
1034
Duane Griffin6ff23202008-12-19 16:45:21 -06001035 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1036 IS_ERR(s) ? "<error>" : s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001037 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001038 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001039}
1040
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001041/**
1042 * v9fs_vfs_mkspecial - create a special file
1043 * @dir: inode to create special file in
1044 * @dentry: dentry to create
1045 * @mode: mode to create special file
1046 * @extension: 9p2000.u format extension string representing special file
1047 *
1048 */
1049
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001050static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1051 int mode, const char *extension)
1052{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001053 u32 perm;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001054 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001055 struct p9_fid *fid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001056
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001057 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov24056692007-10-17 14:31:07 -05001058 if (!v9fs_extended(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001059 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001060 return -EPERM;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001061 }
1062
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001063 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001064 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1065 P9_OREAD);
1066 if (IS_ERR(fid))
1067 return PTR_ERR(fid);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001068
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001069 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001070 return 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001071}
1072
1073/**
1074 * v9fs_vfs_symlink - helper function to create symlinks
1075 * @dir: directory inode containing symlink
1076 * @dentry: dentry for symlink
1077 * @symname: symlink data
1078 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001079 * See Also: 9P2000.u RFC for more information
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001080 *
1081 */
1082
1083static int
1084v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1085{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001086 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1087 dentry->d_name.name, symname);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001088
1089 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1090}
1091
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001092/**
1093 * v9fs_vfs_link - create a hardlink
1094 * @old_dentry: dentry for file to link to
1095 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001096 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001097 *
1098 */
1099
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001100static int
1101v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1102 struct dentry *dentry)
1103{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001104 int retval;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001105 struct p9_fid *oldfid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001106 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001107
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001108 P9_DPRINTK(P9_DEBUG_VFS,
1109 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001110 old_dentry->d_name.name);
1111
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001112 oldfid = v9fs_fid_clone(old_dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -05001113 if (IS_ERR(oldfid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001114 return PTR_ERR(oldfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001115
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001116 name = __getname();
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001117 if (unlikely(!name)) {
1118 retval = -ENOMEM;
1119 goto clunk_fid;
1120 }
Florin Malita0710d362006-06-25 05:48:31 -07001121
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001122 sprintf(name, "%d\n", oldfid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001123 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001124 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001125
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001126clunk_fid:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001127 p9_client_clunk(oldfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001128 return retval;
1129}
1130
1131/**
1132 * v9fs_vfs_mknod - create a special file
1133 * @dir: inode destination for new link
1134 * @dentry: dentry for file
1135 * @mode: mode for creation
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001136 * @rdev: device associated with special file
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001137 *
1138 */
1139
1140static int
1141v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1142{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001143 int retval;
1144 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001145
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001146 P9_DPRINTK(P9_DEBUG_VFS,
1147 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001148 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1149
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001150 if (!new_valid_dev(rdev))
1151 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001152
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001153 name = __getname();
Eugene Teoc0291a02006-03-25 03:07:27 -08001154 if (!name)
1155 return -ENOMEM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001156 /* build extension */
1157 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001158 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001159 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001160 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001161 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001162 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001163 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001164 __putname(name);
1165 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001166 }
1167
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001168 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1169 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001170
1171 return retval;
1172}
1173
Arjan van de Ven754661f2007-02-12 00:55:38 -08001174static const struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001175 .create = v9fs_vfs_create,
1176 .lookup = v9fs_vfs_lookup,
1177 .symlink = v9fs_vfs_symlink,
1178 .link = v9fs_vfs_link,
1179 .unlink = v9fs_vfs_unlink,
1180 .mkdir = v9fs_vfs_mkdir,
1181 .rmdir = v9fs_vfs_rmdir,
1182 .mknod = v9fs_vfs_mknod,
1183 .rename = v9fs_vfs_rename,
1184 .readlink = v9fs_vfs_readlink,
1185 .getattr = v9fs_vfs_getattr,
1186 .setattr = v9fs_vfs_setattr,
1187};
1188
Arjan van de Ven754661f2007-02-12 00:55:38 -08001189static const struct inode_operations v9fs_dir_inode_operations = {
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001190 .create = v9fs_vfs_create,
1191 .lookup = v9fs_vfs_lookup,
1192 .unlink = v9fs_vfs_unlink,
1193 .mkdir = v9fs_vfs_mkdir,
1194 .rmdir = v9fs_vfs_rmdir,
1195 .mknod = v9fs_vfs_mknod,
1196 .rename = v9fs_vfs_rename,
1197 .getattr = v9fs_vfs_getattr,
1198 .setattr = v9fs_vfs_setattr,
1199};
1200
Arjan van de Ven754661f2007-02-12 00:55:38 -08001201static const struct inode_operations v9fs_file_inode_operations = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001202 .getattr = v9fs_vfs_getattr,
1203 .setattr = v9fs_vfs_setattr,
1204};
1205
Arjan van de Ven754661f2007-02-12 00:55:38 -08001206static const struct inode_operations v9fs_symlink_inode_operations = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001207 .readlink = v9fs_vfs_readlink,
1208 .follow_link = v9fs_vfs_follow_link,
1209 .put_link = v9fs_vfs_put_link,
1210 .getattr = v9fs_vfs_getattr,
1211 .setattr = v9fs_vfs_setattr,
1212};