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