xref: /linux/fs/9p/vfs_inode.c (revision 27258e448eb301cf89e351df87aa8cb916653bf2)
1 /*
2  *  linux/fs/9p/vfs_inode.c
3  *
4  * This file contains vfs inode ops for the 9P2000 protocol.
5  *
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
10  *  it under the terms of the GNU General Public License version 2
11  *  as published by the Free Software Foundation.
12  *
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>
33 #include <linux/inet.h>
34 #include <linux/namei.h>
35 #include <linux/idr.h>
36 #include <linux/sched.h>
37 #include <net/9p/9p.h>
38 #include <net/9p/client.h>
39 
40 #include "v9fs.h"
41 #include "v9fs_vfs.h"
42 #include "fid.h"
43 
44 static const struct inode_operations v9fs_dir_inode_operations;
45 static const struct inode_operations v9fs_dir_inode_operations_ext;
46 static const struct inode_operations v9fs_file_inode_operations;
47 static const struct inode_operations v9fs_symlink_inode_operations;
48 
49 /**
50  * unixmode2p9mode - convert unix mode bits to plan 9
51  * @v9ses: v9fs session information
52  * @mode: mode to convert
53  *
54  */
55 
56 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
57 {
58 	int res;
59 	res = mode & 0777;
60 	if (S_ISDIR(mode))
61 		res |= P9_DMDIR;
62 	if (v9fs_extended(v9ses)) {
63 		if (S_ISLNK(mode))
64 			res |= P9_DMSYMLINK;
65 		if (v9ses->nodev == 0) {
66 			if (S_ISSOCK(mode))
67 				res |= P9_DMSOCKET;
68 			if (S_ISFIFO(mode))
69 				res |= P9_DMNAMEDPIPE;
70 			if (S_ISBLK(mode))
71 				res |= P9_DMDEVICE;
72 			if (S_ISCHR(mode))
73 				res |= P9_DMDEVICE;
74 		}
75 
76 		if ((mode & S_ISUID) == S_ISUID)
77 			res |= P9_DMSETUID;
78 		if ((mode & S_ISGID) == S_ISGID)
79 			res |= P9_DMSETGID;
80 		if ((mode & S_ISVTX) == S_ISVTX)
81 			res |= P9_DMSETVTX;
82 		if ((mode & P9_DMLINK))
83 			res |= P9_DMLINK;
84 	}
85 
86 	return res;
87 }
88 
89 /**
90  * p9mode2unixmode- convert plan9 mode bits to unix mode bits
91  * @v9ses: v9fs session information
92  * @mode: mode to convert
93  *
94  */
95 
96 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
97 {
98 	int res;
99 
100 	res = mode & 0777;
101 
102 	if ((mode & P9_DMDIR) == P9_DMDIR)
103 		res |= S_IFDIR;
104 	else if ((mode & P9_DMSYMLINK) && (v9fs_extended(v9ses)))
105 		res |= S_IFLNK;
106 	else if ((mode & P9_DMSOCKET) && (v9fs_extended(v9ses))
107 		 && (v9ses->nodev == 0))
108 		res |= S_IFSOCK;
109 	else if ((mode & P9_DMNAMEDPIPE) && (v9fs_extended(v9ses))
110 		 && (v9ses->nodev == 0))
111 		res |= S_IFIFO;
112 	else if ((mode & P9_DMDEVICE) && (v9fs_extended(v9ses))
113 		 && (v9ses->nodev == 0))
114 		res |= S_IFBLK;
115 	else
116 		res |= S_IFREG;
117 
118 	if (v9fs_extended(v9ses)) {
119 		if ((mode & P9_DMSETUID) == P9_DMSETUID)
120 			res |= S_ISUID;
121 
122 		if ((mode & P9_DMSETGID) == P9_DMSETGID)
123 			res |= S_ISGID;
124 
125 		if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
126 			res |= S_ISVTX;
127 	}
128 
129 	return res;
130 }
131 
132 /**
133  * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
134  * @uflags: flags to convert
135  * @extended: if .u extensions are active
136  */
137 
138 int v9fs_uflags2omode(int uflags, int extended)
139 {
140 	int ret;
141 
142 	ret = 0;
143 	switch (uflags&3) {
144 	default:
145 	case O_RDONLY:
146 		ret = P9_OREAD;
147 		break;
148 
149 	case O_WRONLY:
150 		ret = P9_OWRITE;
151 		break;
152 
153 	case O_RDWR:
154 		ret = P9_ORDWR;
155 		break;
156 	}
157 
158 	if (uflags & O_TRUNC)
159 		ret |= P9_OTRUNC;
160 
161 	if (extended) {
162 		if (uflags & O_EXCL)
163 			ret |= P9_OEXCL;
164 
165 		if (uflags & O_APPEND)
166 			ret |= P9_OAPPEND;
167 	}
168 
169 	return ret;
170 }
171 
172 /**
173  * v9fs_blank_wstat - helper function to setup a 9P stat structure
174  * @wstat: structure to initialize
175  *
176  */
177 
178 static void
179 v9fs_blank_wstat(struct p9_wstat *wstat)
180 {
181 	wstat->type = ~0;
182 	wstat->dev = ~0;
183 	wstat->qid.type = ~0;
184 	wstat->qid.version = ~0;
185 	*((long long *)&wstat->qid.path) = ~0;
186 	wstat->mode = ~0;
187 	wstat->atime = ~0;
188 	wstat->mtime = ~0;
189 	wstat->length = ~0;
190 	wstat->name = NULL;
191 	wstat->uid = NULL;
192 	wstat->gid = NULL;
193 	wstat->muid = NULL;
194 	wstat->n_uid = ~0;
195 	wstat->n_gid = ~0;
196 	wstat->n_muid = ~0;
197 	wstat->extension = NULL;
198 }
199 
200 /**
201  * v9fs_get_inode - helper function to setup an inode
202  * @sb: superblock
203  * @mode: mode to setup inode with
204  *
205  */
206 
207 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
208 {
209 	int err;
210 	struct inode *inode;
211 	struct v9fs_session_info *v9ses = sb->s_fs_info;
212 
213 	P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
214 
215 	inode = new_inode(sb);
216 	if (!inode) {
217 		P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
218 		return ERR_PTR(-ENOMEM);
219 	}
220 
221 	inode->i_mode = mode;
222 	inode->i_uid = current_fsuid();
223 	inode->i_gid = current_fsgid();
224 	inode->i_blocks = 0;
225 	inode->i_rdev = 0;
226 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
227 	inode->i_mapping->a_ops = &v9fs_addr_operations;
228 
229 	switch (mode & S_IFMT) {
230 	case S_IFIFO:
231 	case S_IFBLK:
232 	case S_IFCHR:
233 	case S_IFSOCK:
234 		if (!v9fs_extended(v9ses)) {
235 			P9_DPRINTK(P9_DEBUG_ERROR,
236 				   "special files without extended mode\n");
237 			err = -EINVAL;
238 			goto error;
239 		}
240 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
241 		break;
242 	case S_IFREG:
243 		inode->i_op = &v9fs_file_inode_operations;
244 		inode->i_fop = &v9fs_file_operations;
245 		break;
246 	case S_IFLNK:
247 		if (!v9fs_extended(v9ses)) {
248 			P9_DPRINTK(P9_DEBUG_ERROR,
249 				   "extended modes used w/o 9P2000.u\n");
250 			err = -EINVAL;
251 			goto error;
252 		}
253 		inode->i_op = &v9fs_symlink_inode_operations;
254 		break;
255 	case S_IFDIR:
256 		inc_nlink(inode);
257 		if (v9fs_extended(v9ses))
258 			inode->i_op = &v9fs_dir_inode_operations_ext;
259 		else
260 			inode->i_op = &v9fs_dir_inode_operations;
261 		inode->i_fop = &v9fs_dir_operations;
262 		break;
263 	default:
264 		P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
265 			   mode, mode & S_IFMT);
266 		err = -EINVAL;
267 		goto error;
268 	}
269 
270 	return inode;
271 
272 error:
273 	iput(inode);
274 	return ERR_PTR(err);
275 }
276 
277 /*
278 static struct v9fs_fid*
279 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
280 {
281 	int err;
282 	int nfid;
283 	struct v9fs_fid *ret;
284 	struct v9fs_fcall *fcall;
285 
286 	nfid = v9fs_get_idpool(&v9ses->fidpool);
287 	if (nfid < 0) {
288 		eprintk(KERN_WARNING, "no free fids available\n");
289 		return ERR_PTR(-ENOSPC);
290 	}
291 
292 	err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
293 		&fcall);
294 
295 	if (err < 0) {
296 		if (fcall && fcall->id == RWALK)
297 			goto clunk_fid;
298 
299 		PRINT_FCALL_ERROR("walk error", fcall);
300 		v9fs_put_idpool(nfid, &v9ses->fidpool);
301 		goto error;
302 	}
303 
304 	kfree(fcall);
305 	fcall = NULL;
306 	ret = v9fs_fid_create(v9ses, nfid);
307 	if (!ret) {
308 		err = -ENOMEM;
309 		goto clunk_fid;
310 	}
311 
312 	err = v9fs_fid_insert(ret, dentry);
313 	if (err < 0) {
314 		v9fs_fid_destroy(ret);
315 		goto clunk_fid;
316 	}
317 
318 	return ret;
319 
320 clunk_fid:
321 	v9fs_t_clunk(v9ses, nfid);
322 
323 error:
324 	kfree(fcall);
325 	return ERR_PTR(err);
326 }
327 */
328 
329 /**
330  * v9fs_inode_from_fid - populate an inode by issuing a attribute request
331  * @v9ses: session information
332  * @fid: fid to issue attribute request for
333  * @sb: superblock on which to create inode
334  *
335  */
336 
337 static struct inode *
338 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
339 	struct super_block *sb)
340 {
341 	int err, umode;
342 	struct inode *ret;
343 	struct p9_wstat *st;
344 
345 	ret = NULL;
346 	st = p9_client_stat(fid);
347 	if (IS_ERR(st))
348 		return ERR_CAST(st);
349 
350 	umode = p9mode2unixmode(v9ses, st->mode);
351 	ret = v9fs_get_inode(sb, umode);
352 	if (IS_ERR(ret)) {
353 		err = PTR_ERR(ret);
354 		goto error;
355 	}
356 
357 	v9fs_stat2inode(st, ret, sb);
358 	ret->i_ino = v9fs_qid2ino(&st->qid);
359 	p9stat_free(st);
360 	kfree(st);
361 	return ret;
362 
363 error:
364 	p9stat_free(st);
365 	kfree(st);
366 	return ERR_PTR(err);
367 }
368 
369 /**
370  * v9fs_remove - helper function to remove files and directories
371  * @dir: directory inode that is being deleted
372  * @file:  dentry that is being deleted
373  * @rmdir: removing a directory
374  *
375  */
376 
377 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
378 {
379 	struct inode *file_inode;
380 	struct v9fs_session_info *v9ses;
381 	struct p9_fid *v9fid;
382 
383 	P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
384 		rmdir);
385 
386 	file_inode = file->d_inode;
387 	v9ses = v9fs_inode2v9ses(file_inode);
388 	v9fid = v9fs_fid_clone(file);
389 	if (IS_ERR(v9fid))
390 		return PTR_ERR(v9fid);
391 
392 	return p9_client_remove(v9fid);
393 }
394 
395 static int
396 v9fs_open_created(struct inode *inode, struct file *file)
397 {
398 	return 0;
399 }
400 
401 
402 /**
403  * v9fs_create - Create a file
404  * @v9ses: session information
405  * @dir: directory that dentry is being created in
406  * @dentry:  dentry that is being created
407  * @extension: 9p2000.u extension string to support devices, etc.
408  * @perm: create permissions
409  * @mode: open mode
410  *
411  */
412 static struct p9_fid *
413 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
414 		struct dentry *dentry, char *extension, u32 perm, u8 mode)
415 {
416 	int err;
417 	char *name;
418 	struct p9_fid *dfid, *ofid, *fid;
419 	struct inode *inode;
420 
421 	P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
422 
423 	err = 0;
424 	ofid = NULL;
425 	fid = NULL;
426 	name = (char *) dentry->d_name.name;
427 	dfid = v9fs_fid_clone(dentry->d_parent);
428 	if (IS_ERR(dfid)) {
429 		err = PTR_ERR(dfid);
430 		P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
431 		dfid = NULL;
432 		goto error;
433 	}
434 
435 	/* clone a fid to use for creation */
436 	ofid = p9_client_walk(dfid, 0, NULL, 1);
437 	if (IS_ERR(ofid)) {
438 		err = PTR_ERR(ofid);
439 		P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
440 		ofid = NULL;
441 		goto error;
442 	}
443 
444 	err = p9_client_fcreate(ofid, name, perm, mode, extension);
445 	if (err < 0) {
446 		P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
447 		goto error;
448 	}
449 
450 	/* now walk from the parent so we can get unopened fid */
451 	fid = p9_client_walk(dfid, 1, &name, 0);
452 	if (IS_ERR(fid)) {
453 		err = PTR_ERR(fid);
454 		P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
455 		fid = NULL;
456 		goto error;
457 	} else
458 		dfid = NULL;
459 
460 	/* instantiate inode and assign the unopened fid to the dentry */
461 	inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
462 	if (IS_ERR(inode)) {
463 		err = PTR_ERR(inode);
464 		P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
465 		goto error;
466 	}
467 
468 	if (v9ses->cache)
469 		dentry->d_op = &v9fs_cached_dentry_operations;
470 	else
471 		dentry->d_op = &v9fs_dentry_operations;
472 
473 	d_instantiate(dentry, inode);
474 	err = v9fs_fid_add(dentry, fid);
475 	if (err < 0)
476 		goto error;
477 
478 	return ofid;
479 
480 error:
481 	if (dfid)
482 		p9_client_clunk(dfid);
483 
484 	if (ofid)
485 		p9_client_clunk(ofid);
486 
487 	if (fid)
488 		p9_client_clunk(fid);
489 
490 	return ERR_PTR(err);
491 }
492 
493 /**
494  * v9fs_vfs_create - VFS hook to create files
495  * @dir: directory inode that is being created
496  * @dentry:  dentry that is being deleted
497  * @mode: create permissions
498  * @nd: path information
499  *
500  */
501 
502 static int
503 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
504 		struct nameidata *nd)
505 {
506 	int err;
507 	u32 perm;
508 	int flags;
509 	struct v9fs_session_info *v9ses;
510 	struct p9_fid *fid;
511 	struct file *filp;
512 
513 	err = 0;
514 	fid = NULL;
515 	v9ses = v9fs_inode2v9ses(dir);
516 	perm = unixmode2p9mode(v9ses, mode);
517 	if (nd && nd->flags & LOOKUP_OPEN)
518 		flags = nd->intent.open.flags - 1;
519 	else
520 		flags = O_RDWR;
521 
522 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
523 				v9fs_uflags2omode(flags, v9fs_extended(v9ses)));
524 	if (IS_ERR(fid)) {
525 		err = PTR_ERR(fid);
526 		fid = NULL;
527 		goto error;
528 	}
529 
530 	/* if we are opening a file, assign the open fid to the file */
531 	if (nd && nd->flags & LOOKUP_OPEN) {
532 		filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
533 		if (IS_ERR(filp)) {
534 			err = PTR_ERR(filp);
535 			goto error;
536 		}
537 
538 		filp->private_data = fid;
539 	} else
540 		p9_client_clunk(fid);
541 
542 	return 0;
543 
544 error:
545 	if (fid)
546 		p9_client_clunk(fid);
547 
548 	return err;
549 }
550 
551 /**
552  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
553  * @dir:  inode that is being unlinked
554  * @dentry: dentry that is being unlinked
555  * @mode: mode for new directory
556  *
557  */
558 
559 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
560 {
561 	int err;
562 	u32 perm;
563 	struct v9fs_session_info *v9ses;
564 	struct p9_fid *fid;
565 
566 	P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
567 	err = 0;
568 	v9ses = v9fs_inode2v9ses(dir);
569 	perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
570 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
571 	if (IS_ERR(fid)) {
572 		err = PTR_ERR(fid);
573 		fid = NULL;
574 	}
575 
576 	if (fid)
577 		p9_client_clunk(fid);
578 
579 	return err;
580 }
581 
582 /**
583  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
584  * @dir:  inode that is being walked from
585  * @dentry: dentry that is being walked to?
586  * @nameidata: path data
587  *
588  */
589 
590 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
591 				      struct nameidata *nameidata)
592 {
593 	struct super_block *sb;
594 	struct v9fs_session_info *v9ses;
595 	struct p9_fid *dfid, *fid;
596 	struct inode *inode;
597 	char *name;
598 	int result = 0;
599 
600 	P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
601 		dir, dentry->d_name.name, dentry, nameidata);
602 
603 	sb = dir->i_sb;
604 	v9ses = v9fs_inode2v9ses(dir);
605 	dfid = v9fs_fid_lookup(dentry->d_parent);
606 	if (IS_ERR(dfid))
607 		return ERR_CAST(dfid);
608 
609 	name = (char *) dentry->d_name.name;
610 	fid = p9_client_walk(dfid, 1, &name, 1);
611 	if (IS_ERR(fid)) {
612 		result = PTR_ERR(fid);
613 		if (result == -ENOENT) {
614 			d_add(dentry, NULL);
615 			return NULL;
616 		}
617 
618 		return ERR_PTR(result);
619 	}
620 
621 	inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
622 	if (IS_ERR(inode)) {
623 		result = PTR_ERR(inode);
624 		inode = NULL;
625 		goto error;
626 	}
627 
628 	result = v9fs_fid_add(dentry, fid);
629 	if (result < 0)
630 		goto error;
631 
632 	if ((fid->qid.version) && (v9ses->cache))
633 		dentry->d_op = &v9fs_cached_dentry_operations;
634 	else
635 		dentry->d_op = &v9fs_dentry_operations;
636 
637 	d_add(dentry, inode);
638 	return NULL;
639 
640 error:
641 	p9_client_clunk(fid);
642 
643 	return ERR_PTR(result);
644 }
645 
646 /**
647  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
648  * @i:  inode that is being unlinked
649  * @d: dentry that is being unlinked
650  *
651  */
652 
653 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
654 {
655 	return v9fs_remove(i, d, 0);
656 }
657 
658 /**
659  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
660  * @i:  inode that is being unlinked
661  * @d: dentry that is being unlinked
662  *
663  */
664 
665 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
666 {
667 	return v9fs_remove(i, d, 1);
668 }
669 
670 /**
671  * v9fs_vfs_rename - VFS hook to rename an inode
672  * @old_dir:  old dir inode
673  * @old_dentry: old dentry
674  * @new_dir: new dir inode
675  * @new_dentry: new dentry
676  *
677  */
678 
679 static int
680 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
681 		struct inode *new_dir, struct dentry *new_dentry)
682 {
683 	struct inode *old_inode;
684 	struct v9fs_session_info *v9ses;
685 	struct p9_fid *oldfid;
686 	struct p9_fid *olddirfid;
687 	struct p9_fid *newdirfid;
688 	struct p9_wstat wstat;
689 	int retval;
690 
691 	P9_DPRINTK(P9_DEBUG_VFS, "\n");
692 	retval = 0;
693 	old_inode = old_dentry->d_inode;
694 	v9ses = v9fs_inode2v9ses(old_inode);
695 	oldfid = v9fs_fid_lookup(old_dentry);
696 	if (IS_ERR(oldfid))
697 		return PTR_ERR(oldfid);
698 
699 	olddirfid = v9fs_fid_clone(old_dentry->d_parent);
700 	if (IS_ERR(olddirfid)) {
701 		retval = PTR_ERR(olddirfid);
702 		goto done;
703 	}
704 
705 	newdirfid = v9fs_fid_clone(new_dentry->d_parent);
706 	if (IS_ERR(newdirfid)) {
707 		retval = PTR_ERR(newdirfid);
708 		goto clunk_olddir;
709 	}
710 
711 	/* 9P can only handle file rename in the same directory */
712 	if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
713 		P9_DPRINTK(P9_DEBUG_ERROR,
714 				"old dir and new dir are different\n");
715 		retval = -EXDEV;
716 		goto clunk_newdir;
717 	}
718 
719 	v9fs_blank_wstat(&wstat);
720 	wstat.muid = v9ses->uname;
721 	wstat.name = (char *) new_dentry->d_name.name;
722 	retval = p9_client_wstat(oldfid, &wstat);
723 
724 clunk_newdir:
725 	p9_client_clunk(newdirfid);
726 
727 clunk_olddir:
728 	p9_client_clunk(olddirfid);
729 
730 done:
731 	return retval;
732 }
733 
734 /**
735  * v9fs_vfs_getattr - retrieve file metadata
736  * @mnt: mount information
737  * @dentry: file to get attributes on
738  * @stat: metadata structure to populate
739  *
740  */
741 
742 static int
743 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
744 		 struct kstat *stat)
745 {
746 	int err;
747 	struct v9fs_session_info *v9ses;
748 	struct p9_fid *fid;
749 	struct p9_wstat *st;
750 
751 	P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
752 	err = -EPERM;
753 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
754 	if (v9ses->cache == CACHE_LOOSE)
755 		return simple_getattr(mnt, dentry, stat);
756 
757 	fid = v9fs_fid_lookup(dentry);
758 	if (IS_ERR(fid))
759 		return PTR_ERR(fid);
760 
761 	st = p9_client_stat(fid);
762 	if (IS_ERR(st))
763 		return PTR_ERR(st);
764 
765 	v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
766 		generic_fillattr(dentry->d_inode, stat);
767 
768 	kfree(st);
769 	return 0;
770 }
771 
772 /**
773  * v9fs_vfs_setattr - set file metadata
774  * @dentry: file whose metadata to set
775  * @iattr: metadata assignment structure
776  *
777  */
778 
779 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
780 {
781 	int retval;
782 	struct v9fs_session_info *v9ses;
783 	struct p9_fid *fid;
784 	struct p9_wstat wstat;
785 
786 	P9_DPRINTK(P9_DEBUG_VFS, "\n");
787 	retval = -EPERM;
788 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
789 	fid = v9fs_fid_lookup(dentry);
790 	if(IS_ERR(fid))
791 		return PTR_ERR(fid);
792 
793 	v9fs_blank_wstat(&wstat);
794 	if (iattr->ia_valid & ATTR_MODE)
795 		wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
796 
797 	if (iattr->ia_valid & ATTR_MTIME)
798 		wstat.mtime = iattr->ia_mtime.tv_sec;
799 
800 	if (iattr->ia_valid & ATTR_ATIME)
801 		wstat.atime = iattr->ia_atime.tv_sec;
802 
803 	if (iattr->ia_valid & ATTR_SIZE)
804 		wstat.length = iattr->ia_size;
805 
806 	if (v9fs_extended(v9ses)) {
807 		if (iattr->ia_valid & ATTR_UID)
808 			wstat.n_uid = iattr->ia_uid;
809 
810 		if (iattr->ia_valid & ATTR_GID)
811 			wstat.n_gid = iattr->ia_gid;
812 	}
813 
814 	retval = p9_client_wstat(fid, &wstat);
815 	if (retval >= 0)
816 		retval = inode_setattr(dentry->d_inode, iattr);
817 
818 	return retval;
819 }
820 
821 /**
822  * v9fs_stat2inode - populate an inode structure with mistat info
823  * @stat: Plan 9 metadata (mistat) structure
824  * @inode: inode to populate
825  * @sb: superblock of filesystem
826  *
827  */
828 
829 void
830 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
831 	struct super_block *sb)
832 {
833 	char ext[32];
834 	struct v9fs_session_info *v9ses = sb->s_fs_info;
835 
836 	inode->i_nlink = 1;
837 
838 	inode->i_atime.tv_sec = stat->atime;
839 	inode->i_mtime.tv_sec = stat->mtime;
840 	inode->i_ctime.tv_sec = stat->mtime;
841 
842 	inode->i_uid = v9ses->dfltuid;
843 	inode->i_gid = v9ses->dfltgid;
844 
845 	if (v9fs_extended(v9ses)) {
846 		inode->i_uid = stat->n_uid;
847 		inode->i_gid = stat->n_gid;
848 	}
849 
850 	inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
851 	if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
852 		char type = 0;
853 		int major = -1;
854 		int minor = -1;
855 
856 		strncpy(ext, stat->extension, sizeof(ext));
857 		sscanf(ext, "%c %u %u", &type, &major, &minor);
858 		switch (type) {
859 		case 'c':
860 			inode->i_mode &= ~S_IFBLK;
861 			inode->i_mode |= S_IFCHR;
862 			break;
863 		case 'b':
864 			break;
865 		default:
866 			P9_DPRINTK(P9_DEBUG_ERROR,
867 				"Unknown special type %c %s\n", type,
868 				stat->extension);
869 		};
870 		inode->i_rdev = MKDEV(major, minor);
871 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
872 	} else
873 		inode->i_rdev = 0;
874 
875 	inode->i_size = stat->length;
876 
877 	/* not real number of blocks, but 512 byte ones ... */
878 	inode->i_blocks = (inode->i_size + 512 - 1) >> 9;
879 }
880 
881 /**
882  * v9fs_qid2ino - convert qid into inode number
883  * @qid: qid to hash
884  *
885  * BUG: potential for inode number collisions?
886  */
887 
888 ino_t v9fs_qid2ino(struct p9_qid *qid)
889 {
890 	u64 path = qid->path + 2;
891 	ino_t i = 0;
892 
893 	if (sizeof(ino_t) == sizeof(path))
894 		memcpy(&i, &path, sizeof(ino_t));
895 	else
896 		i = (ino_t) (path ^ (path >> 32));
897 
898 	return i;
899 }
900 
901 /**
902  * v9fs_readlink - read a symlink's location (internal version)
903  * @dentry: dentry for symlink
904  * @buffer: buffer to load symlink location into
905  * @buflen: length of buffer
906  *
907  */
908 
909 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
910 {
911 	int retval;
912 
913 	struct v9fs_session_info *v9ses;
914 	struct p9_fid *fid;
915 	struct p9_wstat *st;
916 
917 	P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
918 	retval = -EPERM;
919 	v9ses = v9fs_inode2v9ses(dentry->d_inode);
920 	fid = v9fs_fid_lookup(dentry);
921 	if (IS_ERR(fid))
922 		return PTR_ERR(fid);
923 
924 	if (!v9fs_extended(v9ses))
925 		return -EBADF;
926 
927 	st = p9_client_stat(fid);
928 	if (IS_ERR(st))
929 		return PTR_ERR(st);
930 
931 	if (!(st->mode & P9_DMSYMLINK)) {
932 		retval = -EINVAL;
933 		goto done;
934 	}
935 
936 	/* copy extension buffer into buffer */
937 	strncpy(buffer, st->extension, buflen);
938 
939 	P9_DPRINTK(P9_DEBUG_VFS,
940 		"%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
941 
942 	retval = buflen;
943 
944 done:
945 	kfree(st);
946 	return retval;
947 }
948 
949 /**
950  * v9fs_vfs_readlink - read a symlink's location
951  * @dentry: dentry for symlink
952  * @buffer: buffer to load symlink location into
953  * @buflen: length of buffer
954  *
955  */
956 
957 static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
958 			     int buflen)
959 {
960 	int retval;
961 	int ret;
962 	char *link = __getname();
963 
964 	if (unlikely(!link))
965 		return -ENOMEM;
966 
967 	if (buflen > PATH_MAX)
968 		buflen = PATH_MAX;
969 
970 	P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_name.name,
971 									dentry);
972 
973 	retval = v9fs_readlink(dentry, link, buflen);
974 
975 	if (retval > 0) {
976 		if ((ret = copy_to_user(buffer, link, retval)) != 0) {
977 			P9_DPRINTK(P9_DEBUG_ERROR,
978 					"problem copying to user: %d\n", ret);
979 			retval = ret;
980 		}
981 	}
982 
983 	__putname(link);
984 	return retval;
985 }
986 
987 /**
988  * v9fs_vfs_follow_link - follow a symlink path
989  * @dentry: dentry for symlink
990  * @nd: nameidata
991  *
992  */
993 
994 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
995 {
996 	int len = 0;
997 	char *link = __getname();
998 
999 	P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1000 
1001 	if (!link)
1002 		link = ERR_PTR(-ENOMEM);
1003 	else {
1004 		len = v9fs_readlink(dentry, link, PATH_MAX);
1005 
1006 		if (len < 0) {
1007 			__putname(link);
1008 			link = ERR_PTR(len);
1009 		} else
1010 			link[len] = 0;
1011 	}
1012 	nd_set_link(nd, link);
1013 
1014 	return NULL;
1015 }
1016 
1017 /**
1018  * v9fs_vfs_put_link - release a symlink path
1019  * @dentry: dentry for symlink
1020  * @nd: nameidata
1021  * @p: unused
1022  *
1023  */
1024 
1025 static void
1026 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1027 {
1028 	char *s = nd_get_link(nd);
1029 
1030 	P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1031 		IS_ERR(s) ? "<error>" : s);
1032 	if (!IS_ERR(s))
1033 		__putname(s);
1034 }
1035 
1036 /**
1037  * v9fs_vfs_mkspecial - create a special file
1038  * @dir: inode to create special file in
1039  * @dentry: dentry to create
1040  * @mode: mode to create special file
1041  * @extension: 9p2000.u format extension string representing special file
1042  *
1043  */
1044 
1045 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1046 	int mode, const char *extension)
1047 {
1048 	u32 perm;
1049 	struct v9fs_session_info *v9ses;
1050 	struct p9_fid *fid;
1051 
1052 	v9ses = v9fs_inode2v9ses(dir);
1053 	if (!v9fs_extended(v9ses)) {
1054 		P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1055 		return -EPERM;
1056 	}
1057 
1058 	perm = unixmode2p9mode(v9ses, mode);
1059 	fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1060 								P9_OREAD);
1061 	if (IS_ERR(fid))
1062 		return PTR_ERR(fid);
1063 
1064 	p9_client_clunk(fid);
1065 	return 0;
1066 }
1067 
1068 /**
1069  * v9fs_vfs_symlink - helper function to create symlinks
1070  * @dir: directory inode containing symlink
1071  * @dentry: dentry for symlink
1072  * @symname: symlink data
1073  *
1074  * See Also: 9P2000.u RFC for more information
1075  *
1076  */
1077 
1078 static int
1079 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1080 {
1081 	P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1082 					dentry->d_name.name, symname);
1083 
1084 	return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1085 }
1086 
1087 /**
1088  * v9fs_vfs_link - create a hardlink
1089  * @old_dentry: dentry for file to link to
1090  * @dir: inode destination for new link
1091  * @dentry: dentry for link
1092  *
1093  */
1094 
1095 static int
1096 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1097 	      struct dentry *dentry)
1098 {
1099 	int retval;
1100 	struct p9_fid *oldfid;
1101 	char *name;
1102 
1103 	P9_DPRINTK(P9_DEBUG_VFS,
1104 		" %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1105 		old_dentry->d_name.name);
1106 
1107 	oldfid = v9fs_fid_clone(old_dentry);
1108 	if (IS_ERR(oldfid))
1109 		return PTR_ERR(oldfid);
1110 
1111 	name = __getname();
1112 	if (unlikely(!name)) {
1113 		retval = -ENOMEM;
1114 		goto clunk_fid;
1115 	}
1116 
1117 	sprintf(name, "%d\n", oldfid->fid);
1118 	retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1119 	__putname(name);
1120 
1121 clunk_fid:
1122 	p9_client_clunk(oldfid);
1123 	return retval;
1124 }
1125 
1126 /**
1127  * v9fs_vfs_mknod - create a special file
1128  * @dir: inode destination for new link
1129  * @dentry: dentry for file
1130  * @mode: mode for creation
1131  * @rdev: device associated with special file
1132  *
1133  */
1134 
1135 static int
1136 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1137 {
1138 	int retval;
1139 	char *name;
1140 
1141 	P9_DPRINTK(P9_DEBUG_VFS,
1142 		" %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1143 		dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1144 
1145 	if (!new_valid_dev(rdev))
1146 		return -EINVAL;
1147 
1148 	name = __getname();
1149 	if (!name)
1150 		return -ENOMEM;
1151 	/* build extension */
1152 	if (S_ISBLK(mode))
1153 		sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1154 	else if (S_ISCHR(mode))
1155 		sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1156 	else if (S_ISFIFO(mode))
1157 		*name = 0;
1158 	else {
1159 		__putname(name);
1160 		return -EINVAL;
1161 	}
1162 
1163 	retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1164 	__putname(name);
1165 
1166 	return retval;
1167 }
1168 
1169 static const struct inode_operations v9fs_dir_inode_operations_ext = {
1170 	.create = v9fs_vfs_create,
1171 	.lookup = v9fs_vfs_lookup,
1172 	.symlink = v9fs_vfs_symlink,
1173 	.link = v9fs_vfs_link,
1174 	.unlink = v9fs_vfs_unlink,
1175 	.mkdir = v9fs_vfs_mkdir,
1176 	.rmdir = v9fs_vfs_rmdir,
1177 	.mknod = v9fs_vfs_mknod,
1178 	.rename = v9fs_vfs_rename,
1179 	.readlink = v9fs_vfs_readlink,
1180 	.getattr = v9fs_vfs_getattr,
1181 	.setattr = v9fs_vfs_setattr,
1182 };
1183 
1184 static const struct inode_operations v9fs_dir_inode_operations = {
1185 	.create = v9fs_vfs_create,
1186 	.lookup = v9fs_vfs_lookup,
1187 	.unlink = v9fs_vfs_unlink,
1188 	.mkdir = v9fs_vfs_mkdir,
1189 	.rmdir = v9fs_vfs_rmdir,
1190 	.mknod = v9fs_vfs_mknod,
1191 	.rename = v9fs_vfs_rename,
1192 	.getattr = v9fs_vfs_getattr,
1193 	.setattr = v9fs_vfs_setattr,
1194 };
1195 
1196 static const struct inode_operations v9fs_file_inode_operations = {
1197 	.getattr = v9fs_vfs_getattr,
1198 	.setattr = v9fs_vfs_setattr,
1199 };
1200 
1201 static const struct inode_operations v9fs_symlink_inode_operations = {
1202 	.readlink = v9fs_vfs_readlink,
1203 	.follow_link = v9fs_vfs_follow_link,
1204 	.put_link = v9fs_vfs_put_link,
1205 	.getattr = v9fs_vfs_getattr,
1206 	.setattr = v9fs_vfs_setattr,
1207 };
1208