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 inode
*dir
,
19 struct dentry
*dentry
,
23 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
24 struct orangefs_kernel_op_s
*new_op
;
25 struct orangefs_object_kref ref
;
30 gossip_debug(GOSSIP_NAME_DEBUG
, "%s: %pd\n",
34 new_op
= op_alloc(ORANGEFS_VFS_OP_CREATE
);
38 new_op
->upcall
.req
.create
.parent_refn
= parent
->refn
;
40 fill_default_sys_attrs(new_op
->upcall
.req
.create
.attributes
,
41 ORANGEFS_TYPE_METAFILE
, mode
);
43 strncpy(new_op
->upcall
.req
.create
.d_name
,
44 dentry
->d_name
.name
, ORANGEFS_NAME_MAX
- 1);
46 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
48 gossip_debug(GOSSIP_NAME_DEBUG
,
49 "%s: %pd: handle:%pU: fsid:%d: new_op:%p: ret:%d:\n",
52 &new_op
->downcall
.resp
.create
.refn
.khandle
,
53 new_op
->downcall
.resp
.create
.refn
.fs_id
,
60 ref
= new_op
->downcall
.resp
.create
.refn
;
62 inode
= orangefs_new_inode(dir
->i_sb
, dir
, S_IFREG
| mode
, 0, &ref
);
64 gossip_err("%s: Failed to allocate inode for file :%pd:\n",
71 gossip_debug(GOSSIP_NAME_DEBUG
,
72 "%s: Assigned inode :%pU: for file :%pd:\n",
74 get_khandle_from_ino(inode
),
77 d_instantiate_new(dentry
, inode
);
78 orangefs_set_timeout(dentry
);
79 ORANGEFS_I(inode
)->getattr_time
= jiffies
- 1;
80 ORANGEFS_I(inode
)->getattr_mask
= STATX_BASIC_STATS
;
82 gossip_debug(GOSSIP_NAME_DEBUG
,
83 "%s: dentry instantiated for %pd\n",
87 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
88 memset(&iattr
, 0, sizeof iattr
);
89 iattr
.ia_valid
|= ATTR_MTIME
;
90 orangefs_inode_setattr(dir
, &iattr
);
91 mark_inode_dirty_sync(dir
);
95 gossip_debug(GOSSIP_NAME_DEBUG
,
96 "%s: %pd: returning %d\n",
104 * Attempt to resolve an object name (dentry->d_name), parent handle, and
105 * fsid into a handle for the object.
107 static struct dentry
*orangefs_lookup(struct inode
*dir
, struct dentry
*dentry
,
110 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
111 struct orangefs_kernel_op_s
*new_op
;
116 * in theory we could skip a lookup here (if the intent is to
117 * create) in order to avoid a potentially failed lookup, but
118 * leaving it in can skip a valid lookup and try to create a file
119 * that already exists (e.g. the vfs already handles checking for
120 * -EEXIST on O_EXCL opens, which is broken if we skip this lookup
121 * in the create path)
123 gossip_debug(GOSSIP_NAME_DEBUG
, "%s called on %pd\n",
126 if (dentry
->d_name
.len
> (ORANGEFS_NAME_MAX
- 1))
127 return ERR_PTR(-ENAMETOOLONG
);
129 new_op
= op_alloc(ORANGEFS_VFS_OP_LOOKUP
);
131 return ERR_PTR(-ENOMEM
);
133 new_op
->upcall
.req
.lookup
.sym_follow
= ORANGEFS_LOOKUP_LINK_NO_FOLLOW
;
135 gossip_debug(GOSSIP_NAME_DEBUG
, "%s:%s:%d using parent %pU\n",
139 &parent
->refn
.khandle
);
140 new_op
->upcall
.req
.lookup
.parent_refn
= parent
->refn
;
142 strncpy(new_op
->upcall
.req
.lookup
.d_name
, dentry
->d_name
.name
,
143 ORANGEFS_NAME_MAX
- 1);
145 gossip_debug(GOSSIP_NAME_DEBUG
,
146 "%s: doing lookup on %s under %pU,%d\n",
148 new_op
->upcall
.req
.lookup
.d_name
,
149 &new_op
->upcall
.req
.lookup
.parent_refn
.khandle
,
150 new_op
->upcall
.req
.lookup
.parent_refn
.fs_id
);
152 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
154 gossip_debug(GOSSIP_NAME_DEBUG
,
155 "Lookup Got %pU, fsid %d (ret=%d)\n",
156 &new_op
->downcall
.resp
.lookup
.refn
.khandle
,
157 new_op
->downcall
.resp
.lookup
.refn
.fs_id
,
161 orangefs_set_timeout(dentry
);
162 inode
= orangefs_iget(dir
->i_sb
, &new_op
->downcall
.resp
.lookup
.refn
);
163 } else if (ret
== -ENOENT
) {
166 /* must be a non-recoverable error */
167 inode
= ERR_PTR(ret
);
171 return d_splice_alias(inode
, dentry
);
174 /* return 0 on success; non-zero otherwise */
175 static int orangefs_unlink(struct inode
*dir
, struct dentry
*dentry
)
177 struct inode
*inode
= dentry
->d_inode
;
178 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
179 struct orangefs_kernel_op_s
*new_op
;
183 gossip_debug(GOSSIP_NAME_DEBUG
,
184 "%s: called on %pd\n"
185 " (inode %pU): Parent is %pU | fs_id %d\n",
188 get_khandle_from_ino(inode
),
189 &parent
->refn
.khandle
,
192 new_op
= op_alloc(ORANGEFS_VFS_OP_REMOVE
);
196 new_op
->upcall
.req
.remove
.parent_refn
= parent
->refn
;
197 strncpy(new_op
->upcall
.req
.remove
.d_name
, dentry
->d_name
.name
,
198 ORANGEFS_NAME_MAX
- 1);
200 ret
= service_operation(new_op
, "orangefs_unlink",
201 get_interruptible_flag(inode
));
203 gossip_debug(GOSSIP_NAME_DEBUG
,
204 "%s: service_operation returned:%d:\n",
213 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
214 memset(&iattr
, 0, sizeof iattr
);
215 iattr
.ia_valid
|= ATTR_MTIME
;
216 orangefs_inode_setattr(dir
, &iattr
);
217 mark_inode_dirty_sync(dir
);
222 static int orangefs_symlink(struct inode
*dir
,
223 struct dentry
*dentry
,
226 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
227 struct orangefs_kernel_op_s
*new_op
;
228 struct orangefs_object_kref ref
;
234 gossip_debug(GOSSIP_NAME_DEBUG
, "%s: called\n", __func__
);
239 if (strlen(symname
)+1 > ORANGEFS_NAME_MAX
)
240 return -ENAMETOOLONG
;
242 new_op
= op_alloc(ORANGEFS_VFS_OP_SYMLINK
);
246 new_op
->upcall
.req
.sym
.parent_refn
= parent
->refn
;
248 fill_default_sys_attrs(new_op
->upcall
.req
.sym
.attributes
,
249 ORANGEFS_TYPE_SYMLINK
,
252 strncpy(new_op
->upcall
.req
.sym
.entry_name
,
254 ORANGEFS_NAME_MAX
- 1);
255 strncpy(new_op
->upcall
.req
.sym
.target
, symname
, ORANGEFS_NAME_MAX
- 1);
257 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
259 gossip_debug(GOSSIP_NAME_DEBUG
,
260 "Symlink Got ORANGEFS handle %pU on fsid %d (ret=%d)\n",
261 &new_op
->downcall
.resp
.sym
.refn
.khandle
,
262 new_op
->downcall
.resp
.sym
.refn
.fs_id
, ret
);
265 gossip_debug(GOSSIP_NAME_DEBUG
,
266 "%s: failed with error code %d\n",
271 ref
= new_op
->downcall
.resp
.sym
.refn
;
273 inode
= orangefs_new_inode(dir
->i_sb
, dir
, S_IFLNK
| mode
, 0, &ref
);
276 ("*** Failed to allocate orangefs symlink inode\n");
277 ret
= PTR_ERR(inode
);
281 * This is necessary because orangefs_inode_getattr will not
282 * re-read symlink size as it is impossible for it to change.
283 * Invalidating the cache does not help. orangefs_new_inode
284 * does not set the correct size (it does not know symname).
286 inode
->i_size
= strlen(symname
);
288 gossip_debug(GOSSIP_NAME_DEBUG
,
289 "Assigned symlink inode new number of %pU\n",
290 get_khandle_from_ino(inode
));
292 d_instantiate_new(dentry
, inode
);
293 orangefs_set_timeout(dentry
);
294 ORANGEFS_I(inode
)->getattr_time
= jiffies
- 1;
295 ORANGEFS_I(inode
)->getattr_mask
= STATX_BASIC_STATS
;
297 gossip_debug(GOSSIP_NAME_DEBUG
,
298 "Inode (Symlink) %pU -> %pd\n",
299 get_khandle_from_ino(inode
),
302 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
303 memset(&iattr
, 0, sizeof iattr
);
304 iattr
.ia_valid
|= ATTR_MTIME
;
305 orangefs_inode_setattr(dir
, &iattr
);
306 mark_inode_dirty_sync(dir
);
313 static int orangefs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
315 struct orangefs_inode_s
*parent
= ORANGEFS_I(dir
);
316 struct orangefs_kernel_op_s
*new_op
;
317 struct orangefs_object_kref ref
;
322 new_op
= op_alloc(ORANGEFS_VFS_OP_MKDIR
);
326 new_op
->upcall
.req
.mkdir
.parent_refn
= parent
->refn
;
328 fill_default_sys_attrs(new_op
->upcall
.req
.mkdir
.attributes
,
329 ORANGEFS_TYPE_DIRECTORY
, mode
);
331 strncpy(new_op
->upcall
.req
.mkdir
.d_name
,
332 dentry
->d_name
.name
, ORANGEFS_NAME_MAX
- 1);
334 ret
= service_operation(new_op
, __func__
, get_interruptible_flag(dir
));
336 gossip_debug(GOSSIP_NAME_DEBUG
,
337 "Mkdir Got ORANGEFS handle %pU on fsid %d\n",
338 &new_op
->downcall
.resp
.mkdir
.refn
.khandle
,
339 new_op
->downcall
.resp
.mkdir
.refn
.fs_id
);
342 gossip_debug(GOSSIP_NAME_DEBUG
,
343 "%s: failed with error code %d\n",
348 ref
= new_op
->downcall
.resp
.mkdir
.refn
;
350 inode
= orangefs_new_inode(dir
->i_sb
, dir
, S_IFDIR
| mode
, 0, &ref
);
352 gossip_err("*** Failed to allocate orangefs dir inode\n");
353 ret
= PTR_ERR(inode
);
357 gossip_debug(GOSSIP_NAME_DEBUG
,
358 "Assigned dir inode new number of %pU\n",
359 get_khandle_from_ino(inode
));
361 d_instantiate_new(dentry
, inode
);
362 orangefs_set_timeout(dentry
);
363 ORANGEFS_I(inode
)->getattr_time
= jiffies
- 1;
364 ORANGEFS_I(inode
)->getattr_mask
= STATX_BASIC_STATS
;
366 gossip_debug(GOSSIP_NAME_DEBUG
,
367 "Inode (Directory) %pU -> %pd\n",
368 get_khandle_from_ino(inode
),
372 * NOTE: we have no good way to keep nlink consistent for directories
373 * across clients; keep constant at 1.
375 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
376 memset(&iattr
, 0, sizeof iattr
);
377 iattr
.ia_valid
|= ATTR_MTIME
;
378 orangefs_inode_setattr(dir
, &iattr
);
379 mark_inode_dirty_sync(dir
);
385 static int orangefs_rename(struct inode
*old_dir
,
386 struct dentry
*old_dentry
,
387 struct inode
*new_dir
,
388 struct dentry
*new_dentry
,
391 struct orangefs_kernel_op_s
*new_op
;
397 gossip_debug(GOSSIP_NAME_DEBUG
,
398 "orangefs_rename: called (%pd2 => %pd2) ct=%d\n",
399 old_dentry
, new_dentry
, d_count(new_dentry
));
401 ORANGEFS_I(new_dentry
->d_parent
->d_inode
)->getattr_time
= jiffies
- 1;
403 new_op
= op_alloc(ORANGEFS_VFS_OP_RENAME
);
407 new_op
->upcall
.req
.rename
.old_parent_refn
= ORANGEFS_I(old_dir
)->refn
;
408 new_op
->upcall
.req
.rename
.new_parent_refn
= ORANGEFS_I(new_dir
)->refn
;
410 strncpy(new_op
->upcall
.req
.rename
.d_old_name
,
411 old_dentry
->d_name
.name
,
412 ORANGEFS_NAME_MAX
- 1);
413 strncpy(new_op
->upcall
.req
.rename
.d_new_name
,
414 new_dentry
->d_name
.name
,
415 ORANGEFS_NAME_MAX
- 1);
417 ret
= service_operation(new_op
,
419 get_interruptible_flag(old_dentry
->d_inode
));
421 gossip_debug(GOSSIP_NAME_DEBUG
,
422 "orangefs_rename: got downcall status %d\n",
425 if (new_dentry
->d_inode
)
426 new_dentry
->d_inode
->i_ctime
= current_time(new_dentry
->d_inode
);
432 /* ORANGEFS implementation of VFS inode operations for directories */
433 const struct inode_operations orangefs_dir_inode_operations
= {
434 .lookup
= orangefs_lookup
,
435 .get_acl
= orangefs_get_acl
,
436 .set_acl
= orangefs_set_acl
,
437 .create
= orangefs_create
,
438 .unlink
= orangefs_unlink
,
439 .symlink
= orangefs_symlink
,
440 .mkdir
= orangefs_mkdir
,
441 .rmdir
= orangefs_unlink
,
442 .rename
= orangefs_rename
,
443 .setattr
= orangefs_setattr
,
444 .getattr
= orangefs_getattr
,
445 .listxattr
= orangefs_listxattr
,
446 .permission
= orangefs_permission
,
447 .update_time
= orangefs_update_time
,