1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2001 Clemson University and The University of Chicago
5 * See COPYING in top-level directory.
9 #include "orangefs-kernel.h"
10 #include "orangefs-bufmap.h"
12 #include <linux/parser.h>
14 /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
15 static struct kmem_cache
*orangefs_inode_cache
;
17 /* list for storing orangefs specific superblocks in use */
18 LIST_HEAD(orangefs_superblocks
);
20 DEFINE_SPINLOCK(orangefs_superblocks_lock
);
30 static const match_table_t tokens
= {
33 { Opt_local_lock
, "local_lock" },
37 uint64_t orangefs_features
;
39 static int orangefs_show_options(struct seq_file
*m
, struct dentry
*root
)
41 struct orangefs_sb_info_s
*orangefs_sb
= ORANGEFS_SB(root
->d_sb
);
43 if (root
->d_sb
->s_flags
& MS_POSIXACL
)
45 if (orangefs_sb
->flags
& ORANGEFS_OPT_INTR
)
47 if (orangefs_sb
->flags
& ORANGEFS_OPT_LOCAL_LOCK
)
48 seq_puts(m
, ",local_lock");
52 static int parse_mount_options(struct super_block
*sb
, char *options
,
55 struct orangefs_sb_info_s
*orangefs_sb
= ORANGEFS_SB(sb
);
56 substring_t args
[MAX_OPT_ARGS
];
60 * Force any potential flags that might be set from the mount
61 * to zero, ie, initialize to unset.
63 sb
->s_flags
&= ~MS_POSIXACL
;
64 orangefs_sb
->flags
&= ~ORANGEFS_OPT_INTR
;
65 orangefs_sb
->flags
&= ~ORANGEFS_OPT_LOCAL_LOCK
;
67 while ((p
= strsep(&options
, ",")) != NULL
) {
73 token
= match_token(p
, tokens
, args
);
76 sb
->s_flags
|= MS_POSIXACL
;
79 orangefs_sb
->flags
|= ORANGEFS_OPT_INTR
;
82 orangefs_sb
->flags
|= ORANGEFS_OPT_LOCAL_LOCK
;
92 gossip_err("Error: mount option [%s] is not supported.\n", p
);
96 static void orangefs_inode_cache_ctor(void *req
)
98 struct orangefs_inode_s
*orangefs_inode
= req
;
100 inode_init_once(&orangefs_inode
->vfs_inode
);
101 init_rwsem(&orangefs_inode
->xattr_sem
);
103 orangefs_inode
->vfs_inode
.i_version
= 1;
106 static struct inode
*orangefs_alloc_inode(struct super_block
*sb
)
108 struct orangefs_inode_s
*orangefs_inode
;
110 orangefs_inode
= kmem_cache_alloc(orangefs_inode_cache
, GFP_KERNEL
);
115 * We want to clear everything except for rw_semaphore and the
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;
124 gossip_debug(GOSSIP_SUPER_DEBUG
,
125 "orangefs_alloc_inode: allocated %p\n",
126 &orangefs_inode
->vfs_inode
);
127 return &orangefs_inode
->vfs_inode
;
130 static void orangefs_i_callback(struct rcu_head
*head
)
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
);
137 static void orangefs_destroy_inode(struct inode
*inode
)
139 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
141 gossip_debug(GOSSIP_SUPER_DEBUG
,
142 "%s: deallocated %p destroying inode %pU\n",
143 __func__
, orangefs_inode
, get_khandle_from_ino(inode
));
145 call_rcu(&inode
->i_rcu
, orangefs_i_callback
);
149 * NOTE: information filled in here is typically reflected in the
150 * output of the system command 'df'
152 static int orangefs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
155 struct orangefs_kernel_op_s
*new_op
= NULL
;
157 struct super_block
*sb
= NULL
;
161 gossip_debug(GOSSIP_SUPER_DEBUG
,
162 "orangefs_statfs: called on sb %p (fs_id is %d)\n",
164 (int)(ORANGEFS_SB(sb
)->fs_id
));
166 new_op
= op_alloc(ORANGEFS_VFS_OP_STATFS
);
169 new_op
->upcall
.req
.statfs
.fs_id
= ORANGEFS_SB(sb
)->fs_id
;
171 if (ORANGEFS_SB(sb
)->flags
& ORANGEFS_OPT_INTR
)
172 flags
= ORANGEFS_OP_INTERRUPTIBLE
;
174 ret
= service_operation(new_op
, "orangefs_statfs", flags
);
176 if (new_op
->downcall
.status
< 0)
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",
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
);
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
;
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
= sb
->s_blocksize
;
204 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_statfs: returning %d\n", ret
);
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.
212 static int orangefs_remount_fs(struct super_block
*sb
, int *flags
, char *data
)
214 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_remount_fs: called\n");
215 return parse_mount_options(sb
, data
, 1);
219 * Remount as initiated by pvfs2-client-core on restart. This is used to
220 * repopulate mount information left from previous pvfs2-client-core.
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!
233 int orangefs_remount(struct orangefs_sb_info_s
*orangefs_sb
)
235 struct orangefs_kernel_op_s
*new_op
;
238 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_remount: called\n");
240 new_op
= op_alloc(ORANGEFS_VFS_OP_FS_MOUNT
);
243 strncpy(new_op
->upcall
.req
.fs_mount
.orangefs_config_server
,
244 orangefs_sb
->devname
,
245 ORANGEFS_MAX_SERVER_ADDR_LEN
);
247 gossip_debug(GOSSIP_SUPER_DEBUG
,
248 "Attempting ORANGEFS Remount via host %s\n",
249 new_op
->upcall
.req
.fs_mount
.orangefs_config_server
);
252 * we assume that the calling function has already acquired the
253 * request_mutex to prevent other operations from bypassing
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",
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
267 orangefs_sb
->id
= new_op
->downcall
.resp
.fs_mount
.id
;
268 orangefs_sb
->mount_pending
= 0;
273 if (orangefs_userspace_version
>= 20906) {
274 new_op
= op_alloc(ORANGEFS_VFS_OP_FEATURES
);
277 new_op
->upcall
.req
.features
.features
= 0;
278 ret
= service_operation(new_op
, "orangefs_features",
279 ORANGEFS_OP_PRIORITY
| ORANGEFS_OP_NO_MUTEX
);
282 new_op
->downcall
.resp
.features
.features
;
284 orangefs_features
= 0;
287 orangefs_features
= 0;
293 int fsid_key_table_initialize(void)
298 void fsid_key_table_finalize(void)
302 /* Called whenever the VFS dirties the inode in response to atime updates */
303 static void orangefs_dirty_inode(struct inode
*inode
, int flags
)
305 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
307 gossip_debug(GOSSIP_SUPER_DEBUG
,
308 "orangefs_dirty_inode: %pU\n",
309 get_khandle_from_ino(inode
));
310 SetAtimeFlag(orangefs_inode
);
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
,
323 static struct dentry
*orangefs_fh_to_dentry(struct super_block
*sb
,
328 struct orangefs_object_kref refn
;
330 if (fh_len
< 5 || fh_type
> 2)
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",
340 return d_obtain_alias(orangefs_iget(sb
, &refn
));
343 static int orangefs_encode_fh(struct inode
*inode
,
346 struct inode
*parent
)
348 int len
= parent
? 10 : 5;
350 struct orangefs_object_kref refn
;
352 if (*max_len
< len
) {
353 gossip_lerr("fh buffer is too small for encoding\n");
359 refn
= ORANGEFS_I(inode
)->refn
;
360 ORANGEFS_khandle_to(&refn
.khandle
, fh
, 16);
363 gossip_debug(GOSSIP_SUPER_DEBUG
,
364 "Encoding fh: handle %pU, fsid %u\n",
370 refn
= ORANGEFS_I(parent
)->refn
;
371 ORANGEFS_khandle_to(&refn
.khandle
, (char *) fh
+ 20, 16);
375 gossip_debug(GOSSIP_SUPER_DEBUG
,
376 "Encoding parent: handle %pU, fsid %u\n",
386 static const struct export_operations orangefs_export_ops
= {
387 .encode_fh
= orangefs_encode_fh
,
388 .fh_to_dentry
= orangefs_fh_to_dentry
,
391 static int orangefs_unmount(int id
, __s32 fs_id
, const char *devname
)
393 struct orangefs_kernel_op_s
*op
;
395 op
= op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT
);
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. */
405 gossip_err("orangefs_unmount: service_operation %d\n", r
);
410 static int orangefs_fill_sb(struct super_block
*sb
,
411 struct orangefs_fs_mount_response
*fs_mount
,
412 void *data
, int silent
)
415 struct inode
*root
= NULL
;
416 struct dentry
*root_dentry
= NULL
;
417 struct orangefs_object_kref root_object
;
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
))
423 ORANGEFS_SB(sb
)->sb
= sb
;
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
;
430 ret
= parse_mount_options(sb
, data
, silent
);
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
;
441 sb
->s_blocksize
= orangefs_bufmap_size_query();
442 sb
->s_blocksize_bits
= orangefs_bufmap_shift_query();
443 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
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
,
452 root
= orangefs_iget(sb
, &root_object
);
454 return PTR_ERR(root
);
456 gossip_debug(GOSSIP_SUPER_DEBUG
,
457 "Allocated root inode [%p] with mode %x\n",
461 /* allocates and places root dentry in dcache */
462 root_dentry
= d_make_root(root
);
466 sb
->s_export_op
= &orangefs_export_ops
;
467 sb
->s_root
= root_dentry
;
471 struct dentry
*orangefs_mount(struct file_system_type
*fst
,
477 struct super_block
*sb
= ERR_PTR(-EINVAL
);
478 struct orangefs_kernel_op_s
*new_op
;
479 struct dentry
*d
= ERR_PTR(-EINVAL
);
481 gossip_debug(GOSSIP_SUPER_DEBUG
,
482 "orangefs_mount: called with devname %s\n",
486 gossip_err("ERROR: device name not specified.\n");
487 return ERR_PTR(-EINVAL
);
490 new_op
= op_alloc(ORANGEFS_VFS_OP_FS_MOUNT
);
492 return ERR_PTR(-ENOMEM
);
494 strncpy(new_op
->upcall
.req
.fs_mount
.orangefs_config_server
,
496 ORANGEFS_MAX_SERVER_ADDR_LEN
);
498 gossip_debug(GOSSIP_SUPER_DEBUG
,
499 "Attempting ORANGEFS Mount via host %s\n",
500 new_op
->upcall
.req
.fs_mount
.orangefs_config_server
);
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
);
508 if (new_op
->downcall
.resp
.fs_mount
.fs_id
== ORANGEFS_FS_ID_NULL
) {
509 gossip_err("ERROR: Retrieved null fs_id\n");
514 sb
= sget(fst
, NULL
, set_anon_super
, flags
, NULL
);
518 orangefs_unmount(new_op
->downcall
.resp
.fs_mount
.id
,
519 new_op
->downcall
.resp
.fs_mount
.fs_id
, devname
);
523 ret
= orangefs_fill_sb(sb
,
524 &new_op
->downcall
.resp
.fs_mount
, data
,
525 flags
& MS_SILENT
? 1 : 0);
533 * on successful mount, store the devname and data
536 strncpy(ORANGEFS_SB(sb
)->devname
,
538 ORANGEFS_MAX_SERVER_ADDR_LEN
);
540 /* mount_pending must be cleared */
541 ORANGEFS_SB(sb
)->mount_pending
= 0;
544 * finally, add this sb to our list of known orangefs
547 gossip_debug(GOSSIP_SUPER_DEBUG
,
548 "Adding SB %p to orangefs superblocks\n",
550 spin_lock(&orangefs_superblocks_lock
);
551 list_add_tail(&ORANGEFS_SB(sb
)->list
, &orangefs_superblocks
);
552 spin_unlock(&orangefs_superblocks_lock
);
555 /* Must be removed from the list now. */
556 ORANGEFS_SB(sb
)->no_list
= 0;
558 if (orangefs_userspace_version
>= 20906) {
559 new_op
= op_alloc(ORANGEFS_VFS_OP_FEATURES
);
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
;
567 orangefs_features
= 0;
570 return dget(sb
->s_root
);
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
);
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");
589 void orangefs_kill_sb(struct super_block
*sb
)
592 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_kill_sb: called\n");
594 /* provided sb cleanup */
598 * issue the unmount to userspace to tell it to remove the
599 * dynamic mount info it has for this superblock
601 r
= orangefs_unmount(ORANGEFS_SB(sb
)->id
, ORANGEFS_SB(sb
)->fs_id
,
602 ORANGEFS_SB(sb
)->devname
);
604 ORANGEFS_SB(sb
)->mount_pending
= 1;
606 if (!ORANGEFS_SB(sb
)->no_list
) {
607 /* remove the sb from our list of orangefs specific sb's */
608 spin_lock(&orangefs_superblocks_lock
);
609 /* not list_del_init */
610 __list_del_entry(&ORANGEFS_SB(sb
)->list
);
611 ORANGEFS_SB(sb
)->list
.prev
= NULL
;
612 spin_unlock(&orangefs_superblocks_lock
);
616 * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
617 * gets completed before we free the dang thing.
619 mutex_lock(&orangefs_request_mutex
);
620 mutex_unlock(&orangefs_request_mutex
);
622 /* free the orangefs superblock private data */
623 kfree(ORANGEFS_SB(sb
));
626 int orangefs_inode_cache_initialize(void)
628 orangefs_inode_cache
= kmem_cache_create("orangefs_inode_cache",
629 sizeof(struct orangefs_inode_s
),
631 ORANGEFS_CACHE_CREATE_FLAGS
,
632 orangefs_inode_cache_ctor
);
634 if (!orangefs_inode_cache
) {
635 gossip_err("Cannot create orangefs_inode_cache\n");
641 int orangefs_inode_cache_finalize(void)
643 kmem_cache_destroy(orangefs_inode_cache
);