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