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