ea7bc5249f9da8c19846dcb0e5e6c5db10a7f9c4
[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         module_put(THIS_MODULE);
138     }
139
140     AFS_GUNLOCK();
141     return code ? -EINVAL : 0;
142 }
143
144
145 /* afs_root - stat the root of the file system. AFS global held on entry. */
146 static int
147 afs_root(struct super_block *afsp)
148 {
149     register afs_int32 code = 0;
150     struct vrequest treq;
151     register struct vcache *tvp = 0;
152
153     AFS_STATCNT(afs_root);
154     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
155         tvp = afs_globalVp;
156     } else {
157         cred_t *credp = crref();
158
159         if (afs_globalVp) {
160             afs_PutVCache(afs_globalVp);
161             afs_globalVp = NULL;
162         }
163
164         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
165             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
166             if (tvp) {
167                 struct inode *ip = AFSTOV(tvp);
168                 struct vattr vattr;
169
170                 afs_getattr(tvp, &vattr, credp);
171                 afs_fill_inode(ip, &vattr);
172
173                 /* setup super_block and mount point inode. */
174                 afs_globalVp = tvp;
175                 afsp->s_root = d_alloc_root(ip);
176                 afsp->s_root->d_op = &afs_dentry_operations;
177             } else
178                 code = ENOENT;
179         }
180         crfree(credp);
181     }
182
183     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
184                ICL_TYPE_INT32, code);
185     return code;
186 }
187
188 /* super_operations */
189
190 /* afs_notify_change
191  * Linux version of setattr call. What to change is in the iattr struct.
192  * We need to set bits in both the Linux inode as well as the vcache.
193  */
194 int
195 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
196 {
197     struct vattr vattr;
198     cred_t *credp = crref();
199     struct inode *ip = dp->d_inode;
200     int code;
201
202     VATTR_NULL(&vattr);
203     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
204
205     lock_kernel();
206     AFS_GLOCK();
207     code = afs_setattr(VTOAFS(ip), &vattr, credp);
208     if (!code) {
209         afs_getattr(VTOAFS(ip), &vattr, credp);
210         vattr2inode(ip, &vattr);
211     }
212     AFS_GUNLOCK();
213     unlock_kernel();
214     crfree(credp);
215     return -code;
216 }
217
218
219 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
220 static afs_kmem_cache_t *afs_inode_cachep;
221
222 static struct inode *
223 afs_alloc_inode(struct super_block *sb)
224 {
225     struct vcache *vcp;
226
227     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, KALLOC_TYPE);
228     if (!vcp)
229         return NULL;
230
231     return AFSTOV(vcp);
232 }
233
234 static void
235 afs_destroy_inode(struct inode *inode)
236 {
237     kmem_cache_free(afs_inode_cachep, inode);
238 }
239
240 void
241 init_once(void * foo)
242 {
243     struct vcache *vcp = (struct vcache *) foo;
244
245     inode_init_once(AFSTOV(vcp));
246 }
247
248 int
249 afs_init_inodecache(void)
250 {
251 #if defined(KMEM_CACHE_TAKES_DTOR)
252     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
253                 sizeof(struct vcache), 0,
254                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func, NULL);
255 #else
256     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
257                 sizeof(struct vcache), 0,
258                 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT, init_once_func);
259 #endif
260     if (afs_inode_cachep == NULL)
261         return -ENOMEM;
262     return 0;
263 }
264
265 void
266 afs_destroy_inodecache(void)
267 {
268     if (afs_inode_cachep)
269         (void) kmem_cache_destroy(afs_inode_cachep);
270 }
271 #else
272 int
273 afs_init_inodecache(void)
274 {
275     return 0;
276 }
277
278 void
279 afs_destroy_inodecache(void)
280 {
281     return;
282 }
283 #endif
284
285 static void
286 afs_clear_inode(struct inode *ip)
287 {
288     struct vcache *vcp = VTOAFS(ip);
289
290     if (vcp->vlruq.prev || vcp->vlruq.next)
291         osi_Panic("inode freed while on LRU");
292     if (vcp->hnext)
293         osi_Panic("inode freed while still hashed");
294
295 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
296     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
297 #endif
298 }
299
300 /* afs_put_super
301  * Called from unmount to release super_block. */
302 static void
303 afs_put_super(struct super_block *sbp)
304 {
305     AFS_GLOCK();
306     AFS_STATCNT(afs_unmount);
307
308     afs_globalVFS = 0;
309     afs_globalVp = 0;
310
311     osi_linux_free_inode_pages();       /* invalidate and release remaining AFS inodes. */
312     afs_shutdown();
313     mntput(afs_cacheMnt);
314
315     osi_linux_verify_alloced_memory();
316 #if defined(HAVE_BDI_INIT)
317     bdi_destroy(&afs_backing_dev_info);
318 #endif
319     AFS_GUNLOCK();
320
321     sbp->s_dev = 0;
322     module_put(THIS_MODULE);
323 }
324
325
326 /* afs_statfs
327  * statp is in user space, so we need to cobble together a statfs, then
328  * copy it.
329  */
330 int
331 #if defined(STATFS_TAKES_DENTRY)
332 afs_statfs(struct dentry *dentry, struct kstatfs *statp)
333 #else
334 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
335 #endif
336 {
337     memset(statp, 0, sizeof(*statp));
338
339     AFS_STATCNT(afs_statfs);
340
341     /* hardcode in case that which is giveth is taken away */
342     statp->f_type = 0x5346414F;
343 #if defined(STATFS_TAKES_DENTRY)
344     statp->f_bsize = dentry->d_sb->s_blocksize;
345 #else
346     statp->f_bsize = sbp->s_blocksize;
347 #endif
348     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
349         statp->f_ffree = 9000000;
350     statp->f_fsid.val[0] = AFS_VFSMAGIC;
351     statp->f_fsid.val[1] = AFS_VFSFSID;
352     statp->f_namelen = 256;
353
354     return 0;
355 }
356
357 struct super_operations afs_sops = {
358 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
359   .alloc_inode =        afs_alloc_inode,
360   .destroy_inode =      afs_destroy_inode,
361 #endif
362   .clear_inode =        afs_clear_inode,
363   .put_super =          afs_put_super,
364   .statfs =             afs_statfs,
365 };
366
367 /************** Support routines ************************/
368
369 /* vattr_setattr
370  * Set iattr data into vattr. Assume vattr cleared before call.
371  */
372 static void
373 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.tv_sec;
386         vattrp->va_atime.tv_usec = 0;
387     }
388     if (iattrp->ia_valid & ATTR_MTIME) {
389         vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
390         vattrp->va_mtime.tv_usec = 0;
391     }
392     if (iattrp->ia_valid & ATTR_CTIME) {
393         vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
394         vattrp->va_ctime.tv_usec = 0;
395     }
396 }
397
398 /* vattr2inode
399  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
400  */
401 void
402 vattr2inode(struct inode *ip, struct vattr *vp)
403 {
404     ip->i_ino = vp->va_nodeid;
405     ip->i_nlink = vp->va_nlink;
406     ip->i_blocks = vp->va_blocks;
407 #ifdef STRUCT_INODE_HAS_I_BLKBITS
408     ip->i_blkbits = AFS_BLKBITS;
409 #endif
410 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
411     ip->i_blksize = vp->va_blocksize;
412 #endif
413     ip->i_rdev = vp->va_rdev;
414     ip->i_mode = vp->va_mode;
415     ip->i_uid = vp->va_uid;
416     ip->i_gid = vp->va_gid;
417     i_size_write(ip, vp->va_size);
418     ip->i_atime.tv_sec = vp->va_atime.tv_sec;
419     ip->i_atime.tv_nsec = 0;
420     ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
421     /* Set the mtime nanoseconds to the sysname generation number.
422      * This convinces NFS clients that all directories have changed
423      * any time the sysname list changes.
424      */
425     ip->i_mtime.tv_nsec = afs_sysnamegen;
426     ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
427     ip->i_ctime.tv_nsec = 0;
428 }
429
430 /* osi_linux_free_inode_pages
431  *
432  * Free all vnodes remaining in the afs hash.  Must be done before
433  * shutting down afs and freeing all memory.
434  */
435 void
436 osi_linux_free_inode_pages(void)
437 {
438     int i;
439     struct vcache *tvc, *nvc;
440     extern struct vcache *afs_vhashT[VCSIZE];
441
442     for (i = 0; i < VCSIZE; i++) {
443         for (tvc = afs_vhashT[i]; tvc; ) {
444             int slept;
445         
446             nvc = tvc->hnext;
447             if (afs_FlushVCache(tvc, &slept))           /* slept always 0 for linux? */
448                 printf("Failed to invalidate all pages on inode 0x%p\n", tvc);
449             tvc = nvc;
450         }
451     }
452 }