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