]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
274dcf55 MM |
2 | /* |
3 | * (C) 2001 Clemson University and The University of Chicago | |
4 | * | |
5 | * See COPYING in top-level directory. | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Linux VFS namei operations. | |
10 | */ | |
11 | ||
12 | #include "protocol.h" | |
575e9461 | 13 | #include "orangefs-kernel.h" |
274dcf55 MM |
14 | |
15 | /* | |
16 | * Get a newly allocated inode to go with a negative dentry. | |
17 | */ | |
8bb8aefd | 18 | static int orangefs_create(struct inode *dir, |
274dcf55 MM |
19 | struct dentry *dentry, |
20 | umode_t mode, | |
21 | bool exclusive) | |
22 | { | |
8bb8aefd YL |
23 | struct orangefs_inode_s *parent = ORANGEFS_I(dir); |
24 | struct orangefs_kernel_op_s *new_op; | |
db0267e7 | 25 | struct orangefs_object_kref ref; |
274dcf55 | 26 | struct inode *inode; |
a55f2d86 | 27 | struct iattr iattr; |
274dcf55 MM |
28 | int ret; |
29 | ||
f66debf1 | 30 | gossip_debug(GOSSIP_NAME_DEBUG, "%s: %pd\n", |
5253487e | 31 | __func__, |
f66debf1 | 32 | dentry); |
274dcf55 | 33 | |
8bb8aefd | 34 | new_op = op_alloc(ORANGEFS_VFS_OP_CREATE); |
274dcf55 MM |
35 | if (!new_op) |
36 | return -ENOMEM; | |
37 | ||
38 | new_op->upcall.req.create.parent_refn = parent->refn; | |
39 | ||
40 | fill_default_sys_attrs(new_op->upcall.req.create.attributes, | |
8bb8aefd | 41 | ORANGEFS_TYPE_METAFILE, mode); |
274dcf55 MM |
42 | |
43 | strncpy(new_op->upcall.req.create.d_name, | |
6bdfb48d | 44 | dentry->d_name.name, ORANGEFS_NAME_MAX - 1); |
274dcf55 MM |
45 | |
46 | ret = service_operation(new_op, __func__, get_interruptible_flag(dir)); | |
47 | ||
48 | gossip_debug(GOSSIP_NAME_DEBUG, | |
f66debf1 | 49 | "%s: %pd: handle:%pU: fsid:%d: new_op:%p: ret:%d:\n", |
5253487e | 50 | __func__, |
f66debf1 | 51 | dentry, |
274dcf55 | 52 | &new_op->downcall.resp.create.refn.khandle, |
5253487e MM |
53 | new_op->downcall.resp.create.refn.fs_id, |
54 | new_op, | |
55 | ret); | |
274dcf55 | 56 | |
5253487e | 57 | if (ret < 0) |
274dcf55 | 58 | goto out; |
274dcf55 | 59 | |
db0267e7 | 60 | ref = new_op->downcall.resp.create.refn; |
db0267e7 MB |
61 | |
62 | inode = orangefs_new_inode(dir->i_sb, dir, S_IFREG | mode, 0, &ref); | |
274dcf55 | 63 | if (IS_ERR(inode)) { |
f66debf1 | 64 | gossip_err("%s: Failed to allocate inode for file :%pd:\n", |
5253487e | 65 | __func__, |
f66debf1 | 66 | dentry); |
274dcf55 MM |
67 | ret = PTR_ERR(inode); |
68 | goto out; | |
69 | } | |
70 | ||
71 | gossip_debug(GOSSIP_NAME_DEBUG, | |
f66debf1 | 72 | "%s: Assigned inode :%pU: for file :%pd:\n", |
5253487e MM |
73 | __func__, |
74 | get_khandle_from_ino(inode), | |
f66debf1 | 75 | dentry); |
274dcf55 | 76 | |
1e2e547a | 77 | d_instantiate_new(dentry, inode); |
804b1737 | 78 | orangefs_set_timeout(dentry); |
274dcf55 MM |
79 | |
80 | gossip_debug(GOSSIP_NAME_DEBUG, | |
f66debf1 | 81 | "%s: dentry instantiated for %pd\n", |
5253487e | 82 | __func__, |
f66debf1 | 83 | dentry); |
274dcf55 | 84 | |
a55f2d86 | 85 | memset(&iattr, 0, sizeof iattr); |
afd9fb2a MB |
86 | iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME; |
87 | iattr.ia_mtime = iattr.ia_ctime = current_time(dir); | |
88 | __orangefs_setattr(dir, &iattr); | |
274dcf55 MM |
89 | ret = 0; |
90 | out: | |
34e6148a | 91 | op_release(new_op); |
5253487e | 92 | gossip_debug(GOSSIP_NAME_DEBUG, |
f66debf1 | 93 | "%s: %pd: returning %d\n", |
5253487e | 94 | __func__, |
f66debf1 | 95 | dentry, |
5253487e | 96 | ret); |
274dcf55 MM |
97 | return ret; |
98 | } | |
99 | ||
100 | /* | |
101 | * Attempt to resolve an object name (dentry->d_name), parent handle, and | |
102 | * fsid into a handle for the object. | |
103 | */ | |
8bb8aefd | 104 | static struct dentry *orangefs_lookup(struct inode *dir, struct dentry *dentry, |
274dcf55 MM |
105 | unsigned int flags) |
106 | { | |
8bb8aefd YL |
107 | struct orangefs_inode_s *parent = ORANGEFS_I(dir); |
108 | struct orangefs_kernel_op_s *new_op; | |
274dcf55 | 109 | struct inode *inode; |
274dcf55 MM |
110 | int ret = -EINVAL; |
111 | ||
112 | /* | |
113 | * in theory we could skip a lookup here (if the intent is to | |
114 | * create) in order to avoid a potentially failed lookup, but | |
115 | * leaving it in can skip a valid lookup and try to create a file | |
116 | * that already exists (e.g. the vfs already handles checking for | |
117 | * -EEXIST on O_EXCL opens, which is broken if we skip this lookup | |
118 | * in the create path) | |
119 | */ | |
f66debf1 AV |
120 | gossip_debug(GOSSIP_NAME_DEBUG, "%s called on %pd\n", |
121 | __func__, dentry); | |
274dcf55 | 122 | |
47b4948f | 123 | if (dentry->d_name.len > (ORANGEFS_NAME_MAX - 1)) |
274dcf55 MM |
124 | return ERR_PTR(-ENAMETOOLONG); |
125 | ||
8bb8aefd | 126 | new_op = op_alloc(ORANGEFS_VFS_OP_LOOKUP); |
274dcf55 MM |
127 | if (!new_op) |
128 | return ERR_PTR(-ENOMEM); | |
129 | ||
7cec28e9 | 130 | new_op->upcall.req.lookup.sym_follow = ORANGEFS_LOOKUP_LINK_NO_FOLLOW; |
274dcf55 MM |
131 | |
132 | gossip_debug(GOSSIP_NAME_DEBUG, "%s:%s:%d using parent %pU\n", | |
133 | __FILE__, | |
134 | __func__, | |
135 | __LINE__, | |
136 | &parent->refn.khandle); | |
137 | new_op->upcall.req.lookup.parent_refn = parent->refn; | |
138 | ||
139 | strncpy(new_op->upcall.req.lookup.d_name, dentry->d_name.name, | |
6bdfb48d | 140 | ORANGEFS_NAME_MAX - 1); |
274dcf55 MM |
141 | |
142 | gossip_debug(GOSSIP_NAME_DEBUG, | |
6ceaf781 | 143 | "%s: doing lookup on %s under %pU,%d\n", |
274dcf55 MM |
144 | __func__, |
145 | new_op->upcall.req.lookup.d_name, | |
146 | &new_op->upcall.req.lookup.parent_refn.khandle, | |
6ceaf781 | 147 | new_op->upcall.req.lookup.parent_refn.fs_id); |
274dcf55 MM |
148 | |
149 | ret = service_operation(new_op, __func__, get_interruptible_flag(dir)); | |
150 | ||
151 | gossip_debug(GOSSIP_NAME_DEBUG, | |
152 | "Lookup Got %pU, fsid %d (ret=%d)\n", | |
153 | &new_op->downcall.resp.lookup.refn.khandle, | |
154 | new_op->downcall.resp.lookup.refn.fs_id, | |
155 | ret); | |
156 | ||
22fc9db2 | 157 | if (ret == 0) { |
04bb1ba1 AV |
158 | orangefs_set_timeout(dentry); |
159 | inode = orangefs_iget(dir->i_sb, &new_op->downcall.resp.lookup.refn); | |
160 | } else if (ret == -ENOENT) { | |
161 | inode = NULL; | |
162 | } else { | |
274dcf55 | 163 | /* must be a non-recoverable error */ |
04bb1ba1 | 164 | inode = ERR_PTR(ret); |
274dcf55 MM |
165 | } |
166 | ||
274dcf55 | 167 | op_release(new_op); |
04bb1ba1 | 168 | return d_splice_alias(inode, dentry); |
274dcf55 MM |
169 | } |
170 | ||
171 | /* return 0 on success; non-zero otherwise */ | |
8bb8aefd | 172 | static int orangefs_unlink(struct inode *dir, struct dentry *dentry) |
274dcf55 MM |
173 | { |
174 | struct inode *inode = dentry->d_inode; | |
8bb8aefd YL |
175 | struct orangefs_inode_s *parent = ORANGEFS_I(dir); |
176 | struct orangefs_kernel_op_s *new_op; | |
a55f2d86 | 177 | struct iattr iattr; |
274dcf55 MM |
178 | int ret; |
179 | ||
180 | gossip_debug(GOSSIP_NAME_DEBUG, | |
f66debf1 | 181 | "%s: called on %pd\n" |
274dcf55 MM |
182 | " (inode %pU): Parent is %pU | fs_id %d\n", |
183 | __func__, | |
f66debf1 | 184 | dentry, |
274dcf55 MM |
185 | get_khandle_from_ino(inode), |
186 | &parent->refn.khandle, | |
187 | parent->refn.fs_id); | |
188 | ||
8bb8aefd | 189 | new_op = op_alloc(ORANGEFS_VFS_OP_REMOVE); |
274dcf55 MM |
190 | if (!new_op) |
191 | return -ENOMEM; | |
192 | ||
193 | new_op->upcall.req.remove.parent_refn = parent->refn; | |
194 | strncpy(new_op->upcall.req.remove.d_name, dentry->d_name.name, | |
6bdfb48d | 195 | ORANGEFS_NAME_MAX - 1); |
274dcf55 | 196 | |
8bb8aefd | 197 | ret = service_operation(new_op, "orangefs_unlink", |
274dcf55 MM |
198 | get_interruptible_flag(inode)); |
199 | ||
5253487e MM |
200 | gossip_debug(GOSSIP_NAME_DEBUG, |
201 | "%s: service_operation returned:%d:\n", | |
202 | __func__, | |
203 | ret); | |
204 | ||
274dcf55 MM |
205 | op_release(new_op); |
206 | ||
207 | if (!ret) { | |
208 | drop_nlink(inode); | |
209 | ||
a55f2d86 | 210 | memset(&iattr, 0, sizeof iattr); |
afd9fb2a MB |
211 | iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME; |
212 | iattr.ia_mtime = iattr.ia_ctime = current_time(dir); | |
213 | __orangefs_setattr(dir, &iattr); | |
274dcf55 MM |
214 | } |
215 | return ret; | |
216 | } | |
217 | ||
8bb8aefd | 218 | static int orangefs_symlink(struct inode *dir, |
274dcf55 MM |
219 | struct dentry *dentry, |
220 | const char *symname) | |
221 | { | |
8bb8aefd YL |
222 | struct orangefs_inode_s *parent = ORANGEFS_I(dir); |
223 | struct orangefs_kernel_op_s *new_op; | |
db0267e7 | 224 | struct orangefs_object_kref ref; |
274dcf55 | 225 | struct inode *inode; |
a55f2d86 | 226 | struct iattr iattr; |
c42293a9 | 227 | int mode = 0755; |
274dcf55 MM |
228 | int ret; |
229 | ||
230 | gossip_debug(GOSSIP_NAME_DEBUG, "%s: called\n", __func__); | |
231 | ||
232 | if (!symname) | |
233 | return -EINVAL; | |
234 | ||
c62da585 MB |
235 | if (strlen(symname)+1 > ORANGEFS_NAME_MAX) |
236 | return -ENAMETOOLONG; | |
237 | ||
8bb8aefd | 238 | new_op = op_alloc(ORANGEFS_VFS_OP_SYMLINK); |
274dcf55 MM |
239 | if (!new_op) |
240 | return -ENOMEM; | |
241 | ||
242 | new_op->upcall.req.sym.parent_refn = parent->refn; | |
243 | ||
244 | fill_default_sys_attrs(new_op->upcall.req.sym.attributes, | |
8bb8aefd | 245 | ORANGEFS_TYPE_SYMLINK, |
274dcf55 MM |
246 | mode); |
247 | ||
248 | strncpy(new_op->upcall.req.sym.entry_name, | |
249 | dentry->d_name.name, | |
6bdfb48d XW |
250 | ORANGEFS_NAME_MAX - 1); |
251 | strncpy(new_op->upcall.req.sym.target, symname, ORANGEFS_NAME_MAX - 1); | |
274dcf55 MM |
252 | |
253 | ret = service_operation(new_op, __func__, get_interruptible_flag(dir)); | |
254 | ||
255 | gossip_debug(GOSSIP_NAME_DEBUG, | |
8bb8aefd | 256 | "Symlink Got ORANGEFS handle %pU on fsid %d (ret=%d)\n", |
274dcf55 MM |
257 | &new_op->downcall.resp.sym.refn.khandle, |
258 | new_op->downcall.resp.sym.refn.fs_id, ret); | |
259 | ||
260 | if (ret < 0) { | |
261 | gossip_debug(GOSSIP_NAME_DEBUG, | |
262 | "%s: failed with error code %d\n", | |
263 | __func__, ret); | |
264 | goto out; | |
265 | } | |
266 | ||
db0267e7 | 267 | ref = new_op->downcall.resp.sym.refn; |
db0267e7 MB |
268 | |
269 | inode = orangefs_new_inode(dir->i_sb, dir, S_IFLNK | mode, 0, &ref); | |
274dcf55 MM |
270 | if (IS_ERR(inode)) { |
271 | gossip_err | |
8bb8aefd | 272 | ("*** Failed to allocate orangefs symlink inode\n"); |
274dcf55 MM |
273 | ret = PTR_ERR(inode); |
274 | goto out; | |
275 | } | |
f6a4b4c9 MB |
276 | /* |
277 | * This is necessary because orangefs_inode_getattr will not | |
278 | * re-read symlink size as it is impossible for it to change. | |
279 | * Invalidating the cache does not help. orangefs_new_inode | |
280 | * does not set the correct size (it does not know symname). | |
281 | */ | |
282 | inode->i_size = strlen(symname); | |
274dcf55 MM |
283 | |
284 | gossip_debug(GOSSIP_NAME_DEBUG, | |
285 | "Assigned symlink inode new number of %pU\n", | |
286 | get_khandle_from_ino(inode)); | |
287 | ||
1e2e547a | 288 | d_instantiate_new(dentry, inode); |
804b1737 | 289 | orangefs_set_timeout(dentry); |
274dcf55 MM |
290 | |
291 | gossip_debug(GOSSIP_NAME_DEBUG, | |
f66debf1 | 292 | "Inode (Symlink) %pU -> %pd\n", |
274dcf55 | 293 | get_khandle_from_ino(inode), |
f66debf1 | 294 | dentry); |
274dcf55 | 295 | |
a55f2d86 | 296 | memset(&iattr, 0, sizeof iattr); |
afd9fb2a MB |
297 | iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME; |
298 | iattr.ia_mtime = iattr.ia_ctime = current_time(dir); | |
299 | __orangefs_setattr(dir, &iattr); | |
274dcf55 MM |
300 | ret = 0; |
301 | out: | |
34e6148a | 302 | op_release(new_op); |
274dcf55 MM |
303 | return ret; |
304 | } | |
305 | ||
8bb8aefd | 306 | static int orangefs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
274dcf55 | 307 | { |
8bb8aefd YL |
308 | struct orangefs_inode_s *parent = ORANGEFS_I(dir); |
309 | struct orangefs_kernel_op_s *new_op; | |
db0267e7 | 310 | struct orangefs_object_kref ref; |
274dcf55 | 311 | struct inode *inode; |
a55f2d86 | 312 | struct iattr iattr; |
274dcf55 MM |
313 | int ret; |
314 | ||
8bb8aefd | 315 | new_op = op_alloc(ORANGEFS_VFS_OP_MKDIR); |
274dcf55 MM |
316 | if (!new_op) |
317 | return -ENOMEM; | |
318 | ||
319 | new_op->upcall.req.mkdir.parent_refn = parent->refn; | |
320 | ||
321 | fill_default_sys_attrs(new_op->upcall.req.mkdir.attributes, | |
8bb8aefd | 322 | ORANGEFS_TYPE_DIRECTORY, mode); |
274dcf55 MM |
323 | |
324 | strncpy(new_op->upcall.req.mkdir.d_name, | |
6bdfb48d | 325 | dentry->d_name.name, ORANGEFS_NAME_MAX - 1); |
274dcf55 MM |
326 | |
327 | ret = service_operation(new_op, __func__, get_interruptible_flag(dir)); | |
328 | ||
329 | gossip_debug(GOSSIP_NAME_DEBUG, | |
8bb8aefd | 330 | "Mkdir Got ORANGEFS handle %pU on fsid %d\n", |
274dcf55 MM |
331 | &new_op->downcall.resp.mkdir.refn.khandle, |
332 | new_op->downcall.resp.mkdir.refn.fs_id); | |
333 | ||
334 | if (ret < 0) { | |
335 | gossip_debug(GOSSIP_NAME_DEBUG, | |
336 | "%s: failed with error code %d\n", | |
337 | __func__, ret); | |
338 | goto out; | |
339 | } | |
340 | ||
db0267e7 | 341 | ref = new_op->downcall.resp.mkdir.refn; |
db0267e7 MB |
342 | |
343 | inode = orangefs_new_inode(dir->i_sb, dir, S_IFDIR | mode, 0, &ref); | |
274dcf55 | 344 | if (IS_ERR(inode)) { |
8bb8aefd | 345 | gossip_err("*** Failed to allocate orangefs dir inode\n"); |
274dcf55 MM |
346 | ret = PTR_ERR(inode); |
347 | goto out; | |
348 | } | |
349 | ||
350 | gossip_debug(GOSSIP_NAME_DEBUG, | |
351 | "Assigned dir inode new number of %pU\n", | |
352 | get_khandle_from_ino(inode)); | |
353 | ||
1e2e547a | 354 | d_instantiate_new(dentry, inode); |
804b1737 | 355 | orangefs_set_timeout(dentry); |
274dcf55 MM |
356 | |
357 | gossip_debug(GOSSIP_NAME_DEBUG, | |
f66debf1 | 358 | "Inode (Directory) %pU -> %pd\n", |
274dcf55 | 359 | get_khandle_from_ino(inode), |
f66debf1 | 360 | dentry); |
274dcf55 MM |
361 | |
362 | /* | |
363 | * NOTE: we have no good way to keep nlink consistent for directories | |
364 | * across clients; keep constant at 1. | |
365 | */ | |
a55f2d86 | 366 | memset(&iattr, 0, sizeof iattr); |
afd9fb2a MB |
367 | iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME; |
368 | iattr.ia_mtime = iattr.ia_ctime = current_time(dir); | |
369 | __orangefs_setattr(dir, &iattr); | |
274dcf55 | 370 | out: |
34e6148a | 371 | op_release(new_op); |
274dcf55 MM |
372 | return ret; |
373 | } | |
374 | ||
8bb8aefd | 375 | static int orangefs_rename(struct inode *old_dir, |
274dcf55 MM |
376 | struct dentry *old_dentry, |
377 | struct inode *new_dir, | |
1cd66c93 MS |
378 | struct dentry *new_dentry, |
379 | unsigned int flags) | |
274dcf55 | 380 | { |
8bb8aefd | 381 | struct orangefs_kernel_op_s *new_op; |
5e7f1d43 | 382 | struct iattr iattr; |
274dcf55 MM |
383 | int ret; |
384 | ||
1cd66c93 MS |
385 | if (flags) |
386 | return -EINVAL; | |
387 | ||
274dcf55 | 388 | gossip_debug(GOSSIP_NAME_DEBUG, |
96b0cffb AV |
389 | "orangefs_rename: called (%pd2 => %pd2) ct=%d\n", |
390 | old_dentry, new_dentry, d_count(new_dentry)); | |
274dcf55 | 391 | |
5e7f1d43 | 392 | memset(&iattr, 0, sizeof iattr); |
afd9fb2a MB |
393 | iattr.ia_valid |= ATTR_MTIME | ATTR_CTIME; |
394 | iattr.ia_mtime = iattr.ia_ctime = current_time(new_dir); | |
395 | __orangefs_setattr(new_dir, &iattr); | |
71680c18 | 396 | |
8bb8aefd | 397 | new_op = op_alloc(ORANGEFS_VFS_OP_RENAME); |
274dcf55 MM |
398 | if (!new_op) |
399 | return -EINVAL; | |
400 | ||
8bb8aefd YL |
401 | new_op->upcall.req.rename.old_parent_refn = ORANGEFS_I(old_dir)->refn; |
402 | new_op->upcall.req.rename.new_parent_refn = ORANGEFS_I(new_dir)->refn; | |
274dcf55 MM |
403 | |
404 | strncpy(new_op->upcall.req.rename.d_old_name, | |
405 | old_dentry->d_name.name, | |
6bdfb48d | 406 | ORANGEFS_NAME_MAX - 1); |
274dcf55 MM |
407 | strncpy(new_op->upcall.req.rename.d_new_name, |
408 | new_dentry->d_name.name, | |
6bdfb48d | 409 | ORANGEFS_NAME_MAX - 1); |
274dcf55 MM |
410 | |
411 | ret = service_operation(new_op, | |
8bb8aefd | 412 | "orangefs_rename", |
274dcf55 MM |
413 | get_interruptible_flag(old_dentry->d_inode)); |
414 | ||
415 | gossip_debug(GOSSIP_NAME_DEBUG, | |
8bb8aefd | 416 | "orangefs_rename: got downcall status %d\n", |
274dcf55 MM |
417 | ret); |
418 | ||
419 | if (new_dentry->d_inode) | |
c2050a45 | 420 | new_dentry->d_inode->i_ctime = current_time(new_dentry->d_inode); |
274dcf55 MM |
421 | |
422 | op_release(new_op); | |
423 | return ret; | |
424 | } | |
425 | ||
8bb8aefd | 426 | /* ORANGEFS implementation of VFS inode operations for directories */ |
6f3fc107 | 427 | const struct inode_operations orangefs_dir_inode_operations = { |
8bb8aefd YL |
428 | .lookup = orangefs_lookup, |
429 | .get_acl = orangefs_get_acl, | |
430 | .set_acl = orangefs_set_acl, | |
431 | .create = orangefs_create, | |
432 | .unlink = orangefs_unlink, | |
433 | .symlink = orangefs_symlink, | |
434 | .mkdir = orangefs_mkdir, | |
435 | .rmdir = orangefs_unlink, | |
436 | .rename = orangefs_rename, | |
437 | .setattr = orangefs_setattr, | |
438 | .getattr = orangefs_getattr, | |
8bb8aefd | 439 | .listxattr = orangefs_listxattr, |
933287da | 440 | .permission = orangefs_permission, |
a55f2d86 | 441 | .update_time = orangefs_update_time, |
274dcf55 | 442 | }; |