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