blob: 23581bcb599b2f7d6031644de612742512e218e3 [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;
80 if ((mode & P9_DMLINK))
81 res |= P9_DMLINK;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070082 }
83
84 return res;
85}
86
87/**
88 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
89 * @v9ses: v9fs session information
90 * @mode: mode to convert
91 *
92 */
93
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070094static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070095{
96 int res;
97
98 res = mode & 0777;
99
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500100 if ((mode & P9_DMDIR) == P9_DMDIR)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700101 res |= S_IFDIR;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500102 else if ((mode & P9_DMSYMLINK) && (v9fs_extended(v9ses)))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700103 res |= S_IFLNK;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500104 else if ((mode & P9_DMSOCKET) && (v9fs_extended(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700105 && (v9ses->nodev == 0))
106 res |= S_IFSOCK;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500107 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_extended(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700108 && (v9ses->nodev == 0))
109 res |= S_IFIFO;
Latchesar Ionkov24056692007-10-17 14:31:07 -0500110 else if ((mode & P9_DMDEVICE) && (v9fs_extended(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700111 && (v9ses->nodev == 0))
112 res |= S_IFBLK;
113 else
114 res |= S_IFREG;
115
Latchesar Ionkov24056692007-10-17 14:31:07 -0500116 if (v9fs_extended(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500117 if ((mode & P9_DMSETUID) == P9_DMSETUID)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700118 res |= S_ISUID;
119
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500120 if ((mode & P9_DMSETGID) == P9_DMSETGID)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700121 res |= S_ISGID;
122 }
123
124 return res;
125}
126
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800127int v9fs_uflags2omode(int uflags)
128{
129 int ret;
130
131 ret = 0;
132 switch (uflags&3) {
133 default:
134 case O_RDONLY:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500135 ret = P9_OREAD;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800136 break;
137
138 case O_WRONLY:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500139 ret = P9_OWRITE;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800140 break;
141
142 case O_RDWR:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500143 ret = P9_ORDWR;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800144 break;
145 }
146
147 if (uflags & O_EXCL)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500148 ret |= P9_OEXCL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800149
150 if (uflags & O_TRUNC)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500151 ret |= P9_OTRUNC;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800152
153 if (uflags & O_APPEND)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500154 ret |= P9_OAPPEND;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800155
156 return ret;
157}
158
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700159/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800160 * v9fs_blank_wstat - helper function to setup a 9P stat structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700161 * @v9ses: 9P session info (for determining extended mode)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800162 * @wstat: structure to initialize
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700163 *
164 */
165
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700166static void
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500167v9fs_blank_wstat(struct p9_wstat *wstat)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700168{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800169 wstat->type = ~0;
170 wstat->dev = ~0;
171 wstat->qid.type = ~0;
172 wstat->qid.version = ~0;
173 *((long long *)&wstat->qid.path) = ~0;
174 wstat->mode = ~0;
175 wstat->atime = ~0;
176 wstat->mtime = ~0;
177 wstat->length = ~0;
178 wstat->name = NULL;
179 wstat->uid = NULL;
180 wstat->gid = NULL;
181 wstat->muid = NULL;
182 wstat->n_uid = ~0;
183 wstat->n_gid = ~0;
184 wstat->n_muid = ~0;
185 wstat->extension = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700186}
187
188/**
189 * v9fs_get_inode - helper function to setup an inode
190 * @sb: superblock
191 * @mode: mode to setup inode with
192 *
193 */
194
195struct inode *v9fs_get_inode(struct super_block *sb, int mode)
196{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800197 struct inode *inode;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700198 struct v9fs_session_info *v9ses = sb->s_fs_info;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700199
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500200 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700201
202 inode = new_inode(sb);
203 if (inode) {
204 inode->i_mode = mode;
205 inode->i_uid = current->fsuid;
206 inode->i_gid = current->fsgid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700207 inode->i_blocks = 0;
208 inode->i_rdev = 0;
209 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800210 inode->i_mapping->a_ops = &v9fs_addr_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700211
212 switch (mode & S_IFMT) {
213 case S_IFIFO:
214 case S_IFBLK:
215 case S_IFCHR:
216 case S_IFSOCK:
Latchesar Ionkov24056692007-10-17 14:31:07 -0500217 if (!v9fs_extended(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500218 P9_DPRINTK(P9_DEBUG_ERROR,
219 "special files without extended mode\n");
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700220 return ERR_PTR(-EINVAL);
221 }
Eric Van Hensbergen5d58bec2005-09-09 13:04:27 -0700222 init_special_inode(inode, inode->i_mode,
223 inode->i_rdev);
224 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700225 case S_IFREG:
226 inode->i_op = &v9fs_file_inode_operations;
227 inode->i_fop = &v9fs_file_operations;
228 break;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700229 case S_IFLNK:
Latchesar Ionkov24056692007-10-17 14:31:07 -0500230 if (!v9fs_extended(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500231 P9_DPRINTK(P9_DEBUG_ERROR,
232 "extended modes used w/o 9P2000.u\n");
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700233 return ERR_PTR(-EINVAL);
234 }
235 inode->i_op = &v9fs_symlink_inode_operations;
236 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700237 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -0700238 inc_nlink(inode);
Latchesar Ionkov24056692007-10-17 14:31:07 -0500239 if (v9fs_extended(v9ses))
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700240 inode->i_op = &v9fs_dir_inode_operations_ext;
241 else
242 inode->i_op = &v9fs_dir_inode_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700243 inode->i_fop = &v9fs_dir_operations;
244 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700245 default:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500246 P9_DPRINTK(P9_DEBUG_ERROR,
247 "BAD mode 0x%x S_IFMT 0x%x\n",
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700248 mode, mode & S_IFMT);
249 return ERR_PTR(-EINVAL);
250 }
251 } else {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500252 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700253 return ERR_PTR(-ENOMEM);
254 }
255 return inode;
256}
257
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500258/*
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800259static struct v9fs_fid*
260v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
261{
262 int err;
Mika Kukkonen736c4b82006-12-06 20:36:29 -0800263 int nfid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800264 struct v9fs_fid *ret;
265 struct v9fs_fcall *fcall;
266
267 nfid = v9fs_get_idpool(&v9ses->fidpool);
268 if (nfid < 0) {
269 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800270 return ERR_PTR(-ENOSPC);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800271 }
272
273 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
274 &fcall);
275
276 if (err < 0) {
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700277 if (fcall && fcall->id == RWALK)
278 goto clunk_fid;
279
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800280 PRINT_FCALL_ERROR("walk error", fcall);
281 v9fs_put_idpool(nfid, &v9ses->fidpool);
282 goto error;
283 }
284
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700285 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800286 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800287 ret = v9fs_fid_create(v9ses, nfid);
288 if (!ret) {
289 err = -ENOMEM;
290 goto clunk_fid;
291 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700292
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800293 err = v9fs_fid_insert(ret, dentry);
294 if (err < 0) {
295 v9fs_fid_destroy(ret);
296 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700297 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800298
299 return ret;
300
301clunk_fid:
302 v9fs_t_clunk(v9ses, nfid);
303
304error:
305 kfree(fcall);
306 return ERR_PTR(err);
307}
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500308*/
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800309
Latchesar Ionkov5174fda2006-03-25 03:07:25 -0800310static struct inode *
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500311v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800312 struct super_block *sb)
313{
314 int err, umode;
315 struct inode *ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500316 struct p9_stat *st;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800317
318 ret = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500319 st = p9_client_stat(fid);
320 if (IS_ERR(st)) {
321 err = PTR_ERR(st);
322 st = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800323 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700324 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800325
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500326 umode = p9mode2unixmode(v9ses, st->mode);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800327 ret = v9fs_get_inode(sb, umode);
328 if (IS_ERR(ret)) {
329 err = PTR_ERR(ret);
330 ret = NULL;
331 goto error;
332 }
333
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500334 v9fs_stat2inode(st, ret, sb);
335 ret->i_ino = v9fs_qid2ino(&st->qid);
336 kfree(st);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800337 return ret;
338
339error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500340 kfree(st);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800341 if (ret)
342 iput(ret);
343
344 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700345}
346
347/**
348 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700349 * @dir: directory inode that is being deleted
350 * @file: dentry that is being deleted
351 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700352 *
353 */
354
355static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
356{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500357 struct inode *file_inode;
358 struct v9fs_session_info *v9ses;
359 struct p9_fid *v9fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700360
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500361 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700362 rmdir);
363
364 file_inode = file->d_inode;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700365 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkovc959df92007-04-23 14:41:11 -0700366 v9fid = v9fs_fid_clone(file);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500367 if (IS_ERR(v9fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800368 return PTR_ERR(v9fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700369
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500370 return p9_client_remove(v9fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700371}
372
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800373static int
374v9fs_open_created(struct inode *inode, struct file *file)
375{
376 return 0;
377}
378
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500379
380/**
381 * v9fs_create - Create a file
382 * @dentry: dentry that is being created
383 * @perm: create permissions
384 * @mode: open mode
385 *
386 */
387static struct p9_fid *
388v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
389 struct dentry *dentry, char *extension, u32 perm, u8 mode)
390{
391 int err;
392 char *name;
393 struct p9_fid *dfid, *ofid, *fid;
394 struct inode *inode;
395
396 err = 0;
397 ofid = NULL;
398 fid = NULL;
399 name = (char *) dentry->d_name.name;
400 dfid = v9fs_fid_clone(dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500401 if (IS_ERR(dfid)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500402 err = PTR_ERR(dfid);
403 dfid = NULL;
404 goto error;
405 }
406
407 /* clone a fid to use for creation */
408 ofid = p9_client_walk(dfid, 0, NULL, 1);
409 if (IS_ERR(ofid)) {
410 err = PTR_ERR(ofid);
411 ofid = NULL;
412 goto error;
413 }
414
415 err = p9_client_fcreate(ofid, name, perm, mode, extension);
416 if (err < 0)
417 goto error;
418
419 /* now walk from the parent so we can get unopened fid */
420 fid = p9_client_walk(dfid, 1, &name, 0);
421 if (IS_ERR(fid)) {
422 err = PTR_ERR(fid);
423 fid = NULL;
424 goto error;
425 } else
426 dfid = NULL;
427
428 /* instantiate inode and assign the unopened fid to the dentry */
429 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
430 if (IS_ERR(inode)) {
431 err = PTR_ERR(inode);
432 goto error;
433 }
434
Latchesar Ionkovba176742007-10-17 14:31:07 -0500435 if (v9ses->cache)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500436 dentry->d_op = &v9fs_cached_dentry_operations;
437 else
438 dentry->d_op = &v9fs_dentry_operations;
439
440 d_instantiate(dentry, inode);
441 v9fs_fid_add(dentry, fid);
442 return ofid;
443
444error:
445 if (dfid)
446 p9_client_clunk(dfid);
447
448 if (ofid)
449 p9_client_clunk(ofid);
450
451 if (fid)
452 p9_client_clunk(fid);
453
454 return ERR_PTR(err);
455}
456
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700457/**
458 * v9fs_vfs_create - VFS hook to create files
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500459 * @inode: directory inode that is being created
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700460 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800461 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700462 * @nd: path information
463 *
464 */
465
466static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800467v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700468 struct nameidata *nd)
469{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800470 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500471 u32 perm;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800472 int flags;
473 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500474 struct p9_fid *fid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800475 struct file *filp;
476
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500477 err = 0;
478 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800479 v9ses = v9fs_inode2v9ses(dir);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800480 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800481 if (nd && nd->flags & LOOKUP_OPEN)
482 flags = nd->intent.open.flags - 1;
483 else
484 flags = O_RDWR;
485
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500486 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
487 v9fs_uflags2omode(flags));
488 if (IS_ERR(fid)) {
489 err = PTR_ERR(fid);
490 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800491 goto error;
492 }
493
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500494 /* if we are opening a file, assign the open fid to the file */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800495 if (nd && nd->flags & LOOKUP_OPEN) {
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800496 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
497 if (IS_ERR(filp)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500498 err = PTR_ERR(filp);
499 goto error;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800500 }
501
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500502 filp->private_data = fid;
503 } else
504 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800505
506 return 0;
507
508error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500509 if (fid)
510 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800511
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800512 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700513}
514
515/**
516 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700517 * @inode: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700518 * @dentry: dentry that is being unlinked
519 * @mode: mode for new directory
520 *
521 */
522
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800523static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700524{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800525 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500526 u32 perm;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800527 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500528 struct p9_fid *fid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800529
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500530 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
531 err = 0;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800532 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800533 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500534 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
535 if (IS_ERR(fid)) {
536 err = PTR_ERR(fid);
537 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800538 }
539
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500540 if (fid)
541 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800542
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800543 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700544}
545
546/**
547 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
548 * @dir: inode that is being walked from
549 * @dentry: dentry that is being walked to?
550 * @nameidata: path data
551 *
552 */
553
554static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
555 struct nameidata *nameidata)
556{
557 struct super_block *sb;
558 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500559 struct p9_fid *dfid, *fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700560 struct inode *inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500561 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700562 int result = 0;
563
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500564 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800565 dir, dentry->d_name.name, dentry, nameidata);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700566
567 sb = dir->i_sb;
568 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500569 dfid = v9fs_fid_lookup(dentry->d_parent);
570 if (IS_ERR(dfid))
571 return ERR_PTR(PTR_ERR(dfid));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700572
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500573 name = (char *) dentry->d_name.name;
574 fid = p9_client_walk(dfid, 1, &name, 1);
575 if (IS_ERR(fid)) {
576 result = PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700577 if (result == -ENOENT) {
578 d_add(dentry, NULL);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700579 return NULL;
580 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700581
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500582 return ERR_PTR(result);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700583 }
584
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500585 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
586 if (IS_ERR(inode)) {
587 result = PTR_ERR(inode);
588 inode = NULL;
589 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700590 }
591
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500592 result = v9fs_fid_add(dentry, fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800593 if (result < 0)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500594 goto error;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800595
Latchesar Ionkovba176742007-10-17 14:31:07 -0500596 if ((fid->qid.version) && (v9ses->cache))
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600597 dentry->d_op = &v9fs_cached_dentry_operations;
598 else
599 dentry->d_op = &v9fs_dentry_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700600
601 d_add(dentry, inode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700602 return NULL;
603
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500604error:
605 if (fid)
606 p9_client_clunk(fid);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800607
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700608 return ERR_PTR(result);
609}
610
611/**
612 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
613 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700614 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700615 *
616 */
617
618static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
619{
620 return v9fs_remove(i, d, 0);
621}
622
623/**
624 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
625 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700626 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700627 *
628 */
629
630static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
631{
632 return v9fs_remove(i, d, 1);
633}
634
635/**
636 * v9fs_vfs_rename - VFS hook to rename an inode
637 * @old_dir: old dir inode
638 * @old_dentry: old dentry
639 * @new_dir: new dir inode
640 * @new_dentry: new dentry
641 *
642 */
643
644static int
645v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
646 struct inode *new_dir, struct dentry *new_dentry)
647{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500648 struct inode *old_inode;
649 struct v9fs_session_info *v9ses;
650 struct p9_fid *oldfid;
651 struct p9_fid *olddirfid;
652 struct p9_fid *newdirfid;
653 struct p9_wstat wstat;
654 int retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700655
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500656 P9_DPRINTK(P9_DEBUG_VFS, "\n");
657 retval = 0;
658 old_inode = old_dentry->d_inode;
659 v9ses = v9fs_inode2v9ses(old_inode);
660 oldfid = v9fs_fid_lookup(old_dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500661 if (IS_ERR(oldfid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800662 return PTR_ERR(oldfid);
663
664 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500665 if (IS_ERR(olddirfid)) {
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800666 retval = PTR_ERR(olddirfid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500667 goto done;
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800668 }
669
670 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500671 if (IS_ERR(newdirfid)) {
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800672 retval = PTR_ERR(newdirfid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500673 goto clunk_olddir;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700674 }
675
676 /* 9P can only handle file rename in the same directory */
677 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500678 P9_DPRINTK(P9_DEBUG_ERROR,
679 "old dir and new dir are different\n");
Eric Van Hensbergen621997c2007-01-26 00:57:04 -0800680 retval = -EXDEV;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500681 goto clunk_newdir;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700682 }
683
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800684 v9fs_blank_wstat(&wstat);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500685 wstat.muid = v9ses->uname;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800686 wstat.name = (char *) new_dentry->d_name.name;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500687 retval = p9_client_wstat(oldfid, &wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700688
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500689clunk_newdir:
Latchesar Ionkov22150c42007-10-23 13:48:33 -0500690 p9_client_clunk(newdirfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700691
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500692clunk_olddir:
Latchesar Ionkov22150c42007-10-23 13:48:33 -0500693 p9_client_clunk(olddirfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700694
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500695done:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700696 return retval;
697}
698
699/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100700 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700701 * @mnt - mount information
702 * @dentry - file to get attributes on
703 * @stat - metadata structure to populate
704 *
705 */
706
707static int
708v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
709 struct kstat *stat)
710{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500711 int err;
712 struct v9fs_session_info *v9ses;
713 struct p9_fid *fid;
714 struct p9_stat *st;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700715
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500716 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
717 err = -EPERM;
718 v9ses = v9fs_inode2v9ses(dentry->d_inode);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -0500719 if (v9ses->cache == CACHE_LOOSE)
720 return simple_getattr(mnt, dentry, stat);
721
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500722 fid = v9fs_fid_lookup(dentry);
723 if (IS_ERR(fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800724 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700725
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500726 st = p9_client_stat(fid);
727 if (IS_ERR(st))
728 return PTR_ERR(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700729
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500730 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700731 generic_fillattr(dentry->d_inode, stat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700732
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500733 kfree(st);
734 return 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700735}
736
737/**
738 * v9fs_vfs_setattr - set file metadata
739 * @dentry: file whose metadata to set
740 * @iattr: metadata assignment structure
741 *
742 */
743
744static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
745{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500746 int retval;
747 struct v9fs_session_info *v9ses;
748 struct p9_fid *fid;
749 struct p9_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700750
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500751 P9_DPRINTK(P9_DEBUG_VFS, "\n");
752 retval = -EPERM;
753 v9ses = v9fs_inode2v9ses(dentry->d_inode);
754 fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800755 if(IS_ERR(fid))
756 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700757
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800758 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700759 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800760 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700761
762 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800763 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700764
765 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800766 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700767
768 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800769 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700770
Latchesar Ionkov24056692007-10-17 14:31:07 -0500771 if (v9fs_extended(v9ses)) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800772 if (iattr->ia_valid & ATTR_UID)
773 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700774
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800775 if (iattr->ia_valid & ATTR_GID)
776 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700777 }
778
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500779 retval = p9_client_wstat(fid, &wstat);
780 if (retval >= 0)
781 retval = inode_setattr(dentry->d_inode, iattr);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700782
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500783 return retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700784}
785
786/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800787 * v9fs_stat2inode - populate an inode structure with mistat info
788 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700789 * @inode: inode to populate
790 * @sb: superblock of filesystem
791 *
792 */
793
794void
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500795v9fs_stat2inode(struct p9_stat *stat, struct inode *inode,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800796 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700797{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800798 int n;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800799 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700800 struct v9fs_session_info *v9ses = sb->s_fs_info;
801
802 inode->i_nlink = 1;
803
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800804 inode->i_atime.tv_sec = stat->atime;
805 inode->i_mtime.tv_sec = stat->mtime;
806 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700807
Latchesar Ionkovbd32b822007-10-17 14:31:07 -0500808 inode->i_uid = v9ses->dfltuid;
809 inode->i_gid = v9ses->dfltgid;
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 inode->i_uid = stat->n_uid;
813 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700814 }
815
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800816 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700817 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
818 char type = 0;
819 int major = -1;
820 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800821
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800822 n = stat->extension.len;
823 if (n > sizeof(ext)-1)
824 n = sizeof(ext)-1;
825 memmove(ext, stat->extension.str, n);
826 ext[n] = 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800827 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700828 switch (type) {
829 case 'c':
830 inode->i_mode &= ~S_IFBLK;
831 inode->i_mode |= S_IFCHR;
832 break;
833 case 'b':
834 break;
835 default:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500836 P9_DPRINTK(P9_DEBUG_ERROR,
837 "Unknown special type %c (%.*s)\n", type,
838 stat->extension.len, stat->extension.str);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700839 };
840 inode->i_rdev = MKDEV(major, minor);
841 } else
842 inode->i_rdev = 0;
843
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800844 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700845
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500846 /* not real number of blocks, but 512 byte ones ... */
847 inode->i_blocks = (inode->i_size + 512 - 1) >> 9;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700848}
849
850/**
851 * v9fs_qid2ino - convert qid into inode number
852 * @qid: qid to hash
853 *
854 * BUG: potential for inode number collisions?
855 */
856
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500857ino_t v9fs_qid2ino(struct p9_qid *qid)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700858{
859 u64 path = qid->path + 2;
860 ino_t i = 0;
861
862 if (sizeof(ino_t) == sizeof(path))
863 memcpy(&i, &path, sizeof(ino_t));
864 else
865 i = (ino_t) (path ^ (path >> 32));
866
867 return i;
868}
869
870/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700871 * v9fs_readlink - read a symlink's location (internal version)
872 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700873 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700874 * @buflen: length of buffer
875 *
876 */
877
878static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
879{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500880 int retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700881
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500882 struct v9fs_session_info *v9ses;
883 struct p9_fid *fid;
884 struct p9_stat *st;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700885
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500886 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
887 retval = -EPERM;
888 v9ses = v9fs_inode2v9ses(dentry->d_inode);
889 fid = v9fs_fid_lookup(dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500890 if (IS_ERR(fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800891 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700892
Latchesar Ionkov24056692007-10-17 14:31:07 -0500893 if (!v9fs_extended(v9ses))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500894 return -EBADF;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700895
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500896 st = p9_client_stat(fid);
897 if (IS_ERR(st))
898 return PTR_ERR(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700899
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500900 if (!(st->mode & P9_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700901 retval = -EINVAL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500902 goto done;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700903 }
904
905 /* copy extension buffer into buffer */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500906 if (st->extension.len < buflen)
907 buflen = st->extension.len + 1;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700908
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500909 memmove(buffer, st->extension.str, buflen - 1);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800910 buffer[buflen-1] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700911
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500912 P9_DPRINTK(P9_DEBUG_VFS,
913 "%s -> %.*s (%s)\n", dentry->d_name.name, st->extension.len,
914 st->extension.str, buffer);
915
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700916 retval = buflen;
917
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500918done:
919 kfree(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700920 return retval;
921}
922
923/**
924 * v9fs_vfs_readlink - read a symlink's location
925 * @dentry: dentry for symlink
926 * @buf: buffer to load symlink location into
927 * @buflen: length of buffer
928 *
929 */
930
931static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
932 int buflen)
933{
934 int retval;
935 int ret;
936 char *link = __getname();
937
Florin Malita0710d362006-06-25 05:48:31 -0700938 if (unlikely(!link))
939 return -ENOMEM;
940
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -0700941 if (buflen > PATH_MAX)
942 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700943
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500944 P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700945
946 retval = v9fs_readlink(dentry, link, buflen);
947
948 if (retval > 0) {
949 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500950 P9_DPRINTK(P9_DEBUG_ERROR,
951 "problem copying to user: %d\n", ret);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700952 retval = ret;
953 }
954 }
955
Davi Arnautce44eeb2005-11-07 00:59:36 -0800956 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700957 return retval;
958}
959
960/**
961 * v9fs_vfs_follow_link - follow a symlink path
962 * @dentry: dentry for symlink
963 * @nd: nameidata
964 *
965 */
966
967static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
968{
969 int len = 0;
970 char *link = __getname();
971
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500972 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700973
974 if (!link)
975 link = ERR_PTR(-ENOMEM);
976 else {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800977 len = v9fs_readlink(dentry, link, PATH_MAX);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700978
979 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -0800980 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700981 link = ERR_PTR(len);
982 } else
983 link[len] = 0;
984 }
985 nd_set_link(nd, link);
986
987 return NULL;
988}
989
990/**
991 * v9fs_vfs_put_link - release a symlink path
992 * @dentry: dentry for symlink
993 * @nd: nameidata
994 *
995 */
996
997static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
998{
999 char *s = nd_get_link(nd);
1000
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001001 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001002 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001003 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001004}
1005
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001006static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1007 int mode, const char *extension)
1008{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001009 u32 perm;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001010 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001011 struct p9_fid *fid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001012
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001013 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov24056692007-10-17 14:31:07 -05001014 if (!v9fs_extended(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001015 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001016 return -EPERM;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001017 }
1018
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001019 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001020 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1021 P9_OREAD);
1022 if (IS_ERR(fid))
1023 return PTR_ERR(fid);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001024
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001025 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001026 return 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001027}
1028
1029/**
1030 * v9fs_vfs_symlink - helper function to create symlinks
1031 * @dir: directory inode containing symlink
1032 * @dentry: dentry for symlink
1033 * @symname: symlink data
1034 *
1035 * See 9P2000.u RFC for more information
1036 *
1037 */
1038
1039static int
1040v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1041{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001042 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1043 dentry->d_name.name, symname);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001044
1045 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1046}
1047
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001048/**
1049 * v9fs_vfs_link - create a hardlink
1050 * @old_dentry: dentry for file to link to
1051 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001052 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001053 *
1054 */
1055
1056/* XXX - lots of code dup'd from symlink and creates,
1057 * figure out a better reuse strategy
1058 */
1059
1060static int
1061v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1062 struct dentry *dentry)
1063{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001064 int retval;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001065 struct p9_fid *oldfid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001066 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001067
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001068 P9_DPRINTK(P9_DEBUG_VFS,
1069 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001070 old_dentry->d_name.name);
1071
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001072 oldfid = v9fs_fid_clone(old_dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -05001073 if (IS_ERR(oldfid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001074 return PTR_ERR(oldfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001075
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001076 name = __getname();
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001077 if (unlikely(!name)) {
1078 retval = -ENOMEM;
1079 goto clunk_fid;
1080 }
Florin Malita0710d362006-06-25 05:48:31 -07001081
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001082 sprintf(name, "%d\n", oldfid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001083 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001084 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001085
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001086clunk_fid:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001087 p9_client_clunk(oldfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001088 return retval;
1089}
1090
1091/**
1092 * v9fs_vfs_mknod - create a special file
1093 * @dir: inode destination for new link
1094 * @dentry: dentry for file
1095 * @mode: mode for creation
1096 * @dev_t: device associated with special file
1097 *
1098 */
1099
1100static int
1101v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1102{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001103 int retval;
1104 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001105
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001106 P9_DPRINTK(P9_DEBUG_VFS,
1107 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001108 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1109
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001110 if (!new_valid_dev(rdev))
1111 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001112
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001113 name = __getname();
Eugene Teoc0291a02006-03-25 03:07:27 -08001114 if (!name)
1115 return -ENOMEM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001116 /* build extension */
1117 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001118 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001119 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001120 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001121 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001122 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001123 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001124 __putname(name);
1125 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001126 }
1127
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001128 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1129 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001130
1131 return retval;
1132}
1133
Arjan van de Ven754661f2007-02-12 00:55:38 -08001134static const struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001135 .create = v9fs_vfs_create,
1136 .lookup = v9fs_vfs_lookup,
1137 .symlink = v9fs_vfs_symlink,
1138 .link = v9fs_vfs_link,
1139 .unlink = v9fs_vfs_unlink,
1140 .mkdir = v9fs_vfs_mkdir,
1141 .rmdir = v9fs_vfs_rmdir,
1142 .mknod = v9fs_vfs_mknod,
1143 .rename = v9fs_vfs_rename,
1144 .readlink = v9fs_vfs_readlink,
1145 .getattr = v9fs_vfs_getattr,
1146 .setattr = v9fs_vfs_setattr,
1147};
1148
Arjan van de Ven754661f2007-02-12 00:55:38 -08001149static const struct inode_operations v9fs_dir_inode_operations = {
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001150 .create = v9fs_vfs_create,
1151 .lookup = v9fs_vfs_lookup,
1152 .unlink = v9fs_vfs_unlink,
1153 .mkdir = v9fs_vfs_mkdir,
1154 .rmdir = v9fs_vfs_rmdir,
1155 .mknod = v9fs_vfs_mknod,
1156 .rename = v9fs_vfs_rename,
1157 .getattr = v9fs_vfs_getattr,
1158 .setattr = v9fs_vfs_setattr,
1159};
1160
Arjan van de Ven754661f2007-02-12 00:55:38 -08001161static const struct inode_operations v9fs_file_inode_operations = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001162 .getattr = v9fs_vfs_getattr,
1163 .setattr = v9fs_vfs_setattr,
1164};
1165
Arjan van de Ven754661f2007-02-12 00:55:38 -08001166static const struct inode_operations v9fs_symlink_inode_operations = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001167 .readlink = v9fs_vfs_readlink,
1168 .follow_link = v9fs_vfs_follow_link,
1169 .put_link = v9fs_vfs_put_link,
1170 .getattr = v9fs_vfs_getattr,
1171 .setattr = v9fs_vfs_setattr,
1172};