missing brace in afs_get_sb
[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
19 #define __NO_VERSION__          /* don't define kernel_version in module.h */
20 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/afs_stats.h"
24 #include <linux/smp_lock.h>
25
26 #include "osi_compat.h"
27
28 struct vcache *afs_globalVp = 0;
29 struct vfs *afs_globalVFS = 0;
30 struct vfsmount *afs_cacheMnt;
31 int afs_was_mounted = 0;        /* Used to force reload if mount/unmount/mount */
32
33 extern struct super_operations afs_sops;
34 #if !defined(AFS_NONFSTRANS)
35 extern struct export_operations afs_export_ops;
36 #endif
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 int afs_root(struct super_block *afsp);
45 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
46 int afs_fill_super(struct super_block *sb, void *data, int silent);
47
48 /* afs_file_system
49  * VFS entry for Linux - installed in init_module
50  * Linux mounts file systems by:
51  * 1) register_filesystem(&afs_file_system) - done in init_module
52  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
53  *    We are expected to setup the super_block. See afs_read_super.
54  */
55
56
57 /* afs_read_super
58  * read the "super block" for AFS - roughly eguivalent to struct vfs.
59  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
60  */
61 #ifdef GET_SB_HAS_STRUCT_VFSMOUNT
62 static int
63 afs_get_sb(struct file_system_type *fs_type, int flags,
64            const char *dev_name, void *data, struct vfsmount *mnt) {
65     return get_sb_nodev(fs_type, flags, data, afs_fill_super, mnt);
66 }
67 #else
68 static struct superblock *
69 afs_get_sb(struct file_system_type *fs_type, int flags,
70            const char *dev_name, void *data) {
71     return get_sb_nodev(fs_type, flags, data, afs_fill_super);
72 }
73 #endif
74
75 struct file_system_type afs_fs_type = {
76     .owner = THIS_MODULE,
77     .name = "afs",
78     .get_sb = afs_get_sb,
79     .kill_sb = kill_anon_super,
80     .fs_flags = FS_BINARY_MOUNTDATA,
81 };
82
83 struct backing_dev_info afs_backing_dev_info = {
84     .ra_pages           = 32,
85 };
86
87 int
88 afs_fill_super(struct super_block *sb, void *data, int silent)
89 {
90     int code = 0;
91
92     AFS_GLOCK();
93     if (afs_was_mounted) {
94         printf
95             ("You must reload the AFS kernel extensions before remounting AFS.\n");
96         AFS_GUNLOCK();
97         return -EINVAL;
98     }
99     afs_was_mounted = 1;
100
101     /* Set basics of super_block */
102    __module_get(THIS_MODULE);
103
104     afs_globalVFS = sb;
105     sb->s_blocksize = 1024;
106     sb->s_blocksize_bits = 10;
107     sb->s_magic = AFS_VFSMAGIC;
108     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
109 #if defined(HAVE_BDI_INIT)
110     bdi_init(&afs_backing_dev_info);
111 #endif
112 #if defined (STRUCT_SUPER_BLOCK_HAS_S_BDI)
113     sb->s_bdi = &afs_backing_dev_info;
114     /* The name specified here will appear in the flushing thread name - flush-afs */
115     bdi_register(&afs_backing_dev_info, NULL, "afs");
116 #endif
117 #if !defined(AFS_NONFSTRANS)
118     sb->s_export_op = &afs_export_ops;
119 #endif
120 #if defined(MAX_NON_LFS)
121 #ifdef AFS_64BIT_CLIENT
122 #if !defined(MAX_LFS_FILESIZE)
123 #if BITS_PER_LONG==32
124 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
125 #elif BITS_PER_LONG==64
126 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
127 #endif
128 #endif
129     sb->s_maxbytes = MAX_LFS_FILESIZE;
130 #else
131     sb->s_maxbytes = MAX_NON_LFS;
132 #endif
133 #endif
134     code = afs_root(sb);
135     if (code) {
136         afs_globalVFS = NULL;
137         osi_linux_free_inode_pages();
138         module_put(THIS_MODULE);
139     }
140
141     AFS_GUNLOCK();
142     return code ? -EINVAL : 0;
143 }
144
145
146 /* afs_root - stat the root of the file system. AFS global held on entry. */
147 static int
148 afs_root(struct super_block *afsp)
149 {
150     register afs_int32 code = 0;
151     struct vrequest treq;
152     register struct vcache *tvp = 0;
153
154     AFS_STATCNT(afs_root);
155     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
156         tvp = afs_globalVp;
157     } else {
158         cred_t *credp = crref();
159
160         if (afs_globalVp) {
161             afs_PutVCache(afs_globalVp);
162             afs_globalVp = NULL;
163         }
164
165         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
166             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
167             if (tvp) {
168                 struct inode *ip = AFSTOV(tvp);
169                 struct vattr vattr;
170
171                 afs_getattr(tvp, &vattr, credp);
172                 afs_fill_inode(ip, &vattr);
173
174                 /* setup super_block and mount point inode. */
175                 afs_globalVp = tvp;
176                 afsp->s_root = d_alloc_root(ip);
177                 afsp->s_root->d_op = &afs_dentry_operations;
178             } else
179                 code = ENOENT;
180         }
181         crfree(credp);
182     }
183
184     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
185                ICL_TYPE_INT32, code);
186     return code;
187 }
188
189 /* super_operations */
190
191 /* afs_notify_change
192  * Linux version of setattr call. What to change is in the iattr struct.
193  * We need to set bits in both the Linux inode as well as the vcache.
194  */
195 int
196 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
197 {
198     struct vattr vattr;
199     cred_t *credp = crref();
200     struct inode *ip = dp->d_inode;
201     int code;
202
203     VATTR_NULL(&vattr);
204     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
205
206     lock_kernel();
207     AFS_GLOCK();
208     code = afs_setattr(VTOAFS(ip), &vattr, credp);
209     if (!code) {
210         afs_getattr(VTOAFS(ip), &vattr, credp);
211         vattr2inode(ip, &vattr);
212     }
213     AFS_GUNLOCK();
214     unlock_kernel();
215     crfree(credp);
216     return -code;
217 }
218
219
220 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
221 static afs_kmem_cache_t *afs_inode_cachep;
222
223 static struct inode *
224 afs_alloc_inode(struct super_block *sb)
225 {
226     struct vcache *vcp;
227
228     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, KALLOC_TYPE);
229     if (!vcp)
230         return NULL;
231
232     return AFSTOV(vcp);
233 }
234
235 static void
236 afs_destroy_inode(struct inode *inode)
237 {
238     kmem_cache_free(afs_inode_cachep, inode);
239 }
240
241 void
242 init_once(void * foo)
243 {
244     struct vcache *vcp = (struct vcache *) foo;
245
246     inode_init_once(AFSTOV(vcp));
247 }
248
249 int
250 afs_init_inodecache(void)
251 {
252 #if defined(KMEM_CACHE_TAKES_DTOR)
253     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
254                 sizeof(struct vcache), 0,
255                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func, NULL);
256 #else
257     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
258                 sizeof(struct vcache), 0,
259                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func);
260 #endif
261     if (afs_inode_cachep == NULL)
262         return -ENOMEM;
263     return 0;
264 }
265
266 void
267 afs_destroy_inodecache(void)
268 {
269     if (afs_inode_cachep)
270         (void) kmem_cache_destroy(afs_inode_cachep);
271 }
272 #else
273 int
274 afs_init_inodecache(void)
275 {
276     return 0;
277 }
278
279 void
280 afs_destroy_inodecache(void)
281 {
282     return;
283 }
284 #endif
285
286 static void
287 afs_clear_inode(struct inode *ip)
288 {
289     struct vcache *vcp = VTOAFS(ip);
290
291     if (vcp->vlruq.prev || vcp->vlruq.next)
292         osi_Panic("inode freed while on LRU");
293     if (vcp->hnext)
294         osi_Panic("inode freed while still hashed");
295
296 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
297     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
298 #endif
299 }
300
301 /* afs_put_super
302  * Called from unmount to release super_block. */
303 static void
304 afs_put_super(struct super_block *sbp)
305 {
306     AFS_GLOCK();
307     AFS_STATCNT(afs_unmount);
308
309     afs_globalVFS = 0;
310     afs_globalVp = 0;
311
312     osi_linux_free_inode_pages();       /* invalidate and release remaining AFS inodes. */
313     afs_shutdown();
314     mntput(afs_cacheMnt);
315
316     osi_linux_verify_alloced_memory();
317 #if defined(HAVE_BDI_INIT)
318     bdi_destroy(&afs_backing_dev_info);
319 #endif
320     AFS_GUNLOCK();
321
322     sbp->s_dev = 0;
323     module_put(THIS_MODULE);
324 }
325
326
327 /* afs_statfs
328  * statp is in user space, so we need to cobble together a statfs, then
329  * copy it.
330  */
331 int
332 #if defined(STATFS_TAKES_DENTRY)
333 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
334 #else
335 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
336 #endif
337 {
338     memset(statp, 0, sizeof(*statp));
339
340     AFS_STATCNT(afs_statfs);
341
342     /* hardcode in case that which is giveth is taken away */
343     statp->f_type = 0x5346414F;
344 #if defined(STATFS_TAKES_DENTRY)
345     statp->f_bsize = dentry->d_sb->s_blocksize;
346 #else
347     statp->f_bsize = sbp->s_blocksize;
348 #endif
349     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
350         statp->f_ffree = 9000000;
351     statp->f_fsid.val[0] = AFS_VFSMAGIC;
352     statp->f_fsid.val[1] = AFS_VFSFSID;
353     statp->f_namelen = 256;
354
355     return 0;
356 }
357
358 struct super_operations afs_sops = {
359 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
360   .alloc_inode =        afs_alloc_inode,
361   .destroy_inode =      afs_destroy_inode,
362 #endif
363   .clear_inode =        afs_clear_inode,
364   .put_super =          afs_put_super,
365   .statfs =             afs_statfs,
366 };
367
368 /************** Support routines ************************/
369
370 /* vattr_setattr
371  * Set iattr data into vattr. Assume vattr cleared before call.
372  */
373 static void
374 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
375 {
376     vattrp->va_mask = iattrp->ia_valid;
377     if (iattrp->ia_valid & ATTR_MODE)
378         vattrp->va_mode = iattrp->ia_mode;
379     if (iattrp->ia_valid & ATTR_UID)
380         vattrp->va_uid = iattrp->ia_uid;
381     if (iattrp->ia_valid & ATTR_GID)
382         vattrp->va_gid = iattrp->ia_gid;
383     if (iattrp->ia_valid & ATTR_SIZE)
384         vattrp->va_size = iattrp->ia_size;
385     if (iattrp->ia_valid & ATTR_ATIME) {
386         vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
387         vattrp->va_atime.tv_usec = 0;
388     }
389     if (iattrp->ia_valid & ATTR_MTIME) {
390         vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
391         vattrp->va_mtime.tv_usec = 0;
392     }
393     if (iattrp->ia_valid & ATTR_CTIME) {
394         vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
395         vattrp->va_ctime.tv_usec = 0;
396     }
397 }
398
399 /* vattr2inode
400  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
401  */
402 void
403 vattr2inode(struct inode *ip, struct vattr *vp)
404 {
405     ip->i_ino = vp->va_nodeid;
406     ip->i_nlink = vp->va_nlink;
407     ip->i_blocks = vp->va_blocks;
408 #ifdef STRUCT_INODE_HAS_I_BLKBITS
409     ip->i_blkbits = AFS_BLKBITS;
410 #endif
411 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
412     ip->i_blksize = vp->va_blocksize;
413 #endif
414     ip->i_rdev = vp->va_rdev;
415     ip->i_mode = vp->va_mode;
416     ip->i_uid = vp->va_uid;
417     ip->i_gid = vp->va_gid;
418     i_size_write(ip, vp->va_size);
419     ip->i_atime.tv_sec = vp->va_atime.tv_sec;
420     ip->i_atime.tv_nsec = 0;
421     ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
422     /* Set the mtime nanoseconds to the sysname generation number.
423      * This convinces NFS clients that all directories have changed
424      * any time the sysname list changes.
425      */
426     ip->i_mtime.tv_nsec = afs_sysnamegen;
427     ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
428     ip->i_ctime.tv_nsec = 0;
429 }
430
431 /* osi_linux_free_inode_pages
432  *
433  * Free all vnodes remaining in the afs hash.  Must be done before
434  * shutting down afs and freeing all memory.
435  */
436 void
437 osi_linux_free_inode_pages(void)
438 {
439     int i;
440     struct vcache *tvc, *nvc;
441     extern struct vcache *afs_vhashT[VCSIZE];
442
443     for (i = 0; i < VCSIZE; i++) {
444         for (tvc = afs_vhashT[i]; tvc; ) {
445             int slept;
446         
447             nvc = tvc->hnext;
448             if (afs_FlushVCache(tvc, &slept))           /* slept always 0 for linux? */
449                 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);
450             tvc = nvc;
451         }
452     }
453 }