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