1 /*
2  * common.h - Common definitions for both Kernel and user-mode utilities
3  *
4  * Copyright (C) 2005, 2006
5  * Avishay Traeger (avishay@gmail.com)
6  * Copyright (C) 2008, 2009
7  * Boaz Harrosh <bharrosh@panasas.com>
8  *
9  * Copyrights for code taken from ext2:
10  *     Copyright (C) 1992, 1993, 1994, 1995
11  *     Remy Card (card@masi.ibp.fr)
12  *     Laboratoire MASI - Institut Blaise Pascal
13  *     Universite Pierre et Marie Curie (Paris VI)
14  *     from
15  *     linux/fs/minix/inode.c
16  *     Copyright (C) 1991, 1992  Linus Torvalds
17  *
18  * This file is part of exofs.
19  *
20  * exofs is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation.  Since it is based on ext2, and the only
23  * valid version of GPL for the Linux kernel is version 2, the only valid
24  * version of GPL for exofs is version 2.
25  *
26  * exofs is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with exofs; if not, write to the Free Software
33  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
34  */
35 
36 #ifndef __EXOFS_COM_H__
37 #define __EXOFS_COM_H__
38 
39 #include <linux/types.h>
40 
41 #include <scsi/osd_attributes.h>
42 #include <scsi/osd_initiator.h>
43 #include <scsi/osd_sec.h>
44 
45 /****************************************************************************
46  * Object ID related defines
47  * NOTE: inode# = object ID - EXOFS_OBJ_OFF
48  ****************************************************************************/
49 #define EXOFS_MIN_PID   0x10000	/* Smallest partition ID */
50 #define EXOFS_OBJ_OFF	0x10000	/* offset for objects */
51 #define EXOFS_SUPER_ID	0x10000	/* object ID for on-disk superblock */
52 #define EXOFS_DEVTABLE_ID 0x10001 /* object ID for on-disk device table */
53 #define EXOFS_ROOT_ID	0x10002	/* object ID for root directory */
54 
55 /* exofs Application specific page/attribute */
56 /* Inode attrs */
57 # define EXOFS_APAGE_FS_DATA	(OSD_APAGE_APP_DEFINED_FIRST + 3)
58 # define EXOFS_ATTR_INODE_DATA	1
59 # define EXOFS_ATTR_INODE_FILE_LAYOUT	2
60 # define EXOFS_ATTR_INODE_DIR_LAYOUT	3
61 /* Partition attrs */
62 # define EXOFS_APAGE_SB_DATA	(0xF0000000U + 3)
63 # define EXOFS_ATTR_SB_STATS	1
64 
65 /*
66  * The maximum number of files we can have is limited by the size of the
67  * inode number.  This is the largest object ID that the file system supports.
68  * Object IDs 0, 1, and 2 are always in use (see above defines).
69  */
70 enum {
71 	EXOFS_MAX_INO_ID = (sizeof(ino_t) * 8 == 64) ? ULLONG_MAX :
72 					(1ULL << (sizeof(ino_t) * 8ULL - 1ULL)),
73 	EXOFS_MAX_ID	 = (EXOFS_MAX_INO_ID - 1 - EXOFS_OBJ_OFF),
74 };
75 
76 /****************************************************************************
77  * Misc.
78  ****************************************************************************/
79 #define EXOFS_BLKSHIFT	12
80 #define EXOFS_BLKSIZE	(1UL << EXOFS_BLKSHIFT)
81 
82 /****************************************************************************
83  * superblock-related things
84  ****************************************************************************/
85 #define EXOFS_SUPER_MAGIC	0x5DF5
86 
87 /*
88  * The file system control block - stored in object EXOFS_SUPER_ID's data.
89  * This is where the in-memory superblock is stored on disk.
90  */
91 enum {EXOFS_FSCB_VER = 1, EXOFS_DT_VER = 1};
92 struct exofs_fscb {
93 	__le64  s_nextid;	/* Only used after mkfs */
94 	__le64  s_numfiles;	/* Only used after mkfs */
95 	__le32	s_version;	/* == EXOFS_FSCB_VER */
96 	__le16  s_magic;	/* Magic signature */
97 	__le16  s_newfs;	/* Non-zero if this is a new fs */
98 
99 	/* From here on it's a static part, only written by mkexofs */
100 	__le64	s_dev_table_oid;   /* Resurved, not used */
101 	__le64	s_dev_table_count; /* == 0 means no dev_table */
102 } __packed;
103 
104 /*
105  * This struct is set on the FS partition's attributes.
106  * [EXOFS_APAGE_SB_DATA, EXOFS_ATTR_SB_STATS] and is written together
107  * with the create command, to atomically persist the sb writeable information.
108  */
109 struct exofs_sb_stats {
110 	__le64  s_nextid;	/* Highest object ID used */
111 	__le64  s_numfiles;	/* Number of files on fs */
112 } __packed;
113 
114 /*
115  * Describes the raid used in the FS. It is part of the device table.
116  * This here is taken from the pNFS-objects definition. In exofs we
117  * use one raid policy through-out the filesystem. (NOTE: the funny
118  * alignment at beginning. We take care of it at exofs_device_table.
119  */
120 struct exofs_dt_data_map {
121 	__le32	cb_num_comps;
122 	__le64	cb_stripe_unit;
123 	__le32	cb_group_width;
124 	__le32	cb_group_depth;
125 	__le32	cb_mirror_cnt;
126 	__le32	cb_raid_algorithm;
127 } __packed;
128 
129 /*
130  * This is an osd device information descriptor. It is a single entry in
131  * the exofs device table. It describes an osd target lun which
132  * contains data belonging to this FS. (Same partition_id on all devices)
133  */
134 struct exofs_dt_device_info {
135 	__le32	systemid_len;
136 	u8	systemid[OSD_SYSTEMID_LEN];
137 	__le64	long_name_offset;	/* If !0 then offset-in-file */
138 	__le32	osdname_len;		/* */
139 	u8	osdname[44];		/* Embbeded, Usually an asci uuid */
140 } __packed;
141 
142 /*
143  * The EXOFS device table - stored in object EXOFS_DEVTABLE_ID's data.
144  * It contains the raid used for this multy-device FS and an array of
145  * participating devices.
146  */
147 struct exofs_device_table {
148 	__le32				dt_version;	/* == EXOFS_DT_VER */
149 	struct exofs_dt_data_map	dt_data_map;	/* Raid policy to use */
150 
151 	/* Resurved space For future use. Total includeing this:
152 	 * (8 * sizeof(le64))
153 	 */
154 	__le64				__Resurved[4];
155 
156 	__le64				dt_num_devices;	/* Array size */
157 	struct exofs_dt_device_info	dt_dev_table[];	/* Array of devices */
158 } __packed;
159 
160 /****************************************************************************
161  * inode-related things
162  ****************************************************************************/
163 #define EXOFS_IDATA		5
164 
165 /*
166  * The file control block - stored in an object's attributes.  This is where
167  * the in-memory inode is stored on disk.
168  */
169 struct exofs_fcb {
170 	__le64  i_size;			/* Size of the file */
171 	__le16  i_mode;         	/* File mode */
172 	__le16  i_links_count;  	/* Links count */
173 	__le32  i_uid;          	/* Owner Uid */
174 	__le32  i_gid;          	/* Group Id */
175 	__le32  i_atime;        	/* Access time */
176 	__le32  i_ctime;        	/* Creation time */
177 	__le32  i_mtime;        	/* Modification time */
178 	__le32  i_flags;        	/* File flags (unused for now)*/
179 	__le32  i_generation;   	/* File version (for NFS) */
180 	__le32  i_data[EXOFS_IDATA];	/* Short symlink names and device #s */
181 };
182 
183 #define EXOFS_INO_ATTR_SIZE	sizeof(struct exofs_fcb)
184 
185 /* This is the Attribute the fcb is stored in */
186 static const struct __weak osd_attr g_attr_inode_data = ATTR_DEF(
187 	EXOFS_APAGE_FS_DATA,
188 	EXOFS_ATTR_INODE_DATA,
189 	EXOFS_INO_ATTR_SIZE);
190 
191 /****************************************************************************
192  * dentry-related things
193  ****************************************************************************/
194 #define EXOFS_NAME_LEN	255
195 
196 /*
197  * The on-disk directory entry
198  */
199 struct exofs_dir_entry {
200 	__le64		inode_no;		/* inode number           */
201 	__le16		rec_len;		/* directory entry length */
202 	u8		name_len;		/* name length            */
203 	u8		file_type;		/* umm...file type        */
204 	char		name[EXOFS_NAME_LEN];	/* file name              */
205 };
206 
207 enum {
208 	EXOFS_FT_UNKNOWN,
209 	EXOFS_FT_REG_FILE,
210 	EXOFS_FT_DIR,
211 	EXOFS_FT_CHRDEV,
212 	EXOFS_FT_BLKDEV,
213 	EXOFS_FT_FIFO,
214 	EXOFS_FT_SOCK,
215 	EXOFS_FT_SYMLINK,
216 	EXOFS_FT_MAX
217 };
218 
219 #define EXOFS_DIR_PAD			4
220 #define EXOFS_DIR_ROUND			(EXOFS_DIR_PAD - 1)
221 #define EXOFS_DIR_REC_LEN(name_len) \
222 	(((name_len) + offsetof(struct exofs_dir_entry, name)  + \
223 	  EXOFS_DIR_ROUND) & ~EXOFS_DIR_ROUND)
224 
225 /*
226  * The on-disk (optional) layout structure.
227  * sits in an EXOFS_ATTR_INODE_FILE_LAYOUT or EXOFS_ATTR_INODE_DIR_LAYOUT
228  * attribute, attached to any inode, usually to a directory.
229  */
230 
231 enum exofs_inode_layout_gen_functions {
232 	LAYOUT_MOVING_WINDOW = 0,
233 	LAYOUT_IMPLICT = 1,
234 };
235 
236 struct exofs_on_disk_inode_layout {
237 	__le16 gen_func; /* One of enum exofs_inode_layout_gen_functions */
238 	__le16 pad;
239 	union {
240 		/* gen_func == LAYOUT_MOVING_WINDOW (default) */
241 		struct exofs_layout_sliding_window {
242 			__le32 num_devices; /* first n devices in global-table*/
243 		} sliding_window __packed;
244 
245 		/* gen_func == LAYOUT_IMPLICT */
246 		struct exofs_layout_implict_list {
247 			struct exofs_dt_data_map data_map;
248 			/* Variable array of size data_map.cb_num_comps. These
249 			 * are device indexes of the devices in the global table
250 			 */
251 			__le32 dev_indexes[];
252 		} implict __packed;
253 	};
254 } __packed;
255 
exofs_on_disk_inode_layout_size(unsigned max_devs)256 static inline size_t exofs_on_disk_inode_layout_size(unsigned max_devs)
257 {
258 	return sizeof(struct exofs_on_disk_inode_layout) +
259 		max_devs * sizeof(__le32);
260 }
261 
262 #endif /*ifndef __EXOFS_COM_H__*/
263