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