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
& SB_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
&= ~SB_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
|= SB_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
);
104 static struct inode
*orangefs_alloc_inode(struct super_block
*sb
)
106 struct orangefs_inode_s
*orangefs_inode
;
108 orangefs_inode
= kmem_cache_alloc(orangefs_inode_cache
, GFP_KERNEL
);
113 * We want to clear everything except for rw_semaphore and the
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
));
121 gossip_debug(GOSSIP_SUPER_DEBUG
,
122 "orangefs_alloc_inode: allocated %p\n",
123 &orangefs_inode
->vfs_inode
);
124 return &orangefs_inode
->vfs_inode
;
127 static void orangefs_i_callback(struct rcu_head
*head
)
129 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
130 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
131 kmem_cache_free(orangefs_inode_cache
, orangefs_inode
);
134 static void orangefs_destroy_inode(struct inode
*inode
)
136 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
138 gossip_debug(GOSSIP_SUPER_DEBUG
,
139 "%s: deallocated %p destroying inode %pU\n",
140 __func__
, orangefs_inode
, get_khandle_from_ino(inode
));
142 call_rcu(&inode
->i_rcu
, orangefs_i_callback
);
146 * NOTE: information filled in here is typically reflected in the
147 * output of the system command 'df'
149 static int orangefs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
152 struct orangefs_kernel_op_s
*new_op
= NULL
;
154 struct super_block
*sb
= NULL
;
158 gossip_debug(GOSSIP_SUPER_DEBUG
,
159 "%s: called on sb %p (fs_id is %d)\n",
162 (int)(ORANGEFS_SB(sb
)->fs_id
));
164 new_op
= op_alloc(ORANGEFS_VFS_OP_STATFS
);
167 new_op
->upcall
.req
.statfs
.fs_id
= ORANGEFS_SB(sb
)->fs_id
;
169 if (ORANGEFS_SB(sb
)->flags
& ORANGEFS_OPT_INTR
)
170 flags
= ORANGEFS_OP_INTERRUPTIBLE
;
172 ret
= service_operation(new_op
, "orangefs_statfs", flags
);
174 if (new_op
->downcall
.status
< 0)
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",
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
);
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
;
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
;
202 gossip_debug(GOSSIP_SUPER_DEBUG
, "%s: returning %d\n", __func__
, ret
);
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.
210 static int orangefs_remount_fs(struct super_block
*sb
, int *flags
, char *data
)
212 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_remount_fs: called\n");
213 return parse_mount_options(sb
, data
, 1);
217 * Remount as initiated by pvfs2-client-core on restart. This is used to
218 * repopulate mount information left from previous pvfs2-client-core.
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!
231 int orangefs_remount(struct orangefs_sb_info_s
*orangefs_sb
)
233 struct orangefs_kernel_op_s
*new_op
;
236 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_remount: called\n");
238 new_op
= op_alloc(ORANGEFS_VFS_OP_FS_MOUNT
);
241 strncpy(new_op
->upcall
.req
.fs_mount
.orangefs_config_server
,
242 orangefs_sb
->devname
,
243 ORANGEFS_MAX_SERVER_ADDR_LEN
);
245 gossip_debug(GOSSIP_SUPER_DEBUG
,
246 "Attempting ORANGEFS Remount via host %s\n",
247 new_op
->upcall
.req
.fs_mount
.orangefs_config_server
);
250 * we assume that the calling function has already acquired the
251 * request_mutex to prevent other operations from bypassing
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",
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
265 orangefs_sb
->id
= new_op
->downcall
.resp
.fs_mount
.id
;
266 orangefs_sb
->mount_pending
= 0;
271 if (orangefs_userspace_version
>= 20906) {
272 new_op
= op_alloc(ORANGEFS_VFS_OP_FEATURES
);
275 new_op
->upcall
.req
.features
.features
= 0;
276 ret
= service_operation(new_op
, "orangefs_features",
277 ORANGEFS_OP_PRIORITY
| ORANGEFS_OP_NO_MUTEX
);
280 new_op
->downcall
.resp
.features
.features
;
282 orangefs_features
= 0;
285 orangefs_features
= 0;
291 int fsid_key_table_initialize(void)
296 void fsid_key_table_finalize(void)
300 static const struct super_operations orangefs_s_ops
= {
301 .alloc_inode
= orangefs_alloc_inode
,
302 .destroy_inode
= orangefs_destroy_inode
,
303 .drop_inode
= generic_delete_inode
,
304 .statfs
= orangefs_statfs
,
305 .remount_fs
= orangefs_remount_fs
,
306 .show_options
= orangefs_show_options
,
309 static struct dentry
*orangefs_fh_to_dentry(struct super_block
*sb
,
314 struct orangefs_object_kref refn
;
316 if (fh_len
< 5 || fh_type
> 2)
319 ORANGEFS_khandle_from(&(refn
.khandle
), fid
->raw
, 16);
320 refn
.fs_id
= (u32
) fid
->raw
[4];
321 gossip_debug(GOSSIP_SUPER_DEBUG
,
322 "fh_to_dentry: handle %pU, fs_id %d\n",
326 return d_obtain_alias(orangefs_iget(sb
, &refn
));
329 static int orangefs_encode_fh(struct inode
*inode
,
332 struct inode
*parent
)
334 int len
= parent
? 10 : 5;
336 struct orangefs_object_kref refn
;
338 if (*max_len
< len
) {
339 gossip_err("fh buffer is too small for encoding\n");
345 refn
= ORANGEFS_I(inode
)->refn
;
346 ORANGEFS_khandle_to(&refn
.khandle
, fh
, 16);
349 gossip_debug(GOSSIP_SUPER_DEBUG
,
350 "Encoding fh: handle %pU, fsid %u\n",
356 refn
= ORANGEFS_I(parent
)->refn
;
357 ORANGEFS_khandle_to(&refn
.khandle
, (char *) fh
+ 20, 16);
361 gossip_debug(GOSSIP_SUPER_DEBUG
,
362 "Encoding parent: handle %pU, fsid %u\n",
372 static const struct export_operations orangefs_export_ops
= {
373 .encode_fh
= orangefs_encode_fh
,
374 .fh_to_dentry
= orangefs_fh_to_dentry
,
377 static int orangefs_unmount(int id
, __s32 fs_id
, const char *devname
)
379 struct orangefs_kernel_op_s
*op
;
381 op
= op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT
);
384 op
->upcall
.req
.fs_umount
.id
= id
;
385 op
->upcall
.req
.fs_umount
.fs_id
= fs_id
;
386 strncpy(op
->upcall
.req
.fs_umount
.orangefs_config_server
,
387 devname
, ORANGEFS_MAX_SERVER_ADDR_LEN
- 1);
388 r
= service_operation(op
, "orangefs_fs_umount", 0);
389 /* Not much to do about an error here. */
391 gossip_err("orangefs_unmount: service_operation %d\n", r
);
396 static int orangefs_fill_sb(struct super_block
*sb
,
397 struct orangefs_fs_mount_response
*fs_mount
,
398 void *data
, int silent
)
401 struct inode
*root
= NULL
;
402 struct dentry
*root_dentry
= NULL
;
403 struct orangefs_object_kref root_object
;
405 /* alloc and init our private orangefs sb info */
406 sb
->s_fs_info
= kzalloc(sizeof(struct orangefs_sb_info_s
), GFP_KERNEL
);
407 if (!ORANGEFS_SB(sb
))
409 ORANGEFS_SB(sb
)->sb
= sb
;
411 ORANGEFS_SB(sb
)->root_khandle
= fs_mount
->root_khandle
;
412 ORANGEFS_SB(sb
)->fs_id
= fs_mount
->fs_id
;
413 ORANGEFS_SB(sb
)->id
= fs_mount
->id
;
416 ret
= parse_mount_options(sb
, data
, silent
);
421 /* Hang the xattr handlers off the superblock */
422 sb
->s_xattr
= orangefs_xattr_handlers
;
423 sb
->s_magic
= ORANGEFS_SUPER_MAGIC
;
424 sb
->s_op
= &orangefs_s_ops
;
425 sb
->s_d_op
= &orangefs_dentry_operations
;
427 sb
->s_blocksize
= PAGE_SIZE
;
428 sb
->s_blocksize_bits
= PAGE_SHIFT
;
429 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
431 root_object
.khandle
= ORANGEFS_SB(sb
)->root_khandle
;
432 root_object
.fs_id
= ORANGEFS_SB(sb
)->fs_id
;
433 gossip_debug(GOSSIP_SUPER_DEBUG
,
434 "get inode %pU, fsid %d\n",
435 &root_object
.khandle
,
438 root
= orangefs_iget(sb
, &root_object
);
440 return PTR_ERR(root
);
442 gossip_debug(GOSSIP_SUPER_DEBUG
,
443 "Allocated root inode [%p] with mode %x\n",
447 /* allocates and places root dentry in dcache */
448 root_dentry
= d_make_root(root
);
452 sb
->s_export_op
= &orangefs_export_ops
;
453 sb
->s_root
= root_dentry
;
457 struct dentry
*orangefs_mount(struct file_system_type
*fst
,
463 struct super_block
*sb
= ERR_PTR(-EINVAL
);
464 struct orangefs_kernel_op_s
*new_op
;
465 struct dentry
*d
= ERR_PTR(-EINVAL
);
467 gossip_debug(GOSSIP_SUPER_DEBUG
,
468 "orangefs_mount: called with devname %s\n",
472 gossip_err("ERROR: device name not specified.\n");
473 return ERR_PTR(-EINVAL
);
476 new_op
= op_alloc(ORANGEFS_VFS_OP_FS_MOUNT
);
478 return ERR_PTR(-ENOMEM
);
480 strncpy(new_op
->upcall
.req
.fs_mount
.orangefs_config_server
,
482 ORANGEFS_MAX_SERVER_ADDR_LEN
- 1);
484 gossip_debug(GOSSIP_SUPER_DEBUG
,
485 "Attempting ORANGEFS Mount via host %s\n",
486 new_op
->upcall
.req
.fs_mount
.orangefs_config_server
);
488 ret
= service_operation(new_op
, "orangefs_mount", 0);
489 gossip_debug(GOSSIP_SUPER_DEBUG
,
490 "orangefs_mount: mount got return value of %d\n", ret
);
494 if (new_op
->downcall
.resp
.fs_mount
.fs_id
== ORANGEFS_FS_ID_NULL
) {
495 gossip_err("ERROR: Retrieved null fs_id\n");
500 sb
= sget(fst
, NULL
, set_anon_super
, flags
, NULL
);
504 orangefs_unmount(new_op
->downcall
.resp
.fs_mount
.id
,
505 new_op
->downcall
.resp
.fs_mount
.fs_id
, devname
);
509 ret
= orangefs_fill_sb(sb
,
510 &new_op
->downcall
.resp
.fs_mount
, data
,
511 flags
& SB_SILENT
? 1 : 0);
519 * on successful mount, store the devname and data
522 strncpy(ORANGEFS_SB(sb
)->devname
,
524 ORANGEFS_MAX_SERVER_ADDR_LEN
- 1);
526 /* mount_pending must be cleared */
527 ORANGEFS_SB(sb
)->mount_pending
= 0;
530 * finally, add this sb to our list of known orangefs
533 gossip_debug(GOSSIP_SUPER_DEBUG
,
534 "Adding SB %p to orangefs superblocks\n",
536 spin_lock(&orangefs_superblocks_lock
);
537 list_add_tail(&ORANGEFS_SB(sb
)->list
, &orangefs_superblocks
);
538 spin_unlock(&orangefs_superblocks_lock
);
541 /* Must be removed from the list now. */
542 ORANGEFS_SB(sb
)->no_list
= 0;
544 if (orangefs_userspace_version
>= 20906) {
545 new_op
= op_alloc(ORANGEFS_VFS_OP_FEATURES
);
547 return ERR_PTR(-ENOMEM
);
548 new_op
->upcall
.req
.features
.features
= 0;
549 ret
= service_operation(new_op
, "orangefs_features", 0);
550 orangefs_features
= new_op
->downcall
.resp
.features
.features
;
553 orangefs_features
= 0;
556 return dget(sb
->s_root
);
559 /* Will call orangefs_kill_sb with sb not in list. */
560 ORANGEFS_SB(sb
)->no_list
= 1;
561 /* ORANGEFS_VFS_OP_FS_UMOUNT is done by orangefs_kill_sb. */
562 deactivate_locked_super(sb
);
564 gossip_err("orangefs_mount: mount request failed with %d\n", ret
);
565 if (ret
== -EINVAL
) {
566 gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
567 gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
575 void orangefs_kill_sb(struct super_block
*sb
)
578 gossip_debug(GOSSIP_SUPER_DEBUG
, "orangefs_kill_sb: called\n");
580 /* provided sb cleanup */
583 if (!ORANGEFS_SB(sb
)) {
584 mutex_lock(&orangefs_request_mutex
);
585 mutex_unlock(&orangefs_request_mutex
);
589 * issue the unmount to userspace to tell it to remove the
590 * dynamic mount info it has for this superblock
592 r
= orangefs_unmount(ORANGEFS_SB(sb
)->id
, ORANGEFS_SB(sb
)->fs_id
,
593 ORANGEFS_SB(sb
)->devname
);
595 ORANGEFS_SB(sb
)->mount_pending
= 1;
597 if (!ORANGEFS_SB(sb
)->no_list
) {
598 /* remove the sb from our list of orangefs specific sb's */
599 spin_lock(&orangefs_superblocks_lock
);
600 /* not list_del_init */
601 __list_del_entry(&ORANGEFS_SB(sb
)->list
);
602 ORANGEFS_SB(sb
)->list
.prev
= NULL
;
603 spin_unlock(&orangefs_superblocks_lock
);
607 * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
608 * gets completed before we free the dang thing.
610 mutex_lock(&orangefs_request_mutex
);
611 mutex_unlock(&orangefs_request_mutex
);
613 /* free the orangefs superblock private data */
614 kfree(ORANGEFS_SB(sb
));
617 int orangefs_inode_cache_initialize(void)
619 orangefs_inode_cache
= kmem_cache_create_usercopy(
620 "orangefs_inode_cache",
621 sizeof(struct orangefs_inode_s
),
623 ORANGEFS_CACHE_CREATE_FLAGS
,
624 offsetof(struct orangefs_inode_s
,
626 sizeof_field(struct orangefs_inode_s
,
628 orangefs_inode_cache_ctor
);
630 if (!orangefs_inode_cache
) {
631 gossip_err("Cannot create orangefs_inode_cache\n");
637 int orangefs_inode_cache_finalize(void)
639 kmem_cache_destroy(orangefs_inode_cache
);