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 #include "orangefs-kernel.h"
10 #include "orangefs-bufmap.h"
12 #include <linux/hashtable.h>
13 #include <linux/seq_file.h>
15 /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
16 static struct kmem_cache *orangefs_inode_cache;
18 /* list for storing orangefs specific superblocks in use */
19 LIST_HEAD(orangefs_superblocks);
21 DEFINE_SPINLOCK(orangefs_superblocks_lock);
29 const struct fs_parameter_spec orangefs_fs_param_spec[] = {
30 fsparam_flag ("acl", Opt_acl),
31 fsparam_flag ("intr", Opt_intr),
32 fsparam_flag ("local_lock", Opt_local_lock),
36 uint64_t orangefs_features;
38 static int orangefs_show_options(struct seq_file *m, struct dentry *root)
40 struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(root->d_sb);
42 if (root->d_sb->s_flags & SB_POSIXACL)
44 if (orangefs_sb->flags & ORANGEFS_OPT_INTR)
46 if (orangefs_sb->flags & ORANGEFS_OPT_LOCAL_LOCK)
47 seq_puts(m, ",local_lock");
51 static int orangefs_parse_param(struct fs_context *fc,
52 struct fs_parameter *param)
54 struct orangefs_sb_info_s *orangefs_sb = fc->s_fs_info;
55 struct fs_parse_result result;
58 opt = fs_parse(fc, orangefs_fs_param_spec, param, &result);
64 fc->sb_flags |= SB_POSIXACL;
67 orangefs_sb->flags |= ORANGEFS_OPT_INTR;
70 orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
77 static void orangefs_inode_cache_ctor(void *req)
79 struct orangefs_inode_s *orangefs_inode = req;
81 inode_init_once(&orangefs_inode->vfs_inode);
82 init_rwsem(&orangefs_inode->xattr_sem);
85 static struct inode *orangefs_alloc_inode(struct super_block *sb)
87 struct orangefs_inode_s *orangefs_inode;
89 orangefs_inode = alloc_inode_sb(sb, orangefs_inode_cache, GFP_KERNEL);
94 * We want to clear everything except for rw_semaphore and the
97 memset(&orangefs_inode->refn.khandle, 0, 16);
98 orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL;
99 orangefs_inode->last_failed_block_index_read = 0;
100 memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target));
102 gossip_debug(GOSSIP_SUPER_DEBUG,
103 "orangefs_alloc_inode: allocated %p\n",
104 &orangefs_inode->vfs_inode);
105 return &orangefs_inode->vfs_inode;
108 static void orangefs_free_inode(struct inode *inode)
110 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
111 struct orangefs_cached_xattr *cx;
112 struct hlist_node *tmp;
115 hash_for_each_safe(orangefs_inode->xattr_cache, i, tmp, cx, node) {
116 hlist_del(&cx->node);
120 kmem_cache_free(orangefs_inode_cache, orangefs_inode);
123 static void orangefs_destroy_inode(struct inode *inode)
125 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
127 gossip_debug(GOSSIP_SUPER_DEBUG,
128 "%s: deallocated %p destroying inode %pU\n",
129 __func__, orangefs_inode, get_khandle_from_ino(inode));
132 static int orangefs_write_inode(struct inode *inode,
133 struct writeback_control *wbc)
135 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_write_inode\n");
136 return orangefs_inode_setattr(inode);
140 * NOTE: information filled in here is typically reflected in the
141 * output of the system command 'df'
143 static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
146 struct orangefs_kernel_op_s *new_op = NULL;
148 struct super_block *sb = NULL;
152 gossip_debug(GOSSIP_SUPER_DEBUG,
153 "%s: called on sb %p (fs_id is %d)\n",
156 (int)(ORANGEFS_SB(sb)->fs_id));
158 new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
161 new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
163 if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
164 flags = ORANGEFS_OP_INTERRUPTIBLE;
166 ret = service_operation(new_op, "orangefs_statfs", flags);
168 if (new_op->downcall.status < 0)
171 gossip_debug(GOSSIP_SUPER_DEBUG,
172 "%s: got %ld blocks available | "
173 "%ld blocks total | %ld block size | "
174 "%ld files total | %ld files avail\n",
176 (long)new_op->downcall.resp.statfs.blocks_avail,
177 (long)new_op->downcall.resp.statfs.blocks_total,
178 (long)new_op->downcall.resp.statfs.block_size,
179 (long)new_op->downcall.resp.statfs.files_total,
180 (long)new_op->downcall.resp.statfs.files_avail);
182 buf->f_type = sb->s_magic;
183 buf->f_fsid.val[0] = ORANGEFS_SB(sb)->fs_id;
184 buf->f_fsid.val[1] = ORANGEFS_SB(sb)->id;
185 buf->f_bsize = new_op->downcall.resp.statfs.block_size;
186 buf->f_namelen = ORANGEFS_NAME_MAX;
188 buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
189 buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
190 buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
191 buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
192 buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
197 gossip_debug(GOSSIP_SUPER_DEBUG, "%s: returning %d\n", __func__, ret);
202 * Remount as initiated by VFS layer. We just need to reparse the mount
203 * options, no need to signal pvfs2-client-core about it.
205 static int orangefs_reconfigure(struct fs_context *fc)
207 struct super_block *sb = fc->root->d_sb;
208 struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
209 struct orangefs_sb_info_s *revised = fc->s_fs_info;
212 flags = orangefs_sb->flags;
213 flags &= ~(ORANGEFS_OPT_INTR | ORANGEFS_OPT_LOCAL_LOCK);
214 flags |= revised->flags;
215 WRITE_ONCE(orangefs_sb->flags, flags);
217 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_reconfigure: called\n");
222 * Remount as initiated by pvfs2-client-core on restart. This is used to
223 * repopulate mount information left from previous pvfs2-client-core.
225 * the idea here is that given a valid superblock, we're
226 * re-initializing the user space client with the initial mount
227 * information specified when the super block was first initialized.
228 * this is very different than the first initialization/creation of a
229 * superblock. we use the special service_priority_operation to make
230 * sure that the mount gets ahead of any other pending operation that
231 * is waiting for servicing. this means that the pvfs2-client won't
232 * fail to start several times for all other pending operations before
233 * the client regains all of the mount information from us.
234 * NOTE: this function assumes that the request_mutex is already acquired!
236 int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
238 struct orangefs_kernel_op_s *new_op;
241 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
243 new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
246 strscpy(new_op->upcall.req.fs_mount.orangefs_config_server,
247 orangefs_sb->devname);
249 gossip_debug(GOSSIP_SUPER_DEBUG,
250 "Attempting ORANGEFS Remount via host %s\n",
251 new_op->upcall.req.fs_mount.orangefs_config_server);
254 * we assume that the calling function has already acquired the
255 * request_mutex to prevent other operations from bypassing
258 ret = service_operation(new_op, "orangefs_remount",
259 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
260 gossip_debug(GOSSIP_SUPER_DEBUG,
261 "orangefs_remount: mount got return value of %d\n",
265 * store the id assigned to this sb -- it's just a
266 * short-lived mapping that the system interface uses
267 * to map this superblock to a particular mount entry
269 orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
270 orangefs_sb->mount_pending = 0;
275 if (orangefs_userspace_version >= 20906) {
276 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
279 new_op->upcall.req.features.features = 0;
280 ret = service_operation(new_op, "orangefs_features",
281 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
284 new_op->downcall.resp.features.features;
286 orangefs_features = 0;
289 orangefs_features = 0;
295 int fsid_key_table_initialize(void)
300 void fsid_key_table_finalize(void)
304 static const struct super_operations orangefs_s_ops = {
305 .alloc_inode = orangefs_alloc_inode,
306 .free_inode = orangefs_free_inode,
307 .destroy_inode = orangefs_destroy_inode,
308 .write_inode = orangefs_write_inode,
309 .drop_inode = inode_just_drop,
310 .statfs = orangefs_statfs,
311 .show_options = orangefs_show_options,
314 static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
319 struct orangefs_object_kref refn;
321 if (fh_len < 5 || fh_type > 2)
324 ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
325 refn.fs_id = (u32) fid->raw[4];
326 gossip_debug(GOSSIP_SUPER_DEBUG,
327 "fh_to_dentry: handle %pU, fs_id %d\n",
331 return d_obtain_alias(orangefs_iget(sb, &refn));
334 static int orangefs_encode_fh(struct inode *inode,
337 struct inode *parent)
339 int len = parent ? 10 : 5;
341 struct orangefs_object_kref refn;
343 if (*max_len < len) {
344 gossip_err("fh buffer is too small for encoding\n");
350 refn = ORANGEFS_I(inode)->refn;
351 ORANGEFS_khandle_to(&refn.khandle, fh, 16);
354 gossip_debug(GOSSIP_SUPER_DEBUG,
355 "Encoding fh: handle %pU, fsid %u\n",
361 refn = ORANGEFS_I(parent)->refn;
362 ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
366 gossip_debug(GOSSIP_SUPER_DEBUG,
367 "Encoding parent: handle %pU, fsid %u\n",
377 static const struct export_operations orangefs_export_ops = {
378 .encode_fh = orangefs_encode_fh,
379 .fh_to_dentry = orangefs_fh_to_dentry,
382 static int orangefs_unmount(int id, __s32 fs_id, const char *devname)
384 struct orangefs_kernel_op_s *op;
386 op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT);
389 op->upcall.req.fs_umount.id = id;
390 op->upcall.req.fs_umount.fs_id = fs_id;
391 strscpy(op->upcall.req.fs_umount.orangefs_config_server, devname);
392 r = service_operation(op, "orangefs_fs_umount", 0);
393 /* Not much to do about an error here. */
395 gossip_err("orangefs_unmount: service_operation %d\n", r);
400 static int orangefs_fill_sb(struct super_block *sb,
401 struct fs_context *fc,
402 struct orangefs_fs_mount_response *fs_mount)
406 struct dentry *root_dentry;
407 struct orangefs_object_kref root_object;
409 ORANGEFS_SB(sb)->sb = sb;
411 ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle;
412 ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id;
413 ORANGEFS_SB(sb)->id = fs_mount->id;
415 /* Hang the xattr handlers off the superblock */
416 sb->s_xattr = orangefs_xattr_handlers;
417 sb->s_magic = ORANGEFS_SUPER_MAGIC;
418 sb->s_op = &orangefs_s_ops;
419 set_default_d_op(sb, &orangefs_dentry_operations);
421 sb->s_blocksize = PAGE_SIZE;
422 sb->s_blocksize_bits = PAGE_SHIFT;
423 sb->s_maxbytes = MAX_LFS_FILESIZE;
425 ret = super_setup_bdi(sb);
429 root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
430 root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
431 gossip_debug(GOSSIP_SUPER_DEBUG,
432 "get inode %pU, fsid %d\n",
433 &root_object.khandle,
436 root = orangefs_iget(sb, &root_object);
438 return PTR_ERR(root);
440 gossip_debug(GOSSIP_SUPER_DEBUG,
441 "Allocated root inode [%p] with mode %x\n",
445 /* allocates and places root dentry in dcache */
446 root_dentry = d_make_root(root);
450 sb->s_export_op = &orangefs_export_ops;
451 sb->s_root = root_dentry;
455 static int orangefs_get_tree(struct fs_context *fc)
458 struct super_block *sb = ERR_PTR(-EINVAL);
459 struct orangefs_kernel_op_s *new_op;
462 return invalf(fc, "Device name not specified.\n");
464 gossip_debug(GOSSIP_SUPER_DEBUG,
465 "orangefs_mount: called with devname %s\n",
468 new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
472 strscpy(new_op->upcall.req.fs_mount.orangefs_config_server, fc->source);
474 gossip_debug(GOSSIP_SUPER_DEBUG,
475 "Attempting ORANGEFS Mount via host %s\n",
476 new_op->upcall.req.fs_mount.orangefs_config_server);
478 ret = service_operation(new_op, "orangefs_mount", 0);
479 gossip_debug(GOSSIP_SUPER_DEBUG,
480 "orangefs_mount: mount got return value of %d\n", ret);
484 if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
485 gossip_err("ERROR: Retrieved null fs_id\n");
490 sb = sget_fc(fc, NULL, set_anon_super_fc);
494 orangefs_unmount(new_op->downcall.resp.fs_mount.id,
495 new_op->downcall.resp.fs_mount.fs_id,
500 /* init our private orangefs sb info */
501 ret = orangefs_fill_sb(sb, fc, &new_op->downcall.resp.fs_mount);
507 * on successful mount, store the devname and data
510 strscpy(ORANGEFS_SB(sb)->devname, fc->source);
512 /* mount_pending must be cleared */
513 ORANGEFS_SB(sb)->mount_pending = 0;
516 * finally, add this sb to our list of known orangefs
519 gossip_debug(GOSSIP_SUPER_DEBUG,
520 "Adding SB %p to orangefs superblocks\n",
522 spin_lock(&orangefs_superblocks_lock);
523 list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
524 spin_unlock(&orangefs_superblocks_lock);
527 /* Must be removed from the list now. */
528 ORANGEFS_SB(sb)->no_list = 0;
530 if (orangefs_userspace_version >= 20906) {
531 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
534 new_op->upcall.req.features.features = 0;
535 ret = service_operation(new_op, "orangefs_features", 0);
536 orangefs_features = new_op->downcall.resp.features.features;
539 orangefs_features = 0;
542 fc->root = dget(sb->s_root);
546 /* Will call orangefs_kill_sb with sb not in list. */
547 ORANGEFS_SB(sb)->no_list = 1;
548 /* ORANGEFS_VFS_OP_FS_UMOUNT is done by orangefs_kill_sb. */
549 deactivate_locked_super(sb);
551 gossip_err("orangefs_mount: mount request failed with %d\n", ret);
552 if (ret == -EINVAL) {
553 gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
554 gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
562 static void orangefs_free_fc(struct fs_context *fc)
564 kfree(fc->s_fs_info);
567 static const struct fs_context_operations orangefs_context_ops = {
568 .free = orangefs_free_fc,
569 .parse_param = orangefs_parse_param,
570 .get_tree = orangefs_get_tree,
571 .reconfigure = orangefs_reconfigure,
575 * Set up the filesystem mount context.
577 int orangefs_init_fs_context(struct fs_context *fc)
579 struct orangefs_sb_info_s *osi;
581 osi = kzalloc_obj(struct orangefs_sb_info_s, GFP_KERNEL);
586 * Force any potential flags that might be set from the mount
587 * to zero, ie, initialize to unset.
589 fc->sb_flags_mask &= ~SB_POSIXACL;
590 osi->flags &= ~ORANGEFS_OPT_INTR;
591 osi->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
594 fc->ops = &orangefs_context_ops;
598 void orangefs_kill_sb(struct super_block *sb)
601 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
603 /* provided sb cleanup */
606 if (!ORANGEFS_SB(sb)) {
607 mutex_lock(&orangefs_request_mutex);
608 mutex_unlock(&orangefs_request_mutex);
612 * issue the unmount to userspace to tell it to remove the
613 * dynamic mount info it has for this superblock
615 r = orangefs_unmount(ORANGEFS_SB(sb)->id, ORANGEFS_SB(sb)->fs_id,
616 ORANGEFS_SB(sb)->devname);
618 ORANGEFS_SB(sb)->mount_pending = 1;
620 if (!ORANGEFS_SB(sb)->no_list) {
621 /* remove the sb from our list of orangefs specific sb's */
622 spin_lock(&orangefs_superblocks_lock);
623 /* not list_del_init */
624 __list_del_entry(&ORANGEFS_SB(sb)->list);
625 ORANGEFS_SB(sb)->list.prev = NULL;
626 spin_unlock(&orangefs_superblocks_lock);
630 * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
631 * gets completed before we free the dang thing.
633 mutex_lock(&orangefs_request_mutex);
634 mutex_unlock(&orangefs_request_mutex);
636 /* free the orangefs superblock private data */
637 kfree(ORANGEFS_SB(sb));
640 int orangefs_inode_cache_initialize(void)
642 orangefs_inode_cache = kmem_cache_create_usercopy(
643 "orangefs_inode_cache",
644 sizeof(struct orangefs_inode_s),
647 offsetof(struct orangefs_inode_s,
649 sizeof_field(struct orangefs_inode_s,
651 orangefs_inode_cache_ctor);
653 if (!orangefs_inode_cache) {
654 gossip_err("Cannot create orangefs_inode_cache\n");
660 int orangefs_inode_cache_finalize(void)
662 kmem_cache_destroy(orangefs_inode_cache);