1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2001 Clemson University and The University of Chicago
5 * See COPYING in top-level directory.
9 * Linux VFS namei operations.
13 #include "orangefs-kernel.h"
16 * Get a newly allocated inode to go with a negative dentry.
18 static int orangefs_create(struct user_namespace
*mnt_userns
,
20 struct dentry
*dentry
,
24 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
25 struct orangefs_kernel_op_s
*new_op
;
26 struct orangefs_object_kref ref
;
31 gossip_debug(GOSSIP_NAME_DEBUG
, "%s: %pd\n",
35 new_op
= op_alloc(ORANGEFS_VFS_OP_CREATE
);
39 new_op
->upcall
.req
.create
.parent_refn
= parent
->refn
;
41 fill_default_sys_attrs(new_op
->upcall
.req
.create
.attributes
,
42 ORANGEFS_TYPE_METAFILE
, mode
);
44 strncpy(new_op
->upcall
.req
.create
.d_name
,
45 dentry
->d_name
.name
, ORANGEFS_NAME_MAX
- 1);
47 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
49 gossip_debug(GOSSIP_NAME_DEBUG
,
50 "%s: %pd: handle:%pU: fsid:%d: new_op:%p: ret:%d:\n",
53 &new_op
->downcall
.resp
.create
.refn
.khandle
,
54 new_op
->downcall
.resp
.create
.refn
.fs_id
,
61 ref
= new_op
->downcall
.resp
.create
.refn
;
63 inode
= orangefs_new_inode(dir
->i_sb
, dir
, S_IFREG
| mode
, 0, &ref
);
65 gossip_err("%s: Failed to allocate inode for file :%pd:\n",
72 gossip_debug(GOSSIP_NAME_DEBUG
,
73 "%s: Assigned inode :%pU: for file :%pd:\n",
75 get_khandle_from_ino(inode
),
78 d_instantiate_new(dentry
, inode
);
79 orangefs_set_timeout(dentry
);
81 gossip_debug(GOSSIP_NAME_DEBUG
,
82 "%s: dentry instantiated for %pd\n",
86 memset(&iattr
, 0, sizeof iattr
);
87 iattr
.ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
88 iattr
.ia_mtime
= iattr
.ia_ctime
= current_time(dir
);
89 __orangefs_setattr(dir
, &iattr
);
93 gossip_debug(GOSSIP_NAME_DEBUG
,
94 "%s: %pd: returning %d\n",
102 * Attempt to resolve an object name (dentry->d_name), parent handle, and
103 * fsid into a handle for the object.
105 static struct dentry
*orangefs_lookup(struct inode
*dir
, struct dentry
*dentry
,
108 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
109 struct orangefs_kernel_op_s
*new_op
;
114 * in theory we could skip a lookup here (if the intent is to
115 * create) in order to avoid a potentially failed lookup, but
116 * leaving it in can skip a valid lookup and try to create a file
117 * that already exists (e.g. the vfs already handles checking for
118 * -EEXIST on O_EXCL opens, which is broken if we skip this lookup
119 * in the create path)
121 gossip_debug(GOSSIP_NAME_DEBUG
, "%s called on %pd\n",
124 if (dentry
->d_name
.len
> (ORANGEFS_NAME_MAX
- 1))
125 return ERR_PTR(-ENAMETOOLONG
);
127 new_op
= op_alloc(ORANGEFS_VFS_OP_LOOKUP
);
129 return ERR_PTR(-ENOMEM
);
131 new_op
->upcall
.req
.lookup
.sym_follow
= ORANGEFS_LOOKUP_LINK_NO_FOLLOW
;
133 gossip_debug(GOSSIP_NAME_DEBUG
, "%s:%s:%d using parent %pU\n",
137 &parent
->refn
.khandle
);
138 new_op
->upcall
.req
.lookup
.parent_refn
= parent
->refn
;
140 strncpy(new_op
->upcall
.req
.lookup
.d_name
, dentry
->d_name
.name
,
141 ORANGEFS_NAME_MAX
- 1);
143 gossip_debug(GOSSIP_NAME_DEBUG
,
144 "%s: doing lookup on %s under %pU,%d\n",
146 new_op
->upcall
.req
.lookup
.d_name
,
147 &new_op
->upcall
.req
.lookup
.parent_refn
.khandle
,
148 new_op
->upcall
.req
.lookup
.parent_refn
.fs_id
);
150 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
152 gossip_debug(GOSSIP_NAME_DEBUG
,
153 "Lookup Got %pU, fsid %d (ret=%d)\n",
154 &new_op
->downcall
.resp
.lookup
.refn
.khandle
,
155 new_op
->downcall
.resp
.lookup
.refn
.fs_id
,
159 orangefs_set_timeout(dentry
);
160 inode
= orangefs_iget(dir
->i_sb
, &new_op
->downcall
.resp
.lookup
.refn
);
161 } else if (ret
== -ENOENT
) {
164 /* must be a non-recoverable error */
165 inode
= ERR_PTR(ret
);
169 return d_splice_alias(inode
, dentry
);
172 /* return 0 on success; non-zero otherwise */
173 static int orangefs_unlink(struct inode
*dir
, struct dentry
*dentry
)
175 struct inode
*inode
= dentry
->d_inode
;
176 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
177 struct orangefs_kernel_op_s
*new_op
;
181 gossip_debug(GOSSIP_NAME_DEBUG
,
182 "%s: called on %pd\n"
183 " (inode %pU): Parent is %pU | fs_id %d\n",
186 get_khandle_from_ino(inode
),
187 &parent
->refn
.khandle
,
190 new_op
= op_alloc(ORANGEFS_VFS_OP_REMOVE
);
194 new_op
->upcall
.req
.remove
.parent_refn
= parent
->refn
;
195 strncpy(new_op
->upcall
.req
.remove
.d_name
, dentry
->d_name
.name
,
196 ORANGEFS_NAME_MAX
- 1);
198 ret
= service_operation(new_op
, "orangefs_unlink",
199 get_interruptible_flag(inode
));
201 gossip_debug(GOSSIP_NAME_DEBUG
,
202 "%s: service_operation returned:%d:\n",
211 memset(&iattr
, 0, sizeof iattr
);
212 iattr
.ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
213 iattr
.ia_mtime
= iattr
.ia_ctime
= current_time(dir
);
214 __orangefs_setattr(dir
, &iattr
);
219 static int orangefs_symlink(struct user_namespace
*mnt_userns
,
221 struct dentry
*dentry
,
224 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
225 struct orangefs_kernel_op_s
*new_op
;
226 struct orangefs_object_kref ref
;
232 gossip_debug(GOSSIP_NAME_DEBUG
, "%s: called\n", __func__
);
237 if (strlen(symname
)+1 > ORANGEFS_NAME_MAX
)
238 return -ENAMETOOLONG
;
240 new_op
= op_alloc(ORANGEFS_VFS_OP_SYMLINK
);
244 new_op
->upcall
.req
.sym
.parent_refn
= parent
->refn
;
246 fill_default_sys_attrs(new_op
->upcall
.req
.sym
.attributes
,
247 ORANGEFS_TYPE_SYMLINK
,
250 strncpy(new_op
->upcall
.req
.sym
.entry_name
,
252 ORANGEFS_NAME_MAX
- 1);
253 strncpy(new_op
->upcall
.req
.sym
.target
, symname
, ORANGEFS_NAME_MAX
- 1);
255 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
257 gossip_debug(GOSSIP_NAME_DEBUG
,
258 "Symlink Got ORANGEFS handle %pU on fsid %d (ret=%d)\n",
259 &new_op
->downcall
.resp
.sym
.refn
.khandle
,
260 new_op
->downcall
.resp
.sym
.refn
.fs_id
, ret
);
263 gossip_debug(GOSSIP_NAME_DEBUG
,
264 "%s: failed with error code %d\n",
269 ref
= new_op
->downcall
.resp
.sym
.refn
;
271 inode
= orangefs_new_inode(dir
->i_sb
, dir
, S_IFLNK
| mode
, 0, &ref
);
274 ("*** Failed to allocate orangefs symlink inode\n");
275 ret
= PTR_ERR(inode
);
279 * This is necessary because orangefs_inode_getattr will not
280 * re-read symlink size as it is impossible for it to change.
281 * Invalidating the cache does not help. orangefs_new_inode
282 * does not set the correct size (it does not know symname).
284 inode
->i_size
= strlen(symname
);
286 gossip_debug(GOSSIP_NAME_DEBUG
,
287 "Assigned symlink inode new number of %pU\n",
288 get_khandle_from_ino(inode
));
290 d_instantiate_new(dentry
, inode
);
291 orangefs_set_timeout(dentry
);
293 gossip_debug(GOSSIP_NAME_DEBUG
,
294 "Inode (Symlink) %pU -> %pd\n",
295 get_khandle_from_ino(inode
),
298 memset(&iattr
, 0, sizeof iattr
);
299 iattr
.ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
300 iattr
.ia_mtime
= iattr
.ia_ctime
= current_time(dir
);
301 __orangefs_setattr(dir
, &iattr
);
308 static int orangefs_mkdir(struct user_namespace
*mnt_userns
, struct inode
*dir
,
309 struct dentry
*dentry
, umode_t mode
)
311 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
312 struct orangefs_kernel_op_s
*new_op
;
313 struct orangefs_object_kref ref
;
318 new_op
= op_alloc(ORANGEFS_VFS_OP_MKDIR
);
322 new_op
->upcall
.req
.mkdir
.parent_refn
= parent
->refn
;
324 fill_default_sys_attrs(new_op
->upcall
.req
.mkdir
.attributes
,
325 ORANGEFS_TYPE_DIRECTORY
, mode
);
327 strncpy(new_op
->upcall
.req
.mkdir
.d_name
,
328 dentry
->d_name
.name
, ORANGEFS_NAME_MAX
- 1);
330 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
332 gossip_debug(GOSSIP_NAME_DEBUG
,
333 "Mkdir Got ORANGEFS handle %pU on fsid %d\n",
334 &new_op
->downcall
.resp
.mkdir
.refn
.khandle
,
335 new_op
->downcall
.resp
.mkdir
.refn
.fs_id
);
338 gossip_debug(GOSSIP_NAME_DEBUG
,
339 "%s: failed with error code %d\n",
344 ref
= new_op
->downcall
.resp
.mkdir
.refn
;
346 inode
= orangefs_new_inode(dir
->i_sb
, dir
, S_IFDIR
| mode
, 0, &ref
);
348 gossip_err("*** Failed to allocate orangefs dir inode\n");
349 ret
= PTR_ERR(inode
);
353 gossip_debug(GOSSIP_NAME_DEBUG
,
354 "Assigned dir inode new number of %pU\n",
355 get_khandle_from_ino(inode
));
357 d_instantiate_new(dentry
, inode
);
358 orangefs_set_timeout(dentry
);
360 gossip_debug(GOSSIP_NAME_DEBUG
,
361 "Inode (Directory) %pU -> %pd\n",
362 get_khandle_from_ino(inode
),
366 * NOTE: we have no good way to keep nlink consistent for directories
367 * across clients; keep constant at 1.
369 memset(&iattr
, 0, sizeof iattr
);
370 iattr
.ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
371 iattr
.ia_mtime
= iattr
.ia_ctime
= current_time(dir
);
372 __orangefs_setattr(dir
, &iattr
);
378 static int orangefs_rename(struct user_namespace
*mnt_userns
,
379 struct inode
*old_dir
,
380 struct dentry
*old_dentry
,
381 struct inode
*new_dir
,
382 struct dentry
*new_dentry
,
385 struct orangefs_kernel_op_s
*new_op
;
392 gossip_debug(GOSSIP_NAME_DEBUG
,
393 "orangefs_rename: called (%pd2 => %pd2) ct=%d\n",
394 old_dentry
, new_dentry
, d_count(new_dentry
));
396 memset(&iattr
, 0, sizeof iattr
);
397 iattr
.ia_valid
|= ATTR_MTIME
| ATTR_CTIME
;
398 iattr
.ia_mtime
= iattr
.ia_ctime
= current_time(new_dir
);
399 __orangefs_setattr(new_dir
, &iattr
);
401 new_op
= op_alloc(ORANGEFS_VFS_OP_RENAME
);
405 new_op
->upcall
.req
.rename
.old_parent_refn
= ORANGEFS_I(old_dir
)->refn
;
406 new_op
->upcall
.req
.rename
.new_parent_refn
= ORANGEFS_I(new_dir
)->refn
;
408 strncpy(new_op
->upcall
.req
.rename
.d_old_name
,
409 old_dentry
->d_name
.name
,
410 ORANGEFS_NAME_MAX
- 1);
411 strncpy(new_op
->upcall
.req
.rename
.d_new_name
,
412 new_dentry
->d_name
.name
,
413 ORANGEFS_NAME_MAX
- 1);
415 ret
= service_operation(new_op
,
417 get_interruptible_flag(old_dentry
->d_inode
));
419 gossip_debug(GOSSIP_NAME_DEBUG
,
420 "orangefs_rename: got downcall status %d\n",
423 if (new_dentry
->d_inode
)
424 new_dentry
->d_inode
->i_ctime
= current_time(new_dentry
->d_inode
);
430 /* ORANGEFS implementation of VFS inode operations for directories */
431 const struct inode_operations orangefs_dir_inode_operations
= {
432 .lookup
= orangefs_lookup
,
433 .get_acl
= orangefs_get_acl
,
434 .set_acl
= orangefs_set_acl
,
435 .create
= orangefs_create
,
436 .unlink
= orangefs_unlink
,
437 .symlink
= orangefs_symlink
,
438 .mkdir
= orangefs_mkdir
,
439 .rmdir
= orangefs_unlink
,
440 .rename
= orangefs_rename
,
441 .setattr
= orangefs_setattr
,
442 .getattr
= orangefs_getattr
,
443 .listxattr
= orangefs_listxattr
,
444 .permission
= orangefs_permission
,
445 .update_time
= orangefs_update_time
,