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