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