xref: /openbmc/linux/fs/orangefs/super.c (revision ee1cd5048959de496cd005c50b137212a5b62062)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21182fca3SMike Marshall /*
31182fca3SMike Marshall  * (C) 2001 Clemson University and The University of Chicago
41182fca3SMike Marshall  *
51182fca3SMike Marshall  * See COPYING in top-level directory.
61182fca3SMike Marshall  */
71182fca3SMike Marshall 
81182fca3SMike Marshall #include "protocol.h"
9575e9461SMike Marshall #include "orangefs-kernel.h"
10575e9461SMike Marshall #include "orangefs-bufmap.h"
111182fca3SMike Marshall 
121182fca3SMike Marshall #include <linux/parser.h>
13fc2e2e9cSMartin Brandenburg #include <linux/hashtable.h>
14e41d12f5SChristoph Hellwig #include <linux/seq_file.h>
151182fca3SMike Marshall 
168bb8aefdSYi Liu /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
178bb8aefdSYi Liu static struct kmem_cache *orangefs_inode_cache;
181182fca3SMike Marshall 
198bb8aefdSYi Liu /* list for storing orangefs specific superblocks in use */
208bb8aefdSYi Liu LIST_HEAD(orangefs_superblocks);
211182fca3SMike Marshall 
228bb8aefdSYi Liu DEFINE_SPINLOCK(orangefs_superblocks_lock);
231182fca3SMike Marshall 
241182fca3SMike Marshall enum {
251182fca3SMike Marshall 	Opt_intr,
261182fca3SMike Marshall 	Opt_acl,
271182fca3SMike Marshall 	Opt_local_lock,
281182fca3SMike Marshall 
291182fca3SMike Marshall 	Opt_err
301182fca3SMike Marshall };
311182fca3SMike Marshall 
321182fca3SMike Marshall static const match_table_t tokens = {
331182fca3SMike Marshall 	{ Opt_acl,		"acl" },
341182fca3SMike Marshall 	{ Opt_intr,		"intr" },
351182fca3SMike Marshall 	{ Opt_local_lock,	"local_lock" },
361182fca3SMike Marshall 	{ Opt_err,	NULL }
371182fca3SMike Marshall };
381182fca3SMike Marshall 
39482664ddSMartin Brandenburg uint64_t orangefs_features;
401182fca3SMike Marshall 
orangefs_show_options(struct seq_file * m,struct dentry * root)414dfdb713SDavid Howells static int orangefs_show_options(struct seq_file *m, struct dentry *root)
424dfdb713SDavid Howells {
434dfdb713SDavid Howells 	struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(root->d_sb);
444dfdb713SDavid Howells 
451751e8a6SLinus Torvalds 	if (root->d_sb->s_flags & SB_POSIXACL)
464dfdb713SDavid Howells 		seq_puts(m, ",acl");
474dfdb713SDavid Howells 	if (orangefs_sb->flags & ORANGEFS_OPT_INTR)
484dfdb713SDavid Howells 		seq_puts(m, ",intr");
494dfdb713SDavid Howells 	if (orangefs_sb->flags & ORANGEFS_OPT_LOCAL_LOCK)
504dfdb713SDavid Howells 		seq_puts(m, ",local_lock");
514dfdb713SDavid Howells 	return 0;
524dfdb713SDavid Howells }
534dfdb713SDavid Howells 
parse_mount_options(struct super_block * sb,char * options,int silent)541182fca3SMike Marshall static int parse_mount_options(struct super_block *sb, char *options,
551182fca3SMike Marshall 		int silent)
561182fca3SMike Marshall {
578bb8aefdSYi Liu 	struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
581182fca3SMike Marshall 	substring_t args[MAX_OPT_ARGS];
591182fca3SMike Marshall 	char *p;
601182fca3SMike Marshall 
611182fca3SMike Marshall 	/*
621182fca3SMike Marshall 	 * Force any potential flags that might be set from the mount
631182fca3SMike Marshall 	 * to zero, ie, initialize to unset.
641182fca3SMike Marshall 	 */
651751e8a6SLinus Torvalds 	sb->s_flags &= ~SB_POSIXACL;
668bb8aefdSYi Liu 	orangefs_sb->flags &= ~ORANGEFS_OPT_INTR;
678bb8aefdSYi Liu 	orangefs_sb->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
681182fca3SMike Marshall 
691182fca3SMike Marshall 	while ((p = strsep(&options, ",")) != NULL) {
701182fca3SMike Marshall 		int token;
711182fca3SMike Marshall 
721182fca3SMike Marshall 		if (!*p)
731182fca3SMike Marshall 			continue;
741182fca3SMike Marshall 
751182fca3SMike Marshall 		token = match_token(p, tokens, args);
761182fca3SMike Marshall 		switch (token) {
771182fca3SMike Marshall 		case Opt_acl:
781751e8a6SLinus Torvalds 			sb->s_flags |= SB_POSIXACL;
791182fca3SMike Marshall 			break;
801182fca3SMike Marshall 		case Opt_intr:
818bb8aefdSYi Liu 			orangefs_sb->flags |= ORANGEFS_OPT_INTR;
821182fca3SMike Marshall 			break;
831182fca3SMike Marshall 		case Opt_local_lock:
848bb8aefdSYi Liu 			orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
851182fca3SMike Marshall 			break;
861182fca3SMike Marshall 		default:
871182fca3SMike Marshall 			goto fail;
881182fca3SMike Marshall 		}
891182fca3SMike Marshall 	}
901182fca3SMike Marshall 
911182fca3SMike Marshall 	return 0;
921182fca3SMike Marshall fail:
931182fca3SMike Marshall 	if (!silent)
941182fca3SMike Marshall 		gossip_err("Error: mount option [%s] is not supported.\n", p);
951182fca3SMike Marshall 	return -EINVAL;
961182fca3SMike Marshall }
971182fca3SMike Marshall 
orangefs_inode_cache_ctor(void * req)988bb8aefdSYi Liu static void orangefs_inode_cache_ctor(void *req)
991182fca3SMike Marshall {
1008bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode = req;
1011182fca3SMike Marshall 
1028bb8aefdSYi Liu 	inode_init_once(&orangefs_inode->vfs_inode);
1038bb8aefdSYi Liu 	init_rwsem(&orangefs_inode->xattr_sem);
1041182fca3SMike Marshall }
1051182fca3SMike Marshall 
orangefs_alloc_inode(struct super_block * sb)1068bb8aefdSYi Liu static struct inode *orangefs_alloc_inode(struct super_block *sb)
1071182fca3SMike Marshall {
1088bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode;
1091182fca3SMike Marshall 
110fd60b288SMuchun Song 	orangefs_inode = alloc_inode_sb(sb, orangefs_inode_cache, GFP_KERNEL);
11107a25853SMarkus Elfring 	if (!orangefs_inode)
1121182fca3SMike Marshall 		return NULL;
1131182fca3SMike Marshall 
1141182fca3SMike Marshall 	/*
1151182fca3SMike Marshall 	 * We want to clear everything except for rw_semaphore and the
1161182fca3SMike Marshall 	 * vfs_inode.
1171182fca3SMike Marshall 	 */
1188bb8aefdSYi Liu 	memset(&orangefs_inode->refn.khandle, 0, 16);
1198bb8aefdSYi Liu 	orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL;
1208bb8aefdSYi Liu 	orangefs_inode->last_failed_block_index_read = 0;
1218bb8aefdSYi Liu 	memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target));
1221182fca3SMike Marshall 
1231182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
1248bb8aefdSYi Liu 		     "orangefs_alloc_inode: allocated %p\n",
1258bb8aefdSYi Liu 		     &orangefs_inode->vfs_inode);
1268bb8aefdSYi Liu 	return &orangefs_inode->vfs_inode;
1271182fca3SMike Marshall }
1281182fca3SMike Marshall 
orangefs_free_inode(struct inode * inode)129f276ae0dSAl Viro static void orangefs_free_inode(struct inode *inode)
1300695d7dcSPeter Zijlstra {
1310695d7dcSPeter Zijlstra 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
132fc2e2e9cSMartin Brandenburg 	struct orangefs_cached_xattr *cx;
133fc2e2e9cSMartin Brandenburg 	struct hlist_node *tmp;
134fc2e2e9cSMartin Brandenburg 	int i;
135fc2e2e9cSMartin Brandenburg 
136fc2e2e9cSMartin Brandenburg 	hash_for_each_safe(orangefs_inode->xattr_cache, i, tmp, cx, node) {
137fc2e2e9cSMartin Brandenburg 		hlist_del(&cx->node);
138fc2e2e9cSMartin Brandenburg 		kfree(cx);
139fc2e2e9cSMartin Brandenburg 	}
140fc2e2e9cSMartin Brandenburg 
1410695d7dcSPeter Zijlstra 	kmem_cache_free(orangefs_inode_cache, orangefs_inode);
1420695d7dcSPeter Zijlstra }
1430695d7dcSPeter Zijlstra 
orangefs_destroy_inode(struct inode * inode)1448bb8aefdSYi Liu static void orangefs_destroy_inode(struct inode *inode)
1451182fca3SMike Marshall {
1468bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
1471182fca3SMike Marshall 
1481182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
1491182fca3SMike Marshall 			"%s: deallocated %p destroying inode %pU\n",
1508bb8aefdSYi Liu 			__func__, orangefs_inode, get_khandle_from_ino(inode));
1511182fca3SMike Marshall }
1521182fca3SMike Marshall 
orangefs_write_inode(struct inode * inode,struct writeback_control * wbc)153df2d7337SMartin Brandenburg static int orangefs_write_inode(struct inode *inode,
154df2d7337SMartin Brandenburg 				struct writeback_control *wbc)
155df2d7337SMartin Brandenburg {
156df2d7337SMartin Brandenburg 	gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_write_inode\n");
157afd9fb2aSMartin Brandenburg 	return orangefs_inode_setattr(inode);
158df2d7337SMartin Brandenburg }
159df2d7337SMartin Brandenburg 
1601182fca3SMike Marshall /*
1611182fca3SMike Marshall  * NOTE: information filled in here is typically reflected in the
1621182fca3SMike Marshall  * output of the system command 'df'
1631182fca3SMike Marshall */
orangefs_statfs(struct dentry * dentry,struct kstatfs * buf)1648bb8aefdSYi Liu static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
1651182fca3SMike Marshall {
1661182fca3SMike Marshall 	int ret = -ENOMEM;
1678bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op = NULL;
1681182fca3SMike Marshall 	int flags = 0;
1691182fca3SMike Marshall 	struct super_block *sb = NULL;
1701182fca3SMike Marshall 
1711182fca3SMike Marshall 	sb = dentry->d_sb;
1721182fca3SMike Marshall 
1731182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
17495f5f88fSMike Marshall 			"%s: called on sb %p (fs_id is %d)\n",
17595f5f88fSMike Marshall 			__func__,
1761182fca3SMike Marshall 			sb,
1778bb8aefdSYi Liu 			(int)(ORANGEFS_SB(sb)->fs_id));
1781182fca3SMike Marshall 
1798bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
1801182fca3SMike Marshall 	if (!new_op)
1811182fca3SMike Marshall 		return ret;
1828bb8aefdSYi Liu 	new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
1831182fca3SMike Marshall 
1848bb8aefdSYi Liu 	if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
1858bb8aefdSYi Liu 		flags = ORANGEFS_OP_INTERRUPTIBLE;
1861182fca3SMike Marshall 
1878bb8aefdSYi Liu 	ret = service_operation(new_op, "orangefs_statfs", flags);
1881182fca3SMike Marshall 
1891182fca3SMike Marshall 	if (new_op->downcall.status < 0)
1901182fca3SMike Marshall 		goto out_op_release;
1911182fca3SMike Marshall 
1921182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
193be57366eSMike Marshall 		     "%s: got %ld blocks available | "
194be57366eSMike Marshall 		     "%ld blocks total | %ld block size | "
195be57366eSMike Marshall 		     "%ld files total | %ld files avail\n",
196be57366eSMike Marshall 		     __func__,
1971182fca3SMike Marshall 		     (long)new_op->downcall.resp.statfs.blocks_avail,
1981182fca3SMike Marshall 		     (long)new_op->downcall.resp.statfs.blocks_total,
199be57366eSMike Marshall 		     (long)new_op->downcall.resp.statfs.block_size,
200be57366eSMike Marshall 		     (long)new_op->downcall.resp.statfs.files_total,
201be57366eSMike Marshall 		     (long)new_op->downcall.resp.statfs.files_avail);
2021182fca3SMike Marshall 
2031182fca3SMike Marshall 	buf->f_type = sb->s_magic;
204*6a3cacf6SMike Marshall 	buf->f_fsid.val[0] = ORANGEFS_SB(sb)->fs_id;
205*6a3cacf6SMike Marshall 	buf->f_fsid.val[1] = ORANGEFS_SB(sb)->id;
2061182fca3SMike Marshall 	buf->f_bsize = new_op->downcall.resp.statfs.block_size;
20747b4948fSMartin Brandenburg 	buf->f_namelen = ORANGEFS_NAME_MAX;
2081182fca3SMike Marshall 
2091182fca3SMike Marshall 	buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
2101182fca3SMike Marshall 	buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
2111182fca3SMike Marshall 	buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
2121182fca3SMike Marshall 	buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
2131182fca3SMike Marshall 	buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
2140fdec1b3SMike Marshall 	buf->f_frsize = 0;
2151182fca3SMike Marshall 
2161182fca3SMike Marshall out_op_release:
2171182fca3SMike Marshall 	op_release(new_op);
21895f5f88fSMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG, "%s: returning %d\n", __func__, ret);
2191182fca3SMike Marshall 	return ret;
2201182fca3SMike Marshall }
2211182fca3SMike Marshall 
2221182fca3SMike Marshall /*
2231182fca3SMike Marshall  * Remount as initiated by VFS layer.  We just need to reparse the mount
2241182fca3SMike Marshall  * options, no need to signal pvfs2-client-core about it.
2251182fca3SMike Marshall  */
orangefs_remount_fs(struct super_block * sb,int * flags,char * data)2268bb8aefdSYi Liu static int orangefs_remount_fs(struct super_block *sb, int *flags, char *data)
2271182fca3SMike Marshall {
2288bb8aefdSYi Liu 	gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount_fs: called\n");
2291182fca3SMike Marshall 	return parse_mount_options(sb, data, 1);
2301182fca3SMike Marshall }
2311182fca3SMike Marshall 
2321182fca3SMike Marshall /*
2331182fca3SMike Marshall  * Remount as initiated by pvfs2-client-core on restart.  This is used to
2341182fca3SMike Marshall  * repopulate mount information left from previous pvfs2-client-core.
2351182fca3SMike Marshall  *
2361182fca3SMike Marshall  * the idea here is that given a valid superblock, we're
2371182fca3SMike Marshall  * re-initializing the user space client with the initial mount
2381182fca3SMike Marshall  * information specified when the super block was first initialized.
2391182fca3SMike Marshall  * this is very different than the first initialization/creation of a
2401182fca3SMike Marshall  * superblock.  we use the special service_priority_operation to make
2411182fca3SMike Marshall  * sure that the mount gets ahead of any other pending operation that
2421182fca3SMike Marshall  * is waiting for servicing.  this means that the pvfs2-client won't
2431182fca3SMike Marshall  * fail to start several times for all other pending operations before
2441182fca3SMike Marshall  * the client regains all of the mount information from us.
2451182fca3SMike Marshall  * NOTE: this function assumes that the request_mutex is already acquired!
2461182fca3SMike Marshall  */
orangefs_remount(struct orangefs_sb_info_s * orangefs_sb)24745996492SAl Viro int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
2481182fca3SMike Marshall {
2498bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
2501182fca3SMike Marshall 	int ret = -EINVAL;
2511182fca3SMike Marshall 
2528bb8aefdSYi Liu 	gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
2531182fca3SMike Marshall 
2548bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
2551182fca3SMike Marshall 	if (!new_op)
2561182fca3SMike Marshall 		return -ENOMEM;
2578bb8aefdSYi Liu 	strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
25845996492SAl Viro 		orangefs_sb->devname,
2598bb8aefdSYi Liu 		ORANGEFS_MAX_SERVER_ADDR_LEN);
2601182fca3SMike Marshall 
2611182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
2628bb8aefdSYi Liu 		     "Attempting ORANGEFS Remount via host %s\n",
2638bb8aefdSYi Liu 		     new_op->upcall.req.fs_mount.orangefs_config_server);
2641182fca3SMike Marshall 
2651182fca3SMike Marshall 	/*
266adcf34a2SMike Marshall 	 * we assume that the calling function has already acquired the
2671182fca3SMike Marshall 	 * request_mutex to prevent other operations from bypassing
2681182fca3SMike Marshall 	 * this one
2691182fca3SMike Marshall 	 */
2708bb8aefdSYi Liu 	ret = service_operation(new_op, "orangefs_remount",
271adcf34a2SMike Marshall 		ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
2721182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
2738bb8aefdSYi Liu 		     "orangefs_remount: mount got return value of %d\n",
2741182fca3SMike Marshall 		     ret);
2751182fca3SMike Marshall 	if (ret == 0) {
2761182fca3SMike Marshall 		/*
2771182fca3SMike Marshall 		 * store the id assigned to this sb -- it's just a
2781182fca3SMike Marshall 		 * short-lived mapping that the system interface uses
2791182fca3SMike Marshall 		 * to map this superblock to a particular mount entry
2801182fca3SMike Marshall 		 */
28145996492SAl Viro 		orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
28245996492SAl Viro 		orangefs_sb->mount_pending = 0;
2831182fca3SMike Marshall 	}
2841182fca3SMike Marshall 
2851182fca3SMike Marshall 	op_release(new_op);
286482664ddSMartin Brandenburg 
287f60fbdbfSMike Marshall 	if (orangefs_userspace_version >= 20906) {
288482664ddSMartin Brandenburg 		new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
289482664ddSMartin Brandenburg 		if (!new_op)
290482664ddSMartin Brandenburg 			return -ENOMEM;
291482664ddSMartin Brandenburg 		new_op->upcall.req.features.features = 0;
292cefdc26eSMartin Brandenburg 		ret = service_operation(new_op, "orangefs_features",
293cefdc26eSMartin Brandenburg 		    ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
294cefdc26eSMartin Brandenburg 		if (!ret)
295cefdc26eSMartin Brandenburg 			orangefs_features =
296cefdc26eSMartin Brandenburg 			    new_op->downcall.resp.features.features;
297cefdc26eSMartin Brandenburg 		else
298cefdc26eSMartin Brandenburg 			orangefs_features = 0;
299482664ddSMartin Brandenburg 		op_release(new_op);
300482664ddSMartin Brandenburg 	} else {
301482664ddSMartin Brandenburg 		orangefs_features = 0;
302482664ddSMartin Brandenburg 	}
303482664ddSMartin Brandenburg 
3041182fca3SMike Marshall 	return ret;
3051182fca3SMike Marshall }
3061182fca3SMike Marshall 
fsid_key_table_initialize(void)3071182fca3SMike Marshall int fsid_key_table_initialize(void)
3081182fca3SMike Marshall {
3091182fca3SMike Marshall 	return 0;
3101182fca3SMike Marshall }
3111182fca3SMike Marshall 
fsid_key_table_finalize(void)3121182fca3SMike Marshall void fsid_key_table_finalize(void)
3131182fca3SMike Marshall {
3141182fca3SMike Marshall }
3151182fca3SMike Marshall 
3168bb8aefdSYi Liu static const struct super_operations orangefs_s_ops = {
3178bb8aefdSYi Liu 	.alloc_inode = orangefs_alloc_inode,
318f276ae0dSAl Viro 	.free_inode = orangefs_free_inode,
3198bb8aefdSYi Liu 	.destroy_inode = orangefs_destroy_inode,
320df2d7337SMartin Brandenburg 	.write_inode = orangefs_write_inode,
3211182fca3SMike Marshall 	.drop_inode = generic_delete_inode,
3228bb8aefdSYi Liu 	.statfs = orangefs_statfs,
3238bb8aefdSYi Liu 	.remount_fs = orangefs_remount_fs,
3244dfdb713SDavid Howells 	.show_options = orangefs_show_options,
3251182fca3SMike Marshall };
3261182fca3SMike Marshall 
orangefs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)3278bb8aefdSYi Liu static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
3281182fca3SMike Marshall 				  struct fid *fid,
3291182fca3SMike Marshall 				  int fh_len,
3301182fca3SMike Marshall 				  int fh_type)
3311182fca3SMike Marshall {
3328bb8aefdSYi Liu 	struct orangefs_object_kref refn;
3331182fca3SMike Marshall 
3341182fca3SMike Marshall 	if (fh_len < 5 || fh_type > 2)
3351182fca3SMike Marshall 		return NULL;
3361182fca3SMike Marshall 
3378bb8aefdSYi Liu 	ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
3381182fca3SMike Marshall 	refn.fs_id = (u32) fid->raw[4];
3391182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
3401182fca3SMike Marshall 		     "fh_to_dentry: handle %pU, fs_id %d\n",
3411182fca3SMike Marshall 		     &refn.khandle,
3421182fca3SMike Marshall 		     refn.fs_id);
3431182fca3SMike Marshall 
3448bb8aefdSYi Liu 	return d_obtain_alias(orangefs_iget(sb, &refn));
3451182fca3SMike Marshall }
3461182fca3SMike Marshall 
orangefs_encode_fh(struct inode * inode,__u32 * fh,int * max_len,struct inode * parent)3478bb8aefdSYi Liu static int orangefs_encode_fh(struct inode *inode,
3481182fca3SMike Marshall 		    __u32 *fh,
3491182fca3SMike Marshall 		    int *max_len,
3501182fca3SMike Marshall 		    struct inode *parent)
3511182fca3SMike Marshall {
3521182fca3SMike Marshall 	int len = parent ? 10 : 5;
3531182fca3SMike Marshall 	int type = 1;
3548bb8aefdSYi Liu 	struct orangefs_object_kref refn;
3551182fca3SMike Marshall 
3561182fca3SMike Marshall 	if (*max_len < len) {
35779d7cd61SMartin Brandenburg 		gossip_err("fh buffer is too small for encoding\n");
3581182fca3SMike Marshall 		*max_len = len;
3591182fca3SMike Marshall 		type = 255;
3601182fca3SMike Marshall 		goto out;
3611182fca3SMike Marshall 	}
3621182fca3SMike Marshall 
3638bb8aefdSYi Liu 	refn = ORANGEFS_I(inode)->refn;
3648bb8aefdSYi Liu 	ORANGEFS_khandle_to(&refn.khandle, fh, 16);
3651182fca3SMike Marshall 	fh[4] = refn.fs_id;
3661182fca3SMike Marshall 
3671182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
3681182fca3SMike Marshall 		     "Encoding fh: handle %pU, fsid %u\n",
3691182fca3SMike Marshall 		     &refn.khandle,
3701182fca3SMike Marshall 		     refn.fs_id);
3711182fca3SMike Marshall 
3721182fca3SMike Marshall 
3731182fca3SMike Marshall 	if (parent) {
3748bb8aefdSYi Liu 		refn = ORANGEFS_I(parent)->refn;
3758bb8aefdSYi Liu 		ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
3761182fca3SMike Marshall 		fh[9] = refn.fs_id;
3771182fca3SMike Marshall 
3781182fca3SMike Marshall 		type = 2;
3791182fca3SMike Marshall 		gossip_debug(GOSSIP_SUPER_DEBUG,
3801182fca3SMike Marshall 			     "Encoding parent: handle %pU, fsid %u\n",
3811182fca3SMike Marshall 			     &refn.khandle,
3821182fca3SMike Marshall 			     refn.fs_id);
3831182fca3SMike Marshall 	}
3841182fca3SMike Marshall 	*max_len = len;
3851182fca3SMike Marshall 
3861182fca3SMike Marshall out:
3871182fca3SMike Marshall 	return type;
3881182fca3SMike Marshall }
3891182fca3SMike Marshall 
390acaca36dSJulia Lawall static const struct export_operations orangefs_export_ops = {
3918bb8aefdSYi Liu 	.encode_fh = orangefs_encode_fh,
3928bb8aefdSYi Liu 	.fh_to_dentry = orangefs_fh_to_dentry,
3931182fca3SMike Marshall };
3941182fca3SMike Marshall 
orangefs_unmount(int id,__s32 fs_id,const char * devname)3959d286b0dSMartin Brandenburg static int orangefs_unmount(int id, __s32 fs_id, const char *devname)
3969d286b0dSMartin Brandenburg {
3979d286b0dSMartin Brandenburg 	struct orangefs_kernel_op_s *op;
3989d286b0dSMartin Brandenburg 	int r;
3999d286b0dSMartin Brandenburg 	op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT);
4009d286b0dSMartin Brandenburg 	if (!op)
4019d286b0dSMartin Brandenburg 		return -ENOMEM;
4029d286b0dSMartin Brandenburg 	op->upcall.req.fs_umount.id = id;
4039d286b0dSMartin Brandenburg 	op->upcall.req.fs_umount.fs_id = fs_id;
4049d286b0dSMartin Brandenburg 	strncpy(op->upcall.req.fs_umount.orangefs_config_server,
4056bdfb48dSXiongfeng Wang 	    devname, ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
4069d286b0dSMartin Brandenburg 	r = service_operation(op, "orangefs_fs_umount", 0);
4079d286b0dSMartin Brandenburg 	/* Not much to do about an error here. */
4089d286b0dSMartin Brandenburg 	if (r)
4099d286b0dSMartin Brandenburg 		gossip_err("orangefs_unmount: service_operation %d\n", r);
4109d286b0dSMartin Brandenburg 	op_release(op);
4119d286b0dSMartin Brandenburg 	return r;
4129d286b0dSMartin Brandenburg }
4139d286b0dSMartin Brandenburg 
orangefs_fill_sb(struct super_block * sb,struct orangefs_fs_mount_response * fs_mount,void * data,int silent)4148bb8aefdSYi Liu static int orangefs_fill_sb(struct super_block *sb,
4158bb8aefdSYi Liu 		struct orangefs_fs_mount_response *fs_mount,
4165c0dbbc6SAl Viro 		void *data, int silent)
4171182fca3SMike Marshall {
418f2d34c73SMartin Brandenburg 	int ret;
419f2d34c73SMartin Brandenburg 	struct inode *root;
420f2d34c73SMartin Brandenburg 	struct dentry *root_dentry;
4218bb8aefdSYi Liu 	struct orangefs_object_kref root_object;
4221182fca3SMike Marshall 
4238bb8aefdSYi Liu 	ORANGEFS_SB(sb)->sb = sb;
4241182fca3SMike Marshall 
4258bb8aefdSYi Liu 	ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle;
4268bb8aefdSYi Liu 	ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id;
4278bb8aefdSYi Liu 	ORANGEFS_SB(sb)->id = fs_mount->id;
4281182fca3SMike Marshall 
4295c0dbbc6SAl Viro 	if (data) {
4305c0dbbc6SAl Viro 		ret = parse_mount_options(sb, data, silent);
4311182fca3SMike Marshall 		if (ret)
4321182fca3SMike Marshall 			return ret;
4331182fca3SMike Marshall 	}
4341182fca3SMike Marshall 
4351182fca3SMike Marshall 	/* Hang the xattr handlers off the superblock */
4368bb8aefdSYi Liu 	sb->s_xattr = orangefs_xattr_handlers;
4378bb8aefdSYi Liu 	sb->s_magic = ORANGEFS_SUPER_MAGIC;
4388bb8aefdSYi Liu 	sb->s_op = &orangefs_s_ops;
4398bb8aefdSYi Liu 	sb->s_d_op = &orangefs_dentry_operations;
4401182fca3SMike Marshall 
4419f8fd53cSMartin Brandenburg 	sb->s_blocksize = PAGE_SIZE;
4429f8fd53cSMartin Brandenburg 	sb->s_blocksize_bits = PAGE_SHIFT;
4431182fca3SMike Marshall 	sb->s_maxbytes = MAX_LFS_FILESIZE;
4441182fca3SMike Marshall 
445f2d34c73SMartin Brandenburg 	ret = super_setup_bdi(sb);
446f2d34c73SMartin Brandenburg 	if (ret)
447f2d34c73SMartin Brandenburg 		return ret;
448f2d34c73SMartin Brandenburg 
4498bb8aefdSYi Liu 	root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
4508bb8aefdSYi Liu 	root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
4511182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
4521182fca3SMike Marshall 		     "get inode %pU, fsid %d\n",
4531182fca3SMike Marshall 		     &root_object.khandle,
4541182fca3SMike Marshall 		     root_object.fs_id);
4551182fca3SMike Marshall 
4568bb8aefdSYi Liu 	root = orangefs_iget(sb, &root_object);
4571182fca3SMike Marshall 	if (IS_ERR(root))
4581182fca3SMike Marshall 		return PTR_ERR(root);
4591182fca3SMike Marshall 
4601182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
4611182fca3SMike Marshall 		     "Allocated root inode [%p] with mode %x\n",
4621182fca3SMike Marshall 		     root,
4631182fca3SMike Marshall 		     root->i_mode);
4641182fca3SMike Marshall 
4651182fca3SMike Marshall 	/* allocates and places root dentry in dcache */
4661182fca3SMike Marshall 	root_dentry = d_make_root(root);
467b05a7851SAl Viro 	if (!root_dentry)
4681182fca3SMike Marshall 		return -ENOMEM;
4691182fca3SMike Marshall 
4708bb8aefdSYi Liu 	sb->s_export_op = &orangefs_export_ops;
4711182fca3SMike Marshall 	sb->s_root = root_dentry;
4721182fca3SMike Marshall 	return 0;
4731182fca3SMike Marshall }
4741182fca3SMike Marshall 
orangefs_mount(struct file_system_type * fst,int flags,const char * devname,void * data)4758bb8aefdSYi Liu struct dentry *orangefs_mount(struct file_system_type *fst,
4761182fca3SMike Marshall 			   int flags,
4771182fca3SMike Marshall 			   const char *devname,
4781182fca3SMike Marshall 			   void *data)
4791182fca3SMike Marshall {
480507874c0SColin Ian King 	int ret;
4811182fca3SMike Marshall 	struct super_block *sb = ERR_PTR(-EINVAL);
4828bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
4831be21f86SMike Marshall 	struct dentry *d = ERR_PTR(-EINVAL);
4841182fca3SMike Marshall 
4851182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
4868bb8aefdSYi Liu 		     "orangefs_mount: called with devname %s\n",
4871182fca3SMike Marshall 		     devname);
4881182fca3SMike Marshall 
4891182fca3SMike Marshall 	if (!devname) {
4901182fca3SMike Marshall 		gossip_err("ERROR: device name not specified.\n");
4911182fca3SMike Marshall 		return ERR_PTR(-EINVAL);
4921182fca3SMike Marshall 	}
4931182fca3SMike Marshall 
4948bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
4951182fca3SMike Marshall 	if (!new_op)
4961182fca3SMike Marshall 		return ERR_PTR(-ENOMEM);
4971182fca3SMike Marshall 
4988bb8aefdSYi Liu 	strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
4991182fca3SMike Marshall 		devname,
5006bdfb48dSXiongfeng Wang 		ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
5011182fca3SMike Marshall 
5021182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
5038bb8aefdSYi Liu 		     "Attempting ORANGEFS Mount via host %s\n",
5048bb8aefdSYi Liu 		     new_op->upcall.req.fs_mount.orangefs_config_server);
5051182fca3SMike Marshall 
5068bb8aefdSYi Liu 	ret = service_operation(new_op, "orangefs_mount", 0);
5071182fca3SMike Marshall 	gossip_debug(GOSSIP_SUPER_DEBUG,
5088bb8aefdSYi Liu 		     "orangefs_mount: mount got return value of %d\n", ret);
5091182fca3SMike Marshall 	if (ret)
5101182fca3SMike Marshall 		goto free_op;
5111182fca3SMike Marshall 
5128bb8aefdSYi Liu 	if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
5131182fca3SMike Marshall 		gossip_err("ERROR: Retrieved null fs_id\n");
5141182fca3SMike Marshall 		ret = -EINVAL;
5151182fca3SMike Marshall 		goto free_op;
5161182fca3SMike Marshall 	}
5171182fca3SMike Marshall 
5181be21f86SMike Marshall 	sb = sget(fst, NULL, set_anon_super, flags, NULL);
5191be21f86SMike Marshall 
5201be21f86SMike Marshall 	if (IS_ERR(sb)) {
5211be21f86SMike Marshall 		d = ERR_CAST(sb);
5229d286b0dSMartin Brandenburg 		orangefs_unmount(new_op->downcall.resp.fs_mount.id,
5239d286b0dSMartin Brandenburg 		    new_op->downcall.resp.fs_mount.fs_id, devname);
5241182fca3SMike Marshall 		goto free_op;
5251182fca3SMike Marshall 	}
5261182fca3SMike Marshall 
527f2d34c73SMartin Brandenburg 	/* alloc and init our private orangefs sb info */
528f2d34c73SMartin Brandenburg 	sb->s_fs_info = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL);
529f2d34c73SMartin Brandenburg 	if (!ORANGEFS_SB(sb)) {
530f2d34c73SMartin Brandenburg 		d = ERR_PTR(-ENOMEM);
531214a6c4aSMike Marshall 		goto free_op;
532f2d34c73SMartin Brandenburg 	}
533f2d34c73SMartin Brandenburg 
5348bb8aefdSYi Liu 	ret = orangefs_fill_sb(sb,
5355c0dbbc6SAl Viro 	      &new_op->downcall.resp.fs_mount, data,
5361751e8a6SLinus Torvalds 	      flags & SB_SILENT ? 1 : 0);
5371be21f86SMike Marshall 
5381be21f86SMike Marshall 	if (ret) {
5391be21f86SMike Marshall 		d = ERR_PTR(ret);
5401ec1688cSMartin Brandenburg 		goto free_sb_and_op;
5411be21f86SMike Marshall 	}
5421182fca3SMike Marshall 
5431182fca3SMike Marshall 	/*
5441182fca3SMike Marshall 	 * on successful mount, store the devname and data
5451182fca3SMike Marshall 	 * used
5461182fca3SMike Marshall 	 */
5478bb8aefdSYi Liu 	strncpy(ORANGEFS_SB(sb)->devname,
5481182fca3SMike Marshall 		devname,
5496bdfb48dSXiongfeng Wang 		ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
5501182fca3SMike Marshall 
5511182fca3SMike Marshall 	/* mount_pending must be cleared */
5528bb8aefdSYi Liu 	ORANGEFS_SB(sb)->mount_pending = 0;
5531182fca3SMike Marshall 
5541182fca3SMike Marshall 	/*
5558bb8aefdSYi Liu 	 * finally, add this sb to our list of known orangefs
5561182fca3SMike Marshall 	 * sb's
5571182fca3SMike Marshall 	 */
55845996492SAl Viro 	gossip_debug(GOSSIP_SUPER_DEBUG,
55945996492SAl Viro 		     "Adding SB %p to orangefs superblocks\n",
56045996492SAl Viro 		     ORANGEFS_SB(sb));
56145996492SAl Viro 	spin_lock(&orangefs_superblocks_lock);
56245996492SAl Viro 	list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
56345996492SAl Viro 	spin_unlock(&orangefs_superblocks_lock);
5641182fca3SMike Marshall 	op_release(new_op);
565482664ddSMartin Brandenburg 
5661ec1688cSMartin Brandenburg 	/* Must be removed from the list now. */
5671ec1688cSMartin Brandenburg 	ORANGEFS_SB(sb)->no_list = 0;
5681ec1688cSMartin Brandenburg 
569f60fbdbfSMike Marshall 	if (orangefs_userspace_version >= 20906) {
570482664ddSMartin Brandenburg 		new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
571482664ddSMartin Brandenburg 		if (!new_op)
572482664ddSMartin Brandenburg 			return ERR_PTR(-ENOMEM);
573482664ddSMartin Brandenburg 		new_op->upcall.req.features.features = 0;
574482664ddSMartin Brandenburg 		ret = service_operation(new_op, "orangefs_features", 0);
575482664ddSMartin Brandenburg 		orangefs_features = new_op->downcall.resp.features.features;
576482664ddSMartin Brandenburg 		op_release(new_op);
577482664ddSMartin Brandenburg 	} else {
578482664ddSMartin Brandenburg 		orangefs_features = 0;
579482664ddSMartin Brandenburg 	}
580482664ddSMartin Brandenburg 
5811be21f86SMike Marshall 	return dget(sb->s_root);
5821182fca3SMike Marshall 
5831ec1688cSMartin Brandenburg free_sb_and_op:
5841ec1688cSMartin Brandenburg 	/* Will call orangefs_kill_sb with sb not in list. */
5851ec1688cSMartin Brandenburg 	ORANGEFS_SB(sb)->no_list = 1;
5869d286b0dSMartin Brandenburg 	/* ORANGEFS_VFS_OP_FS_UMOUNT is done by orangefs_kill_sb. */
5871ec1688cSMartin Brandenburg 	deactivate_locked_super(sb);
5881182fca3SMike Marshall free_op:
5898bb8aefdSYi Liu 	gossip_err("orangefs_mount: mount request failed with %d\n", ret);
5901182fca3SMike Marshall 	if (ret == -EINVAL) {
5918bb8aefdSYi Liu 		gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
5921182fca3SMike Marshall 		gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
5931182fca3SMike Marshall 	}
5941182fca3SMike Marshall 
5951182fca3SMike Marshall 	op_release(new_op);
5961182fca3SMike Marshall 
5971be21f86SMike Marshall 	return d;
5981182fca3SMike Marshall }
5991182fca3SMike Marshall 
orangefs_kill_sb(struct super_block * sb)6008bb8aefdSYi Liu void orangefs_kill_sb(struct super_block *sb)
6011182fca3SMike Marshall {
6029d286b0dSMartin Brandenburg 	int r;
6038bb8aefdSYi Liu 	gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
6041182fca3SMike Marshall 
605524b1d30SAl Viro 	/* provided sb cleanup */
606524b1d30SAl Viro 	kill_anon_super(sb);
607524b1d30SAl Viro 
60865903842SAl Viro 	if (!ORANGEFS_SB(sb)) {
60965903842SAl Viro 		mutex_lock(&orangefs_request_mutex);
61065903842SAl Viro 		mutex_unlock(&orangefs_request_mutex);
61165903842SAl Viro 		return;
61265903842SAl Viro 	}
6131182fca3SMike Marshall 	/*
6141182fca3SMike Marshall 	 * issue the unmount to userspace to tell it to remove the
6151182fca3SMike Marshall 	 * dynamic mount info it has for this superblock
6161182fca3SMike Marshall 	 */
6179d286b0dSMartin Brandenburg 	r = orangefs_unmount(ORANGEFS_SB(sb)->id, ORANGEFS_SB(sb)->fs_id,
6189d286b0dSMartin Brandenburg 	    ORANGEFS_SB(sb)->devname);
6199d286b0dSMartin Brandenburg 	if (!r)
6209d286b0dSMartin Brandenburg 		ORANGEFS_SB(sb)->mount_pending = 1;
6211182fca3SMike Marshall 
6221ec1688cSMartin Brandenburg 	if (!ORANGEFS_SB(sb)->no_list) {
6238bb8aefdSYi Liu 		/* remove the sb from our list of orangefs specific sb's */
62445996492SAl Viro 		spin_lock(&orangefs_superblocks_lock);
6251ec1688cSMartin Brandenburg 		/* not list_del_init */
6261ec1688cSMartin Brandenburg 		__list_del_entry(&ORANGEFS_SB(sb)->list);
62745996492SAl Viro 		ORANGEFS_SB(sb)->list.prev = NULL;
62845996492SAl Viro 		spin_unlock(&orangefs_superblocks_lock);
6291ec1688cSMartin Brandenburg 	}
63045996492SAl Viro 
63145996492SAl Viro 	/*
63245996492SAl Viro 	 * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
63345996492SAl Viro 	 * gets completed before we free the dang thing.
63445996492SAl Viro 	 */
6351d503617SMartin Brandenburg 	mutex_lock(&orangefs_request_mutex);
6361d503617SMartin Brandenburg 	mutex_unlock(&orangefs_request_mutex);
6371182fca3SMike Marshall 
6388bb8aefdSYi Liu 	/* free the orangefs superblock private data */
6398bb8aefdSYi Liu 	kfree(ORANGEFS_SB(sb));
6401182fca3SMike Marshall }
6411182fca3SMike Marshall 
orangefs_inode_cache_initialize(void)6428bb8aefdSYi Liu int orangefs_inode_cache_initialize(void)
6431182fca3SMike Marshall {
6446b330623SDavid Windsor 	orangefs_inode_cache = kmem_cache_create_usercopy(
6456b330623SDavid Windsor 					"orangefs_inode_cache",
6468bb8aefdSYi Liu 					sizeof(struct orangefs_inode_s),
6471182fca3SMike Marshall 					0,
6488bb8aefdSYi Liu 					ORANGEFS_CACHE_CREATE_FLAGS,
6496b330623SDavid Windsor 					offsetof(struct orangefs_inode_s,
6506b330623SDavid Windsor 						link_target),
6516b330623SDavid Windsor 					sizeof_field(struct orangefs_inode_s,
6526b330623SDavid Windsor 						link_target),
6538bb8aefdSYi Liu 					orangefs_inode_cache_ctor);
6541182fca3SMike Marshall 
6558bb8aefdSYi Liu 	if (!orangefs_inode_cache) {
6568bb8aefdSYi Liu 		gossip_err("Cannot create orangefs_inode_cache\n");
6571182fca3SMike Marshall 		return -ENOMEM;
6581182fca3SMike Marshall 	}
6591182fca3SMike Marshall 	return 0;
6601182fca3SMike Marshall }
6611182fca3SMike Marshall 
orangefs_inode_cache_finalize(void)6628bb8aefdSYi Liu int orangefs_inode_cache_finalize(void)
6631182fca3SMike Marshall {
6648bb8aefdSYi Liu 	kmem_cache_destroy(orangefs_inode_cache);
6651182fca3SMike Marshall 	return 0;
6661182fca3SMike Marshall }
667