Whamcloud - gitweb
LU-1346 libcfs: cleanup libcfs atomic primitives
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / winnt-fs.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/winnt/winnt-fs.h
37  *
38  * File operations & routines.
39  */
40
41 #ifndef __LIBCFS_WINNT_CFS_FS_H__
42 #define __LIBCFS_WINNT_CFS_FS_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48
49 #define MINORBITS       8
50 #define MINORMASK       ((1U << MINORBITS) - 1)
51
52 #define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
53 #define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
54 #define NODEV           0
55 #define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))
56
57 #define PATH_MAX (260)
58
59 #ifdef __KERNEL__
60
61 /* linux/fs.h */
62
63 #define MAY_EXEC 1
64 #define MAY_WRITE 2
65 #define MAY_READ 4
66 #define MAY_APPEND 8
67
68 #define FMODE_READ 1
69 #define FMODE_WRITE 2
70
71 /* Internal kernel extensions */
72 #define FMODE_LSEEK     4
73 #define FMODE_PREAD     8
74 #define FMODE_PWRITE    FMODE_PREAD     /* These go hand in hand */
75
76 /* File is being opened for execution. Primary users of this flag are
77    distributed filesystems that can use it to achieve correct ETXTBUSY
78    behavior for cross-node execution/opening_for_writing of files */
79 #define FMODE_EXEC      16
80
81 #define RW_MASK         1
82 #define RWA_MASK        2
83 #define READ 0
84 #define WRITE 1
85 #define READA 2         /* read-ahead  - don't block if no resources */
86 #define SWRITE 3        /* for ll_rw_block() - wait for buffer lock */
87 #define SPECIAL 4       /* For non-blockdevice requests in request queue */
88 #define READ_SYNC       (READ | (1 << BIO_RW_SYNC))
89 #define WRITE_SYNC      (WRITE | (1 << BIO_RW_SYNC))
90 #define WRITE_BARRIER   ((1 << BIO_RW) | (1 << BIO_RW_BARRIER))
91
92 struct file_operations
93 {
94     struct module *owner;
95     loff_t (*llseek)(struct file * file, loff_t offset, int origin);
96     ssize_t (*read) (struct file * file, char * buf, size_t nbytes, loff_t *ppos);
97     ssize_t (*write)(struct file * file, const char * buffer,
98         size_t count, loff_t *ppos);
99     int (*ioctl) (struct file *, unsigned int, ulong_ptr_t);
100     int (*open) (struct inode*, struct file *);
101     int (*release) (struct inode*, struct file *);
102 };
103
104 struct file {
105
106     cfs_handle_t            f_handle;
107     unsigned int            f_flags;
108     mode_t                  f_mode;
109     __u32                   f_count;
110
111     size_t                  f_size;
112     loff_t                  f_pos;
113     unsigned int            f_uid, f_gid;
114     int                     f_error;
115
116     __u32                   f_version;
117
118     //struct list_head      f_list;
119     struct dentry *         f_dentry;
120
121     cfs_proc_entry_t *      proc_dentry;
122     cfs_file_operations_t * f_op;
123
124     void *                  private_data;
125     struct inode *          f_inode;
126     char                    f_name[1];
127
128 };
129
130 #define filp_size(f)            ((f)->f_size)
131 #define filp_poff(f)            (&(f)->f_pos)
132
133 struct file *filp_open(const char *name, int flags, int mode);
134 int filp_close(struct file *fp, void *id);
135 int filp_read(struct file *fp, void *buf, size_t nbytes, loff_t *pos);
136 int filp_write(struct file *fp, void *buf, size_t nbytes, loff_t *pos);
137 int filp_fsync(struct file *fp);
138 int get_file(struct file *fp);
139 int fput(struct file *fp);
140 int file_count(struct file *fp);
141 #define cfs_filp_unlink(x, y) (KdBreakPoint(), 0)
142 /*
143  * CFS_FLOCK routines
144  */
145
146 struct file_lock {
147         int     fl_type;
148         pid_t   fl_pid;
149         size_t  fl_len;
150         off_t   fl_start;
151         off_t   fl_end;
152 };
153
154 #define INT_LIMIT(x)                    (~((x)1 << (sizeof(x)*8 - 1)))
155 #define OFFSET_MAX                      INT_LIMIT(loff_t)
156
157 #define flock_type(fl)                  ((fl)->fl_type)
158 #define flock_set_type(fl, type)        do { (fl)->fl_type = (type); } while (0)
159 #define flock_pid(fl)                   ((fl)->fl_pid)
160 #define flock_set_pid(fl, pid)          do { (fl)->fl_pid = (pid); } while (0)
161 #define flock_start(fl)                 ((fl)->fl_start)
162 #define flock_set_start(fl, st)         do { (fl)->fl_start = (st); } while (0)
163 #define flock_end(fl)                   ((fl)->fl_end)
164 #define flock_set_end(fl, end)          do { (fl)->fl_end = (end); } while (0)
165
166 #define ATTR_MODE       0x0001
167 #define ATTR_UID        0x0002
168 #define ATTR_GID        0x0004
169 #define ATTR_SIZE       0x0008
170 #define ATTR_ATIME      0x0010
171 #define ATTR_MTIME      0x0020
172 #define ATTR_CTIME      0x0040
173 #define ATTR_ATIME_SET  0x0080
174 #define ATTR_MTIME_SET  0x0100
175 #define ATTR_FORCE      0x0200  /* Not a change, but a change it */
176 #define ATTR_ATTR_FLAG  0x0400
177 #define ATTR_RAW        0x0800  /* file system, not vfs will massage attrs */
178 #define ATTR_FROM_OPEN  0x1000  /* called from open path, ie O_TRUNC */
179 //#define ATTR_CTIME_SET  0x2000
180
181 /*
182  * set ATTR_BLOCKS to a high value to avoid any risk of collision with other
183  * ATTR_* attributes (see bug 13828): lustre/include/winnt/lustre_compat25.h
184  */
185 /* #define ATTR_BLOCKS     0x4000 */
186 #define ATTR_BLOCKS    (1 << 27)
187
188 #define ATTR_KILL_SUID  0
189 #define ATTR_KILL_SGID  0
190
191
192
193 #define in_group_p(x)   (0)
194
195
196 /* VFS structures for windows */
197
198 /* 
199  * inode formats
200  */
201
202 #define S_IFMT   00170000
203 #define S_IFSOCK 0140000
204 #define S_IFLNK  0120000
205 #define S_IFREG  0100000
206 #define S_IFBLK  0060000
207 #define S_IFDIR  0040000
208 #define S_IFCHR  0020000
209 #define S_IFIFO  0010000
210 #define S_ISUID  0004000
211 #define S_ISGID  0002000
212 #define S_ISVTX  0001000
213
214 /* Inode flags - they have nothing to superblock flags now */
215
216 #define S_SYNC          1       /* Writes are synced at once */
217 #define S_NOATIME       2       /* Do not update access times */
218 #define S_APPEND        4       /* Append-only file */
219 #define S_IMMUTABLE     8       /* Immutable file */
220 #define S_DEAD          16      /* removed, but still open directory */
221 #define S_NOQUOTA       32      /* Inode is not counted to quota */
222 #define S_DIRSYNC       64      /* Directory modifications are synchronous */
223 #define S_NOCMTIME      128     /* Do not update file c/mtime */
224 #define S_SWAPFILE      256     /* Do not truncate: swapon got its bmaps */
225 #define S_PRIVATE       512     /* Inode is fs-internal */
226
227
228 struct inode {
229         __u32           i_mode;
230         __u64           i_size;
231         __u64           i_blocks;
232         struct timespec i_atime;
233         struct timespec i_ctime;
234         struct timespec i_mtime;
235         struct timespec i_dtime;
236         __u32           i_ino;
237         __u32           i_generation;
238         __u32           i_state;
239         __u32           i_blkbits;
240         int             i_uid;
241         int             i_gid;
242         __u32           i_flags;
243         struct mutex    i_sem;
244         void *          i_priv;
245 };
246
247 #define I_FREEING       0x0001
248
249 struct dentry {
250         atomic_t    d_count;
251         struct {
252             int         len;
253             char *      name;
254         } d_name;
255         struct inode *  d_inode;
256         struct dentry*  d_parent;
257 };
258
259 extern struct dentry *dget(struct dentry *de);
260 extern void dput(struct dentry *de);
261 static __inline struct dentry *lookup_one_len(const char *name, struct dentry *de, int len)
262 {
263     cfs_enter_debugger();
264     return NULL;
265 }
266
267 static inline loff_t i_size_read(const struct inode *inode)
268 {
269     cfs_enter_debugger();
270     return inode->i_size;
271 }
272
273 static inline void i_size_write(struct inode *inode, loff_t i_size)
274 {
275     cfs_enter_debugger();
276     inode->i_size = i_size;
277 }
278
279 struct kstatfs {
280         u64     f_type;
281         long    f_bsize;
282         u64     f_blocks;
283         u64     f_bfree;
284         u64     f_bavail;
285         u64     f_files;
286         u64     f_ffree;
287         __u32   f_fsid;
288         long    f_namelen;
289         long    f_frsize;
290         long    f_spare[5];
291 };
292
293 struct super_block {
294         void *  s_fs_info;
295 };
296
297 struct vfsmount {
298         struct dentry * pwd;
299         struct dentry * mnt_root;
300         struct super_block *mnt_sb;
301 };
302
303
304 /*
305  * quota definitions (linux/quota.h)
306  */
307
308 #define MAXQUOTAS 2
309 #define USRQUOTA  0             /* element used for user quotas */
310 #define GRPQUOTA  1             /* element used for group quotas */
311
312
313 /*
314  * proc fs routines
315  */
316
317 typedef int (read_proc_t)(char *page, char **start, off_t off,
318                           int count, int *eof, void *data);
319
320 struct file; /* forward ref */
321 typedef int (write_proc_t)(struct file *file, const char *buffer,
322                            unsigned long count, void *data);
323
324 void proc_destory_subtree(cfs_proc_entry_t *entry);
325
326 int proc_init_fs();
327 void proc_destroy_fs();
328
329 /*
330  *  thread affinity
331  */
332
333 HANDLE cfs_open_current_thread();
334 void cfs_close_thread_handle(HANDLE handle);
335 KAFFINITY cfs_query_thread_affinity();
336 int cfs_set_thread_affinity(KAFFINITY affinity);
337 int cfs_tie_thread_to_cpu(int cpu);
338 typedef PVOID mm_segment_t;
339
340 /*
341  * thread priority
342  */
343 int cfs_set_thread_priority(KPRIORITY priority);
344
345 #define MAKE_MM_SEG(s) ((mm_segment_t)(ulong_ptr_t)(s))
346 #define KERNEL_DS       MAKE_MM_SEG(0xFFFFFFFFUL)
347 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
348
349 #define get_ds()        (KERNEL_DS)
350 #define set_fs(x) do {} while(0)
351 #define get_fs() (NULL)
352
353 /*
354  * radix tree (linux/radix_tree.h)
355  */
356
357 /* radix tree root structure */
358 struct radix_tree_root {
359     RTL_GENERIC_TABLE   table;
360 };
361
362 /* #define RADIX_TREE_INIT(mask) {0}
363
364 #define RADIX_TREE(name, mask) \
365         struct radix_tree_root name RADIX_TREE_INIT(mask) */
366
367 VOID RadixInitTable(IN PRTL_GENERIC_TABLE Table);
368 #define INIT_RADIX_TREE(root, mask)     RadixInitTable(&((root)->table))
369
370 /* all radix tree routines should be protected by external locks */
371 unsigned int
372 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
373                        unsigned long first_index, unsigned int max_items);
374 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index);
375 int radix_tree_insert(struct radix_tree_root *root, unsigned long index,
376                       void *item);
377 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index);
378
379 #else  /* !__KERNEL__ */
380
381 #if !defined(_WINDOWS_)
382
383 #define CREATE_NEW          1
384 #define CREATE_ALWAYS       2
385 #define OPEN_EXISTING       3
386 #define OPEN_ALWAYS         4
387 #define TRUNCATE_EXISTING   5
388
389 #define SECTION_QUERY       0x0001
390 #define SECTION_MAP_WRITE   0x0002
391 #define SECTION_MAP_READ    0x0004
392 #define SECTION_MAP_EXECUTE 0x0008
393 #define SECTION_EXTEND_SIZE 0x0010
394
395 #define FILE_MAP_COPY       SECTION_QUERY
396 #define FILE_MAP_WRITE      SECTION_MAP_WRITE
397 #define FILE_MAP_READ       SECTION_MAP_READ
398 #define FILE_MAP_ALL_ACCESS SECTION_ALL_ACCESS
399
400
401 NTSYSAPI
402 HANDLE
403 NTAPI
404 CreateFileA(
405     IN LPCSTR lpFileName,
406     IN DWORD dwDesiredAccess,
407     IN DWORD dwShareMode,
408     IN PVOID lpSecurityAttributes,
409     IN DWORD dwCreationDisposition,
410     IN DWORD dwFlagsAndAttributes,
411     IN HANDLE hTemplateFile
412     );
413
414 #define CreateFile  CreateFileA
415
416 NTSYSAPI
417 BOOL
418 NTAPI
419 CloseHandle(
420     IN OUT HANDLE hObject
421     );
422
423 NTSYSAPI
424 DWORD
425 NTAPI
426 GetLastError(
427    VOID
428    );
429
430 NTSYSAPI
431 HANDLE
432 NTAPI
433 CreateFileMappingA(
434     IN HANDLE hFile,
435     IN PVOID lpFileMappingAttributes,
436     IN DWORD flProtect,
437     IN DWORD dwMaximumSizeHigh,
438     IN DWORD dwMaximumSizeLow,
439     IN LPCSTR lpName
440     );
441 #define CreateFileMapping  CreateFileMappingA
442
443 NTSYSAPI
444 DWORD
445 NTAPI
446 GetFileSize(
447     IN HANDLE hFile,
448     OUT DWORD * lpFileSizeHigh
449     );
450
451 NTSYSAPI
452 PVOID
453 NTAPI
454 MapViewOfFile(
455     IN HANDLE hFileMappingObject,
456     IN DWORD dwDesiredAccess,
457     IN DWORD dwFileOffsetHigh,
458     IN DWORD dwFileOffsetLow,
459     IN SIZE_T dwNumberOfBytesToMap
460     );
461
462 NTSYSAPI
463 BOOL
464 NTAPI
465 UnmapViewOfFile(
466     IN PVOID lpBaseAddress
467     );
468 #endif
469
470 #endif /* __KERNEL__ */
471
472 struct dentry {
473         void    *d;
474 };
475
476 /*
477  *  misc
478  */
479
480 #define ERR_PTR(error) ((void *)(long_ptr_t)(error))
481 #define PTR_ERR(ptr)   ((long)(long_ptr_t) (ptr))
482 #define IS_ERR(ptr)    ((long)(((ulong_ptr_t) (ptr)) > (ulong_ptr_t)(-1000L)))
483
484 #endif /* __LIBCFS_WINNT_CFS_FS_H__*/