blob: c72e20cdfb9a8213a2a82b674ae07fb4547239fa [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053038#include <linux/xattr.h>
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053039#include <linux/posix_acl.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050040#include <net/9p/9p.h>
41#include <net/9p/client.h>
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070042
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070043#include "v9fs.h"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070044#include "v9fs_vfs.h"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070045#include "fid.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050046#include "cache.h"
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053047#include "xattr.h"
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053048#include "acl.h"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070049
Arjan van de Ven754661f2007-02-12 00:55:38 -080050static const struct inode_operations v9fs_dir_inode_operations;
Sripathi Kodi9b6533c2010-03-25 12:41:54 +000051static const struct inode_operations v9fs_dir_inode_operations_dotu;
Arjan van de Ven754661f2007-02-12 00:55:38 -080052static const struct inode_operations v9fs_file_inode_operations;
53static const struct inode_operations v9fs_symlink_inode_operations;
Aneesh Kumar K.Vf5fc6142010-10-12 13:02:25 +053054
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070055/**
56 * unixmode2p9mode - convert unix mode bits to plan 9
57 * @v9ses: v9fs session information
58 * @mode: mode to convert
59 *
60 */
61
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070062static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070063{
64 int res;
65 res = mode & 0777;
66 if (S_ISDIR(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050067 res |= P9_DMDIR;
Sripathi Kodidd6102f2010-03-05 18:48:00 +000068 if (v9fs_proto_dotu(v9ses)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070069 if (S_ISLNK(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050070 res |= P9_DMSYMLINK;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070071 if (v9ses->nodev == 0) {
72 if (S_ISSOCK(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050073 res |= P9_DMSOCKET;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070074 if (S_ISFIFO(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050075 res |= P9_DMNAMEDPIPE;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070076 if (S_ISBLK(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050077 res |= P9_DMDEVICE;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070078 if (S_ISCHR(mode))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050079 res |= P9_DMDEVICE;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070080 }
81
82 if ((mode & S_ISUID) == S_ISUID)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050083 res |= P9_DMSETUID;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070084 if ((mode & S_ISGID) == S_ISGID)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050085 res |= P9_DMSETGID;
Anthony Liguorid199d652008-02-06 19:25:06 -060086 if ((mode & S_ISVTX) == S_ISVTX)
87 res |= P9_DMSETVTX;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050088 if ((mode & P9_DMLINK))
89 res |= P9_DMLINK;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070090 }
91
92 return res;
93}
94
95/**
96 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
97 * @v9ses: v9fs session information
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +000098 * @stat: p9_wstat from which mode need to be derived
99 * @rdev: major number, minor number in case of device files.
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700100 *
101 */
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000102static int p9mode2unixmode(struct v9fs_session_info *v9ses,
103 struct p9_wstat *stat, dev_t *rdev)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700104{
105 int res;
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000106 int mode = stat->mode;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700107
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000108 res = mode & S_IALLUGO;
109 *rdev = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700110
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500111 if ((mode & P9_DMDIR) == P9_DMDIR)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700112 res |= S_IFDIR;
Sripathi Kodidd6102f2010-03-05 18:48:00 +0000113 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700114 res |= S_IFLNK;
Sripathi Kodidd6102f2010-03-05 18:48:00 +0000115 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700116 && (v9ses->nodev == 0))
117 res |= S_IFSOCK;
Sripathi Kodidd6102f2010-03-05 18:48:00 +0000118 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700119 && (v9ses->nodev == 0))
120 res |= S_IFIFO;
Sripathi Kodidd6102f2010-03-05 18:48:00 +0000121 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000122 && (v9ses->nodev == 0)) {
123 char type = 0, ext[32];
124 int major = -1, minor = -1;
125
126 strncpy(ext, stat->extension, sizeof(ext));
127 sscanf(ext, "%c %u %u", &type, &major, &minor);
128 switch (type) {
129 case 'c':
130 res |= S_IFCHR;
131 break;
132 case 'b':
133 res |= S_IFBLK;
134 break;
135 default:
136 P9_DPRINTK(P9_DEBUG_ERROR,
137 "Unknown special type %c %s\n", type,
138 stat->extension);
139 };
140 *rdev = MKDEV(major, minor);
141 } else
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700142 res |= S_IFREG;
143
Sripathi Kodidd6102f2010-03-05 18:48:00 +0000144 if (v9fs_proto_dotu(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500145 if ((mode & P9_DMSETUID) == P9_DMSETUID)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700146 res |= S_ISUID;
147
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500148 if ((mode & P9_DMSETGID) == P9_DMSETGID)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700149 res |= S_ISGID;
Anthony Liguorid199d652008-02-06 19:25:06 -0600150
151 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
152 res |= S_ISVTX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700153 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700154 return res;
155}
156
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600157/**
158 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
159 * @uflags: flags to convert
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500160 * @extended: if .u extensions are active
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600161 */
162
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500163int v9fs_uflags2omode(int uflags, int extended)
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800164{
165 int ret;
166
167 ret = 0;
168 switch (uflags&3) {
169 default:
170 case O_RDONLY:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500171 ret = P9_OREAD;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800172 break;
173
174 case O_WRONLY:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500175 ret = P9_OWRITE;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800176 break;
177
178 case O_RDWR:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500179 ret = P9_ORDWR;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800180 break;
181 }
182
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800183 if (uflags & O_TRUNC)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500184 ret |= P9_OTRUNC;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800185
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -0500186 if (extended) {
187 if (uflags & O_EXCL)
188 ret |= P9_OEXCL;
189
190 if (uflags & O_APPEND)
191 ret |= P9_OAPPEND;
192 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800193
194 return ret;
195}
196
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700197/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800198 * v9fs_blank_wstat - helper function to setup a 9P stat structure
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800199 * @wstat: structure to initialize
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700200 *
201 */
202
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600203void
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500204v9fs_blank_wstat(struct p9_wstat *wstat)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700205{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800206 wstat->type = ~0;
207 wstat->dev = ~0;
208 wstat->qid.type = ~0;
209 wstat->qid.version = ~0;
210 *((long long *)&wstat->qid.path) = ~0;
211 wstat->mode = ~0;
212 wstat->atime = ~0;
213 wstat->mtime = ~0;
214 wstat->length = ~0;
215 wstat->name = NULL;
216 wstat->uid = NULL;
217 wstat->gid = NULL;
218 wstat->muid = NULL;
219 wstat->n_uid = ~0;
220 wstat->n_gid = ~0;
221 wstat->n_muid = ~0;
222 wstat->extension = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700223}
224
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500225/**
226 * v9fs_alloc_inode - helper function to allocate an inode
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500227 *
228 */
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500229struct inode *v9fs_alloc_inode(struct super_block *sb)
230{
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530231 struct v9fs_inode *v9inode;
232 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
233 GFP_KERNEL);
234 if (!v9inode)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500235 return NULL;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530236#ifdef CONFIG_9P_FSCACHE
237 v9inode->fscache = NULL;
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530238 spin_lock_init(&v9inode->fscache_lock);
239#endif
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530240 v9inode->writeback_fid = NULL;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530241 v9inode->cache_validity = 0;
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530242 mutex_init(&v9inode->v_mutex);
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530243 return &v9inode->vfs_inode;
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500244}
245
246/**
247 * v9fs_destroy_inode - destroy an inode
248 *
249 */
250
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100251static void v9fs_i_callback(struct rcu_head *head)
252{
253 struct inode *inode = container_of(head, struct inode, i_rcu);
254 INIT_LIST_HEAD(&inode->i_dentry);
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530255 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100256}
257
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500258void v9fs_destroy_inode(struct inode *inode)
259{
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100260 call_rcu(&inode->i_rcu, v9fs_i_callback);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500261}
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500262
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530263int v9fs_init_inode(struct v9fs_session_info *v9ses,
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000264 struct inode *inode, int mode, dev_t rdev)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700265{
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530266 int err = 0;
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600267
Dmitry Monakhov217f2062010-03-04 17:30:57 +0300268 inode_init_owner(inode, NULL, mode);
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600269 inode->i_blocks = 0;
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000270 inode->i_rdev = rdev;
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600271 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
272 inode->i_mapping->a_ops = &v9fs_addr_operations;
273
274 switch (mode & S_IFMT) {
275 case S_IFIFO:
276 case S_IFBLK:
277 case S_IFCHR:
278 case S_IFSOCK:
M. Mohan Kumar4b435162010-06-16 14:27:01 +0530279 if (v9fs_proto_dotl(v9ses)) {
280 inode->i_op = &v9fs_file_inode_operations_dotl;
281 inode->i_fop = &v9fs_file_operations_dotl;
282 } else if (v9fs_proto_dotu(v9ses)) {
283 inode->i_op = &v9fs_file_inode_operations;
284 inode->i_fop = &v9fs_file_operations;
285 } else {
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600286 P9_DPRINTK(P9_DEBUG_ERROR,
287 "special files without extended mode\n");
288 err = -EINVAL;
289 goto error;
290 }
291 init_special_inode(inode, inode->i_mode, inode->i_rdev);
292 break;
293 case S_IFREG:
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000294 if (v9fs_proto_dotl(v9ses)) {
295 inode->i_op = &v9fs_file_inode_operations_dotl;
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530296 if (v9ses->cache)
297 inode->i_fop =
298 &v9fs_cached_file_operations_dotl;
299 else
300 inode->i_fop = &v9fs_file_operations_dotl;
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000301 } else {
302 inode->i_op = &v9fs_file_inode_operations;
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530303 if (v9ses->cache)
304 inode->i_fop = &v9fs_cached_file_operations;
305 else
306 inode->i_fop = &v9fs_file_operations;
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000307 }
308
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600309 break;
310 case S_IFLNK:
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000311 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
312 P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
313 "legacy protocol.\n");
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600314 err = -EINVAL;
315 goto error;
316 }
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000317
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_op = &v9fs_symlink_inode_operations_dotl;
320 else
321 inode->i_op = &v9fs_symlink_inode_operations;
322
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600323 break;
324 case S_IFDIR:
325 inc_nlink(inode);
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000326 if (v9fs_proto_dotl(v9ses))
327 inode->i_op = &v9fs_dir_inode_operations_dotl;
328 else if (v9fs_proto_dotu(v9ses))
329 inode->i_op = &v9fs_dir_inode_operations_dotu;
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600330 else
331 inode->i_op = &v9fs_dir_inode_operations;
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000332
333 if (v9fs_proto_dotl(v9ses))
334 inode->i_fop = &v9fs_dir_operations_dotl;
335 else
336 inode->i_fop = &v9fs_dir_operations;
337
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600338 break;
339 default:
340 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
341 mode, mode & S_IFMT);
342 err = -EINVAL;
343 goto error;
344 }
Abhishek Kulkarni2bb54112009-07-19 13:41:56 -0600345error:
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530346 return err;
347
348}
349
350/**
351 * v9fs_get_inode - helper function to setup an inode
352 * @sb: superblock
353 * @mode: mode to setup inode with
354 *
355 */
356
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000357struct inode *v9fs_get_inode(struct super_block *sb, int mode, dev_t rdev)
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530358{
359 int err;
360 struct inode *inode;
361 struct v9fs_session_info *v9ses = sb->s_fs_info;
362
363 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
364
365 inode = new_inode(sb);
366 if (!inode) {
367 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
368 return ERR_PTR(-ENOMEM);
369 }
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000370 err = v9fs_init_inode(v9ses, inode, mode, rdev);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530371 if (err) {
372 iput(inode);
373 return ERR_PTR(err);
374 }
375 return inode;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700376}
377
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500378/*
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800379static struct v9fs_fid*
380v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
381{
382 int err;
Mika Kukkonen736c4b82006-12-06 20:36:29 -0800383 int nfid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800384 struct v9fs_fid *ret;
385 struct v9fs_fcall *fcall;
386
387 nfid = v9fs_get_idpool(&v9ses->fidpool);
388 if (nfid < 0) {
389 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800390 return ERR_PTR(-ENOSPC);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800391 }
392
393 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
394 &fcall);
395
396 if (err < 0) {
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700397 if (fcall && fcall->id == RWALK)
398 goto clunk_fid;
399
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800400 PRINT_FCALL_ERROR("walk error", fcall);
401 v9fs_put_idpool(nfid, &v9ses->fidpool);
402 goto error;
403 }
404
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700405 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800406 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800407 ret = v9fs_fid_create(v9ses, nfid);
408 if (!ret) {
409 err = -ENOMEM;
410 goto clunk_fid;
411 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700412
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800413 err = v9fs_fid_insert(ret, dentry);
414 if (err < 0) {
415 v9fs_fid_destroy(ret);
416 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700417 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800418
419 return ret;
420
421clunk_fid:
422 v9fs_t_clunk(v9ses, nfid);
423
424error:
425 kfree(fcall);
426 return ERR_PTR(err);
427}
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500428*/
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800429
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500430
431/**
432 * v9fs_clear_inode - release an inode
433 * @inode: inode to release
434 *
435 */
Al Virob57922d2010-06-07 14:34:48 -0400436void v9fs_evict_inode(struct inode *inode)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500437{
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530438 struct v9fs_inode *v9inode = V9FS_I(inode);
439
Al Virob57922d2010-06-07 14:34:48 -0400440 truncate_inode_pages(inode->i_mapping, 0);
441 end_writeback(inode);
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500442 filemap_fdatawrite(inode->i_mapping);
443
444#ifdef CONFIG_9P_FSCACHE
445 v9fs_cache_inode_put_cookie(inode);
446#endif
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530447 /* clunk the fid stashed in writeback_fid */
448 if (v9inode->writeback_fid) {
449 p9_client_clunk(v9inode->writeback_fid);
450 v9inode->writeback_fid = NULL;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530451 }
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500452}
453
Aneesh Kumar K.Ve38b21e2011-07-11 16:40:59 +0000454static int v9fs_test_inode(struct inode *inode, void *data)
455{
456 int umode;
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000457 dev_t rdev;
Aneesh Kumar K.Ve38b21e2011-07-11 16:40:59 +0000458 struct v9fs_inode *v9inode = V9FS_I(inode);
459 struct p9_wstat *st = (struct p9_wstat *)data;
460 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
461
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000462 umode = p9mode2unixmode(v9ses, st, &rdev);
Aneesh Kumar K.Ve38b21e2011-07-11 16:40:59 +0000463 /* don't match inode of different type */
464 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
465 return 0;
466
467 /* compare qid details */
468 if (memcmp(&v9inode->qid.version,
469 &st->qid.version, sizeof(v9inode->qid.version)))
470 return 0;
471
472 if (v9inode->qid.type != st->qid.type)
473 return 0;
474 return 1;
475}
476
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530477static int v9fs_test_new_inode(struct inode *inode, void *data)
478{
479 return 0;
480}
481
Aneesh Kumar K.Ve38b21e2011-07-11 16:40:59 +0000482static int v9fs_set_inode(struct inode *inode, void *data)
483{
484 struct v9fs_inode *v9inode = V9FS_I(inode);
485 struct p9_wstat *st = (struct p9_wstat *)data;
486
487 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
488 return 0;
489}
490
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530491static struct inode *v9fs_qid_iget(struct super_block *sb,
492 struct p9_qid *qid,
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530493 struct p9_wstat *st,
494 int new)
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530495{
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000496 dev_t rdev;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530497 int retval, umode;
498 unsigned long i_ino;
499 struct inode *inode;
500 struct v9fs_session_info *v9ses = sb->s_fs_info;
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530501 int (*test)(struct inode *, void *);
502
503 if (new)
504 test = v9fs_test_new_inode;
505 else
506 test = v9fs_test_inode;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530507
508 i_ino = v9fs_qid2ino(qid);
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530509 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530510 if (!inode)
511 return ERR_PTR(-ENOMEM);
512 if (!(inode->i_state & I_NEW))
513 return inode;
514 /*
515 * initialize the inode with the stat info
516 * FIXME!! we may need support for stale inodes
517 * later.
518 */
Aneesh Kumar K.Ve38b21e2011-07-11 16:40:59 +0000519 inode->i_ino = i_ino;
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +0000520 umode = p9mode2unixmode(v9ses, st, &rdev);
521 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530522 if (retval)
523 goto error;
524
525 v9fs_stat2inode(st, inode, sb);
526#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530527 v9fs_cache_inode_get_cookie(inode);
528#endif
529 unlock_new_inode(inode);
530 return inode;
531error:
532 unlock_new_inode(inode);
533 iput(inode);
534 return ERR_PTR(retval);
535
536}
537
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600538struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530539v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530540 struct super_block *sb, int new)
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800541{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500542 struct p9_wstat *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530543 struct inode *inode = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800544
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500545 st = p9_client_stat(fid);
Abhishek Kulkarni02bc3562009-07-19 13:41:54 -0600546 if (IS_ERR(st))
547 return ERR_CAST(st);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800548
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530549 inode = v9fs_qid_iget(sb, &st->qid, st, new);
Abhishek Kulkarni02bc3562009-07-19 13:41:54 -0600550 p9stat_free(st);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500551 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530552 return inode;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700553}
554
555/**
556 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700557 * @dir: directory inode that is being deleted
558 * @file: dentry that is being deleted
559 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700560 *
561 */
562
563static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
564{
Aneesh Kumar K.Vd994f402010-03-29 18:14:50 -0500565 int retval;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500566 struct p9_fid *v9fid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530567 struct inode *file_inode;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700568
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500569 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700570 rmdir);
571
572 file_inode = file->d_inode;
Latchesar Ionkovc959df92007-04-23 14:41:11 -0700573 v9fid = v9fs_fid_clone(file);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500574 if (IS_ERR(v9fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800575 return PTR_ERR(v9fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700576
Aneesh Kumar K.Vd994f402010-03-29 18:14:50 -0500577 retval = p9_client_remove(v9fid);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530578 if (!retval) {
579 /*
580 * directories on unlink should have zero
581 * link count
582 */
583 if (rmdir) {
584 clear_nlink(file_inode);
585 drop_nlink(dir);
586 } else
587 drop_nlink(file_inode);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530588
Aneesh Kumar K.V3bc86de2011-02-28 17:04:07 +0530589 v9fs_invalidate_inode_attr(file_inode);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530590 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530591 }
Aneesh Kumar K.Vd994f402010-03-29 18:14:50 -0500592 return retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700593}
594
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500595/**
596 * v9fs_create - Create a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600597 * @v9ses: session information
598 * @dir: directory that dentry is being created in
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500599 * @dentry: dentry that is being created
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600600 * @extension: 9p2000.u extension string to support devices, etc.
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500601 * @perm: create permissions
602 * @mode: open mode
603 *
604 */
605static struct p9_fid *
606v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
607 struct dentry *dentry, char *extension, u32 perm, u8 mode)
608{
609 int err;
610 char *name;
611 struct p9_fid *dfid, *ofid, *fid;
612 struct inode *inode;
613
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500614 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
615
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500616 err = 0;
617 ofid = NULL;
618 fid = NULL;
619 name = (char *) dentry->d_name.name;
Venkateswararao Jujjuri6d27e642010-05-10 18:08:28 +0000620 dfid = v9fs_fid_lookup(dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500621 if (IS_ERR(dfid)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500622 err = PTR_ERR(dfid);
Venkateswararao Jujjuri6d27e642010-05-10 18:08:28 +0000623 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
624 return ERR_PTR(err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500625 }
626
627 /* clone a fid to use for creation */
628 ofid = p9_client_walk(dfid, 0, NULL, 1);
629 if (IS_ERR(ofid)) {
630 err = PTR_ERR(ofid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500631 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Venkateswararao Jujjuri6d27e642010-05-10 18:08:28 +0000632 return ERR_PTR(err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500633 }
634
635 err = p9_client_fcreate(ofid, name, perm, mode, extension);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500636 if (err < 0) {
637 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500638 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500639 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500640
641 /* now walk from the parent so we can get unopened fid */
Venkateswararao Jujjuri6d27e642010-05-10 18:08:28 +0000642 fid = p9_client_walk(dfid, 1, &name, 1);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500643 if (IS_ERR(fid)) {
644 err = PTR_ERR(fid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500645 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500646 fid = NULL;
647 goto error;
Venkateswararao Jujjuri6d27e642010-05-10 18:08:28 +0000648 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500649
650 /* instantiate inode and assign the unopened fid to the dentry */
Aneesh Kumar K.V926fa0b2011-07-06 16:32:31 +0530651 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500652 if (IS_ERR(inode)) {
653 err = PTR_ERR(inode);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500654 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500655 goto error;
656 }
Abhishek Kulkarni50fb6d22009-07-19 13:41:57 -0600657 err = v9fs_fid_add(dentry, fid);
658 if (err < 0)
659 goto error;
Aneesh Kumar K.Ve279cdc2011-07-25 18:06:32 +0000660 d_instantiate(dentry, inode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500661 return ofid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500662error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500663 if (ofid)
664 p9_client_clunk(ofid);
665
666 if (fid)
667 p9_client_clunk(fid);
668
669 return ERR_PTR(err);
670}
671
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700672/**
673 * v9fs_vfs_create - VFS hook to create files
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600674 * @dir: directory inode that is being created
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700675 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800676 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700677 * @nd: path information
678 *
679 */
680
681static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800682v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700683 struct nameidata *nd)
684{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800685 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500686 u32 perm;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800687 int flags;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530688 struct file *filp;
689 struct v9fs_inode *v9inode;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800690 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530691 struct p9_fid *fid, *inode_fid;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800692
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500693 err = 0;
694 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800695 v9ses = v9fs_inode2v9ses(dir);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800696 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800697 if (nd && nd->flags & LOOKUP_OPEN)
698 flags = nd->intent.open.flags - 1;
699 else
700 flags = O_RDWR;
701
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500702 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
Sripathi Kodidd6102f2010-03-05 18:48:00 +0000703 v9fs_uflags2omode(flags,
704 v9fs_proto_dotu(v9ses)));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500705 if (IS_ERR(fid)) {
706 err = PTR_ERR(fid);
707 fid = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800708 goto error;
709 }
710
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530711 v9fs_invalidate_inode_attr(dir);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500712 /* if we are opening a file, assign the open fid to the file */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800713 if (nd && nd->flags & LOOKUP_OPEN) {
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530714 v9inode = V9FS_I(dentry->d_inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530715 mutex_lock(&v9inode->v_mutex);
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530716 if (v9ses->cache && !v9inode->writeback_fid &&
717 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530718 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530719 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530720 * we do it during open time instead of
721 * page dirty time via write_begin/page_mkwrite
722 * because we want write after unlink usecase
723 * to work.
724 */
725 inode_fid = v9fs_writeback_fid(dentry);
726 if (IS_ERR(inode_fid)) {
727 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530728 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530729 goto error;
730 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530731 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530732 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530733 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V877cb3d2010-09-22 12:42:30 +0530734 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800735 if (IS_ERR(filp)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500736 err = PTR_ERR(filp);
737 goto error;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800738 }
739
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500740 filp->private_data = fid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530741#ifdef CONFIG_9P_FSCACHE
742 if (v9ses->cache)
743 v9fs_cache_inode_set_cookie(dentry->d_inode, filp);
744#endif
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500745 } else
746 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800747
748 return 0;
749
750error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500751 if (fid)
752 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800753
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800754 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700755}
756
757/**
758 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600759 * @dir: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700760 * @dentry: dentry that is being unlinked
761 * @mode: mode for new directory
762 *
763 */
764
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800765static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700766{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800767 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500768 u32 perm;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500769 struct p9_fid *fid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530770 struct v9fs_session_info *v9ses;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800771
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500772 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
773 err = 0;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800774 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800775 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500776 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
777 if (IS_ERR(fid)) {
778 err = PTR_ERR(fid);
779 fid = NULL;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530780 } else {
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530781 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530782 v9fs_invalidate_inode_attr(dir);
783 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800784
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500785 if (fid)
786 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800787
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800788 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700789}
790
791/**
792 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
793 * @dir: inode that is being walked from
794 * @dentry: dentry that is being walked to?
795 * @nameidata: path data
796 *
797 */
798
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600799struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700800 struct nameidata *nameidata)
801{
Aneesh Kumar K.V8bdb14f2011-08-16 22:19:28 +0530802 struct dentry *res;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700803 struct super_block *sb;
804 struct v9fs_session_info *v9ses;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500805 struct p9_fid *dfid, *fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700806 struct inode *inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500807 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700808 int result = 0;
809
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500810 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800811 dir, dentry->d_name.name, dentry, nameidata);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700812
Sripathi Kodi11e9b492010-03-29 18:13:59 -0500813 if (dentry->d_name.len > NAME_MAX)
814 return ERR_PTR(-ENAMETOOLONG);
815
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700816 sb = dir->i_sb;
817 v9ses = v9fs_inode2v9ses(dir);
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530818 /* We can walk d_parent because we hold the dir->i_mutex */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500819 dfid = v9fs_fid_lookup(dentry->d_parent);
820 if (IS_ERR(dfid))
David Howellse231c2e2008-02-07 00:15:26 -0800821 return ERR_CAST(dfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700822
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500823 name = (char *) dentry->d_name.name;
824 fid = p9_client_walk(dfid, 1, &name, 1);
825 if (IS_ERR(fid)) {
826 result = PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700827 if (result == -ENOENT) {
Aneesh Kumar K.V85e0df22010-05-14 13:04:39 +0000828 inode = NULL;
829 goto inst_out;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700830 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700831
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500832 return ERR_PTR(result);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700833 }
Aneesh Kumar K.V8bdb14f2011-08-16 22:19:28 +0530834 /*
835 * Make sure we don't use a wrong inode due to parallel
836 * unlink. For cached mode create calls request for new
837 * inode. But with cache disabled, lookup should do this.
838 */
839 if (v9ses->cache)
840 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
841 else
842 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500843 if (IS_ERR(inode)) {
844 result = PTR_ERR(inode);
845 inode = NULL;
846 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700847 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500848 result = v9fs_fid_add(dentry, fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800849 if (result < 0)
Aneesh Kumar K.V9856af82010-09-22 12:24:23 +0530850 goto error_iput;
Aneesh Kumar K.V85e0df22010-05-14 13:04:39 +0000851inst_out:
Aneesh Kumar K.V8bdb14f2011-08-16 22:19:28 +0530852 /*
853 * If we had a rename on the server and a parallel lookup
854 * for the new name, then make sure we instantiate with
855 * the new name. ie look up for a/b, while on server somebody
856 * moved b under k and client parallely did a lookup for
857 * k/b.
858 */
859 res = d_materialise_unique(dentry, inode);
860 if (!IS_ERR(res))
861 return res;
862 result = PTR_ERR(res);
Aneesh Kumar K.V9856af82010-09-22 12:24:23 +0530863error_iput:
864 iput(inode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500865error:
Julien Brunel62aa5282008-09-24 16:22:22 -0500866 p9_client_clunk(fid);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800867
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700868 return ERR_PTR(result);
869}
870
871/**
872 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
873 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700874 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700875 *
876 */
877
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600878int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700879{
880 return v9fs_remove(i, d, 0);
881}
882
883/**
884 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
885 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700886 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700887 *
888 */
889
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600890int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700891{
892 return v9fs_remove(i, d, 1);
893}
894
895/**
896 * v9fs_vfs_rename - VFS hook to rename an inode
897 * @old_dir: old dir inode
898 * @old_dentry: old dentry
899 * @new_dir: new dir inode
900 * @new_dentry: new dentry
901 *
902 */
903
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600904int
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700905v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
906 struct inode *new_dir, struct dentry *new_dentry)
907{
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530908 int retval;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500909 struct inode *old_inode;
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530910 struct inode *new_inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500911 struct v9fs_session_info *v9ses;
912 struct p9_fid *oldfid;
913 struct p9_fid *olddirfid;
914 struct p9_fid *newdirfid;
915 struct p9_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700916
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500917 P9_DPRINTK(P9_DEBUG_VFS, "\n");
918 retval = 0;
919 old_inode = old_dentry->d_inode;
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530920 new_inode = new_dentry->d_inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500921 v9ses = v9fs_inode2v9ses(old_inode);
922 oldfid = v9fs_fid_lookup(old_dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500923 if (IS_ERR(oldfid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800924 return PTR_ERR(oldfid);
925
926 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500927 if (IS_ERR(olddirfid)) {
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800928 retval = PTR_ERR(olddirfid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500929 goto done;
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800930 }
931
932 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500933 if (IS_ERR(newdirfid)) {
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800934 retval = PTR_ERR(newdirfid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500935 goto clunk_olddir;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700936 }
937
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530938 down_write(&v9ses->rename_sem);
Sripathi Kodi4681dbd2010-03-25 12:47:26 +0000939 if (v9fs_proto_dotl(v9ses)) {
940 retval = p9_client_rename(oldfid, newdirfid,
941 (char *) new_dentry->d_name.name);
942 if (retval != -ENOSYS)
943 goto clunk_newdir;
944 }
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530945 if (old_dentry->d_parent != new_dentry->d_parent) {
946 /*
947 * 9P .u can only handle file rename in the same directory
948 */
Sripathi Kodi4681dbd2010-03-25 12:47:26 +0000949
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500950 P9_DPRINTK(P9_DEBUG_ERROR,
951 "old dir and new dir are different\n");
Eric Van Hensbergen621997c2007-01-26 00:57:04 -0800952 retval = -EXDEV;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500953 goto clunk_newdir;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700954 }
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800955 v9fs_blank_wstat(&wstat);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500956 wstat.muid = v9ses->uname;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800957 wstat.name = (char *) new_dentry->d_name.name;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500958 retval = p9_client_wstat(oldfid, &wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700959
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500960clunk_newdir:
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530961 if (!retval) {
962 if (new_inode) {
963 if (S_ISDIR(new_inode->i_mode))
964 clear_nlink(new_inode);
965 else
966 drop_nlink(new_inode);
Aneesh Kumar K.V23b08e92011-02-28 17:04:08 +0530967 /*
968 * Work around vfs rename rehash bug with
969 * FS_RENAME_DOES_D_MOVE
970 */
971 v9fs_invalidate_inode_attr(new_inode);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530972 }
973 if (S_ISDIR(old_inode->i_mode)) {
974 if (!new_inode)
975 inc_nlink(new_dir);
976 drop_nlink(old_dir);
977 }
Aneesh Kumar K.V3bc86de2011-02-28 17:04:07 +0530978 v9fs_invalidate_inode_attr(old_inode);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530979 v9fs_invalidate_inode_attr(old_dir);
980 v9fs_invalidate_inode_attr(new_dir);
Aneesh Kumar K.V3bc86de2011-02-28 17:04:07 +0530981
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530982 /* successful rename */
983 d_move(old_dentry, new_dentry);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530984 }
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530985 up_write(&v9ses->rename_sem);
Latchesar Ionkov22150c42007-10-23 13:48:33 -0500986 p9_client_clunk(newdirfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700987
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500988clunk_olddir:
Latchesar Ionkov22150c42007-10-23 13:48:33 -0500989 p9_client_clunk(olddirfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700990
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500991done:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700992 return retval;
993}
994
995/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100996 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600997 * @mnt: mount information
998 * @dentry: file to get attributes on
999 * @stat: metadata structure to populate
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001000 *
1001 */
1002
1003static int
1004v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1005 struct kstat *stat)
1006{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001007 int err;
1008 struct v9fs_session_info *v9ses;
1009 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001010 struct p9_wstat *st;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001011
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001012 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1013 err = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +05301014 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +05301015 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1016 generic_fillattr(dentry->d_inode, stat);
1017 return 0;
1018 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001019 fid = v9fs_fid_lookup(dentry);
1020 if (IS_ERR(fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001021 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001022
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001023 st = p9_client_stat(fid);
1024 if (IS_ERR(st))
1025 return PTR_ERR(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001026
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001027 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +00001028 generic_fillattr(dentry->d_inode, stat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001029
Latchesar Ionkov62b2be52010-08-24 18:13:59 +00001030 p9stat_free(st);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001031 kfree(st);
1032 return 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001033}
1034
1035/**
1036 * v9fs_vfs_setattr - set file metadata
1037 * @dentry: file whose metadata to set
1038 * @iattr: metadata assignment structure
1039 *
1040 */
1041
1042static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1043{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001044 int retval;
1045 struct v9fs_session_info *v9ses;
1046 struct p9_fid *fid;
1047 struct p9_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001048
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001049 P9_DPRINTK(P9_DEBUG_VFS, "\n");
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +05301050 retval = inode_change_ok(dentry->d_inode, iattr);
1051 if (retval)
1052 return retval;
1053
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001054 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +05301055 v9ses = v9fs_dentry2v9ses(dentry);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001056 fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001057 if(IS_ERR(fid))
1058 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001059
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001060 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001061 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001062 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001063
1064 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001065 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001066
1067 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001068 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001069
1070 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001071 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001072
Sripathi Kodidd6102f2010-03-05 18:48:00 +00001073 if (v9fs_proto_dotu(v9ses)) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001074 if (iattr->ia_valid & ATTR_UID)
1075 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001076
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001077 if (iattr->ia_valid & ATTR_GID)
1078 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001079 }
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +05301080
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +05301081 /* Write all dirty data */
1082 if (S_ISREG(dentry->d_inode->i_mode))
1083 filemap_write_and_wait(dentry->d_inode->i_mapping);
1084
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +05301085 retval = p9_client_wstat(fid, &wstat);
1086 if (retval < 0)
1087 return retval;
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +05301088
1089 if ((iattr->ia_valid & ATTR_SIZE) &&
1090 iattr->ia_size != i_size_read(dentry->d_inode))
1091 truncate_setsize(dentry->d_inode, iattr->ia_size);
1092
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +05301093 v9fs_invalidate_inode_attr(dentry->d_inode);
Christoph Hellwig10257742010-06-04 11:30:02 +02001094
1095 setattr_copy(dentry->d_inode, iattr);
1096 mark_inode_dirty(dentry->d_inode);
1097 return 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001098}
1099
1100/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001101 * v9fs_stat2inode - populate an inode structure with mistat info
1102 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001103 * @inode: inode to populate
1104 * @sb: superblock of filesystem
1105 *
1106 */
1107
1108void
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001109v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001110 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001111{
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +00001112 mode_t mode;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001113 char ext[32];
Aneesh Kumar K.V57171442010-03-05 14:43:43 -06001114 char tag_name[14];
1115 unsigned int i_nlink;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001116 struct v9fs_session_info *v9ses = sb->s_fs_info;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +05301117 struct v9fs_inode *v9inode = V9FS_I(inode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001118
1119 inode->i_nlink = 1;
1120
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001121 inode->i_atime.tv_sec = stat->atime;
1122 inode->i_mtime.tv_sec = stat->mtime;
1123 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001124
Latchesar Ionkovbd32b822007-10-17 14:31:07 -05001125 inode->i_uid = v9ses->dfltuid;
1126 inode->i_gid = v9ses->dfltgid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001127
Sripathi Kodidd6102f2010-03-05 18:48:00 +00001128 if (v9fs_proto_dotu(v9ses)) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001129 inode->i_uid = stat->n_uid;
1130 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001131 }
Aneesh Kumar K.V57171442010-03-05 14:43:43 -06001132 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1133 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1134 /*
1135 * Hadlink support got added later to
1136 * to the .u extension. So there can be
1137 * server out there that doesn't support
1138 * this even with .u extension. So check
1139 * for non NULL stat->extension
1140 */
1141 strncpy(ext, stat->extension, sizeof(ext));
1142 /* HARDLINKCOUNT %u */
1143 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1144 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1145 inode->i_nlink = i_nlink;
1146 }
1147 }
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +00001148 mode = stat->mode & S_IALLUGO;
1149 mode |= inode->i_mode & ~S_IALLUGO;
1150 inode->i_mode = mode;
Abhishek Kulkarni7549ae32009-09-22 11:34:05 -05001151 i_size_write(inode, stat->length);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001152
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001153 /* not real number of blocks, but 512 byte ones ... */
Abhishek Kulkarni7549ae32009-09-22 11:34:05 -05001154 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +05301155 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001156}
1157
1158/**
1159 * v9fs_qid2ino - convert qid into inode number
1160 * @qid: qid to hash
1161 *
1162 * BUG: potential for inode number collisions?
1163 */
1164
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001165ino_t v9fs_qid2ino(struct p9_qid *qid)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001166{
1167 u64 path = qid->path + 2;
1168 ino_t i = 0;
1169
1170 if (sizeof(ino_t) == sizeof(path))
1171 memcpy(&i, &path, sizeof(ino_t));
1172 else
1173 i = (ino_t) (path ^ (path >> 32));
1174
1175 return i;
1176}
1177
1178/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001179 * v9fs_readlink - read a symlink's location (internal version)
1180 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001181 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001182 * @buflen: length of buffer
1183 *
1184 */
1185
1186static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1187{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001188 int retval;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001189
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001190 struct v9fs_session_info *v9ses;
1191 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001192 struct p9_wstat *st;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001193
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001194 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1195 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +05301196 v9ses = v9fs_dentry2v9ses(dentry);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001197 fid = v9fs_fid_lookup(dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -05001198 if (IS_ERR(fid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001199 return PTR_ERR(fid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001200
M. Mohan Kumar329176c2010-09-28 19:59:25 +05301201 if (!v9fs_proto_dotu(v9ses))
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001202 return -EBADF;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001203
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001204 st = p9_client_stat(fid);
1205 if (IS_ERR(st))
1206 return PTR_ERR(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001207
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001208 if (!(st->mode & P9_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001209 retval = -EINVAL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001210 goto done;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001211 }
1212
1213 /* copy extension buffer into buffer */
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001214 strncpy(buffer, st->extension, buflen);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001215
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001216 P9_DPRINTK(P9_DEBUG_VFS,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001217 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001218
Martin Stava2511cd02009-11-02 08:39:34 -06001219 retval = strnlen(buffer, buflen);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001220done:
Latchesar Ionkov62b2be52010-08-24 18:13:59 +00001221 p9stat_free(st);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001222 kfree(st);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001223 return retval;
1224}
1225
1226/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001227 * v9fs_vfs_follow_link - follow a symlink path
1228 * @dentry: dentry for symlink
1229 * @nd: nameidata
1230 *
1231 */
1232
1233static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1234{
1235 int len = 0;
1236 char *link = __getname();
1237
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001238 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001239
1240 if (!link)
1241 link = ERR_PTR(-ENOMEM);
1242 else {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001243 len = v9fs_readlink(dentry, link, PATH_MAX);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001244
1245 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -08001246 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001247 link = ERR_PTR(len);
1248 } else
Martin Stava2511cd02009-11-02 08:39:34 -06001249 link[min(len, PATH_MAX-1)] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001250 }
1251 nd_set_link(nd, link);
1252
1253 return NULL;
1254}
1255
1256/**
1257 * v9fs_vfs_put_link - release a symlink path
1258 * @dentry: dentry for symlink
1259 * @nd: nameidata
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001260 * @p: unused
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001261 *
1262 */
1263
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001264void
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001265v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001266{
1267 char *s = nd_get_link(nd);
1268
Duane Griffin6ff23202008-12-19 16:45:21 -06001269 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1270 IS_ERR(s) ? "<error>" : s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001271 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001272 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001273}
1274
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001275/**
1276 * v9fs_vfs_mkspecial - create a special file
1277 * @dir: inode to create special file in
1278 * @dentry: dentry to create
1279 * @mode: mode to create special file
1280 * @extension: 9p2000.u format extension string representing special file
1281 *
1282 */
1283
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001284static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1285 int mode, const char *extension)
1286{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001287 u32 perm;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001288 struct p9_fid *fid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +05301289 struct v9fs_session_info *v9ses;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001290
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001291 v9ses = v9fs_inode2v9ses(dir);
Sripathi Kodidd6102f2010-03-05 18:48:00 +00001292 if (!v9fs_proto_dotu(v9ses)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001293 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001294 return -EPERM;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001295 }
1296
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001297 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001298 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1299 P9_OREAD);
1300 if (IS_ERR(fid))
1301 return PTR_ERR(fid);
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001302
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +05301303 v9fs_invalidate_inode_attr(dir);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001304 p9_client_clunk(fid);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001305 return 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001306}
1307
1308/**
1309 * v9fs_vfs_symlink - helper function to create symlinks
1310 * @dir: directory inode containing symlink
1311 * @dentry: dentry for symlink
1312 * @symname: symlink data
1313 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001314 * See Also: 9P2000.u RFC for more information
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001315 *
1316 */
1317
1318static int
1319v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1320{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001321 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1322 dentry->d_name.name, symname);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001323
1324 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1325}
1326
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001327/**
1328 * v9fs_vfs_link - create a hardlink
1329 * @old_dentry: dentry for file to link to
1330 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001331 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001332 *
1333 */
1334
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001335static int
1336v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1337 struct dentry *dentry)
1338{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001339 int retval;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001340 char *name;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +05301341 struct p9_fid *oldfid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001342
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001343 P9_DPRINTK(P9_DEBUG_VFS,
1344 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001345 old_dentry->d_name.name);
1346
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001347 oldfid = v9fs_fid_clone(old_dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -05001348 if (IS_ERR(oldfid))
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001349 return PTR_ERR(oldfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001350
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001351 name = __getname();
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001352 if (unlikely(!name)) {
1353 retval = -ENOMEM;
1354 goto clunk_fid;
1355 }
Florin Malita0710d362006-06-25 05:48:31 -07001356
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001357 sprintf(name, "%d\n", oldfid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001358 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001359 __putname(name);
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +05301360 if (!retval) {
1361 v9fs_refresh_inode(oldfid, old_dentry->d_inode);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +05301362 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +05301363 }
Eric Van Hensbergenda977b22007-01-26 00:57:06 -08001364clunk_fid:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001365 p9_client_clunk(oldfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001366 return retval;
1367}
1368
1369/**
1370 * v9fs_vfs_mknod - create a special file
1371 * @dir: inode destination for new link
1372 * @dentry: dentry for file
1373 * @mode: mode for creation
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001374 * @rdev: device associated with special file
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001375 *
1376 */
1377
1378static int
1379v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1380{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001381 int retval;
1382 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001383
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001384 P9_DPRINTK(P9_DEBUG_VFS,
1385 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001386 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1387
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001388 if (!new_valid_dev(rdev))
1389 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001390
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001391 name = __getname();
Eugene Teoc0291a02006-03-25 03:07:27 -08001392 if (!name)
1393 return -ENOMEM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001394 /* build extension */
1395 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001396 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001397 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001398 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001399 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001400 *name = 0;
Venkateswararao Jujjuri75cc5c92010-05-22 12:20:30 -05001401 else if (S_ISSOCK(mode))
1402 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001403 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001404 __putname(name);
1405 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001406 }
1407
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001408 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1409 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001410
1411 return retval;
1412}
1413
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +05301414int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1415{
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +00001416 int umode;
1417 dev_t rdev;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +05301418 loff_t i_size;
1419 struct p9_wstat *st;
1420 struct v9fs_session_info *v9ses;
1421
1422 v9ses = v9fs_inode2v9ses(inode);
1423 st = p9_client_stat(fid);
1424 if (IS_ERR(st))
1425 return PTR_ERR(st);
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +00001426 /*
1427 * Don't update inode if the file type is different
1428 */
1429 umode = p9mode2unixmode(v9ses, st, &rdev);
1430 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1431 goto out;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +05301432
1433 spin_lock(&inode->i_lock);
1434 /*
1435 * We don't want to refresh inode->i_size,
1436 * because we may have cached data
1437 */
1438 i_size = inode->i_size;
1439 v9fs_stat2inode(st, inode, inode->i_sb);
1440 if (v9ses->cache)
1441 inode->i_size = i_size;
1442 spin_unlock(&inode->i_lock);
Aneesh Kumar K.V29a3e862011-07-25 18:06:33 +00001443out:
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +05301444 p9stat_free(st);
1445 kfree(st);
1446 return 0;
1447}
1448
Sripathi Kodi9b6533c2010-03-25 12:41:54 +00001449static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1450 .create = v9fs_vfs_create,
1451 .lookup = v9fs_vfs_lookup,
1452 .symlink = v9fs_vfs_symlink,
Venkateswararao Jujjuri (JV)50cc42f2010-06-09 15:59:31 -07001453 .link = v9fs_vfs_link,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +00001454 .unlink = v9fs_vfs_unlink,
1455 .mkdir = v9fs_vfs_mkdir,
1456 .rmdir = v9fs_vfs_rmdir,
Aneesh Kumar K.V1d76e312010-08-30 17:43:07 +00001457 .mknod = v9fs_vfs_mknod,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +00001458 .rename = v9fs_vfs_rename,
1459 .getattr = v9fs_vfs_getattr,
1460 .setattr = v9fs_vfs_setattr,
1461};
1462
Arjan van de Ven754661f2007-02-12 00:55:38 -08001463static const struct inode_operations v9fs_dir_inode_operations = {
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001464 .create = v9fs_vfs_create,
1465 .lookup = v9fs_vfs_lookup,
1466 .unlink = v9fs_vfs_unlink,
1467 .mkdir = v9fs_vfs_mkdir,
1468 .rmdir = v9fs_vfs_rmdir,
1469 .mknod = v9fs_vfs_mknod,
1470 .rename = v9fs_vfs_rename,
1471 .getattr = v9fs_vfs_getattr,
1472 .setattr = v9fs_vfs_setattr,
1473};
1474
Arjan van de Ven754661f2007-02-12 00:55:38 -08001475static const struct inode_operations v9fs_file_inode_operations = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001476 .getattr = v9fs_vfs_getattr,
1477 .setattr = v9fs_vfs_setattr,
1478};
1479
Arjan van de Ven754661f2007-02-12 00:55:38 -08001480static const struct inode_operations v9fs_symlink_inode_operations = {
Al Viro204f2f02009-12-23 00:35:54 -05001481 .readlink = generic_readlink,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001482 .follow_link = v9fs_vfs_follow_link,
1483 .put_link = v9fs_vfs_put_link,
1484 .getattr = v9fs_vfs_getattr,
1485 .setattr = v9fs_vfs_setattr,
1486};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +00001487