blob: dfaee90d30bdedf98ccfb6cc1df1aafa6ba85eab [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mike Marshall1182fca2015-07-17 10:38:15 -04002/*
3 * (C) 2001 Clemson University and The University of Chicago
4 *
5 * See COPYING in top-level directory.
6 */
7
8#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -05009#include "orangefs-kernel.h"
10#include "orangefs-bufmap.h"
Mike Marshall1182fca2015-07-17 10:38:15 -040011
12#include <linux/parser.h>
13
Yi Liu8bb8aef2015-11-24 15:12:14 -050014/* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
15static struct kmem_cache *orangefs_inode_cache;
Mike Marshall1182fca2015-07-17 10:38:15 -040016
Yi Liu8bb8aef2015-11-24 15:12:14 -050017/* list for storing orangefs specific superblocks in use */
18LIST_HEAD(orangefs_superblocks);
Mike Marshall1182fca2015-07-17 10:38:15 -040019
Yi Liu8bb8aef2015-11-24 15:12:14 -050020DEFINE_SPINLOCK(orangefs_superblocks_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -040021
22enum {
23 Opt_intr,
24 Opt_acl,
25 Opt_local_lock,
26
27 Opt_err
28};
29
30static const match_table_t tokens = {
31 { Opt_acl, "acl" },
32 { Opt_intr, "intr" },
33 { Opt_local_lock, "local_lock" },
34 { Opt_err, NULL }
35};
36
Martin Brandenburg482664d2016-08-12 12:02:31 -040037uint64_t orangefs_features;
Mike Marshall1182fca2015-07-17 10:38:15 -040038
David Howells4dfdb712017-07-05 16:25:45 +010039static int orangefs_show_options(struct seq_file *m, struct dentry *root)
40{
41 struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(root->d_sb);
42
Linus Torvalds1751e8a2017-11-27 13:05:09 -080043 if (root->d_sb->s_flags & SB_POSIXACL)
David Howells4dfdb712017-07-05 16:25:45 +010044 seq_puts(m, ",acl");
45 if (orangefs_sb->flags & ORANGEFS_OPT_INTR)
46 seq_puts(m, ",intr");
47 if (orangefs_sb->flags & ORANGEFS_OPT_LOCAL_LOCK)
48 seq_puts(m, ",local_lock");
49 return 0;
50}
51
Mike Marshall1182fca2015-07-17 10:38:15 -040052static int parse_mount_options(struct super_block *sb, char *options,
53 int silent)
54{
Yi Liu8bb8aef2015-11-24 15:12:14 -050055 struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
Mike Marshall1182fca2015-07-17 10:38:15 -040056 substring_t args[MAX_OPT_ARGS];
57 char *p;
58
59 /*
60 * Force any potential flags that might be set from the mount
61 * to zero, ie, initialize to unset.
62 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -080063 sb->s_flags &= ~SB_POSIXACL;
Yi Liu8bb8aef2015-11-24 15:12:14 -050064 orangefs_sb->flags &= ~ORANGEFS_OPT_INTR;
65 orangefs_sb->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
Mike Marshall1182fca2015-07-17 10:38:15 -040066
67 while ((p = strsep(&options, ",")) != NULL) {
68 int token;
69
70 if (!*p)
71 continue;
72
73 token = match_token(p, tokens, args);
74 switch (token) {
75 case Opt_acl:
Linus Torvalds1751e8a2017-11-27 13:05:09 -080076 sb->s_flags |= SB_POSIXACL;
Mike Marshall1182fca2015-07-17 10:38:15 -040077 break;
78 case Opt_intr:
Yi Liu8bb8aef2015-11-24 15:12:14 -050079 orangefs_sb->flags |= ORANGEFS_OPT_INTR;
Mike Marshall1182fca2015-07-17 10:38:15 -040080 break;
81 case Opt_local_lock:
Yi Liu8bb8aef2015-11-24 15:12:14 -050082 orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
Mike Marshall1182fca2015-07-17 10:38:15 -040083 break;
84 default:
85 goto fail;
86 }
87 }
88
89 return 0;
90fail:
91 if (!silent)
92 gossip_err("Error: mount option [%s] is not supported.\n", p);
93 return -EINVAL;
94}
95
Yi Liu8bb8aef2015-11-24 15:12:14 -050096static void orangefs_inode_cache_ctor(void *req)
Mike Marshall1182fca2015-07-17 10:38:15 -040097{
Yi Liu8bb8aef2015-11-24 15:12:14 -050098 struct orangefs_inode_s *orangefs_inode = req;
Mike Marshall1182fca2015-07-17 10:38:15 -040099
Yi Liu8bb8aef2015-11-24 15:12:14 -0500100 inode_init_once(&orangefs_inode->vfs_inode);
101 init_rwsem(&orangefs_inode->xattr_sem);
Mike Marshall1182fca2015-07-17 10:38:15 -0400102}
103
Yi Liu8bb8aef2015-11-24 15:12:14 -0500104static struct inode *orangefs_alloc_inode(struct super_block *sb)
Mike Marshall1182fca2015-07-17 10:38:15 -0400105{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500106 struct orangefs_inode_s *orangefs_inode;
Mike Marshall1182fca2015-07-17 10:38:15 -0400107
Mike Marshall2d4cae02016-02-04 13:48:16 -0500108 orangefs_inode = kmem_cache_alloc(orangefs_inode_cache, GFP_KERNEL);
Markus Elfring07a25852017-08-17 21:00:07 +0200109 if (!orangefs_inode)
Mike Marshall1182fca2015-07-17 10:38:15 -0400110 return NULL;
Mike Marshall1182fca2015-07-17 10:38:15 -0400111
112 /*
113 * We want to clear everything except for rw_semaphore and the
114 * vfs_inode.
115 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500116 memset(&orangefs_inode->refn.khandle, 0, 16);
117 orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL;
118 orangefs_inode->last_failed_block_index_read = 0;
119 memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target));
Mike Marshall1182fca2015-07-17 10:38:15 -0400120
121 gossip_debug(GOSSIP_SUPER_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500122 "orangefs_alloc_inode: allocated %p\n",
123 &orangefs_inode->vfs_inode);
124 return &orangefs_inode->vfs_inode;
Mike Marshall1182fca2015-07-17 10:38:15 -0400125}
126
Peter Zijlstra0695d7d2017-02-24 16:43:36 +0100127static void orangefs_i_callback(struct rcu_head *head)
128{
129 struct inode *inode = container_of(head, struct inode, i_rcu);
130 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
131 kmem_cache_free(orangefs_inode_cache, orangefs_inode);
132}
133
Yi Liu8bb8aef2015-11-24 15:12:14 -0500134static void orangefs_destroy_inode(struct inode *inode)
Mike Marshall1182fca2015-07-17 10:38:15 -0400135{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500136 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
Mike Marshall1182fca2015-07-17 10:38:15 -0400137
138 gossip_debug(GOSSIP_SUPER_DEBUG,
139 "%s: deallocated %p destroying inode %pU\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500140 __func__, orangefs_inode, get_khandle_from_ino(inode));
Mike Marshall1182fca2015-07-17 10:38:15 -0400141
Peter Zijlstra0695d7d2017-02-24 16:43:36 +0100142 call_rcu(&inode->i_rcu, orangefs_i_callback);
Mike Marshall1182fca2015-07-17 10:38:15 -0400143}
144
145/*
146 * NOTE: information filled in here is typically reflected in the
147 * output of the system command 'df'
148*/
Yi Liu8bb8aef2015-11-24 15:12:14 -0500149static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
Mike Marshall1182fca2015-07-17 10:38:15 -0400150{
151 int ret = -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500152 struct orangefs_kernel_op_s *new_op = NULL;
Mike Marshall1182fca2015-07-17 10:38:15 -0400153 int flags = 0;
154 struct super_block *sb = NULL;
155
156 sb = dentry->d_sb;
157
158 gossip_debug(GOSSIP_SUPER_DEBUG,
Mike Marshall95f5f882018-05-11 17:11:48 -0400159 "%s: called on sb %p (fs_id is %d)\n",
160 __func__,
161 sb,
162 (int)(ORANGEFS_SB(sb)->fs_id));
Mike Marshall1182fca2015-07-17 10:38:15 -0400163
Yi Liu8bb8aef2015-11-24 15:12:14 -0500164 new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
Mike Marshall1182fca2015-07-17 10:38:15 -0400165 if (!new_op)
166 return ret;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500167 new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
Mike Marshall1182fca2015-07-17 10:38:15 -0400168
Yi Liu8bb8aef2015-11-24 15:12:14 -0500169 if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
170 flags = ORANGEFS_OP_INTERRUPTIBLE;
Mike Marshall1182fca2015-07-17 10:38:15 -0400171
Yi Liu8bb8aef2015-11-24 15:12:14 -0500172 ret = service_operation(new_op, "orangefs_statfs", flags);
Mike Marshall1182fca2015-07-17 10:38:15 -0400173
174 if (new_op->downcall.status < 0)
175 goto out_op_release;
176
177 gossip_debug(GOSSIP_SUPER_DEBUG,
Mike Marshallbe573662016-01-13 11:38:14 -0500178 "%s: got %ld blocks available | "
179 "%ld blocks total | %ld block size | "
180 "%ld files total | %ld files avail\n",
181 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -0400182 (long)new_op->downcall.resp.statfs.blocks_avail,
183 (long)new_op->downcall.resp.statfs.blocks_total,
Mike Marshallbe573662016-01-13 11:38:14 -0500184 (long)new_op->downcall.resp.statfs.block_size,
185 (long)new_op->downcall.resp.statfs.files_total,
186 (long)new_op->downcall.resp.statfs.files_avail);
Mike Marshall1182fca2015-07-17 10:38:15 -0400187
188 buf->f_type = sb->s_magic;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500189 memcpy(&buf->f_fsid, &ORANGEFS_SB(sb)->fs_id, sizeof(buf->f_fsid));
Mike Marshall1182fca2015-07-17 10:38:15 -0400190 buf->f_bsize = new_op->downcall.resp.statfs.block_size;
Martin Brandenburg47b49482016-02-20 14:22:40 -0500191 buf->f_namelen = ORANGEFS_NAME_MAX;
Mike Marshall1182fca2015-07-17 10:38:15 -0400192
193 buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
194 buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
195 buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
196 buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
197 buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
198 buf->f_frsize = sb->s_blocksize;
199
200out_op_release:
201 op_release(new_op);
Mike Marshall95f5f882018-05-11 17:11:48 -0400202 gossip_debug(GOSSIP_SUPER_DEBUG, "%s: returning %d\n", __func__, ret);
Mike Marshall1182fca2015-07-17 10:38:15 -0400203 return ret;
204}
205
206/*
207 * Remount as initiated by VFS layer. We just need to reparse the mount
208 * options, no need to signal pvfs2-client-core about it.
209 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500210static int orangefs_remount_fs(struct super_block *sb, int *flags, char *data)
Mike Marshall1182fca2015-07-17 10:38:15 -0400211{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500212 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount_fs: called\n");
Mike Marshall1182fca2015-07-17 10:38:15 -0400213 return parse_mount_options(sb, data, 1);
214}
215
216/*
217 * Remount as initiated by pvfs2-client-core on restart. This is used to
218 * repopulate mount information left from previous pvfs2-client-core.
219 *
220 * the idea here is that given a valid superblock, we're
221 * re-initializing the user space client with the initial mount
222 * information specified when the super block was first initialized.
223 * this is very different than the first initialization/creation of a
224 * superblock. we use the special service_priority_operation to make
225 * sure that the mount gets ahead of any other pending operation that
226 * is waiting for servicing. this means that the pvfs2-client won't
227 * fail to start several times for all other pending operations before
228 * the client regains all of the mount information from us.
229 * NOTE: this function assumes that the request_mutex is already acquired!
230 */
Al Viro45996492016-03-25 19:56:34 -0400231int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
Mike Marshall1182fca2015-07-17 10:38:15 -0400232{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500233 struct orangefs_kernel_op_s *new_op;
Mike Marshall1182fca2015-07-17 10:38:15 -0400234 int ret = -EINVAL;
235
Yi Liu8bb8aef2015-11-24 15:12:14 -0500236 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
Mike Marshall1182fca2015-07-17 10:38:15 -0400237
Yi Liu8bb8aef2015-11-24 15:12:14 -0500238 new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
Mike Marshall1182fca2015-07-17 10:38:15 -0400239 if (!new_op)
240 return -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500241 strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
Al Viro45996492016-03-25 19:56:34 -0400242 orangefs_sb->devname,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500243 ORANGEFS_MAX_SERVER_ADDR_LEN);
Mike Marshall1182fca2015-07-17 10:38:15 -0400244
245 gossip_debug(GOSSIP_SUPER_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500246 "Attempting ORANGEFS Remount via host %s\n",
247 new_op->upcall.req.fs_mount.orangefs_config_server);
Mike Marshall1182fca2015-07-17 10:38:15 -0400248
249 /*
Mike Marshalladcf34a2016-02-24 16:54:27 -0500250 * we assume that the calling function has already acquired the
Mike Marshall1182fca2015-07-17 10:38:15 -0400251 * request_mutex to prevent other operations from bypassing
252 * this one
253 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500254 ret = service_operation(new_op, "orangefs_remount",
Mike Marshalladcf34a2016-02-24 16:54:27 -0500255 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
Mike Marshall1182fca2015-07-17 10:38:15 -0400256 gossip_debug(GOSSIP_SUPER_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500257 "orangefs_remount: mount got return value of %d\n",
Mike Marshall1182fca2015-07-17 10:38:15 -0400258 ret);
259 if (ret == 0) {
260 /*
261 * store the id assigned to this sb -- it's just a
262 * short-lived mapping that the system interface uses
263 * to map this superblock to a particular mount entry
264 */
Al Viro45996492016-03-25 19:56:34 -0400265 orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
266 orangefs_sb->mount_pending = 0;
Mike Marshall1182fca2015-07-17 10:38:15 -0400267 }
268
269 op_release(new_op);
Martin Brandenburg482664d2016-08-12 12:02:31 -0400270
Mike Marshallf60fbdb2016-10-03 15:07:36 -0400271 if (orangefs_userspace_version >= 20906) {
Martin Brandenburg482664d2016-08-12 12:02:31 -0400272 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
273 if (!new_op)
274 return -ENOMEM;
275 new_op->upcall.req.features.features = 0;
Martin Brandenburgcefdc262017-04-06 18:11:00 -0400276 ret = service_operation(new_op, "orangefs_features",
277 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
278 if (!ret)
279 orangefs_features =
280 new_op->downcall.resp.features.features;
281 else
282 orangefs_features = 0;
Martin Brandenburg482664d2016-08-12 12:02:31 -0400283 op_release(new_op);
284 } else {
285 orangefs_features = 0;
286 }
287
Mike Marshall1182fca2015-07-17 10:38:15 -0400288 return ret;
289}
290
291int fsid_key_table_initialize(void)
292{
293 return 0;
294}
295
296void fsid_key_table_finalize(void)
297{
298}
299
Yi Liu8bb8aef2015-11-24 15:12:14 -0500300static const struct super_operations orangefs_s_ops = {
301 .alloc_inode = orangefs_alloc_inode,
302 .destroy_inode = orangefs_destroy_inode,
Mike Marshall1182fca2015-07-17 10:38:15 -0400303 .drop_inode = generic_delete_inode,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500304 .statfs = orangefs_statfs,
305 .remount_fs = orangefs_remount_fs,
David Howells4dfdb712017-07-05 16:25:45 +0100306 .show_options = orangefs_show_options,
Mike Marshall1182fca2015-07-17 10:38:15 -0400307};
308
Yi Liu8bb8aef2015-11-24 15:12:14 -0500309static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
Mike Marshall1182fca2015-07-17 10:38:15 -0400310 struct fid *fid,
311 int fh_len,
312 int fh_type)
313{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500314 struct orangefs_object_kref refn;
Mike Marshall1182fca2015-07-17 10:38:15 -0400315
316 if (fh_len < 5 || fh_type > 2)
317 return NULL;
318
Yi Liu8bb8aef2015-11-24 15:12:14 -0500319 ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
Mike Marshall1182fca2015-07-17 10:38:15 -0400320 refn.fs_id = (u32) fid->raw[4];
321 gossip_debug(GOSSIP_SUPER_DEBUG,
322 "fh_to_dentry: handle %pU, fs_id %d\n",
323 &refn.khandle,
324 refn.fs_id);
325
Yi Liu8bb8aef2015-11-24 15:12:14 -0500326 return d_obtain_alias(orangefs_iget(sb, &refn));
Mike Marshall1182fca2015-07-17 10:38:15 -0400327}
328
Yi Liu8bb8aef2015-11-24 15:12:14 -0500329static int orangefs_encode_fh(struct inode *inode,
Mike Marshall1182fca2015-07-17 10:38:15 -0400330 __u32 *fh,
331 int *max_len,
332 struct inode *parent)
333{
334 int len = parent ? 10 : 5;
335 int type = 1;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500336 struct orangefs_object_kref refn;
Mike Marshall1182fca2015-07-17 10:38:15 -0400337
338 if (*max_len < len) {
Martin Brandenburg79d7cd62018-01-26 14:07:12 -0500339 gossip_err("fh buffer is too small for encoding\n");
Mike Marshall1182fca2015-07-17 10:38:15 -0400340 *max_len = len;
341 type = 255;
342 goto out;
343 }
344
Yi Liu8bb8aef2015-11-24 15:12:14 -0500345 refn = ORANGEFS_I(inode)->refn;
346 ORANGEFS_khandle_to(&refn.khandle, fh, 16);
Mike Marshall1182fca2015-07-17 10:38:15 -0400347 fh[4] = refn.fs_id;
348
349 gossip_debug(GOSSIP_SUPER_DEBUG,
350 "Encoding fh: handle %pU, fsid %u\n",
351 &refn.khandle,
352 refn.fs_id);
353
354
355 if (parent) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500356 refn = ORANGEFS_I(parent)->refn;
357 ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
Mike Marshall1182fca2015-07-17 10:38:15 -0400358 fh[9] = refn.fs_id;
359
360 type = 2;
361 gossip_debug(GOSSIP_SUPER_DEBUG,
362 "Encoding parent: handle %pU, fsid %u\n",
363 &refn.khandle,
364 refn.fs_id);
365 }
366 *max_len = len;
367
368out:
369 return type;
370}
371
Julia Lawallacaca362016-01-01 10:01:52 +0100372static const struct export_operations orangefs_export_ops = {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500373 .encode_fh = orangefs_encode_fh,
374 .fh_to_dentry = orangefs_fh_to_dentry,
Mike Marshall1182fca2015-07-17 10:38:15 -0400375};
376
Martin Brandenburg9d286b02017-04-25 15:38:05 -0400377static int orangefs_unmount(int id, __s32 fs_id, const char *devname)
378{
379 struct orangefs_kernel_op_s *op;
380 int r;
381 op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT);
382 if (!op)
383 return -ENOMEM;
384 op->upcall.req.fs_umount.id = id;
385 op->upcall.req.fs_umount.fs_id = fs_id;
386 strncpy(op->upcall.req.fs_umount.orangefs_config_server,
Xiongfeng Wang6bdfb482018-01-08 20:22:33 +0800387 devname, ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
Martin Brandenburg9d286b02017-04-25 15:38:05 -0400388 r = service_operation(op, "orangefs_fs_umount", 0);
389 /* Not much to do about an error here. */
390 if (r)
391 gossip_err("orangefs_unmount: service_operation %d\n", r);
392 op_release(op);
393 return r;
394}
395
Yi Liu8bb8aef2015-11-24 15:12:14 -0500396static int orangefs_fill_sb(struct super_block *sb,
397 struct orangefs_fs_mount_response *fs_mount,
Al Viro5c0dbbc2015-10-08 20:22:43 -0400398 void *data, int silent)
Mike Marshall1182fca2015-07-17 10:38:15 -0400399{
400 int ret = -EINVAL;
401 struct inode *root = NULL;
402 struct dentry *root_dentry = NULL;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500403 struct orangefs_object_kref root_object;
Mike Marshall1182fca2015-07-17 10:38:15 -0400404
Yi Liu8bb8aef2015-11-24 15:12:14 -0500405 /* alloc and init our private orangefs sb info */
Martin Brandenburg05d31c52016-03-18 13:36:45 -0400406 sb->s_fs_info = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500407 if (!ORANGEFS_SB(sb))
Mike Marshall1182fca2015-07-17 10:38:15 -0400408 return -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500409 ORANGEFS_SB(sb)->sb = sb;
Mike Marshall1182fca2015-07-17 10:38:15 -0400410
Yi Liu8bb8aef2015-11-24 15:12:14 -0500411 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;
Mike Marshall1182fca2015-07-17 10:38:15 -0400414
Al Viro5c0dbbc2015-10-08 20:22:43 -0400415 if (data) {
416 ret = parse_mount_options(sb, data, silent);
Mike Marshall1182fca2015-07-17 10:38:15 -0400417 if (ret)
418 return ret;
419 }
420
421 /* Hang the xattr handlers off the superblock */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500422 sb->s_xattr = orangefs_xattr_handlers;
423 sb->s_magic = ORANGEFS_SUPER_MAGIC;
424 sb->s_op = &orangefs_s_ops;
425 sb->s_d_op = &orangefs_dentry_operations;
Mike Marshall1182fca2015-07-17 10:38:15 -0400426
Martin Brandenburg9f8fd532018-05-31 16:36:59 +0000427 sb->s_blocksize = PAGE_SIZE;
428 sb->s_blocksize_bits = PAGE_SHIFT;
Mike Marshall1182fca2015-07-17 10:38:15 -0400429 sb->s_maxbytes = MAX_LFS_FILESIZE;
430
Yi Liu8bb8aef2015-11-24 15:12:14 -0500431 root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
432 root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
Mike Marshall1182fca2015-07-17 10:38:15 -0400433 gossip_debug(GOSSIP_SUPER_DEBUG,
434 "get inode %pU, fsid %d\n",
435 &root_object.khandle,
436 root_object.fs_id);
437
Yi Liu8bb8aef2015-11-24 15:12:14 -0500438 root = orangefs_iget(sb, &root_object);
Mike Marshall1182fca2015-07-17 10:38:15 -0400439 if (IS_ERR(root))
440 return PTR_ERR(root);
441
442 gossip_debug(GOSSIP_SUPER_DEBUG,
443 "Allocated root inode [%p] with mode %x\n",
444 root,
445 root->i_mode);
446
447 /* allocates and places root dentry in dcache */
448 root_dentry = d_make_root(root);
Al Virob05a7852015-10-08 20:18:00 -0400449 if (!root_dentry)
Mike Marshall1182fca2015-07-17 10:38:15 -0400450 return -ENOMEM;
Mike Marshall1182fca2015-07-17 10:38:15 -0400451
Yi Liu8bb8aef2015-11-24 15:12:14 -0500452 sb->s_export_op = &orangefs_export_ops;
Mike Marshall1182fca2015-07-17 10:38:15 -0400453 sb->s_root = root_dentry;
454 return 0;
455}
456
Yi Liu8bb8aef2015-11-24 15:12:14 -0500457struct dentry *orangefs_mount(struct file_system_type *fst,
Mike Marshall1182fca2015-07-17 10:38:15 -0400458 int flags,
459 const char *devname,
460 void *data)
461{
462 int ret = -EINVAL;
463 struct super_block *sb = ERR_PTR(-EINVAL);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500464 struct orangefs_kernel_op_s *new_op;
Mike Marshall1be21f82015-09-29 15:26:37 -0400465 struct dentry *d = ERR_PTR(-EINVAL);
Mike Marshall1182fca2015-07-17 10:38:15 -0400466
467 gossip_debug(GOSSIP_SUPER_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500468 "orangefs_mount: called with devname %s\n",
Mike Marshall1182fca2015-07-17 10:38:15 -0400469 devname);
470
471 if (!devname) {
472 gossip_err("ERROR: device name not specified.\n");
473 return ERR_PTR(-EINVAL);
474 }
475
Yi Liu8bb8aef2015-11-24 15:12:14 -0500476 new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
Mike Marshall1182fca2015-07-17 10:38:15 -0400477 if (!new_op)
478 return ERR_PTR(-ENOMEM);
479
Yi Liu8bb8aef2015-11-24 15:12:14 -0500480 strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
Mike Marshall1182fca2015-07-17 10:38:15 -0400481 devname,
Xiongfeng Wang6bdfb482018-01-08 20:22:33 +0800482 ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
Mike Marshall1182fca2015-07-17 10:38:15 -0400483
484 gossip_debug(GOSSIP_SUPER_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500485 "Attempting ORANGEFS Mount via host %s\n",
486 new_op->upcall.req.fs_mount.orangefs_config_server);
Mike Marshall1182fca2015-07-17 10:38:15 -0400487
Yi Liu8bb8aef2015-11-24 15:12:14 -0500488 ret = service_operation(new_op, "orangefs_mount", 0);
Mike Marshall1182fca2015-07-17 10:38:15 -0400489 gossip_debug(GOSSIP_SUPER_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500490 "orangefs_mount: mount got return value of %d\n", ret);
Mike Marshall1182fca2015-07-17 10:38:15 -0400491 if (ret)
492 goto free_op;
493
Yi Liu8bb8aef2015-11-24 15:12:14 -0500494 if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400495 gossip_err("ERROR: Retrieved null fs_id\n");
496 ret = -EINVAL;
497 goto free_op;
498 }
499
Mike Marshall1be21f82015-09-29 15:26:37 -0400500 sb = sget(fst, NULL, set_anon_super, flags, NULL);
501
502 if (IS_ERR(sb)) {
503 d = ERR_CAST(sb);
Martin Brandenburg9d286b02017-04-25 15:38:05 -0400504 orangefs_unmount(new_op->downcall.resp.fs_mount.id,
505 new_op->downcall.resp.fs_mount.fs_id, devname);
Mike Marshall1182fca2015-07-17 10:38:15 -0400506 goto free_op;
507 }
508
Yi Liu8bb8aef2015-11-24 15:12:14 -0500509 ret = orangefs_fill_sb(sb,
Al Viro5c0dbbc2015-10-08 20:22:43 -0400510 &new_op->downcall.resp.fs_mount, data,
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800511 flags & SB_SILENT ? 1 : 0);
Mike Marshall1be21f82015-09-29 15:26:37 -0400512
513 if (ret) {
514 d = ERR_PTR(ret);
Martin Brandenburg1ec16882017-04-14 14:22:41 -0400515 goto free_sb_and_op;
Mike Marshall1be21f82015-09-29 15:26:37 -0400516 }
Mike Marshall1182fca2015-07-17 10:38:15 -0400517
518 /*
519 * on successful mount, store the devname and data
520 * used
521 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500522 strncpy(ORANGEFS_SB(sb)->devname,
Mike Marshall1182fca2015-07-17 10:38:15 -0400523 devname,
Xiongfeng Wang6bdfb482018-01-08 20:22:33 +0800524 ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
Mike Marshall1182fca2015-07-17 10:38:15 -0400525
526 /* mount_pending must be cleared */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500527 ORANGEFS_SB(sb)->mount_pending = 0;
Mike Marshall1182fca2015-07-17 10:38:15 -0400528
529 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500530 * finally, add this sb to our list of known orangefs
Mike Marshall1182fca2015-07-17 10:38:15 -0400531 * sb's
532 */
Al Viro45996492016-03-25 19:56:34 -0400533 gossip_debug(GOSSIP_SUPER_DEBUG,
534 "Adding SB %p to orangefs superblocks\n",
535 ORANGEFS_SB(sb));
536 spin_lock(&orangefs_superblocks_lock);
537 list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
538 spin_unlock(&orangefs_superblocks_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400539 op_release(new_op);
Martin Brandenburg482664d2016-08-12 12:02:31 -0400540
Martin Brandenburg1ec16882017-04-14 14:22:41 -0400541 /* Must be removed from the list now. */
542 ORANGEFS_SB(sb)->no_list = 0;
543
Mike Marshallf60fbdb2016-10-03 15:07:36 -0400544 if (orangefs_userspace_version >= 20906) {
Martin Brandenburg482664d2016-08-12 12:02:31 -0400545 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
546 if (!new_op)
547 return ERR_PTR(-ENOMEM);
548 new_op->upcall.req.features.features = 0;
549 ret = service_operation(new_op, "orangefs_features", 0);
550 orangefs_features = new_op->downcall.resp.features.features;
551 op_release(new_op);
552 } else {
553 orangefs_features = 0;
554 }
555
Mike Marshall1be21f82015-09-29 15:26:37 -0400556 return dget(sb->s_root);
Mike Marshall1182fca2015-07-17 10:38:15 -0400557
Martin Brandenburg1ec16882017-04-14 14:22:41 -0400558free_sb_and_op:
559 /* Will call orangefs_kill_sb with sb not in list. */
560 ORANGEFS_SB(sb)->no_list = 1;
Martin Brandenburg9d286b02017-04-25 15:38:05 -0400561 /* ORANGEFS_VFS_OP_FS_UMOUNT is done by orangefs_kill_sb. */
Martin Brandenburg1ec16882017-04-14 14:22:41 -0400562 deactivate_locked_super(sb);
Mike Marshall1182fca2015-07-17 10:38:15 -0400563free_op:
Yi Liu8bb8aef2015-11-24 15:12:14 -0500564 gossip_err("orangefs_mount: mount request failed with %d\n", ret);
Mike Marshall1182fca2015-07-17 10:38:15 -0400565 if (ret == -EINVAL) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500566 gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
Mike Marshall1182fca2015-07-17 10:38:15 -0400567 gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
568 }
569
570 op_release(new_op);
571
Mike Marshall1be21f82015-09-29 15:26:37 -0400572 return d;
Mike Marshall1182fca2015-07-17 10:38:15 -0400573}
574
Yi Liu8bb8aef2015-11-24 15:12:14 -0500575void orangefs_kill_sb(struct super_block *sb)
Mike Marshall1182fca2015-07-17 10:38:15 -0400576{
Martin Brandenburg9d286b02017-04-25 15:38:05 -0400577 int r;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500578 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
Mike Marshall1182fca2015-07-17 10:38:15 -0400579
Al Viro524b1d32016-02-16 21:08:29 -0500580 /* provided sb cleanup */
581 kill_anon_super(sb);
582
Al Viro65903842018-04-03 00:13:17 -0400583 if (!ORANGEFS_SB(sb)) {
584 mutex_lock(&orangefs_request_mutex);
585 mutex_unlock(&orangefs_request_mutex);
586 return;
587 }
Mike Marshall1182fca2015-07-17 10:38:15 -0400588 /*
589 * issue the unmount to userspace to tell it to remove the
590 * dynamic mount info it has for this superblock
591 */
Martin Brandenburg9d286b02017-04-25 15:38:05 -0400592 r = orangefs_unmount(ORANGEFS_SB(sb)->id, ORANGEFS_SB(sb)->fs_id,
593 ORANGEFS_SB(sb)->devname);
594 if (!r)
595 ORANGEFS_SB(sb)->mount_pending = 1;
Mike Marshall1182fca2015-07-17 10:38:15 -0400596
Martin Brandenburg1ec16882017-04-14 14:22:41 -0400597 if (!ORANGEFS_SB(sb)->no_list) {
598 /* remove the sb from our list of orangefs specific sb's */
599 spin_lock(&orangefs_superblocks_lock);
600 /* not list_del_init */
601 __list_del_entry(&ORANGEFS_SB(sb)->list);
602 ORANGEFS_SB(sb)->list.prev = NULL;
603 spin_unlock(&orangefs_superblocks_lock);
604 }
Al Viro45996492016-03-25 19:56:34 -0400605
606 /*
607 * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
608 * gets completed before we free the dang thing.
609 */
Martin Brandenburg1d503612016-08-16 11:38:14 -0400610 mutex_lock(&orangefs_request_mutex);
611 mutex_unlock(&orangefs_request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -0400612
Yi Liu8bb8aef2015-11-24 15:12:14 -0500613 /* free the orangefs superblock private data */
614 kfree(ORANGEFS_SB(sb));
Mike Marshall1182fca2015-07-17 10:38:15 -0400615}
616
Yi Liu8bb8aef2015-11-24 15:12:14 -0500617int orangefs_inode_cache_initialize(void)
Mike Marshall1182fca2015-07-17 10:38:15 -0400618{
David Windsor6b330622017-06-10 22:50:39 -0400619 orangefs_inode_cache = kmem_cache_create_usercopy(
620 "orangefs_inode_cache",
621 sizeof(struct orangefs_inode_s),
622 0,
623 ORANGEFS_CACHE_CREATE_FLAGS,
624 offsetof(struct orangefs_inode_s,
625 link_target),
626 sizeof_field(struct orangefs_inode_s,
627 link_target),
628 orangefs_inode_cache_ctor);
Mike Marshall1182fca2015-07-17 10:38:15 -0400629
Yi Liu8bb8aef2015-11-24 15:12:14 -0500630 if (!orangefs_inode_cache) {
631 gossip_err("Cannot create orangefs_inode_cache\n");
Mike Marshall1182fca2015-07-17 10:38:15 -0400632 return -ENOMEM;
633 }
634 return 0;
635}
636
Yi Liu8bb8aef2015-11-24 15:12:14 -0500637int orangefs_inode_cache_finalize(void)
Mike Marshall1182fca2015-07-17 10:38:15 -0400638{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500639 kmem_cache_destroy(orangefs_inode_cache);
Mike Marshall1182fca2015-07-17 10:38:15 -0400640 return 0;
641}