Commit | Line | Data |
---|---|---|
53c06f4e AK |
1 | /* |
2 | * linux/fs/9p/vfs_inode_dotl.c | |
3 | * | |
4 | * This file contains vfs inode ops for the 9P2000.L protocol. | |
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 version 2 | |
11 | * as published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to: | |
20 | * Free Software Foundation | |
21 | * 51 Franklin Street, Fifth Floor | |
22 | * Boston, MA 02111-1301 USA | |
23 | * | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/fs.h> | |
29 | #include <linux/file.h> | |
30 | #include <linux/pagemap.h> | |
31 | #include <linux/stat.h> | |
32 | #include <linux/string.h> | |
33 | #include <linux/inet.h> | |
34 | #include <linux/namei.h> | |
35 | #include <linux/idr.h> | |
36 | #include <linux/sched.h> | |
37 | #include <linux/slab.h> | |
38 | #include <linux/xattr.h> | |
39 | #include <linux/posix_acl.h> | |
40 | #include <net/9p/9p.h> | |
41 | #include <net/9p/client.h> | |
42 | ||
43 | #include "v9fs.h" | |
44 | #include "v9fs_vfs.h" | |
45 | #include "fid.h" | |
46 | #include "cache.h" | |
47 | #include "xattr.h" | |
48 | #include "acl.h" | |
49 | ||
50 | static int | |
51 | v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode, | |
52 | dev_t rdev); | |
53 | ||
54 | /** | |
55 | * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a | |
56 | * new file system object. This checks the S_ISGID to determine the owning | |
57 | * group of the new file system object. | |
58 | */ | |
59 | ||
60 | static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode) | |
61 | { | |
62 | BUG_ON(dir_inode == NULL); | |
63 | ||
64 | if (dir_inode->i_mode & S_ISGID) { | |
65 | /* set_gid bit is set.*/ | |
66 | return dir_inode->i_gid; | |
67 | } | |
68 | return current_fsgid(); | |
69 | } | |
70 | ||
71 | /** | |
72 | * v9fs_dentry_from_dir_inode - helper function to get the dentry from | |
73 | * dir inode. | |
74 | * | |
75 | */ | |
76 | ||
77 | static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode) | |
78 | { | |
79 | struct dentry *dentry; | |
80 | ||
81 | spin_lock(&inode->i_lock); | |
82 | /* Directory should have only one entry. */ | |
83 | BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry)); | |
84 | dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias); | |
85 | spin_unlock(&inode->i_lock); | |
86 | return dentry; | |
87 | } | |
88 | ||
89 | struct inode * | |
90 | v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid, | |
91 | struct super_block *sb) | |
92 | { | |
93 | struct inode *ret = NULL; | |
94 | int err; | |
95 | struct p9_stat_dotl *st; | |
96 | ||
97 | st = p9_client_getattr_dotl(fid, P9_STATS_BASIC); | |
98 | if (IS_ERR(st)) | |
99 | return ERR_CAST(st); | |
100 | ||
101 | ret = v9fs_get_inode(sb, st->st_mode); | |
102 | if (IS_ERR(ret)) { | |
103 | err = PTR_ERR(ret); | |
104 | goto error; | |
105 | } | |
106 | ||
107 | v9fs_stat2inode_dotl(st, ret); | |
108 | ret->i_ino = v9fs_qid2ino(&st->qid); | |
109 | #ifdef CONFIG_9P_FSCACHE | |
110 | v9fs_vcookie_set_qid(ret, &st->qid); | |
111 | v9fs_cache_inode_get_cookie(ret); | |
112 | #endif | |
113 | err = v9fs_get_acl(ret, fid); | |
114 | if (err) { | |
115 | iput(ret); | |
116 | goto error; | |
117 | } | |
118 | kfree(st); | |
119 | return ret; | |
120 | error: | |
121 | kfree(st); | |
122 | return ERR_PTR(err); | |
123 | } | |
124 | ||
125 | /** | |
126 | * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol. | |
127 | * @dir: directory inode that is being created | |
128 | * @dentry: dentry that is being deleted | |
129 | * @mode: create permissions | |
130 | * @nd: path information | |
131 | * | |
132 | */ | |
133 | ||
134 | static int | |
135 | v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode, | |
136 | struct nameidata *nd) | |
137 | { | |
138 | int err = 0; | |
139 | char *name = NULL; | |
140 | gid_t gid; | |
141 | int flags; | |
142 | mode_t mode; | |
143 | struct v9fs_session_info *v9ses; | |
144 | struct p9_fid *fid = NULL; | |
3cf387d7 | 145 | struct p9_fid *dfid, *ofid, *inode_fid; |
53c06f4e AK |
146 | struct file *filp; |
147 | struct p9_qid qid; | |
148 | struct inode *inode; | |
149 | struct posix_acl *pacl = NULL, *dacl = NULL; | |
150 | ||
151 | v9ses = v9fs_inode2v9ses(dir); | |
152 | if (nd && nd->flags & LOOKUP_OPEN) | |
153 | flags = nd->intent.open.flags - 1; | |
154 | else { | |
155 | /* | |
156 | * create call without LOOKUP_OPEN is due | |
157 | * to mknod of regular files. So use mknod | |
158 | * operation. | |
159 | */ | |
160 | return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0); | |
161 | } | |
162 | ||
163 | name = (char *) dentry->d_name.name; | |
164 | P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x " | |
165 | "mode:0x%x\n", name, flags, omode); | |
166 | ||
167 | dfid = v9fs_fid_lookup(dentry->d_parent); | |
168 | if (IS_ERR(dfid)) { | |
169 | err = PTR_ERR(dfid); | |
170 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | |
171 | return err; | |
172 | } | |
173 | ||
174 | /* clone a fid to use for creation */ | |
175 | ofid = p9_client_walk(dfid, 0, NULL, 1); | |
176 | if (IS_ERR(ofid)) { | |
177 | err = PTR_ERR(ofid); | |
178 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); | |
179 | return err; | |
180 | } | |
181 | ||
182 | gid = v9fs_get_fsgid_for_create(dir); | |
183 | ||
184 | mode = omode; | |
185 | /* Update mode based on ACL value */ | |
186 | err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); | |
187 | if (err) { | |
188 | P9_DPRINTK(P9_DEBUG_VFS, | |
189 | "Failed to get acl values in creat %d\n", err); | |
190 | goto error; | |
191 | } | |
192 | err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid); | |
193 | if (err < 0) { | |
194 | P9_DPRINTK(P9_DEBUG_VFS, | |
195 | "p9_client_open_dotl failed in creat %d\n", | |
196 | err); | |
197 | goto error; | |
198 | } | |
53c06f4e | 199 | |
af7542fc AK |
200 | /* instantiate inode and assign the unopened fid to the dentry */ |
201 | fid = p9_client_walk(dfid, 1, &name, 1); | |
202 | if (IS_ERR(fid)) { | |
203 | err = PTR_ERR(fid); | |
c25a61f5 | 204 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); |
53c06f4e | 205 | fid = NULL; |
af7542fc | 206 | goto error; |
53c06f4e | 207 | } |
af7542fc AK |
208 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); |
209 | if (IS_ERR(inode)) { | |
210 | err = PTR_ERR(inode); | |
211 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err); | |
212 | goto error; | |
213 | } | |
af7542fc AK |
214 | d_instantiate(dentry, inode); |
215 | err = v9fs_fid_add(dentry, fid); | |
216 | if (err < 0) | |
217 | goto error; | |
218 | ||
53c06f4e AK |
219 | /* Now set the ACL based on the default value */ |
220 | v9fs_set_create_acl(dentry, dacl, pacl); | |
3cf387d7 AK |
221 | if (v9ses->cache && !inode->i_private) { |
222 | /* | |
223 | * clone a fid and add it to inode->i_private | |
224 | * we do it during open time instead of | |
225 | * page dirty time via write_begin/page_mkwrite | |
226 | * because we want write after unlink usecase | |
227 | * to work. | |
228 | */ | |
229 | inode_fid = v9fs_writeback_fid(dentry); | |
230 | if (IS_ERR(inode_fid)) { | |
231 | err = PTR_ERR(inode_fid); | |
232 | goto error; | |
233 | } | |
234 | inode->i_private = (void *) inode_fid; | |
235 | } | |
af7542fc AK |
236 | /* Since we are opening a file, assign the open fid to the file */ |
237 | filp = lookup_instantiate_filp(nd, dentry, generic_file_open); | |
238 | if (IS_ERR(filp)) { | |
53c06f4e | 239 | p9_client_clunk(ofid); |
af7542fc AK |
240 | return PTR_ERR(filp); |
241 | } | |
242 | filp->private_data = ofid; | |
46848de0 AK |
243 | #ifdef CONFIG_9P_FSCACHE |
244 | if (v9ses->cache) | |
245 | v9fs_cache_inode_set_cookie(inode, filp); | |
246 | #endif | |
53c06f4e AK |
247 | return 0; |
248 | ||
249 | error: | |
250 | if (ofid) | |
251 | p9_client_clunk(ofid); | |
252 | if (fid) | |
253 | p9_client_clunk(fid); | |
254 | return err; | |
255 | } | |
256 | ||
257 | /** | |
258 | * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory | |
259 | * @dir: inode that is being unlinked | |
260 | * @dentry: dentry that is being unlinked | |
261 | * @mode: mode for new directory | |
262 | * | |
263 | */ | |
264 | ||
265 | static int v9fs_vfs_mkdir_dotl(struct inode *dir, | |
266 | struct dentry *dentry, int omode) | |
267 | { | |
268 | int err; | |
269 | struct v9fs_session_info *v9ses; | |
270 | struct p9_fid *fid = NULL, *dfid = NULL; | |
271 | gid_t gid; | |
272 | char *name; | |
273 | mode_t mode; | |
274 | struct inode *inode; | |
275 | struct p9_qid qid; | |
276 | struct dentry *dir_dentry; | |
277 | struct posix_acl *dacl = NULL, *pacl = NULL; | |
278 | ||
279 | P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name); | |
280 | err = 0; | |
281 | v9ses = v9fs_inode2v9ses(dir); | |
282 | ||
283 | omode |= S_IFDIR; | |
284 | if (dir->i_mode & S_ISGID) | |
285 | omode |= S_ISGID; | |
286 | ||
287 | dir_dentry = v9fs_dentry_from_dir_inode(dir); | |
288 | dfid = v9fs_fid_lookup(dir_dentry); | |
289 | if (IS_ERR(dfid)) { | |
290 | err = PTR_ERR(dfid); | |
291 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | |
292 | dfid = NULL; | |
293 | goto error; | |
294 | } | |
295 | ||
296 | gid = v9fs_get_fsgid_for_create(dir); | |
297 | mode = omode; | |
298 | /* Update mode based on ACL value */ | |
299 | err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); | |
300 | if (err) { | |
301 | P9_DPRINTK(P9_DEBUG_VFS, | |
302 | "Failed to get acl values in mkdir %d\n", err); | |
303 | goto error; | |
304 | } | |
305 | name = (char *) dentry->d_name.name; | |
306 | err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid); | |
307 | if (err < 0) | |
308 | goto error; | |
309 | ||
310 | /* instantiate inode and assign the unopened fid to the dentry */ | |
311 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { | |
312 | fid = p9_client_walk(dfid, 1, &name, 1); | |
313 | if (IS_ERR(fid)) { | |
314 | err = PTR_ERR(fid); | |
315 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", | |
316 | err); | |
317 | fid = NULL; | |
318 | goto error; | |
319 | } | |
320 | ||
321 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | |
322 | if (IS_ERR(inode)) { | |
323 | err = PTR_ERR(inode); | |
324 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", | |
325 | err); | |
326 | goto error; | |
327 | } | |
53c06f4e AK |
328 | d_instantiate(dentry, inode); |
329 | err = v9fs_fid_add(dentry, fid); | |
330 | if (err < 0) | |
331 | goto error; | |
332 | fid = NULL; | |
333 | } else { | |
334 | /* | |
335 | * Not in cached mode. No need to populate | |
336 | * inode with stat. We need to get an inode | |
337 | * so that we can set the acl with dentry | |
338 | */ | |
339 | inode = v9fs_get_inode(dir->i_sb, mode); | |
340 | if (IS_ERR(inode)) { | |
341 | err = PTR_ERR(inode); | |
342 | goto error; | |
343 | } | |
53c06f4e AK |
344 | d_instantiate(dentry, inode); |
345 | } | |
346 | /* Now set the ACL based on the default value */ | |
347 | v9fs_set_create_acl(dentry, dacl, pacl); | |
348 | ||
349 | error: | |
350 | if (fid) | |
351 | p9_client_clunk(fid); | |
352 | return err; | |
353 | } | |
354 | ||
355 | static int | |
356 | v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry, | |
357 | struct kstat *stat) | |
358 | { | |
359 | int err; | |
360 | struct v9fs_session_info *v9ses; | |
361 | struct p9_fid *fid; | |
362 | struct p9_stat_dotl *st; | |
363 | ||
364 | P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry); | |
365 | err = -EPERM; | |
366 | v9ses = v9fs_inode2v9ses(dentry->d_inode); | |
367 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) | |
368 | return simple_getattr(mnt, dentry, stat); | |
369 | ||
370 | fid = v9fs_fid_lookup(dentry); | |
371 | if (IS_ERR(fid)) | |
372 | return PTR_ERR(fid); | |
373 | ||
374 | /* Ask for all the fields in stat structure. Server will return | |
375 | * whatever it supports | |
376 | */ | |
377 | ||
378 | st = p9_client_getattr_dotl(fid, P9_STATS_ALL); | |
379 | if (IS_ERR(st)) | |
380 | return PTR_ERR(st); | |
381 | ||
382 | v9fs_stat2inode_dotl(st, dentry->d_inode); | |
383 | generic_fillattr(dentry->d_inode, stat); | |
384 | /* Change block size to what the server returned */ | |
385 | stat->blksize = st->st_blksize; | |
386 | ||
387 | kfree(st); | |
388 | return 0; | |
389 | } | |
390 | ||
391 | /** | |
392 | * v9fs_vfs_setattr_dotl - set file metadata | |
393 | * @dentry: file whose metadata to set | |
394 | * @iattr: metadata assignment structure | |
395 | * | |
396 | */ | |
397 | ||
398 | int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr) | |
399 | { | |
400 | int retval; | |
401 | struct v9fs_session_info *v9ses; | |
402 | struct p9_fid *fid; | |
403 | struct p9_iattr_dotl p9attr; | |
404 | ||
405 | P9_DPRINTK(P9_DEBUG_VFS, "\n"); | |
406 | ||
407 | retval = inode_change_ok(dentry->d_inode, iattr); | |
408 | if (retval) | |
409 | return retval; | |
410 | ||
411 | p9attr.valid = iattr->ia_valid; | |
412 | p9attr.mode = iattr->ia_mode; | |
413 | p9attr.uid = iattr->ia_uid; | |
414 | p9attr.gid = iattr->ia_gid; | |
415 | p9attr.size = iattr->ia_size; | |
416 | p9attr.atime_sec = iattr->ia_atime.tv_sec; | |
417 | p9attr.atime_nsec = iattr->ia_atime.tv_nsec; | |
418 | p9attr.mtime_sec = iattr->ia_mtime.tv_sec; | |
419 | p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec; | |
420 | ||
421 | retval = -EPERM; | |
422 | v9ses = v9fs_inode2v9ses(dentry->d_inode); | |
423 | fid = v9fs_fid_lookup(dentry); | |
424 | if (IS_ERR(fid)) | |
425 | return PTR_ERR(fid); | |
426 | ||
427 | retval = p9_client_setattr(fid, &p9attr); | |
428 | if (retval < 0) | |
429 | return retval; | |
430 | ||
431 | if ((iattr->ia_valid & ATTR_SIZE) && | |
432 | iattr->ia_size != i_size_read(dentry->d_inode)) { | |
433 | retval = vmtruncate(dentry->d_inode, iattr->ia_size); | |
434 | if (retval) | |
435 | return retval; | |
436 | } | |
437 | ||
438 | setattr_copy(dentry->d_inode, iattr); | |
439 | mark_inode_dirty(dentry->d_inode); | |
440 | if (iattr->ia_valid & ATTR_MODE) { | |
441 | /* We also want to update ACL when we update mode bits */ | |
442 | retval = v9fs_acl_chmod(dentry); | |
443 | if (retval < 0) | |
444 | return retval; | |
445 | } | |
446 | return 0; | |
447 | } | |
448 | ||
449 | /** | |
450 | * v9fs_stat2inode_dotl - populate an inode structure with stat info | |
451 | * @stat: stat structure | |
452 | * @inode: inode to populate | |
453 | * @sb: superblock of filesystem | |
454 | * | |
455 | */ | |
456 | ||
457 | void | |
458 | v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode) | |
459 | { | |
460 | ||
461 | if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) { | |
462 | inode->i_atime.tv_sec = stat->st_atime_sec; | |
463 | inode->i_atime.tv_nsec = stat->st_atime_nsec; | |
464 | inode->i_mtime.tv_sec = stat->st_mtime_sec; | |
465 | inode->i_mtime.tv_nsec = stat->st_mtime_nsec; | |
466 | inode->i_ctime.tv_sec = stat->st_ctime_sec; | |
467 | inode->i_ctime.tv_nsec = stat->st_ctime_nsec; | |
468 | inode->i_uid = stat->st_uid; | |
469 | inode->i_gid = stat->st_gid; | |
470 | inode->i_nlink = stat->st_nlink; | |
471 | inode->i_mode = stat->st_mode; | |
472 | inode->i_rdev = new_decode_dev(stat->st_rdev); | |
473 | ||
474 | if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) | |
475 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | |
476 | ||
477 | i_size_write(inode, stat->st_size); | |
478 | inode->i_blocks = stat->st_blocks; | |
479 | } else { | |
480 | if (stat->st_result_mask & P9_STATS_ATIME) { | |
481 | inode->i_atime.tv_sec = stat->st_atime_sec; | |
482 | inode->i_atime.tv_nsec = stat->st_atime_nsec; | |
483 | } | |
484 | if (stat->st_result_mask & P9_STATS_MTIME) { | |
485 | inode->i_mtime.tv_sec = stat->st_mtime_sec; | |
486 | inode->i_mtime.tv_nsec = stat->st_mtime_nsec; | |
487 | } | |
488 | if (stat->st_result_mask & P9_STATS_CTIME) { | |
489 | inode->i_ctime.tv_sec = stat->st_ctime_sec; | |
490 | inode->i_ctime.tv_nsec = stat->st_ctime_nsec; | |
491 | } | |
492 | if (stat->st_result_mask & P9_STATS_UID) | |
493 | inode->i_uid = stat->st_uid; | |
494 | if (stat->st_result_mask & P9_STATS_GID) | |
495 | inode->i_gid = stat->st_gid; | |
496 | if (stat->st_result_mask & P9_STATS_NLINK) | |
497 | inode->i_nlink = stat->st_nlink; | |
498 | if (stat->st_result_mask & P9_STATS_MODE) { | |
499 | inode->i_mode = stat->st_mode; | |
500 | if ((S_ISBLK(inode->i_mode)) || | |
501 | (S_ISCHR(inode->i_mode))) | |
502 | init_special_inode(inode, inode->i_mode, | |
503 | inode->i_rdev); | |
504 | } | |
505 | if (stat->st_result_mask & P9_STATS_RDEV) | |
506 | inode->i_rdev = new_decode_dev(stat->st_rdev); | |
507 | if (stat->st_result_mask & P9_STATS_SIZE) | |
508 | i_size_write(inode, stat->st_size); | |
509 | if (stat->st_result_mask & P9_STATS_BLOCKS) | |
510 | inode->i_blocks = stat->st_blocks; | |
511 | } | |
512 | if (stat->st_result_mask & P9_STATS_GEN) | |
513 | inode->i_generation = stat->st_gen; | |
514 | ||
515 | /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION | |
516 | * because the inode structure does not have fields for them. | |
517 | */ | |
518 | } | |
519 | ||
520 | static int | |
521 | v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry, | |
522 | const char *symname) | |
523 | { | |
524 | struct v9fs_session_info *v9ses; | |
525 | struct p9_fid *dfid; | |
526 | struct p9_fid *fid = NULL; | |
527 | struct inode *inode; | |
528 | struct p9_qid qid; | |
529 | char *name; | |
530 | int err; | |
531 | gid_t gid; | |
532 | ||
533 | name = (char *) dentry->d_name.name; | |
534 | P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n", | |
535 | dir->i_ino, name, symname); | |
536 | v9ses = v9fs_inode2v9ses(dir); | |
537 | ||
538 | dfid = v9fs_fid_lookup(dentry->d_parent); | |
539 | if (IS_ERR(dfid)) { | |
540 | err = PTR_ERR(dfid); | |
541 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | |
542 | return err; | |
543 | } | |
544 | ||
545 | gid = v9fs_get_fsgid_for_create(dir); | |
546 | ||
547 | /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */ | |
548 | err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid); | |
549 | ||
550 | if (err < 0) { | |
551 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err); | |
552 | goto error; | |
553 | } | |
554 | ||
555 | if (v9ses->cache) { | |
556 | /* Now walk from the parent so we can get an unopened fid. */ | |
557 | fid = p9_client_walk(dfid, 1, &name, 1); | |
558 | if (IS_ERR(fid)) { | |
559 | err = PTR_ERR(fid); | |
560 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", | |
561 | err); | |
562 | fid = NULL; | |
563 | goto error; | |
564 | } | |
565 | ||
566 | /* instantiate inode and assign the unopened fid to dentry */ | |
567 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | |
568 | if (IS_ERR(inode)) { | |
569 | err = PTR_ERR(inode); | |
570 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", | |
571 | err); | |
572 | goto error; | |
573 | } | |
53c06f4e AK |
574 | d_instantiate(dentry, inode); |
575 | err = v9fs_fid_add(dentry, fid); | |
576 | if (err < 0) | |
577 | goto error; | |
578 | fid = NULL; | |
579 | } else { | |
580 | /* Not in cached mode. No need to populate inode with stat */ | |
581 | inode = v9fs_get_inode(dir->i_sb, S_IFLNK); | |
582 | if (IS_ERR(inode)) { | |
583 | err = PTR_ERR(inode); | |
584 | goto error; | |
585 | } | |
53c06f4e AK |
586 | d_instantiate(dentry, inode); |
587 | } | |
588 | ||
589 | error: | |
590 | if (fid) | |
591 | p9_client_clunk(fid); | |
592 | ||
593 | return err; | |
594 | } | |
595 | ||
596 | /** | |
597 | * v9fs_vfs_link_dotl - create a hardlink for dotl | |
598 | * @old_dentry: dentry for file to link to | |
599 | * @dir: inode destination for new link | |
600 | * @dentry: dentry for link | |
601 | * | |
602 | */ | |
603 | ||
604 | static int | |
605 | v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir, | |
606 | struct dentry *dentry) | |
607 | { | |
608 | int err; | |
609 | struct p9_fid *dfid, *oldfid; | |
610 | char *name; | |
611 | struct v9fs_session_info *v9ses; | |
612 | struct dentry *dir_dentry; | |
613 | ||
614 | P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n", | |
615 | dir->i_ino, old_dentry->d_name.name, | |
616 | dentry->d_name.name); | |
617 | ||
618 | v9ses = v9fs_inode2v9ses(dir); | |
619 | dir_dentry = v9fs_dentry_from_dir_inode(dir); | |
620 | dfid = v9fs_fid_lookup(dir_dentry); | |
621 | if (IS_ERR(dfid)) | |
622 | return PTR_ERR(dfid); | |
623 | ||
624 | oldfid = v9fs_fid_lookup(old_dentry); | |
625 | if (IS_ERR(oldfid)) | |
626 | return PTR_ERR(oldfid); | |
627 | ||
628 | name = (char *) dentry->d_name.name; | |
629 | ||
630 | err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name); | |
631 | ||
632 | if (err < 0) { | |
633 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err); | |
634 | return err; | |
635 | } | |
636 | ||
637 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { | |
638 | /* Get the latest stat info from server. */ | |
639 | struct p9_fid *fid; | |
640 | struct p9_stat_dotl *st; | |
641 | ||
642 | fid = v9fs_fid_lookup(old_dentry); | |
643 | if (IS_ERR(fid)) | |
644 | return PTR_ERR(fid); | |
645 | ||
646 | st = p9_client_getattr_dotl(fid, P9_STATS_BASIC); | |
647 | if (IS_ERR(st)) | |
648 | return PTR_ERR(st); | |
649 | ||
650 | v9fs_stat2inode_dotl(st, old_dentry->d_inode); | |
651 | ||
652 | kfree(st); | |
53c06f4e | 653 | } |
20656a49 | 654 | ihold(old_dentry->d_inode); |
53c06f4e AK |
655 | d_instantiate(dentry, old_dentry->d_inode); |
656 | ||
657 | return err; | |
658 | } | |
659 | ||
660 | /** | |
661 | * v9fs_vfs_mknod_dotl - create a special file | |
662 | * @dir: inode destination for new link | |
663 | * @dentry: dentry for file | |
664 | * @mode: mode for creation | |
665 | * @rdev: device associated with special file | |
666 | * | |
667 | */ | |
668 | static int | |
669 | v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode, | |
670 | dev_t rdev) | |
671 | { | |
672 | int err; | |
673 | char *name; | |
674 | mode_t mode; | |
675 | struct v9fs_session_info *v9ses; | |
676 | struct p9_fid *fid = NULL, *dfid = NULL; | |
677 | struct inode *inode; | |
678 | gid_t gid; | |
679 | struct p9_qid qid; | |
680 | struct dentry *dir_dentry; | |
681 | struct posix_acl *dacl = NULL, *pacl = NULL; | |
682 | ||
683 | P9_DPRINTK(P9_DEBUG_VFS, | |
684 | " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino, | |
685 | dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev)); | |
686 | ||
687 | if (!new_valid_dev(rdev)) | |
688 | return -EINVAL; | |
689 | ||
690 | v9ses = v9fs_inode2v9ses(dir); | |
691 | dir_dentry = v9fs_dentry_from_dir_inode(dir); | |
692 | dfid = v9fs_fid_lookup(dir_dentry); | |
693 | if (IS_ERR(dfid)) { | |
694 | err = PTR_ERR(dfid); | |
695 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | |
696 | dfid = NULL; | |
697 | goto error; | |
698 | } | |
699 | ||
700 | gid = v9fs_get_fsgid_for_create(dir); | |
701 | mode = omode; | |
702 | /* Update mode based on ACL value */ | |
703 | err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); | |
704 | if (err) { | |
705 | P9_DPRINTK(P9_DEBUG_VFS, | |
706 | "Failed to get acl values in mknod %d\n", err); | |
707 | goto error; | |
708 | } | |
709 | name = (char *) dentry->d_name.name; | |
710 | ||
711 | err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid); | |
712 | if (err < 0) | |
713 | goto error; | |
714 | ||
715 | /* instantiate inode and assign the unopened fid to the dentry */ | |
716 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { | |
717 | fid = p9_client_walk(dfid, 1, &name, 1); | |
718 | if (IS_ERR(fid)) { | |
719 | err = PTR_ERR(fid); | |
720 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", | |
721 | err); | |
722 | fid = NULL; | |
723 | goto error; | |
724 | } | |
725 | ||
726 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | |
727 | if (IS_ERR(inode)) { | |
728 | err = PTR_ERR(inode); | |
729 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", | |
730 | err); | |
731 | goto error; | |
732 | } | |
53c06f4e AK |
733 | d_instantiate(dentry, inode); |
734 | err = v9fs_fid_add(dentry, fid); | |
735 | if (err < 0) | |
736 | goto error; | |
737 | fid = NULL; | |
738 | } else { | |
739 | /* | |
740 | * Not in cached mode. No need to populate inode with stat. | |
741 | * socket syscall returns a fd, so we need instantiate | |
742 | */ | |
743 | inode = v9fs_get_inode(dir->i_sb, mode); | |
744 | if (IS_ERR(inode)) { | |
745 | err = PTR_ERR(inode); | |
746 | goto error; | |
747 | } | |
53c06f4e AK |
748 | d_instantiate(dentry, inode); |
749 | } | |
750 | /* Now set the ACL based on the default value */ | |
751 | v9fs_set_create_acl(dentry, dacl, pacl); | |
752 | error: | |
753 | if (fid) | |
754 | p9_client_clunk(fid); | |
755 | return err; | |
756 | } | |
757 | ||
53c06f4e AK |
758 | /** |
759 | * v9fs_vfs_follow_link_dotl - follow a symlink path | |
760 | * @dentry: dentry for symlink | |
761 | * @nd: nameidata | |
762 | * | |
763 | */ | |
764 | ||
765 | static void * | |
766 | v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd) | |
767 | { | |
31b6ceac MK |
768 | int retval; |
769 | struct p9_fid *fid; | |
53c06f4e | 770 | char *link = __getname(); |
31b6ceac | 771 | char *target; |
53c06f4e | 772 | |
31b6ceac | 773 | P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name); |
53c06f4e | 774 | |
31b6ceac | 775 | if (!link) { |
53c06f4e | 776 | link = ERR_PTR(-ENOMEM); |
31b6ceac | 777 | goto ndset; |
53c06f4e | 778 | } |
31b6ceac MK |
779 | fid = v9fs_fid_lookup(dentry); |
780 | if (IS_ERR(fid)) { | |
781 | __putname(link); | |
782 | link = ERR_PTR(PTR_ERR(fid)); | |
783 | goto ndset; | |
784 | } | |
785 | retval = p9_client_readlink(fid, &target); | |
786 | if (!retval) { | |
787 | strcpy(link, target); | |
788 | kfree(target); | |
789 | goto ndset; | |
790 | } | |
791 | __putname(link); | |
792 | link = ERR_PTR(retval); | |
793 | ndset: | |
53c06f4e | 794 | nd_set_link(nd, link); |
53c06f4e AK |
795 | return NULL; |
796 | } | |
797 | ||
798 | const struct inode_operations v9fs_dir_inode_operations_dotl = { | |
799 | .create = v9fs_vfs_create_dotl, | |
800 | .lookup = v9fs_vfs_lookup, | |
801 | .link = v9fs_vfs_link_dotl, | |
802 | .symlink = v9fs_vfs_symlink_dotl, | |
803 | .unlink = v9fs_vfs_unlink, | |
804 | .mkdir = v9fs_vfs_mkdir_dotl, | |
805 | .rmdir = v9fs_vfs_rmdir, | |
806 | .mknod = v9fs_vfs_mknod_dotl, | |
807 | .rename = v9fs_vfs_rename, | |
808 | .getattr = v9fs_vfs_getattr_dotl, | |
809 | .setattr = v9fs_vfs_setattr_dotl, | |
810 | .setxattr = generic_setxattr, | |
811 | .getxattr = generic_getxattr, | |
812 | .removexattr = generic_removexattr, | |
813 | .listxattr = v9fs_listxattr, | |
814 | .check_acl = v9fs_check_acl, | |
815 | }; | |
816 | ||
817 | const struct inode_operations v9fs_file_inode_operations_dotl = { | |
818 | .getattr = v9fs_vfs_getattr_dotl, | |
819 | .setattr = v9fs_vfs_setattr_dotl, | |
820 | .setxattr = generic_setxattr, | |
821 | .getxattr = generic_getxattr, | |
822 | .removexattr = generic_removexattr, | |
823 | .listxattr = v9fs_listxattr, | |
824 | .check_acl = v9fs_check_acl, | |
825 | }; | |
826 | ||
827 | const struct inode_operations v9fs_symlink_inode_operations_dotl = { | |
31b6ceac | 828 | .readlink = generic_readlink, |
53c06f4e AK |
829 | .follow_link = v9fs_vfs_follow_link_dotl, |
830 | .put_link = v9fs_vfs_put_link, | |
831 | .getattr = v9fs_vfs_getattr_dotl, | |
832 | .setattr = v9fs_vfs_setattr_dotl, | |
833 | .setxattr = generic_setxattr, | |
834 | .getxattr = generic_getxattr, | |
835 | .removexattr = generic_removexattr, | |
836 | .listxattr = v9fs_listxattr, | |
837 | }; |