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