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