initial-linux24-support-20001105
[openafs.git] / src / afs / LINUX / osi_vfsops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * VFS operations for Linux
12  *
13  * super_block operations should return negated errno to Linux.
14  */
15 #include "../afs/param.h"
16 #include "../afs/sysincludes.h"
17 #include "../afs/afsincludes.h"
18 #include "../afs/afs_stats.h"
19 #include "../h/locks.h"
20 #if defined(AFS_LINUX24_ENV)
21 #include "../h/smp_lock.h"
22 #endif
23
24 #define __NO_VERSION__ /* don't define kernel_verion in module.h */
25 #include <linux/module.h>
26
27
28 struct vcache *afs_globalVp = 0;
29 struct vfs *afs_globalVFS = 0;
30 int afs_was_mounted = 0; /* Used to force reload if mount/unmount/mount */
31
32 extern struct super_operations afs_sops;
33 extern afs_rwlock_t afs_xvcache;
34 extern struct afs_q VLRU;
35
36 extern struct dentry_operations afs_dentry_operations;
37
38 /* Forward declarations */
39 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
40 static void update_inode_cache(struct inode *ip, struct vattr *vp);
41 static int afs_root(struct super_block *afsp);
42 struct super_block *afs_read_super(struct super_block *sb, void *data,
43                                    int silent);
44 void put_inode_on_dummy_list(struct inode *ip);
45
46 /* afs_file_system
47  * VFS entry for Linux - installed in init_module
48  * Linux mounts file systems by:
49  * 1) register_filesystem(&afs_file_system) - done in init_module
50  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
51  *    We are expected to setup the super_block. See afs_read_super.
52  */
53 #if defined(AFS_LINUX24_ENV)
54 DECLARE_FSTYPE(afs_file_system, "afs", afs_read_super, 0);
55 #else
56 struct file_system_type afs_file_system = {
57     "afs",      /* name - used by mount operation. */
58     0,          /* requires_dev - no for network filesystems. mount() will 
59                  * pass us an "unnamed" device. */
60     afs_read_super, /* wrapper to afs_mount */
61     NULL        /* pointer to next file_system_type once registered. */
62 };
63 #endif
64
65 /* afs_read_super
66  * read the "super block" for AFS - roughly eguivalent to struct vfs.
67  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
68  */
69 struct super_block *afs_read_super(struct super_block *sb, void *data,
70                                    int silent)
71 {
72     int code = 0;
73
74     AFS_GLOCK();
75     if (afs_was_mounted) {
76         printf("You must reload the AFS kernel extensions before remounting AFS.\n");
77         return NULL;
78     }
79     afs_was_mounted = 1;
80
81     /* Set basics of super_block */
82 #if !defined(AFS_LINUX24_ENV)
83     lock_super(sb);
84 #endif
85     MOD_INC_USE_COUNT;
86
87     afs_globalVFS = sb;
88     sb->s_blocksize = 1024;
89     sb->s_blocksize_bits = 10;
90     sb->s_magic = AFS_VFSMAGIC;
91     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
92     code = afs_root(sb);
93     if (code)
94         MOD_DEC_USE_COUNT;
95
96 #if !defined(AFS_LINUX24_ENV)
97     unlock_super(sb);
98 #endif
99
100     AFS_GUNLOCK();
101     return code ? NULL : sb;
102 }
103
104
105 /* afs_root - stat the root of the file system. AFS global held on entry. */
106 static int afs_root(struct super_block *afsp)
107 {
108     register afs_int32 code = 0;
109     struct vrequest treq;
110     register struct vcache *tvp=0;
111
112     AFS_STATCNT(afs_root);
113     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
114         tvp = afs_globalVp;
115     } else {
116         cred_t *credp = crref();
117         afs_globalVp = 0;
118         if (!(code = afs_InitReq(&treq, credp)) &&
119             !(code = afs_CheckInit())) {
120             tvp = afs_GetVCache(&afs_rootFid, &treq, (afs_int32 *)0,
121                                 (struct vcache*)0, WRITE_LOCK);
122             if (tvp) {
123                 extern struct inode_operations afs_dir_iops;
124 #if defined(AFS_LINUX24_ENV)
125                 extern struct file_operations afs_dir_fops;
126 #endif
127                 
128                 /* "/afs" is a directory, reset inode ops accordingly. */
129                 tvp->v.v_op = &afs_dir_iops;
130 #if defined(AFS_LINUX24_ENV)
131                 tvp->v.v_fop = &afs_dir_fops;
132 #endif
133
134                 /* setup super_block and mount point inode. */
135                 afs_globalVp = tvp;
136 #if defined(AFS_LINUX24_ENV)
137                 afsp->s_root = d_alloc_root((struct inode*)tvp);
138 #else
139                 afsp->s_root = d_alloc_root((struct inode*)tvp, NULL);
140 #endif
141                 afsp->s_root->d_op = &afs_dentry_operations;
142             } else
143                 code = ENOENT;
144         }
145         crfree(credp);
146     }
147
148     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
149                ICL_TYPE_INT32, code);
150     return code;
151 }
152
153 /* super_operations */
154
155 /* afs_read_inode
156  * called via iget to read in the inode. The passed in inode has i_ino, i_dev
157  * and i_sb setup on input. Linux file systems use this to get super block
158  * inode information, so we don't really care what happens here.
159  * For Linux 2.2, we'll be called if we participate in the inode pool.
160  */
161 void afs_read_inode(struct inode *ip)
162 {
163     /* I don't think we ever get called with this. So print if we do. */
164     printf("afs_read_inode: Called for inode %d\n", ip->i_ino);
165 }
166
167
168 /* afs_notify_change
169  * Linux version of setattr call. What to change is in the iattr struct.
170  * We need to set bits in both the Linux inode as well as the vcache.
171  */
172 int afs_notify_change(struct dentry *dp, struct iattr* iattrp)
173 {
174     struct vattr vattr;
175     int code;
176     cred_t *credp = crref();
177     struct inode *ip = dp->d_inode;
178
179     AFS_GLOCK();
180     VATTR_NULL(&vattr);
181     iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
182     update_inode_cache(ip, &vattr);
183     code = afs_setattr((struct vcache*)ip, &vattr, credp);
184     afs_CopyOutAttrs((struct vcache*)ip, &vattr);
185     /* Note that the inode may still not have all the correct info. But at
186      * least we've got the newest version of what was supposed to be set.
187      */
188
189     AFS_GUNLOCK();
190     crfree(credp);
191     return -code;
192 }
193
194
195 /* This list is simply used to initialize the i_list member of the
196  * linux inode. This stops linux inode syncing code from choking on our
197  * inodes.
198  */
199 static LIST_HEAD(dummy_inode_list);
200
201
202 /* This is included for documentation only. */
203 /* afs_write_inode
204  * Used to flush in core inode to disk. We don't need to do this. Top level
205  * write_inode() routine will clear i_dirt. If this routine is in the table,
206  * it's expected to do the cleaning and clear i_dirt.
207  * 
208  * 9/24/99: This is what we thought until we discovered msync() does end up calling
209  * this function to sync a single inode to disk. msync() only flushes selective
210  * pages to disk. So it needs an inode syncing function to update metadata when it
211  * has synced some pages of a file to disk.
212  */
213 void afs_write_inode(struct inode *ip) 
214 {
215     /* and put it back on our dummy list. */
216     list_del(&ip->i_list);
217     list_add(&ip->i_list, &dummy_inode_list);
218
219     /* for now we don't actually update the metadata during msync. This
220      * is just to keep linux happy.  */
221 }
222
223
224 /* afs_put_inode
225  * called from iput when count goes to zero. Linux version of inactive.
226  * For Linux 2.2, this funcionality has moved to the delete inode super op.
227  * If we use the common inode pool, we'll need to set i_nlink to 0 here.
228  * That will trigger the call to delete routine.
229  */
230 void afs_delete_inode(struct inode *ip)
231 {
232     cred_t *credp = crref();
233     struct vcache *vc = (struct vcache*)ip;
234
235     AFS_GLOCK();
236 #if defined(AFS_LINUX24_ENV)
237     lock_kernel();
238     if (atomic_read(&ip->i_count) > 1)
239 #else
240     if (ip->i_count > 1)
241 #endif
242         printf("afs_put_inode: ino %d (0x%x) has count %d\n", ip->i_ino, ip);
243
244     ObtainWriteLock(&vc->lock, 504);
245     afs_InactiveVCache(vc, credp);
246 #if defined(AFS_LINUX24_ENV)
247     atomic_set(&ip->i_count, 0);
248 #else
249     ip->i_count = 0;
250 #endif
251     ip->i_nlink = 0; /* iput checks this after calling this routine. */
252     ReleaseWriteLock(&vc->lock);
253
254 #ifdef AFS_LINUX24_ENV
255     unlock_kernel();
256 #endif
257     AFS_GUNLOCK();
258     crfree(credp);
259 }
260
261 /* afs_put_super
262  * Called from unmount to release super_block. */
263 void afs_put_super(struct super_block *sbp)
264 {
265     extern int afs_afs_cold_shutdown;
266     int code = 0;
267     int fv_slept;
268
269     AFS_GLOCK();
270     AFS_STATCNT(afs_unmount);
271
272     if (!suser())
273         return;
274
275     afs_globalVFS = 0;
276     afs_globalVp = 0;
277     afs_shutdown();
278
279     osi_linux_verify_alloced_memory();
280  done:
281     AFS_GUNLOCK();
282
283     if (!code) {
284         sbp->s_dev = 0;
285         MOD_DEC_USE_COUNT;
286     }
287 }
288
289 #ifdef NOTUSED
290 /* afs_write_super
291  * Not required since we don't write out a super block. */
292 void afs_write_super(struct super_block *sbp)
293 {
294 }
295
296 /* afs_remount_fs
297  * Used to remount filesystems with different flags. Not relevant for AFS.
298  */
299 int afs_remount_fs(struct super_block *sbp, int *, char *)
300 {
301     return -EINVAL;
302 }
303 #endif
304
305 /* afs_statfs
306  * statp is in user space, so we need to cobble together a statfs, then
307  * copy it.
308  */
309 #if defined(AFS_LINUX24_ENV)
310 int afs_statfs(struct super_block *sbp, struct statfs *statp)
311 #else
312 int afs_statfs(struct super_block *sbp, struct statfs *statp, int size)
313 #endif
314 {
315     struct statfs stat;
316
317     AFS_STATCNT(afs_statfs);
318
319 #if !defined(AFS_LINUX24_ENV)
320     if (size < sizeof(struct statfs))
321         return;
322         
323     memset(&stat, 0, size);
324 #endif
325     stat.f_type = 0; /* Can we get a real type sometime? */
326     stat.f_bsize = sbp->s_blocksize;
327     stat.f_blocks =  stat.f_bfree =  stat.f_bavail =  stat.f_files =
328         stat.f_ffree = 9000000;
329     stat.f_fsid.val[0] = AFS_VFSMAGIC;
330     stat.f_fsid.val[1] = AFS_VFSFSID;
331     stat.f_namelen = 256;
332
333 #if defined(AFS_LINUX24_ENV)
334     *statp = stat;
335 #else
336     memcpy_tofs(statp, &stat, size);
337 #endif
338     return 0;
339 }
340
341
342 #if defined(AFS_LINUX24_ENV)
343 struct super_operations afs_sops = {
344     read_inode:        afs_read_inode,
345     write_inode:       afs_write_inode,
346     delete_inode:      afs_delete_inode,
347     put_super:         afs_put_super,
348     statfs:            afs_statfs,
349 };
350 #else
351 struct super_operations afs_sops = {
352     afs_read_inode,
353     afs_write_inode,            /* afs_write_inode - see doc above. */
354     NULL,               /* afs_put_inode */
355     afs_delete_inode,
356     afs_notify_change,
357     afs_put_super,
358     NULL,               /* afs_write_super - see doc above */
359     afs_statfs,
360     NULL,               /* afs_remount_fs - see doc above */
361     NULL,               /* afs_clear_inode */
362     NULL,               /* afs_umount_begin */
363 };
364 #endif
365
366 /************** Support routines ************************/
367
368 /* vattr_setattr
369  * Set iattr data into vattr. Assume vattr cleared before call.
370  */
371 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
372 {
373     vattrp->va_mask = iattrp->ia_valid;
374     if (iattrp->ia_valid & ATTR_MODE)
375         vattrp->va_mode = iattrp->ia_mode;
376     if (iattrp->ia_valid & ATTR_UID)
377         vattrp->va_uid = iattrp->ia_uid;
378     if (iattrp->ia_valid & ATTR_GID)
379         vattrp->va_gid = iattrp->ia_gid;
380     if (iattrp->ia_valid & ATTR_SIZE)
381         vattrp->va_size = iattrp->ia_size;
382     if (iattrp->ia_valid & ATTR_ATIME) {
383         vattrp->va_atime.tv_sec = iattrp->ia_atime;
384         vattrp->va_atime.tv_usec = 0;
385     }
386     if (iattrp->ia_valid & ATTR_MTIME) {
387         vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
388         vattrp->va_mtime.tv_usec = 0;
389     }
390     if (iattrp->ia_valid & ATTR_CTIME) {
391         vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
392         vattrp->va_ctime.tv_usec = 0;
393     }
394 }
395
396 /* update_inode_cache
397  * Update inode with info from vattr struct. Use va_mask to determine what
398  * to update.
399  */
400 static void update_inode_cache(struct inode *ip, struct vattr *vp)
401 {
402     if (vp->va_mask & ATTR_MODE)
403         ip->i_mode = vp->va_mode;
404     if (vp->va_mask & ATTR_UID)
405         ip->i_uid = vp->va_uid;
406     if (vp->va_mask & ATTR_GID)
407         ip->i_gid = vp->va_gid;
408     if (vp->va_mask & ATTR_SIZE)
409         ip->i_size = vp->va_size;
410     if (vp->va_mask & ATTR_ATIME)
411         ip->i_atime = vp->va_atime.tv_sec;
412     if (vp->va_mask & ATTR_MTIME)
413         ip->i_mtime = vp->va_mtime.tv_sec;
414     if (vp->va_mask & ATTR_CTIME)
415         ip->i_ctime = vp->va_ctime.tv_sec;
416 }
417
418 /* vattr2inode
419  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
420  */
421 void vattr2inode(struct inode *ip, struct vattr *vp)
422 {
423     ip->i_ino = vp->va_nodeid;
424     ip->i_nlink = vp->va_nlink;
425     ip->i_blocks = vp->va_blocks;
426     ip->i_blksize = vp->va_blocksize;
427     ip->i_rdev = vp->va_rdev;
428     ip->i_mode = vp->va_mode;
429     ip->i_uid = vp->va_uid;
430     ip->i_gid = vp->va_gid;
431     ip->i_size = vp->va_size;
432     ip->i_atime = vp->va_atime.tv_sec;
433     ip->i_mtime = vp->va_mtime.tv_sec;
434     ip->i_ctime = vp->va_ctime.tv_sec;
435
436     /* we should put our inodes on a dummy inode list to keep linux happy.*/
437     if (!ip->i_list.prev && !ip->i_list.next) { 
438         /* this might be bad as we are reaching under the covers of the 
439          * list structure but we want to avoid putting the inode 
440          * on the list more than once.   */
441         put_inode_on_dummy_list(ip);
442     }
443 }
444
445 /* Put this afs inode on our own dummy list. Linux expects to see inodes
446  * nicely strung up in lists. Linux inode syncing code chokes on our inodes if
447  * they're not on any lists.
448  */
449 void put_inode_on_dummy_list(struct inode *ip)
450 {
451     /* Initialize list. See explanation above. */
452     list_add(&ip->i_list, &dummy_inode_list);
453 }
454
455 /* And yet another routine to update the inode cache - called from ProcessFS */
456 void vcache2inode(struct vcache *avc)
457 {
458     struct vattr vattr;
459
460     VATTR_NULL(&vattr);
461     afs_CopyOutAttrs(avc, &vattr); /* calls vattr2inode */
462 }