Merge tag 'driver-core-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / fs / orangefs / super.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) 2001 Clemson University and The University of Chicago
4  *
5  * See COPYING in top-level directory.
6  */
7
8 #include "protocol.h"
9 #include "orangefs-kernel.h"
10 #include "orangefs-bufmap.h"
11
12 #include <linux/parser.h>
13
14 /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
15 static struct kmem_cache *orangefs_inode_cache;
16
17 /* list for storing orangefs specific superblocks in use */
18 LIST_HEAD(orangefs_superblocks);
19
20 DEFINE_SPINLOCK(orangefs_superblocks_lock);
21
22 enum {
23         Opt_intr,
24         Opt_acl,
25         Opt_local_lock,
26
27         Opt_err
28 };
29
30 static 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
37 uint64_t orangefs_features;
38
39 static 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
43         if (root->d_sb->s_flags & SB_POSIXACL)
44                 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
52 static int parse_mount_options(struct super_block *sb, char *options,
53                 int silent)
54 {
55         struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
56         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          */
63         sb->s_flags &= ~SB_POSIXACL;
64         orangefs_sb->flags &= ~ORANGEFS_OPT_INTR;
65         orangefs_sb->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
66
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:
76                         sb->s_flags |= SB_POSIXACL;
77                         break;
78                 case Opt_intr:
79                         orangefs_sb->flags |= ORANGEFS_OPT_INTR;
80                         break;
81                 case Opt_local_lock:
82                         orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
83                         break;
84                 default:
85                         goto fail;
86                 }
87         }
88
89         return 0;
90 fail:
91         if (!silent)
92                 gossip_err("Error: mount option [%s] is not supported.\n", p);
93         return -EINVAL;
94 }
95
96 static void orangefs_inode_cache_ctor(void *req)
97 {
98         struct orangefs_inode_s *orangefs_inode = req;
99
100         inode_init_once(&orangefs_inode->vfs_inode);
101         init_rwsem(&orangefs_inode->xattr_sem);
102 }
103
104 static struct inode *orangefs_alloc_inode(struct super_block *sb)
105 {
106         struct orangefs_inode_s *orangefs_inode;
107
108         orangefs_inode = kmem_cache_alloc(orangefs_inode_cache, GFP_KERNEL);
109         if (!orangefs_inode)
110                 return NULL;
111
112         /*
113          * We want to clear everything except for rw_semaphore and the
114          * vfs_inode.
115          */
116         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));
120
121         gossip_debug(GOSSIP_SUPER_DEBUG,
122                      "orangefs_alloc_inode: allocated %p\n",
123                      &orangefs_inode->vfs_inode);
124         return &orangefs_inode->vfs_inode;
125 }
126
127 static void orangefs_free_inode(struct inode *inode)
128 {
129         kmem_cache_free(orangefs_inode_cache, ORANGEFS_I(inode));
130 }
131
132 static void orangefs_destroy_inode(struct inode *inode)
133 {
134         struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
135
136         gossip_debug(GOSSIP_SUPER_DEBUG,
137                         "%s: deallocated %p destroying inode %pU\n",
138                         __func__, orangefs_inode, get_khandle_from_ino(inode));
139 }
140
141 /*
142  * NOTE: information filled in here is typically reflected in the
143  * output of the system command 'df'
144 */
145 static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
146 {
147         int ret = -ENOMEM;
148         struct orangefs_kernel_op_s *new_op = NULL;
149         int flags = 0;
150         struct super_block *sb = NULL;
151
152         sb = dentry->d_sb;
153
154         gossip_debug(GOSSIP_SUPER_DEBUG,
155                         "%s: called on sb %p (fs_id is %d)\n",
156                         __func__,
157                         sb,
158                         (int)(ORANGEFS_SB(sb)->fs_id));
159
160         new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
161         if (!new_op)
162                 return ret;
163         new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
164
165         if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
166                 flags = ORANGEFS_OP_INTERRUPTIBLE;
167
168         ret = service_operation(new_op, "orangefs_statfs", flags);
169
170         if (new_op->downcall.status < 0)
171                 goto out_op_release;
172
173         gossip_debug(GOSSIP_SUPER_DEBUG,
174                      "%s: got %ld blocks available | "
175                      "%ld blocks total | %ld block size | "
176                      "%ld files total | %ld files avail\n",
177                      __func__,
178                      (long)new_op->downcall.resp.statfs.blocks_avail,
179                      (long)new_op->downcall.resp.statfs.blocks_total,
180                      (long)new_op->downcall.resp.statfs.block_size,
181                      (long)new_op->downcall.resp.statfs.files_total,
182                      (long)new_op->downcall.resp.statfs.files_avail);
183
184         buf->f_type = sb->s_magic;
185         memcpy(&buf->f_fsid, &ORANGEFS_SB(sb)->fs_id, sizeof(buf->f_fsid));
186         buf->f_bsize = new_op->downcall.resp.statfs.block_size;
187         buf->f_namelen = ORANGEFS_NAME_MAX;
188
189         buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
190         buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
191         buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
192         buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
193         buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
194         buf->f_frsize = sb->s_blocksize;
195
196 out_op_release:
197         op_release(new_op);
198         gossip_debug(GOSSIP_SUPER_DEBUG, "%s: returning %d\n", __func__, ret);
199         return ret;
200 }
201
202 /*
203  * Remount as initiated by VFS layer.  We just need to reparse the mount
204  * options, no need to signal pvfs2-client-core about it.
205  */
206 static int orangefs_remount_fs(struct super_block *sb, int *flags, char *data)
207 {
208         gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount_fs: called\n");
209         return parse_mount_options(sb, data, 1);
210 }
211
212 /*
213  * Remount as initiated by pvfs2-client-core on restart.  This is used to
214  * repopulate mount information left from previous pvfs2-client-core.
215  *
216  * the idea here is that given a valid superblock, we're
217  * re-initializing the user space client with the initial mount
218  * information specified when the super block was first initialized.
219  * this is very different than the first initialization/creation of a
220  * superblock.  we use the special service_priority_operation to make
221  * sure that the mount gets ahead of any other pending operation that
222  * is waiting for servicing.  this means that the pvfs2-client won't
223  * fail to start several times for all other pending operations before
224  * the client regains all of the mount information from us.
225  * NOTE: this function assumes that the request_mutex is already acquired!
226  */
227 int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
228 {
229         struct orangefs_kernel_op_s *new_op;
230         int ret = -EINVAL;
231
232         gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
233
234         new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
235         if (!new_op)
236                 return -ENOMEM;
237         strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
238                 orangefs_sb->devname,
239                 ORANGEFS_MAX_SERVER_ADDR_LEN);
240
241         gossip_debug(GOSSIP_SUPER_DEBUG,
242                      "Attempting ORANGEFS Remount via host %s\n",
243                      new_op->upcall.req.fs_mount.orangefs_config_server);
244
245         /*
246          * we assume that the calling function has already acquired the
247          * request_mutex to prevent other operations from bypassing
248          * this one
249          */
250         ret = service_operation(new_op, "orangefs_remount",
251                 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
252         gossip_debug(GOSSIP_SUPER_DEBUG,
253                      "orangefs_remount: mount got return value of %d\n",
254                      ret);
255         if (ret == 0) {
256                 /*
257                  * store the id assigned to this sb -- it's just a
258                  * short-lived mapping that the system interface uses
259                  * to map this superblock to a particular mount entry
260                  */
261                 orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
262                 orangefs_sb->mount_pending = 0;
263         }
264
265         op_release(new_op);
266
267         if (orangefs_userspace_version >= 20906) {
268                 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
269                 if (!new_op)
270                         return -ENOMEM;
271                 new_op->upcall.req.features.features = 0;
272                 ret = service_operation(new_op, "orangefs_features",
273                     ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
274                 if (!ret)
275                         orangefs_features =
276                             new_op->downcall.resp.features.features;
277                 else
278                         orangefs_features = 0;
279                 op_release(new_op);
280         } else {
281                 orangefs_features = 0;
282         }
283
284         return ret;
285 }
286
287 int fsid_key_table_initialize(void)
288 {
289         return 0;
290 }
291
292 void fsid_key_table_finalize(void)
293 {
294 }
295
296 static const struct super_operations orangefs_s_ops = {
297         .alloc_inode = orangefs_alloc_inode,
298         .free_inode = orangefs_free_inode,
299         .destroy_inode = orangefs_destroy_inode,
300         .drop_inode = generic_delete_inode,
301         .statfs = orangefs_statfs,
302         .remount_fs = orangefs_remount_fs,
303         .show_options = orangefs_show_options,
304 };
305
306 static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
307                                   struct fid *fid,
308                                   int fh_len,
309                                   int fh_type)
310 {
311         struct orangefs_object_kref refn;
312
313         if (fh_len < 5 || fh_type > 2)
314                 return NULL;
315
316         ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
317         refn.fs_id = (u32) fid->raw[4];
318         gossip_debug(GOSSIP_SUPER_DEBUG,
319                      "fh_to_dentry: handle %pU, fs_id %d\n",
320                      &refn.khandle,
321                      refn.fs_id);
322
323         return d_obtain_alias(orangefs_iget(sb, &refn));
324 }
325
326 static int orangefs_encode_fh(struct inode *inode,
327                     __u32 *fh,
328                     int *max_len,
329                     struct inode *parent)
330 {
331         int len = parent ? 10 : 5;
332         int type = 1;
333         struct orangefs_object_kref refn;
334
335         if (*max_len < len) {
336                 gossip_err("fh buffer is too small for encoding\n");
337                 *max_len = len;
338                 type = 255;
339                 goto out;
340         }
341
342         refn = ORANGEFS_I(inode)->refn;
343         ORANGEFS_khandle_to(&refn.khandle, fh, 16);
344         fh[4] = refn.fs_id;
345
346         gossip_debug(GOSSIP_SUPER_DEBUG,
347                      "Encoding fh: handle %pU, fsid %u\n",
348                      &refn.khandle,
349                      refn.fs_id);
350
351
352         if (parent) {
353                 refn = ORANGEFS_I(parent)->refn;
354                 ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
355                 fh[9] = refn.fs_id;
356
357                 type = 2;
358                 gossip_debug(GOSSIP_SUPER_DEBUG,
359                              "Encoding parent: handle %pU, fsid %u\n",
360                              &refn.khandle,
361                              refn.fs_id);
362         }
363         *max_len = len;
364
365 out:
366         return type;
367 }
368
369 static const struct export_operations orangefs_export_ops = {
370         .encode_fh = orangefs_encode_fh,
371         .fh_to_dentry = orangefs_fh_to_dentry,
372 };
373
374 static int orangefs_unmount(int id, __s32 fs_id, const char *devname)
375 {
376         struct orangefs_kernel_op_s *op;
377         int r;
378         op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT);
379         if (!op)
380                 return -ENOMEM;
381         op->upcall.req.fs_umount.id = id;
382         op->upcall.req.fs_umount.fs_id = fs_id;
383         strncpy(op->upcall.req.fs_umount.orangefs_config_server,
384             devname, ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
385         r = service_operation(op, "orangefs_fs_umount", 0);
386         /* Not much to do about an error here. */
387         if (r)
388                 gossip_err("orangefs_unmount: service_operation %d\n", r);
389         op_release(op);
390         return r;
391 }
392
393 static int orangefs_fill_sb(struct super_block *sb,
394                 struct orangefs_fs_mount_response *fs_mount,
395                 void *data, int silent)
396 {
397         int ret = -EINVAL;
398         struct inode *root = NULL;
399         struct dentry *root_dentry = NULL;
400         struct orangefs_object_kref root_object;
401
402         /* alloc and init our private orangefs sb info */
403         sb->s_fs_info = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL);
404         if (!ORANGEFS_SB(sb))
405                 return -ENOMEM;
406         ORANGEFS_SB(sb)->sb = sb;
407
408         ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle;
409         ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id;
410         ORANGEFS_SB(sb)->id = fs_mount->id;
411
412         if (data) {
413                 ret = parse_mount_options(sb, data, silent);
414                 if (ret)
415                         return ret;
416         }
417
418         /* Hang the xattr handlers off the superblock */
419         sb->s_xattr = orangefs_xattr_handlers;
420         sb->s_magic = ORANGEFS_SUPER_MAGIC;
421         sb->s_op = &orangefs_s_ops;
422         sb->s_d_op = &orangefs_dentry_operations;
423
424         sb->s_blocksize = PAGE_SIZE;
425         sb->s_blocksize_bits = PAGE_SHIFT;
426         sb->s_maxbytes = MAX_LFS_FILESIZE;
427
428         root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
429         root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
430         gossip_debug(GOSSIP_SUPER_DEBUG,
431                      "get inode %pU, fsid %d\n",
432                      &root_object.khandle,
433                      root_object.fs_id);
434
435         root = orangefs_iget(sb, &root_object);
436         if (IS_ERR(root))
437                 return PTR_ERR(root);
438
439         gossip_debug(GOSSIP_SUPER_DEBUG,
440                      "Allocated root inode [%p] with mode %x\n",
441                      root,
442                      root->i_mode);
443
444         /* allocates and places root dentry in dcache */
445         root_dentry = d_make_root(root);
446         if (!root_dentry)
447                 return -ENOMEM;
448
449         sb->s_export_op = &orangefs_export_ops;
450         sb->s_root = root_dentry;
451         return 0;
452 }
453
454 struct dentry *orangefs_mount(struct file_system_type *fst,
455                            int flags,
456                            const char *devname,
457                            void *data)
458 {
459         int ret = -EINVAL;
460         struct super_block *sb = ERR_PTR(-EINVAL);
461         struct orangefs_kernel_op_s *new_op;
462         struct dentry *d = ERR_PTR(-EINVAL);
463
464         gossip_debug(GOSSIP_SUPER_DEBUG,
465                      "orangefs_mount: called with devname %s\n",
466                      devname);
467
468         if (!devname) {
469                 gossip_err("ERROR: device name not specified.\n");
470                 return ERR_PTR(-EINVAL);
471         }
472
473         new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
474         if (!new_op)
475                 return ERR_PTR(-ENOMEM);
476
477         strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
478                 devname,
479                 ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
480
481         gossip_debug(GOSSIP_SUPER_DEBUG,
482                      "Attempting ORANGEFS Mount via host %s\n",
483                      new_op->upcall.req.fs_mount.orangefs_config_server);
484
485         ret = service_operation(new_op, "orangefs_mount", 0);
486         gossip_debug(GOSSIP_SUPER_DEBUG,
487                      "orangefs_mount: mount got return value of %d\n", ret);
488         if (ret)
489                 goto free_op;
490
491         if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
492                 gossip_err("ERROR: Retrieved null fs_id\n");
493                 ret = -EINVAL;
494                 goto free_op;
495         }
496
497         sb = sget(fst, NULL, set_anon_super, flags, NULL);
498
499         if (IS_ERR(sb)) {
500                 d = ERR_CAST(sb);
501                 orangefs_unmount(new_op->downcall.resp.fs_mount.id,
502                     new_op->downcall.resp.fs_mount.fs_id, devname);
503                 goto free_op;
504         }
505
506         ret = orangefs_fill_sb(sb,
507               &new_op->downcall.resp.fs_mount, data,
508               flags & SB_SILENT ? 1 : 0);
509
510         if (ret) {
511                 d = ERR_PTR(ret);
512                 goto free_sb_and_op;
513         }
514
515         /*
516          * on successful mount, store the devname and data
517          * used
518          */
519         strncpy(ORANGEFS_SB(sb)->devname,
520                 devname,
521                 ORANGEFS_MAX_SERVER_ADDR_LEN - 1);
522
523         /* mount_pending must be cleared */
524         ORANGEFS_SB(sb)->mount_pending = 0;
525
526         /*
527          * finally, add this sb to our list of known orangefs
528          * sb's
529          */
530         gossip_debug(GOSSIP_SUPER_DEBUG,
531                      "Adding SB %p to orangefs superblocks\n",
532                      ORANGEFS_SB(sb));
533         spin_lock(&orangefs_superblocks_lock);
534         list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
535         spin_unlock(&orangefs_superblocks_lock);
536         op_release(new_op);
537
538         /* Must be removed from the list now. */
539         ORANGEFS_SB(sb)->no_list = 0;
540
541         if (orangefs_userspace_version >= 20906) {
542                 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
543                 if (!new_op)
544                         return ERR_PTR(-ENOMEM);
545                 new_op->upcall.req.features.features = 0;
546                 ret = service_operation(new_op, "orangefs_features", 0);
547                 orangefs_features = new_op->downcall.resp.features.features;
548                 op_release(new_op);
549         } else {
550                 orangefs_features = 0;
551         }
552
553         return dget(sb->s_root);
554
555 free_sb_and_op:
556         /* Will call orangefs_kill_sb with sb not in list. */
557         ORANGEFS_SB(sb)->no_list = 1;
558         /* ORANGEFS_VFS_OP_FS_UMOUNT is done by orangefs_kill_sb. */
559         deactivate_locked_super(sb);
560 free_op:
561         gossip_err("orangefs_mount: mount request failed with %d\n", ret);
562         if (ret == -EINVAL) {
563                 gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
564                 gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
565         }
566
567         op_release(new_op);
568
569         return d;
570 }
571
572 void orangefs_kill_sb(struct super_block *sb)
573 {
574         int r;
575         gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
576
577         /* provided sb cleanup */
578         kill_anon_super(sb);
579
580         if (!ORANGEFS_SB(sb)) {
581                 mutex_lock(&orangefs_request_mutex);
582                 mutex_unlock(&orangefs_request_mutex);
583                 return;
584         }
585         /*
586          * issue the unmount to userspace to tell it to remove the
587          * dynamic mount info it has for this superblock
588          */
589         r = orangefs_unmount(ORANGEFS_SB(sb)->id, ORANGEFS_SB(sb)->fs_id,
590             ORANGEFS_SB(sb)->devname);
591         if (!r)
592                 ORANGEFS_SB(sb)->mount_pending = 1;
593
594         if (!ORANGEFS_SB(sb)->no_list) {
595                 /* remove the sb from our list of orangefs specific sb's */
596                 spin_lock(&orangefs_superblocks_lock);
597                 /* not list_del_init */
598                 __list_del_entry(&ORANGEFS_SB(sb)->list);
599                 ORANGEFS_SB(sb)->list.prev = NULL;
600                 spin_unlock(&orangefs_superblocks_lock);
601         }
602
603         /*
604          * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
605          * gets completed before we free the dang thing.
606          */
607         mutex_lock(&orangefs_request_mutex);
608         mutex_unlock(&orangefs_request_mutex);
609
610         /* free the orangefs superblock private data */
611         kfree(ORANGEFS_SB(sb));
612 }
613
614 int orangefs_inode_cache_initialize(void)
615 {
616         orangefs_inode_cache = kmem_cache_create_usercopy(
617                                         "orangefs_inode_cache",
618                                         sizeof(struct orangefs_inode_s),
619                                         0,
620                                         ORANGEFS_CACHE_CREATE_FLAGS,
621                                         offsetof(struct orangefs_inode_s,
622                                                 link_target),
623                                         sizeof_field(struct orangefs_inode_s,
624                                                 link_target),
625                                         orangefs_inode_cache_ctor);
626
627         if (!orangefs_inode_cache) {
628                 gossip_err("Cannot create orangefs_inode_cache\n");
629                 return -ENOMEM;
630         }
631         return 0;
632 }
633
634 int orangefs_inode_cache_finalize(void)
635 {
636         kmem_cache_destroy(orangefs_inode_cache);
637         return 0;
638 }