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