blob: e962517179666820177fd3cd9a5afa6027d13145 [file] [log] [blame]
Mike Marshallf7ab0932015-07-17 10:38:11 -04001/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7/*
8 * The PVFS2 Linux kernel support allows PVFS2 volumes to be mounted and
9 * accessed through the Linux VFS (i.e. using standard I/O system calls).
10 * This support is only needed on clients that wish to mount the file system.
11 *
12 */
13
14/*
15 * Declarations and macros for the PVFS2 Linux kernel support.
16 */
17
18#ifndef __PVFS2KERNEL_H
19#define __PVFS2KERNEL_H
20
21#include <linux/kernel.h>
22#include <linux/moduleparam.h>
23#include <linux/statfs.h>
24#include <linux/backing-dev.h>
25#include <linux/device.h>
26#include <linux/mpage.h>
27#include <linux/namei.h>
28#include <linux/errno.h>
29#include <linux/init.h>
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/types.h>
33#include <linux/fs.h>
34#include <linux/vmalloc.h>
35
36#include <linux/aio.h>
37#include <linux/posix_acl.h>
38#include <linux/posix_acl_xattr.h>
39#include <linux/compat.h>
40#include <linux/mount.h>
41#include <linux/uaccess.h>
42#include <linux/atomic.h>
43#include <linux/uio.h>
44#include <linux/sched.h>
45#include <linux/mm.h>
46#include <linux/wait.h>
47#include <linux/dcache.h>
48#include <linux/pagemap.h>
49#include <linux/poll.h>
50#include <linux/rwsem.h>
51#include <linux/xattr.h>
52#include <linux/exportfs.h>
53
54#include <asm/unaligned.h>
55
56#include "pvfs2-dev-proto.h"
57
58#ifdef PVFS2_KERNEL_DEBUG
59#define PVFS2_DEFAULT_OP_TIMEOUT_SECS 10
60#else
61#define PVFS2_DEFAULT_OP_TIMEOUT_SECS 20
62#endif
63
64#define PVFS2_BUFMAP_WAIT_TIMEOUT_SECS 30
65
66#define PVFS2_DEFAULT_SLOT_TIMEOUT_SECS 900 /* 15 minutes */
67
68#define PVFS2_REQDEVICE_NAME "pvfs2-req"
69
70#define PVFS2_DEVREQ_MAGIC 0x20030529
71#define PVFS2_LINK_MAX 0x000000FF
72#define PVFS2_PURGE_RETRY_COUNT 0x00000005
73#define PVFS2_SEEK_END 0x00000002
74#define PVFS2_MAX_NUM_OPTIONS 0x00000004
75#define PVFS2_MAX_MOUNT_OPT_LEN 0x00000080
76#define PVFS2_MAX_FSKEY_LEN 64
77
78#define MAX_DEV_REQ_UPSIZE (2*sizeof(__s32) + \
79sizeof(__u64) + sizeof(struct pvfs2_upcall_s))
80#define MAX_DEV_REQ_DOWNSIZE (2*sizeof(__s32) + \
81sizeof(__u64) + sizeof(struct pvfs2_downcall_s))
82
83#define BITS_PER_LONG_DIV_8 (BITS_PER_LONG >> 3)
84
85/* borrowed from irda.h */
86#ifndef MSECS_TO_JIFFIES
87#define MSECS_TO_JIFFIES(ms) (((ms)*HZ+999)/1000)
88#endif
89
90#define MAX_ALIGNED_DEV_REQ_UPSIZE \
91 (MAX_DEV_REQ_UPSIZE + \
92 ((((MAX_DEV_REQ_UPSIZE / \
93 (BITS_PER_LONG_DIV_8)) * \
94 (BITS_PER_LONG_DIV_8)) + \
95 (BITS_PER_LONG_DIV_8)) - \
96 MAX_DEV_REQ_UPSIZE))
97
98#define MAX_ALIGNED_DEV_REQ_DOWNSIZE \
99 (MAX_DEV_REQ_DOWNSIZE + \
100 ((((MAX_DEV_REQ_DOWNSIZE / \
101 (BITS_PER_LONG_DIV_8)) * \
102 (BITS_PER_LONG_DIV_8)) + \
103 (BITS_PER_LONG_DIV_8)) - \
104 MAX_DEV_REQ_DOWNSIZE))
105
106/*
107 * valid pvfs2 kernel operation states
108 *
109 * unknown - op was just initialized
110 * waiting - op is on request_list (upward bound)
111 * inprogr - op is in progress (waiting for downcall)
112 * serviced - op has matching downcall; ok
113 * purged - op has to start a timer since client-core
114 * exited uncleanly before servicing op
115 */
116enum pvfs2_vfs_op_states {
117 OP_VFS_STATE_UNKNOWN = 0,
118 OP_VFS_STATE_WAITING = 1,
119 OP_VFS_STATE_INPROGR = 2,
120 OP_VFS_STATE_SERVICED = 4,
121 OP_VFS_STATE_PURGED = 8,
122};
123
124#define set_op_state_waiting(op) ((op)->op_state = OP_VFS_STATE_WAITING)
125#define set_op_state_inprogress(op) ((op)->op_state = OP_VFS_STATE_INPROGR)
126#define set_op_state_serviced(op) ((op)->op_state = OP_VFS_STATE_SERVICED)
127#define set_op_state_purged(op) ((op)->op_state |= OP_VFS_STATE_PURGED)
128
129#define op_state_waiting(op) ((op)->op_state & OP_VFS_STATE_WAITING)
130#define op_state_in_progress(op) ((op)->op_state & OP_VFS_STATE_INPROGR)
131#define op_state_serviced(op) ((op)->op_state & OP_VFS_STATE_SERVICED)
132#define op_state_purged(op) ((op)->op_state & OP_VFS_STATE_PURGED)
133
134#define get_op(op) \
135 do { \
136 atomic_inc(&(op)->aio_ref_count); \
137 gossip_debug(GOSSIP_DEV_DEBUG, \
138 "(get) Alloced OP (%p:%llu)\n", \
139 op, \
140 llu((op)->tag)); \
141 } while (0)
142
143#define put_op(op) \
144 do { \
145 if (atomic_sub_and_test(1, &(op)->aio_ref_count) == 1) { \
146 gossip_debug(GOSSIP_DEV_DEBUG, \
147 "(put) Releasing OP (%p:%llu)\n", \
148 op, \
149 llu((op)->tag)); \
150 op_release(op); \
151 } \
152 } while (0)
153
154#define op_wait(op) (atomic_read(&(op)->aio_ref_count) <= 2 ? 0 : 1)
155
156/*
157 * Defines for controlling whether I/O upcalls are for async or sync operations
158 */
159enum PVFS_async_io_type {
160 PVFS_VFS_SYNC_IO = 0,
161 PVFS_VFS_ASYNC_IO = 1,
162};
163
164/*
165 * An array of client_debug_mask will be built to hold debug keyword/mask
166 * values fetched from userspace.
167 */
168struct client_debug_mask {
169 char *keyword;
170 __u64 mask1;
171 __u64 mask2;
172};
173
174/*
175 * pvfs2 kernel memory related flags
176 */
177
178#if ((defined PVFS2_KERNEL_DEBUG) && (defined CONFIG_DEBUG_SLAB))
179#define PVFS2_CACHE_CREATE_FLAGS SLAB_RED_ZONE
180#else
181#define PVFS2_CACHE_CREATE_FLAGS 0
182#endif /* ((defined PVFS2_KERNEL_DEBUG) && (defined CONFIG_DEBUG_SLAB)) */
183
184#define PVFS2_CACHE_ALLOC_FLAGS (GFP_KERNEL)
185#define PVFS2_GFP_FLAGS (GFP_KERNEL)
186#define PVFS2_BUFMAP_GFP_FLAGS (GFP_KERNEL)
187
Mike Marshallf7ab0932015-07-17 10:38:11 -0400188/* pvfs2 xattr and acl related defines */
189#define PVFS2_XATTR_INDEX_POSIX_ACL_ACCESS 1
190#define PVFS2_XATTR_INDEX_POSIX_ACL_DEFAULT 2
191#define PVFS2_XATTR_INDEX_TRUSTED 3
192#define PVFS2_XATTR_INDEX_DEFAULT 4
193
194#if 0
195#ifndef POSIX_ACL_XATTR_ACCESS
196#define POSIX_ACL_XATTR_ACCESS "system.posix_acl_access"
197#endif
198#ifndef POSIX_ACL_XATTR_DEFAULT
199#define POSIX_ACL_XATTR_DEFAULT "system.posix_acl_default"
200#endif
201#endif
202
203#define PVFS2_XATTR_NAME_ACL_ACCESS POSIX_ACL_XATTR_ACCESS
204#define PVFS2_XATTR_NAME_ACL_DEFAULT POSIX_ACL_XATTR_DEFAULT
205#define PVFS2_XATTR_NAME_TRUSTED_PREFIX "trusted."
206#define PVFS2_XATTR_NAME_DEFAULT_PREFIX ""
207
208/* these functions are defined in pvfs2-utils.c */
209int orangefs_prepare_cdm_array(char *debug_array_string);
210int orangefs_prepare_debugfs_help_string(int);
211
212/* defined in pvfs2-debugfs.c */
213int pvfs2_client_debug_init(void);
214
215void debug_string_to_mask(char *, void *, int);
216void do_c_mask(int, char *, struct client_debug_mask **);
217void do_k_mask(int, char *, __u64 **);
218
219void debug_mask_to_string(void *, int);
220void do_k_string(void *, int);
221void do_c_string(void *, int);
222int check_amalgam_keyword(void *, int);
223int keyword_is_amalgam(char *);
224
225/*these variables are defined in pvfs2-mod.c */
226extern char kernel_debug_string[PVFS2_MAX_DEBUG_STRING_LEN];
227extern char client_debug_string[PVFS2_MAX_DEBUG_STRING_LEN];
228extern char client_debug_array_string[PVFS2_MAX_DEBUG_STRING_LEN];
Mike Marshallf7ab0932015-07-17 10:38:11 -0400229extern unsigned int kernel_mask_set_mod_init;
230
231extern int pvfs2_init_acl(struct inode *inode, struct inode *dir);
232extern const struct xattr_handler *pvfs2_xattr_handlers[];
233
234extern struct posix_acl *pvfs2_get_acl(struct inode *inode, int type);
235extern int pvfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type);
236
237int pvfs2_xattr_set_default(struct dentry *dentry,
238 const char *name,
239 const void *buffer,
240 size_t size,
241 int flags,
242 int handler_flags);
243
244int pvfs2_xattr_get_default(struct dentry *dentry,
245 const char *name,
246 void *buffer,
247 size_t size,
248 int handler_flags);
249
250/*
251 * Redefine xtvec structure so that we could move helper functions out of
252 * the define
253 */
254struct xtvec {
255 __kernel_off_t xtv_off; /* must be off_t */
256 __kernel_size_t xtv_len; /* must be size_t */
257};
258
259/*
260 * pvfs2 data structures
261 */
262struct pvfs2_kernel_op_s {
263 enum pvfs2_vfs_op_states op_state;
264 __u64 tag;
265
266 /*
267 * Set uses_shared_memory to 1 if this operation uses shared memory.
268 * If true, then a retry on the op must also get a new shared memory
269 * buffer and re-populate it.
270 */
271 int uses_shared_memory;
272
273 struct pvfs2_upcall_s upcall;
274 struct pvfs2_downcall_s downcall;
275
276 wait_queue_head_t waitq;
277 spinlock_t lock;
278
279 int io_completed;
280 wait_queue_head_t io_completion_waitq;
281
282 /*
283 * upcalls requiring variable length trailers require that this struct
284 * be in the request list even after client-core does a read() on the
285 * device to dequeue the upcall.
286 * if op_linger field goes to 0, we dequeue this op off the list.
287 * else we let it stay. What gets passed to the read() is
288 * a) if op_linger field is = 1, pvfs2_kernel_op_s itself
289 * b) else if = 0, we pass ->upcall.trailer_buf
290 * We expect to have only a single upcall trailer buffer,
291 * so we expect callers with trailers
292 * to set this field to 2 and others to set it to 1.
293 */
294 __s32 op_linger, op_linger_tmp;
295 /* VFS aio fields */
296
297 /* used by the async I/O code to stash the pvfs2_kiocb_s structure */
298 void *priv;
299
300 /* used again for the async I/O code for deallocation */
301 atomic_t aio_ref_count;
302
303 int attempts;
304
305 struct list_head list;
306};
307
308/* per inode private pvfs2 info */
309struct pvfs2_inode_s {
310 struct pvfs2_object_kref refn;
311 char link_target[PVFS_NAME_MAX];
312 __s64 blksize;
313 /*
314 * Reading/Writing Extended attributes need to acquire the appropriate
315 * reader/writer semaphore on the pvfs2_inode_s structure.
316 */
317 struct rw_semaphore xattr_sem;
318
319 struct inode vfs_inode;
320 sector_t last_failed_block_index_read;
321
322 /*
323 * State of in-memory attributes not yet flushed to disk associated
324 * with this object
325 */
326 unsigned long pinode_flags;
327
328 /* All allocated pvfs2_inode_s objects are chained to a list */
329 struct list_head list;
330};
331
332#define P_ATIME_FLAG 0
333#define P_MTIME_FLAG 1
334#define P_CTIME_FLAG 2
335#define P_MODE_FLAG 3
336
337#define ClearAtimeFlag(pinode) clear_bit(P_ATIME_FLAG, &(pinode)->pinode_flags)
338#define SetAtimeFlag(pinode) set_bit(P_ATIME_FLAG, &(pinode)->pinode_flags)
339#define AtimeFlag(pinode) test_bit(P_ATIME_FLAG, &(pinode)->pinode_flags)
340
341#define ClearMtimeFlag(pinode) clear_bit(P_MTIME_FLAG, &(pinode)->pinode_flags)
342#define SetMtimeFlag(pinode) set_bit(P_MTIME_FLAG, &(pinode)->pinode_flags)
343#define MtimeFlag(pinode) test_bit(P_MTIME_FLAG, &(pinode)->pinode_flags)
344
345#define ClearCtimeFlag(pinode) clear_bit(P_CTIME_FLAG, &(pinode)->pinode_flags)
346#define SetCtimeFlag(pinode) set_bit(P_CTIME_FLAG, &(pinode)->pinode_flags)
347#define CtimeFlag(pinode) test_bit(P_CTIME_FLAG, &(pinode)->pinode_flags)
348
349#define ClearModeFlag(pinode) clear_bit(P_MODE_FLAG, &(pinode)->pinode_flags)
350#define SetModeFlag(pinode) set_bit(P_MODE_FLAG, &(pinode)->pinode_flags)
351#define ModeFlag(pinode) test_bit(P_MODE_FLAG, &(pinode)->pinode_flags)
352
353/* per superblock private pvfs2 info */
354struct pvfs2_sb_info_s {
355 struct pvfs2_khandle root_khandle;
356 __s32 fs_id;
357 int id;
358 int flags;
359#define PVFS2_OPT_INTR 0x01
360#define PVFS2_OPT_LOCAL_LOCK 0x02
361 char devname[PVFS_MAX_SERVER_ADDR_LEN];
362 struct super_block *sb;
363 int mount_pending;
364 struct list_head list;
365};
366
367/*
Mike Marshallf7ab0932015-07-17 10:38:11 -0400368 * structure that holds the state of any async I/O operation issued
369 * through the VFS. Needed especially to handle cancellation requests
370 * or even completion notification so that the VFS client-side daemon
371 * can free up its vfs_request slots.
372 */
373struct pvfs2_kiocb_s {
374 /* the pointer to the task that initiated the AIO */
375 struct task_struct *tsk;
376
377 /* pointer to the kiocb that kicked this operation */
378 struct kiocb *kiocb;
379
380 /* buffer index that was used for the I/O */
381 struct pvfs2_bufmap *bufmap;
382 int buffer_index;
383
384 /* pvfs2 kernel operation type */
385 struct pvfs2_kernel_op_s *op;
386
387 /* The user space buffers from/to which I/O is being staged */
388 struct iovec *iov;
389
390 /* number of elements in the iovector */
391 unsigned long nr_segs;
392
393 /* set to indicate the type of the operation */
394 int rw;
395
396 /* file offset */
397 loff_t offset;
398
399 /* and the count in bytes */
400 size_t bytes_to_be_copied;
401
402 ssize_t bytes_copied;
403 int needs_cleanup;
404};
405
406struct pvfs2_stats {
407 unsigned long cache_hits;
408 unsigned long cache_misses;
409 unsigned long reads;
410 unsigned long writes;
411};
412
413extern struct pvfs2_stats g_pvfs2_stats;
414
415/*
Mike Marshall54804942015-10-05 13:44:24 -0400416 * NOTE: See Documentation/filesystems/porting for information
417 * on implementing FOO_I and properly accessing fs private data
418 */
Mike Marshallf7ab0932015-07-17 10:38:11 -0400419static inline struct pvfs2_inode_s *PVFS2_I(struct inode *inode)
420{
421 return container_of(inode, struct pvfs2_inode_s, vfs_inode);
422}
423
424static inline struct pvfs2_sb_info_s *PVFS2_SB(struct super_block *sb)
425{
426 return (struct pvfs2_sb_info_s *) sb->s_fs_info;
427}
428
429/* ino_t descends from "unsigned long", 8 bytes, 64 bits. */
430static inline ino_t pvfs2_khandle_to_ino(struct pvfs2_khandle *khandle)
431{
432 union {
433 unsigned char u[8];
434 __u64 ino;
435 } ihandle;
436
437 ihandle.u[0] = khandle->u[0] ^ khandle->u[4];
438 ihandle.u[1] = khandle->u[1] ^ khandle->u[5];
439 ihandle.u[2] = khandle->u[2] ^ khandle->u[6];
440 ihandle.u[3] = khandle->u[3] ^ khandle->u[7];
441 ihandle.u[4] = khandle->u[12] ^ khandle->u[8];
442 ihandle.u[5] = khandle->u[13] ^ khandle->u[9];
443 ihandle.u[6] = khandle->u[14] ^ khandle->u[10];
444 ihandle.u[7] = khandle->u[15] ^ khandle->u[11];
445
446 return ihandle.ino;
447}
448
449static inline struct pvfs2_khandle *get_khandle_from_ino(struct inode *inode)
450{
451 return &(PVFS2_I(inode)->refn.khandle);
452}
453
454static inline __s32 get_fsid_from_ino(struct inode *inode)
455{
456 return PVFS2_I(inode)->refn.fs_id;
457}
458
459static inline ino_t get_ino_from_khandle(struct inode *inode)
460{
461 struct pvfs2_khandle *khandle;
462 ino_t ino;
463
464 khandle = get_khandle_from_ino(inode);
465 ino = pvfs2_khandle_to_ino(khandle);
466 return ino;
467}
468
469static inline ino_t get_parent_ino_from_dentry(struct dentry *dentry)
470{
471 return get_ino_from_khandle(dentry->d_parent->d_inode);
472}
473
474static inline int is_root_handle(struct inode *inode)
475{
476 gossip_debug(GOSSIP_DCACHE_DEBUG,
477 "%s: root handle: %pU, this handle: %pU:\n",
478 __func__,
479 &PVFS2_SB(inode->i_sb)->root_khandle,
480 get_khandle_from_ino(inode));
481
482 if (PVFS_khandle_cmp(&(PVFS2_SB(inode->i_sb)->root_khandle),
483 get_khandle_from_ino(inode)))
484 return 0;
485 else
486 return 1;
487}
488
489static inline int match_handle(struct pvfs2_khandle resp_handle,
490 struct inode *inode)
491{
492 gossip_debug(GOSSIP_DCACHE_DEBUG,
493 "%s: one handle: %pU, another handle:%pU:\n",
494 __func__,
495 &resp_handle,
496 get_khandle_from_ino(inode));
497
498 if (PVFS_khandle_cmp(&resp_handle, get_khandle_from_ino(inode)))
499 return 0;
500 else
501 return 1;
502}
503
504/*
505 * defined in pvfs2-cache.c
506 */
507int op_cache_initialize(void);
508int op_cache_finalize(void);
509struct pvfs2_kernel_op_s *op_alloc(__s32 type);
510struct pvfs2_kernel_op_s *op_alloc_trailer(__s32 type);
511char *get_opname_string(struct pvfs2_kernel_op_s *new_op);
512void op_release(struct pvfs2_kernel_op_s *op);
513
514int dev_req_cache_initialize(void);
515int dev_req_cache_finalize(void);
516void *dev_req_alloc(void);
517void dev_req_release(void *);
518
519int pvfs2_inode_cache_initialize(void);
520int pvfs2_inode_cache_finalize(void);
521
522int kiocb_cache_initialize(void);
523int kiocb_cache_finalize(void);
524struct pvfs2_kiocb_s *kiocb_alloc(void);
525void kiocb_release(struct pvfs2_kiocb_s *ptr);
526
527/*
528 * defined in pvfs2-mod.c
529 */
530void purge_inprogress_ops(void);
531
532/*
533 * defined in waitqueue.c
534 */
535int wait_for_matching_downcall(struct pvfs2_kernel_op_s *op);
536int wait_for_cancellation_downcall(struct pvfs2_kernel_op_s *op);
537void pvfs2_clean_up_interrupted_operation(struct pvfs2_kernel_op_s *op);
538void purge_waiting_ops(void);
539
540/*
541 * defined in super.c
542 */
543struct dentry *pvfs2_mount(struct file_system_type *fst,
544 int flags,
545 const char *devname,
546 void *data);
547
548void pvfs2_kill_sb(struct super_block *sb);
549int pvfs2_remount(struct super_block *sb);
550
551int fsid_key_table_initialize(void);
552void fsid_key_table_finalize(void);
553
554/*
555 * defined in inode.c
556 */
557__u32 convert_to_pvfs2_mask(unsigned long lite_mask);
558struct inode *pvfs2_new_inode(struct super_block *sb,
559 struct inode *dir,
560 int mode,
561 dev_t dev,
562 struct pvfs2_object_kref *ref);
563
564int pvfs2_setattr(struct dentry *dentry, struct iattr *iattr);
565
566int pvfs2_getattr(struct vfsmount *mnt,
567 struct dentry *dentry,
568 struct kstat *kstat);
569
570/*
571 * defined in xattr.c
572 */
573int pvfs2_setxattr(struct dentry *dentry,
574 const char *name,
575 const void *value,
576 size_t size,
577 int flags);
578
579ssize_t pvfs2_getxattr(struct dentry *dentry,
580 const char *name,
581 void *buffer,
582 size_t size);
583
584ssize_t pvfs2_listxattr(struct dentry *dentry, char *buffer, size_t size);
585
586/*
587 * defined in namei.c
588 */
589struct inode *pvfs2_iget(struct super_block *sb,
590 struct pvfs2_object_kref *ref);
591
592ssize_t pvfs2_inode_read(struct inode *inode,
Al Viro74f68fc2015-10-08 18:31:05 -0400593 struct iov_iter *iter,
Mike Marshallf7ab0932015-07-17 10:38:11 -0400594 loff_t *offset,
595 loff_t readahead_size);
596
597/*
598 * defined in devpvfs2-req.c
599 */
600int pvfs2_dev_init(void);
601void pvfs2_dev_cleanup(void);
602int is_daemon_in_service(void);
603int fs_mount_pending(__s32 fsid);
604
605/*
606 * defined in pvfs2-utils.c
607 */
608__s32 fsid_of_op(struct pvfs2_kernel_op_s *op);
609
610int pvfs2_flush_inode(struct inode *inode);
611
612ssize_t pvfs2_inode_getxattr(struct inode *inode,
613 const char *prefix,
614 const char *name,
615 void *buffer,
616 size_t size);
617
618int pvfs2_inode_setxattr(struct inode *inode,
619 const char *prefix,
620 const char *name,
621 const void *value,
622 size_t size,
623 int flags);
624
625int pvfs2_inode_getattr(struct inode *inode, __u32 mask);
626
627int pvfs2_inode_setattr(struct inode *inode, struct iattr *iattr);
628
629void pvfs2_op_initialize(struct pvfs2_kernel_op_s *op);
630
631void pvfs2_make_bad_inode(struct inode *inode);
632
Mike Marshall8c3905a2015-09-29 12:07:46 -0400633void block_signals(sigset_t *);
Mike Marshallf7ab0932015-07-17 10:38:11 -0400634
Mike Marshall8c3905a2015-09-29 12:07:46 -0400635void set_signals(sigset_t *);
Mike Marshallf7ab0932015-07-17 10:38:11 -0400636
637int pvfs2_unmount_sb(struct super_block *sb);
638
639int pvfs2_cancel_op_in_progress(__u64 tag);
640
Al Viro57141562015-10-08 22:02:00 -0400641static inline __u64 pvfs2_convert_time_field(const struct timespec *ts)
642{
643 return (__u64)ts->tv_sec;
644}
Mike Marshallf7ab0932015-07-17 10:38:11 -0400645
646int pvfs2_normalize_to_errno(__s32 error_code);
647
648extern struct mutex devreq_mutex;
649extern struct mutex request_mutex;
650extern int debug;
651extern int op_timeout_secs;
652extern int slot_timeout_secs;
653extern struct list_head pvfs2_superblocks;
654extern spinlock_t pvfs2_superblocks_lock;
655extern struct list_head pvfs2_request_list;
656extern spinlock_t pvfs2_request_list_lock;
657extern wait_queue_head_t pvfs2_request_list_waitq;
658extern struct list_head *htable_ops_in_progress;
659extern spinlock_t htable_ops_in_progress_lock;
660extern int hash_table_size;
661
662extern const struct address_space_operations pvfs2_address_operations;
663extern struct backing_dev_info pvfs2_backing_dev_info;
664extern struct inode_operations pvfs2_file_inode_operations;
665extern const struct file_operations pvfs2_file_operations;
666extern struct inode_operations pvfs2_symlink_inode_operations;
667extern struct inode_operations pvfs2_dir_inode_operations;
668extern const struct file_operations pvfs2_dir_operations;
669extern const struct dentry_operations pvfs2_dentry_operations;
670extern const struct file_operations pvfs2_devreq_file_operations;
671
672extern wait_queue_head_t pvfs2_bufmap_init_waitq;
673
674/*
675 * misc convenience macros
676 */
677#define add_op_to_request_list(op) \
678do { \
679 spin_lock(&pvfs2_request_list_lock); \
680 spin_lock(&op->lock); \
681 set_op_state_waiting(op); \
682 list_add_tail(&op->list, &pvfs2_request_list); \
683 spin_unlock(&pvfs2_request_list_lock); \
684 spin_unlock(&op->lock); \
685 wake_up_interruptible(&pvfs2_request_list_waitq); \
686} while (0)
687
688#define add_priority_op_to_request_list(op) \
689 do { \
690 spin_lock(&pvfs2_request_list_lock); \
691 spin_lock(&op->lock); \
692 set_op_state_waiting(op); \
693 \
694 list_add(&op->list, &pvfs2_request_list); \
695 spin_unlock(&pvfs2_request_list_lock); \
696 spin_unlock(&op->lock); \
697 wake_up_interruptible(&pvfs2_request_list_waitq); \
698} while (0)
699
700#define remove_op_from_request_list(op) \
701 do { \
702 struct list_head *tmp = NULL; \
703 struct list_head *tmp_safe = NULL; \
704 struct pvfs2_kernel_op_s *tmp_op = NULL; \
705 \
706 spin_lock(&pvfs2_request_list_lock); \
707 list_for_each_safe(tmp, tmp_safe, &pvfs2_request_list) { \
708 tmp_op = list_entry(tmp, \
709 struct pvfs2_kernel_op_s, \
710 list); \
711 if (tmp_op && (tmp_op == op)) { \
712 list_del(&tmp_op->list); \
713 break; \
714 } \
715 } \
716 spin_unlock(&pvfs2_request_list_lock); \
717 } while (0)
718
719#define PVFS2_OP_INTERRUPTIBLE 1 /* service_operation() is interruptible */
720#define PVFS2_OP_PRIORITY 2 /* service_operation() is high priority */
721#define PVFS2_OP_CANCELLATION 4 /* this is a cancellation */
722#define PVFS2_OP_NO_SEMAPHORE 8 /* don't acquire semaphore */
723#define PVFS2_OP_ASYNC 16 /* Queue it, but don't wait */
724
725int service_operation(struct pvfs2_kernel_op_s *op,
726 const char *op_name,
727 int flags);
728
729/*
730 * handles two possible error cases, depending on context.
731 *
732 * by design, our vfs i/o errors need to be handled in one of two ways,
733 * depending on where the error occured.
734 *
735 * if the error happens in the waitqueue code because we either timed
736 * out or a signal was raised while waiting, we need to cancel the
737 * userspace i/o operation and free the op manually. this is done to
738 * avoid having the device start writing application data to our shared
739 * bufmap pages without us expecting it.
740 *
741 * FIXME: POSSIBLE OPTIMIZATION:
742 * However, if we timed out or if we got a signal AND our upcall was never
743 * picked off the queue (i.e. we were in OP_VFS_STATE_WAITING), then we don't
744 * need to send a cancellation upcall. The way we can handle this is
745 * set error_exit to 2 in such cases and 1 whenever cancellation has to be
746 * sent and have handle_error
747 * take care of this situation as well..
748 *
749 * if a pvfs2 sysint level error occured and i/o has been completed,
750 * there is no need to cancel the operation, as the user has finished
751 * using the bufmap page and so there is no danger in this case. in
752 * this case, we wake up the device normally so that it may free the
753 * op, as normal.
754 *
755 * note the only reason this is a macro is because both read and write
756 * cases need the exact same handling code.
757 */
758#define handle_io_error() \
759do { \
760 if (!op_state_serviced(new_op)) { \
761 pvfs2_cancel_op_in_progress(new_op->tag); \
762 op_release(new_op); \
763 } else { \
764 wake_up_daemon_for_return(new_op); \
765 } \
766 new_op = NULL; \
767 pvfs_bufmap_put(bufmap, buffer_index); \
768 buffer_index = -1; \
769} while (0)
770
771#define get_interruptible_flag(inode) \
772 ((PVFS2_SB(inode->i_sb)->flags & PVFS2_OPT_INTR) ? \
773 PVFS2_OP_INTERRUPTIBLE : 0)
774
775#define add_pvfs2_sb(sb) \
776do { \
777 gossip_debug(GOSSIP_SUPER_DEBUG, \
778 "Adding SB %p to pvfs2 superblocks\n", \
779 PVFS2_SB(sb)); \
780 spin_lock(&pvfs2_superblocks_lock); \
781 list_add_tail(&PVFS2_SB(sb)->list, &pvfs2_superblocks); \
782 spin_unlock(&pvfs2_superblocks_lock); \
783} while (0)
784
785#define remove_pvfs2_sb(sb) \
786do { \
787 struct list_head *tmp = NULL; \
788 struct list_head *tmp_safe = NULL; \
789 struct pvfs2_sb_info_s *pvfs2_sb = NULL; \
790 \
791 spin_lock(&pvfs2_superblocks_lock); \
792 list_for_each_safe(tmp, tmp_safe, &pvfs2_superblocks) { \
793 pvfs2_sb = list_entry(tmp, \
794 struct pvfs2_sb_info_s, \
795 list); \
796 if (pvfs2_sb && (pvfs2_sb->sb == sb)) { \
797 gossip_debug(GOSSIP_SUPER_DEBUG, \
798 "Removing SB %p from pvfs2 superblocks\n", \
799 pvfs2_sb); \
800 list_del(&pvfs2_sb->list); \
801 break; \
802 } \
803 } \
804 spin_unlock(&pvfs2_superblocks_lock); \
805} while (0)
806
807#define pvfs2_lock_inode(inode) spin_lock(&inode->i_lock)
808#define pvfs2_unlock_inode(inode) spin_unlock(&inode->i_lock)
809#define pvfs2_current_signal_lock current->sighand->siglock
810#define pvfs2_current_sigaction current->sighand->action
811
812#define fill_default_sys_attrs(sys_attr, type, mode) \
813do { \
814 sys_attr.owner = from_kuid(current_user_ns(), current_fsuid()); \
815 sys_attr.group = from_kgid(current_user_ns(), current_fsgid()); \
816 sys_attr.size = 0; \
817 sys_attr.perms = PVFS_util_translate_mode(mode); \
818 sys_attr.objtype = type; \
819 sys_attr.mask = PVFS_ATTR_SYS_ALL_SETABLE; \
820} while (0)
821
822#define pvfs2_inode_lock(__i) mutex_lock(&(__i)->i_mutex)
823
824#define pvfs2_inode_unlock(__i) mutex_unlock(&(__i)->i_mutex)
825
826static inline void pvfs2_i_size_write(struct inode *inode, loff_t i_size)
827{
828#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
829 pvfs2_inode_lock(inode);
830#endif
831 i_size_write(inode, i_size);
832#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
833 pvfs2_inode_unlock(inode);
834#endif
835}
836
837static inline unsigned int diff(struct timeval *end, struct timeval *begin)
838{
839 if (end->tv_usec < begin->tv_usec) {
840 end->tv_usec += 1000000;
841 end->tv_sec--;
842 }
843 end->tv_sec -= begin->tv_sec;
844 end->tv_usec -= begin->tv_usec;
845 return (end->tv_sec * 1000000) + end->tv_usec;
846}
847
848#endif /* __PVFS2KERNEL_H */