1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2001 Clemson University and The University of Chicago
5 * See COPYING in top-level directory.
7 #include <linux/kernel.h>
9 #include "orangefs-kernel.h"
10 #include "orangefs-dev-proto.h"
11 #include "orangefs-bufmap.h"
13 __s32 fsid_of_op(struct orangefs_kernel_op_s *op)
15 __s32 fsid = ORANGEFS_FS_ID_NULL;
18 switch (op->upcall.type) {
19 case ORANGEFS_VFS_OP_FILE_IO:
20 fsid = op->upcall.req.io.refn.fs_id;
22 case ORANGEFS_VFS_OP_LOOKUP:
23 fsid = op->upcall.req.lookup.parent_refn.fs_id;
25 case ORANGEFS_VFS_OP_CREATE:
26 fsid = op->upcall.req.create.parent_refn.fs_id;
28 case ORANGEFS_VFS_OP_GETATTR:
29 fsid = op->upcall.req.getattr.refn.fs_id;
31 case ORANGEFS_VFS_OP_REMOVE:
32 fsid = op->upcall.req.remove.parent_refn.fs_id;
34 case ORANGEFS_VFS_OP_MKDIR:
35 fsid = op->upcall.req.mkdir.parent_refn.fs_id;
37 case ORANGEFS_VFS_OP_READDIR:
38 fsid = op->upcall.req.readdir.refn.fs_id;
40 case ORANGEFS_VFS_OP_SETATTR:
41 fsid = op->upcall.req.setattr.refn.fs_id;
43 case ORANGEFS_VFS_OP_SYMLINK:
44 fsid = op->upcall.req.sym.parent_refn.fs_id;
46 case ORANGEFS_VFS_OP_RENAME:
47 fsid = op->upcall.req.rename.old_parent_refn.fs_id;
49 case ORANGEFS_VFS_OP_STATFS:
50 fsid = op->upcall.req.statfs.fs_id;
52 case ORANGEFS_VFS_OP_TRUNCATE:
53 fsid = op->upcall.req.truncate.refn.fs_id;
55 case ORANGEFS_VFS_OP_RA_FLUSH:
56 fsid = op->upcall.req.ra_cache_flush.refn.fs_id;
58 case ORANGEFS_VFS_OP_FS_UMOUNT:
59 fsid = op->upcall.req.fs_umount.fs_id;
61 case ORANGEFS_VFS_OP_GETXATTR:
62 fsid = op->upcall.req.getxattr.refn.fs_id;
64 case ORANGEFS_VFS_OP_SETXATTR:
65 fsid = op->upcall.req.setxattr.refn.fs_id;
67 case ORANGEFS_VFS_OP_LISTXATTR:
68 fsid = op->upcall.req.listxattr.refn.fs_id;
70 case ORANGEFS_VFS_OP_REMOVEXATTR:
71 fsid = op->upcall.req.removexattr.refn.fs_id;
73 case ORANGEFS_VFS_OP_FSYNC:
74 fsid = op->upcall.req.fsync.refn.fs_id;
83 static int orangefs_inode_flags(struct ORANGEFS_sys_attr_s *attrs)
86 if (attrs->flags & ORANGEFS_IMMUTABLE_FL)
89 flags &= ~S_IMMUTABLE;
90 if (attrs->flags & ORANGEFS_APPEND_FL)
94 if (attrs->flags & ORANGEFS_NOATIME_FL)
101 static int orangefs_inode_perms(struct ORANGEFS_sys_attr_s *attrs)
105 if (attrs->perms & ORANGEFS_O_EXECUTE)
106 perm_mode |= S_IXOTH;
107 if (attrs->perms & ORANGEFS_O_WRITE)
108 perm_mode |= S_IWOTH;
109 if (attrs->perms & ORANGEFS_O_READ)
110 perm_mode |= S_IROTH;
112 if (attrs->perms & ORANGEFS_G_EXECUTE)
113 perm_mode |= S_IXGRP;
114 if (attrs->perms & ORANGEFS_G_WRITE)
115 perm_mode |= S_IWGRP;
116 if (attrs->perms & ORANGEFS_G_READ)
117 perm_mode |= S_IRGRP;
119 if (attrs->perms & ORANGEFS_U_EXECUTE)
120 perm_mode |= S_IXUSR;
121 if (attrs->perms & ORANGEFS_U_WRITE)
122 perm_mode |= S_IWUSR;
123 if (attrs->perms & ORANGEFS_U_READ)
124 perm_mode |= S_IRUSR;
126 if (attrs->perms & ORANGEFS_G_SGID)
127 perm_mode |= S_ISGID;
128 if (attrs->perms & ORANGEFS_U_SUID)
129 perm_mode |= S_ISUID;
135 * NOTE: in kernel land, we never use the sys_attr->link_target for
136 * anything, so don't bother copying it into the sys_attr object here.
138 static inline int copy_attributes_from_inode(struct inode *inode,
139 struct ORANGEFS_sys_attr_s *attrs,
144 if (!iattr || !inode || !attrs) {
145 gossip_err("NULL iattr (%p), inode (%p), attrs (%p) "
146 "in copy_attributes_from_inode!\n",
153 * We need to be careful to only copy the attributes out of the
154 * iattr object that we know are valid.
157 if (iattr->ia_valid & ATTR_UID) {
158 attrs->owner = from_kuid(&init_user_ns, iattr->ia_uid);
159 attrs->mask |= ORANGEFS_ATTR_SYS_UID;
160 gossip_debug(GOSSIP_UTILS_DEBUG, "(UID) %d\n", attrs->owner);
162 if (iattr->ia_valid & ATTR_GID) {
163 attrs->group = from_kgid(&init_user_ns, iattr->ia_gid);
164 attrs->mask |= ORANGEFS_ATTR_SYS_GID;
165 gossip_debug(GOSSIP_UTILS_DEBUG, "(GID) %d\n", attrs->group);
168 if (iattr->ia_valid & ATTR_ATIME) {
169 attrs->mask |= ORANGEFS_ATTR_SYS_ATIME;
170 if (iattr->ia_valid & ATTR_ATIME_SET) {
171 attrs->atime = (time64_t)iattr->ia_atime.tv_sec;
172 attrs->mask |= ORANGEFS_ATTR_SYS_ATIME_SET;
175 if (iattr->ia_valid & ATTR_MTIME) {
176 attrs->mask |= ORANGEFS_ATTR_SYS_MTIME;
177 if (iattr->ia_valid & ATTR_MTIME_SET) {
178 attrs->mtime = (time64_t)iattr->ia_mtime.tv_sec;
179 attrs->mask |= ORANGEFS_ATTR_SYS_MTIME_SET;
182 if (iattr->ia_valid & ATTR_CTIME)
183 attrs->mask |= ORANGEFS_ATTR_SYS_CTIME;
186 * ORANGEFS cannot set size with a setattr operation. Probably not likely
187 * to be requested through the VFS, but just in case, don't worry about
191 if (iattr->ia_valid & ATTR_MODE) {
192 tmp_mode = iattr->ia_mode;
193 if (tmp_mode & (S_ISVTX)) {
194 if (is_root_handle(inode)) {
196 * allow sticky bit to be set on root (since
197 * it shows up that way by default anyhow),
198 * but don't show it to the server
202 gossip_debug(GOSSIP_UTILS_DEBUG,
203 "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
208 if (tmp_mode & (S_ISUID)) {
209 gossip_debug(GOSSIP_UTILS_DEBUG,
210 "Attempting to set setuid bit (not supported); returning EINVAL.\n");
214 attrs->perms = ORANGEFS_util_translate_mode(tmp_mode);
215 attrs->mask |= ORANGEFS_ATTR_SYS_PERM;
221 static int orangefs_inode_type(enum orangefs_ds_type objtype)
223 if (objtype == ORANGEFS_TYPE_METAFILE)
225 else if (objtype == ORANGEFS_TYPE_DIRECTORY)
227 else if (objtype == ORANGEFS_TYPE_SYMLINK)
233 static void orangefs_make_bad_inode(struct inode *inode)
235 if (is_root_handle(inode)) {
237 * if this occurs, the pvfs2-client-core was killed but we
238 * can't afford to lose the inode operations and such
239 * associated with the root handle in any case.
241 gossip_debug(GOSSIP_UTILS_DEBUG,
242 "*** NOT making bad root inode %pU\n",
243 get_khandle_from_ino(inode));
245 gossip_debug(GOSSIP_UTILS_DEBUG,
246 "*** making bad inode %pU\n",
247 get_khandle_from_ino(inode));
248 make_bad_inode(inode);
252 static int orangefs_inode_is_stale(struct inode *inode,
253 struct ORANGEFS_sys_attr_s *attrs, char *link_target)
255 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
256 int type = orangefs_inode_type(attrs->objtype);
258 * If the inode type or symlink target have changed then this
261 if (type == -1 || !(inode->i_mode & type)) {
262 orangefs_make_bad_inode(inode);
265 if (type == S_IFLNK && strncmp(orangefs_inode->link_target,
266 link_target, ORANGEFS_NAME_MAX)) {
267 orangefs_make_bad_inode(inode);
273 int orangefs_inode_getattr(struct inode *inode, int new, int bypass,
276 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
277 struct orangefs_kernel_op_s *new_op;
278 loff_t inode_size, rounded_up_size;
281 gossip_debug(GOSSIP_UTILS_DEBUG, "%s: called on inode %pU\n", __func__,
282 get_khandle_from_ino(inode));
284 if (!new && !bypass) {
286 * Must have all the attributes in the mask and be within cache
289 if ((request_mask & orangefs_inode->getattr_mask) ==
291 time_before(jiffies, orangefs_inode->getattr_time))
295 new_op = op_alloc(ORANGEFS_VFS_OP_GETATTR);
298 new_op->upcall.req.getattr.refn = orangefs_inode->refn;
300 * Size is the hardest attribute to get. The incremental cost of any
301 * other attribute is essentially zero.
303 if (request_mask & STATX_SIZE || new)
304 new_op->upcall.req.getattr.mask = ORANGEFS_ATTR_SYS_ALL_NOHINT;
306 new_op->upcall.req.getattr.mask =
307 ORANGEFS_ATTR_SYS_ALL_NOHINT & ~ORANGEFS_ATTR_SYS_SIZE;
309 ret = service_operation(new_op, __func__,
310 get_interruptible_flag(inode));
315 ret = orangefs_inode_is_stale(inode,
316 &new_op->downcall.resp.getattr.attributes,
317 new_op->downcall.resp.getattr.link_target);
324 type = orangefs_inode_type(new_op->
325 downcall.resp.getattr.attributes.objtype);
328 inode->i_flags = orangefs_inode_flags(&new_op->
329 downcall.resp.getattr.attributes);
330 if (request_mask & STATX_SIZE || new) {
331 inode_size = (loff_t)new_op->
332 downcall.resp.getattr.attributes.size;
334 (inode_size + (4096 - (inode_size % 4096)));
335 inode->i_size = inode_size;
336 orangefs_inode->blksize =
337 new_op->downcall.resp.getattr.attributes.blksize;
338 spin_lock(&inode->i_lock);
339 inode->i_bytes = inode_size;
341 (unsigned long)(rounded_up_size / 512);
342 spin_unlock(&inode->i_lock);
346 if (request_mask & STATX_SIZE || new) {
347 inode->i_size = PAGE_SIZE;
348 orangefs_inode->blksize = i_blocksize(inode);
349 spin_lock(&inode->i_lock);
350 inode_set_bytes(inode, inode->i_size);
351 spin_unlock(&inode->i_lock);
357 inode->i_size = (loff_t)strlen(new_op->
358 downcall.resp.getattr.link_target);
359 orangefs_inode->blksize = i_blocksize(inode);
360 ret = strscpy(orangefs_inode->link_target,
361 new_op->downcall.resp.getattr.link_target,
367 inode->i_link = orangefs_inode->link_target;
372 /* XXX: ESTALE? This is what is done if it is not new. */
373 orangefs_make_bad_inode(inode);
378 inode->i_uid = make_kuid(&init_user_ns, new_op->
379 downcall.resp.getattr.attributes.owner);
380 inode->i_gid = make_kgid(&init_user_ns, new_op->
381 downcall.resp.getattr.attributes.group);
382 inode->i_atime.tv_sec = (time64_t)new_op->
383 downcall.resp.getattr.attributes.atime;
384 inode->i_mtime.tv_sec = (time64_t)new_op->
385 downcall.resp.getattr.attributes.mtime;
386 inode->i_ctime.tv_sec = (time64_t)new_op->
387 downcall.resp.getattr.attributes.ctime;
388 inode->i_atime.tv_nsec = 0;
389 inode->i_mtime.tv_nsec = 0;
390 inode->i_ctime.tv_nsec = 0;
392 /* special case: mark the root inode as sticky */
393 inode->i_mode = type | (is_root_handle(inode) ? S_ISVTX : 0) |
394 orangefs_inode_perms(&new_op->downcall.resp.getattr.attributes);
396 orangefs_inode->getattr_time = jiffies +
397 orangefs_getattr_timeout_msecs*HZ/1000;
398 if (request_mask & STATX_SIZE || new)
399 orangefs_inode->getattr_mask = STATX_BASIC_STATS;
401 orangefs_inode->getattr_mask = STATX_BASIC_STATS & ~STATX_SIZE;
408 int orangefs_inode_check_changed(struct inode *inode)
410 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
411 struct orangefs_kernel_op_s *new_op;
414 gossip_debug(GOSSIP_UTILS_DEBUG, "%s: called on inode %pU\n", __func__,
415 get_khandle_from_ino(inode));
417 new_op = op_alloc(ORANGEFS_VFS_OP_GETATTR);
420 new_op->upcall.req.getattr.refn = orangefs_inode->refn;
421 new_op->upcall.req.getattr.mask = ORANGEFS_ATTR_SYS_TYPE |
422 ORANGEFS_ATTR_SYS_LNK_TARGET;
424 ret = service_operation(new_op, __func__,
425 get_interruptible_flag(inode));
429 ret = orangefs_inode_is_stale(inode,
430 &new_op->downcall.resp.getattr.attributes,
431 new_op->downcall.resp.getattr.link_target);
438 * issues a orangefs setattr request to make sure the new attribute values
439 * take effect if successful. returns 0 on success; -errno otherwise
441 int orangefs_inode_setattr(struct inode *inode, struct iattr *iattr)
443 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
444 struct orangefs_kernel_op_s *new_op;
447 new_op = op_alloc(ORANGEFS_VFS_OP_SETATTR);
451 new_op->upcall.req.setattr.refn = orangefs_inode->refn;
452 ret = copy_attributes_from_inode(inode,
453 &new_op->upcall.req.setattr.attributes,
456 ret = service_operation(new_op, __func__,
457 get_interruptible_flag(inode));
459 gossip_debug(GOSSIP_UTILS_DEBUG,
460 "orangefs_inode_setattr: returning %d\n",
467 orangefs_inode->getattr_time = jiffies - 1;
473 * The following is a very dirty hack that is now a permanent part of the
474 * ORANGEFS protocol. See protocol.h for more error definitions.
477 /* The order matches include/orangefs-types.h in the OrangeFS source. */
478 static int PINT_errno_mapping[] = {
479 0, EPERM, ENOENT, EINTR, EIO, ENXIO, EBADF, EAGAIN, ENOMEM,
480 EFAULT, EBUSY, EEXIST, ENODEV, ENOTDIR, EISDIR, EINVAL, EMFILE,
481 EFBIG, ENOSPC, EROFS, EMLINK, EPIPE, EDEADLK, ENAMETOOLONG,
482 ENOLCK, ENOSYS, ENOTEMPTY, ELOOP, EWOULDBLOCK, ENOMSG, EUNATCH,
483 EBADR, EDEADLOCK, ENODATA, ETIME, ENONET, EREMOTE, ECOMM,
484 EPROTO, EBADMSG, EOVERFLOW, ERESTART, EMSGSIZE, EPROTOTYPE,
485 ENOPROTOOPT, EPROTONOSUPPORT, EOPNOTSUPP, EADDRINUSE,
486 EADDRNOTAVAIL, ENETDOWN, ENETUNREACH, ENETRESET, ENOBUFS,
487 ETIMEDOUT, ECONNREFUSED, EHOSTDOWN, EHOSTUNREACH, EALREADY,
488 EACCES, ECONNRESET, ERANGE
491 int orangefs_normalize_to_errno(__s32 error_code)
496 if (error_code == 0) {
499 * This shouldn't ever happen. If it does it should be fixed on the
502 } else if (error_code > 0) {
503 gossip_err("orangefs: error status received.\n");
504 gossip_err("orangefs: assuming error code is inverted.\n");
505 error_code = -error_code;
509 * XXX: This is very bad since error codes from ORANGEFS may not be
510 * suitable for return into userspace.
514 * Convert ORANGEFS error values into errno values suitable for return
517 if ((-error_code) & ORANGEFS_NON_ERRNO_ERROR_BIT) {
519 (ORANGEFS_ERROR_NUMBER_BITS|ORANGEFS_NON_ERRNO_ERROR_BIT|
520 ORANGEFS_ERROR_BIT)) == ORANGEFS_ECANCEL) {
522 * cancellation error codes generally correspond to
523 * a timeout from the client's perspective
525 error_code = -ETIMEDOUT;
527 /* assume a default error code */
528 gossip_err("orangefs: warning: got error code without errno equivalent: %d.\n", error_code);
529 error_code = -EINVAL;
532 /* Convert ORANGEFS encoded errno values into regular errno values. */
533 } else if ((-error_code) & ORANGEFS_ERROR_BIT) {
534 i = (-error_code) & ~(ORANGEFS_ERROR_BIT|ORANGEFS_ERROR_CLASS_BITS);
535 if (i < ARRAY_SIZE(PINT_errno_mapping))
536 error_code = -PINT_errno_mapping[i];
538 error_code = -EINVAL;
541 * Only ORANGEFS protocol error codes should ever come here. Otherwise
542 * there is a bug somewhere.
545 gossip_err("orangefs: orangefs_normalize_to_errno: got error code which is not from ORANGEFS.\n");
546 error_code = -EINVAL;
552 __s32 ORANGEFS_util_translate_mode(int mode)
556 static int modes[NUM_MODES] = {
557 S_IXOTH, S_IWOTH, S_IROTH,
558 S_IXGRP, S_IWGRP, S_IRGRP,
559 S_IXUSR, S_IWUSR, S_IRUSR,
562 static int orangefs_modes[NUM_MODES] = {
563 ORANGEFS_O_EXECUTE, ORANGEFS_O_WRITE, ORANGEFS_O_READ,
564 ORANGEFS_G_EXECUTE, ORANGEFS_G_WRITE, ORANGEFS_G_READ,
565 ORANGEFS_U_EXECUTE, ORANGEFS_U_WRITE, ORANGEFS_U_READ,
566 ORANGEFS_G_SGID, ORANGEFS_U_SUID
569 for (i = 0; i < NUM_MODES; i++)
571 ret |= orangefs_modes[i];