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