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