Whamcloud - gitweb
It's been a good day: chmod/chown and friends now work for Lustre Light.
[fs/lustre-release.git] / lustre / llite / super.c
1 /*
2  * Lustre Light Super operations
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
7  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
8  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
9  * Copryright (C) 1999 Seagate Technology Inc.
10  * Copryright (C) 2001 Mountain View Data, Inc.
11  * Copryright (C) 2002 Cluster File Systems, Inc.
12  *
13  */
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/errno.h>
22 #include <linux/locks.h>
23 #include <linux/unistd.h>
24
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27
28 #include <linux/fs.h>
29 #include <linux/stat.h>
30 #include <asm/uaccess.h>
31 #include <linux/vmalloc.h>
32 #include <asm/segment.h>
33
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/lustre_light.h>
37
38 //struct list_head ll_super_list;
39 extern struct address_space_operations ll_aops;
40 extern struct address_space_operations ll_dir_aops;
41 struct super_operations ll_super_operations;
42 long ll_cache_count = 0;
43 long ll_mutex_start = 0;
44 long obd_memory = 0;
45
46 static char *ll_read_opt(const char *opt, char *data)
47 {
48         char *value;
49         char *retval;
50
51         CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
52         if ( strncmp(opt, data, strlen(opt)) )
53                 return NULL;
54
55         if ( (value = strchr(data, '=')) == NULL )
56                 return NULL;
57
58         value++;
59         OBD_ALLOC(retval, char *, strlen(value) + 1);
60         if ( !retval ) {
61                 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
62                 return NULL;
63         }
64         
65         memcpy(retval, value, strlen(value)+1);
66         CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
67         return retval;
68 }
69
70 static void ll_options(char *options, char **dev, char **vers)
71 {
72         char *this_char;
73
74         if (!options)
75                 return;
76
77         for (this_char = strtok (options, ",");
78              this_char != NULL;
79              this_char = strtok (NULL, ",")) {
80                 CDEBUG(D_INFO, "this_char %s\n", this_char);
81                 if ( (!*dev && (*dev = ll_read_opt("device", this_char)))||
82                      (!*vers && (*vers = ll_read_opt("version", this_char))) )
83                         continue;
84                 
85         }
86 }
87
88 static struct super_block * ll_read_super(struct super_block *sb, 
89                                             void *data, int silent)
90 {
91         struct inode *root = 0; 
92         struct ll_sb_info *sbi = (struct ll_sb_info *)(&sb->u.generic_sbp);
93         char *device = NULL;
94         char *version = NULL;
95         int connected = 0;
96         int devno;
97         int err;
98         struct mds_rep *rep; 
99         struct mds_rep_hdr *hdr = NULL; 
100
101         ENTRY;
102         MOD_INC_USE_COUNT; 
103
104         memset(sbi, 0, sizeof(*sbi));
105
106         ll_options(data, &device, &version);
107         if ( !device ) {
108                 printk(__FUNCTION__ ": no device\n");
109                 sb = NULL; 
110                 goto ERR;
111         }
112
113         devno = simple_strtoul(device, NULL, 0);
114         if ( devno >= MAX_OBD_DEVICES ) {
115                 printk(__FUNCTION__ ": device of %s too high\n", device);
116                 sb = NULL; 
117                 goto ERR;
118         } 
119
120         sbi->ll_conn.oc_dev = &obd_dev[devno];
121         err = obd_connect(&sbi->ll_conn);
122         if ( err ) {
123                 printk(__FUNCTION__ "cannot connect to %s\n", device);
124                 sb = NULL; 
125                 goto ERR;
126         }
127         connected = 1;
128
129         sbi->ll_super = sb;
130         sbi->ll_rootino = 2;
131
132         sb->s_maxbytes = 1LL << 36;
133         sb->s_blocksize = PAGE_SIZE;
134         sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
135         sb->s_magic = LL_SUPER_MAGIC;
136         sb->s_op = &ll_super_operations;
137
138         /* make root inode */
139         err = mdc_getattr(sbi->ll_rootino, S_IFDIR, 
140                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 
141                           &rep, &hdr);
142         if (err) {
143                 printk(__FUNCTION__ ": mds_getattr failed for root %d\n", err);
144                 sb = NULL; 
145                 goto ERR;
146         }
147                          
148         root = iget4(sb, sbi->ll_rootino, NULL, rep);
149         if (root) {
150                 sb->s_root = d_alloc_root(root);
151         } else {
152             printk("lustre_light: bad iget4 for root\n");
153             sb = NULL; 
154             goto ERR;
155         } 
156         
157 ERR:
158         if (hdr)
159                 kfree(hdr);
160         if (device)
161                 OBD_FREE(device, strlen(device) + 1);
162         if (version)
163                 OBD_FREE(version, strlen(version) + 1);
164         if (!sb && connected) 
165                 obd_disconnect(&sbi->ll_conn);
166
167         if (!sb && root) {
168                 iput(root);
169         }
170         if (!sb) 
171                 MOD_DEC_USE_COUNT;
172
173         EXIT;
174         return sb;
175 } /* ll_read_super */
176
177 static void ll_put_super(struct super_block *sb)
178 {
179         ENTRY;
180
181         obd_disconnect(ID(sb));
182
183         MOD_DEC_USE_COUNT;
184         EXIT;
185 } /* ll_put_super */
186
187
188 extern void write_inode_pages(struct inode *);
189 /* This routine is called from iput() (for each unlink on the inode).
190  * We can't put this call into delete_inode() since that is called only
191  * when i_count == 0, and we need to keep a reference on the inode while
192  * it is in the page cache, which means i_count > 0.  Catch 22.
193  */
194 static void ll_put_inode(struct inode *inode)
195 {
196         ENTRY;
197         if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
198                 write_inode_pages(inode);
199                 EXIT;
200                 return;
201         }
202
203         //ll_dequeue_pages(inode);
204         EXIT;
205 } /* ll_put_inode */
206
207 /* like inode_setattr, but doesn't mark the inode dirty */ 
208 static int ll_attr2inode(struct inode * inode, struct iattr * attr)
209 {
210         unsigned int ia_valid = attr->ia_valid;
211         int error = 0;
212
213         if (ia_valid & ATTR_SIZE) {
214                 error = vmtruncate(inode, attr->ia_size);
215                 if (error)
216                         goto out;
217         }
218         if (ia_valid & ATTR_UID)
219                 inode->i_uid = attr->ia_uid;
220         if (ia_valid & ATTR_GID)
221                 inode->i_gid = attr->ia_gid;
222         if (ia_valid & ATTR_ATIME)
223                 inode->i_atime = attr->ia_atime;
224         if (ia_valid & ATTR_MTIME)
225                 inode->i_mtime = attr->ia_mtime;
226         if (ia_valid & ATTR_CTIME)
227                 inode->i_ctime = attr->ia_ctime;
228         if (ia_valid & ATTR_MODE) {
229                 inode->i_mode = attr->ia_mode;
230                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
231                         inode->i_mode &= ~S_ISGID;
232         }
233 out:
234         return error;
235 }
236
237 int ll_setattr(struct dentry *de, struct iattr *attr)
238 {
239         struct inode *inode = de->d_inode;
240         struct mds_rep_hdr *hdr = NULL;
241         int err;
242
243         ENTRY;
244
245         /* change incore inode */
246         ll_attr2inode(inode, attr);
247
248         err = mdc_setattr(inode, attr, NULL, &hdr); 
249         if ( err )
250                 printk(__FUNCTION__ ": ll_setattr fails (%d)\n", err);
251
252         EXIT;
253         return err;
254 } /* ll_setattr */
255
256
257
258 static int ll_statfs(struct super_block *sb, struct statfs *buf)
259 {
260         struct statfs tmp;
261         int err;
262
263         ENTRY;
264
265         err = obd_statfs(ID(sb), &tmp);
266         if ( err ) { 
267                 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
268                 return err;
269         }
270         memcpy(buf, &tmp, sizeof(*buf));
271         CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
272         EXIT;
273
274         return err; 
275 }
276
277 static inline void ll_read_inode2(struct inode *inode, void *opaque)
278 {
279         struct mds_rep *rep = opaque; 
280         
281         ENTRY;
282         ll_to_inode(inode, rep); 
283
284         INIT_LIST_HEAD(ll_iplist(inode)); /* list of dirty pages on inode */
285         INIT_LIST_HEAD(ll_islist(inode)); /* list of inodes in superblock */
286
287         /* OIDEBUG(inode); */
288
289         if (S_ISREG(inode->i_mode)) {
290                 inode->i_op = &ll_file_inode_operations;
291                 inode->i_fop = &ll_file_operations;
292                 inode->i_mapping->a_ops = &ll_aops;
293                 EXIT;
294         } else if (S_ISDIR(inode->i_mode)) {
295                 inode->i_op = &ll_dir_inode_operations;
296                 inode->i_fop = &ll_dir_operations; 
297                 inode->i_mapping->a_ops = &ll_dir_aops;
298                 EXIT;
299         } else if (S_ISLNK(inode->i_mode)) {
300                 if (inode->i_blocks) { 
301                         inode->i_op = &ll_symlink_inode_operations;
302                         inode->i_mapping->a_ops = &ll_aops;
303                 }else {
304                         inode->i_op = &ll_fast_symlink_inode_operations;
305                 }
306                 EXIT;
307         } else {
308                 init_special_inode(inode, inode->i_mode,
309                                    ((int *)ll_i2info(inode)->lli_inline)[0]);
310         }
311
312         EXIT;
313         return;
314 }
315
316 /* exported operations */
317 struct super_operations ll_super_operations =
318 {
319         read_inode2: ll_read_inode2,
320         // put_inode: ll_put_inode,
321         // delete_inode: ll_delete_inode,
322         put_super: ll_put_super,
323         // statfs: ll_statfs
324 };
325
326 struct file_system_type lustre_light_fs_type = {
327    "lustre_light", 0, ll_read_super, NULL
328 };
329
330 static int __init init_lustre_light(void)
331 {
332         printk(KERN_INFO "Lustre Light 0.0.1, braam@clusterfs.com\n");
333
334         return register_filesystem(&lustre_light_fs_type);
335 }
336
337 static void __exit exit_lustre_light(void)
338 {
339         unregister_filesystem(&lustre_light_fs_type);
340 }
341
342 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
343 MODULE_DESCRIPTION("Lustre Light Client File System v1.0");
344 MODULE_LICENSE("GPL");
345
346 module_init(init_lustre_light);
347 module_exit(exit_lustre_light);