DEVEL15-linux-vfsops-remove-redundant-code-20060601
[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
19     ("$Header$");
20
21 #define __NO_VERSION__          /* don't define kernel_version in module.h */
22 #include <linux/module.h> /* early to avoid printf->printk mapping */
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "afs/afs_stats.h"
26 #if !defined(AFS_LINUX26_ENV)
27 #include "h/locks.h"
28 #endif
29 #if defined(AFS_LINUX24_ENV)
30 #include "h/smp_lock.h"
31 #endif
32
33
34 struct vcache *afs_globalVp = 0;
35 struct vfs *afs_globalVFS = 0;
36 #if defined(AFS_LINUX24_ENV)
37 struct vfsmount *afs_cacheMnt;
38 #endif
39 int afs_was_mounted = 0;        /* Used to force reload if mount/unmount/mount */
40
41 extern struct super_operations afs_sops;
42 extern afs_rwlock_t afs_xvcache;
43 extern struct afs_q VLRU;
44
45 extern struct dentry_operations afs_dentry_operations;
46
47 /* Forward declarations */
48 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
49 static int afs_root(struct super_block *afsp);
50 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
51 int afs_fill_super(struct super_block *sb, void *data, int silent);
52 static struct super_block *afs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data);
53
54 /* afs_file_system
55  * VFS entry for Linux - installed in init_module
56  * Linux mounts file systems by:
57  * 1) register_filesystem(&afs_file_system) - done in init_module
58  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
59  *    We are expected to setup the super_block. See afs_read_super.
60  */
61 #if defined(AFS_LINUX26_ENV)
62 struct backing_dev_info afs_backing_dev_info = {
63         .ra_pages       = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE,
64         .state          = 0,
65 };
66
67 struct file_system_type afs_fs_type = {
68     .owner = THIS_MODULE,
69     .name = "afs",
70     .get_sb = afs_get_sb,
71     .kill_sb = kill_anon_super,
72     .fs_flags = FS_BINARY_MOUNTDATA,
73 };
74 #elif defined(AFS_LINUX24_ENV)
75 DECLARE_FSTYPE(afs_fs_type, "afs", afs_read_super, 0);
76 #else
77 struct file_system_type afs_fs_type = {
78     "afs",                      /* name - used by mount operation. */
79     0,                          /* requires_dev - no for network filesystems. mount() will 
80                                  * pass us an "unnamed" device. */
81     afs_read_super,             /* wrapper to afs_mount */
82     NULL                        /* pointer to next file_system_type once registered. */
83 };
84 #endif
85
86 /* afs_read_super
87  * read the "super block" for AFS - roughly eguivalent to struct vfs.
88  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
89  */
90 #if defined(AFS_LINUX26_ENV)
91 static struct super_block *
92 afs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
93 {
94     return get_sb_nodev(fs_type, flags, data, afs_fill_super);
95 }
96
97 int
98 afs_fill_super(struct super_block *sb, void *data, int silent)
99 #else
100 struct super_block *
101 afs_read_super(struct super_block *sb, void *data, int silent)
102 #endif
103 {
104     int code = 0;
105
106     AFS_GLOCK();
107     if (afs_was_mounted) {
108         printf
109             ("You must reload the AFS kernel extensions before remounting AFS.\n");
110         AFS_GUNLOCK();
111 #if defined(AFS_LINUX26_ENV)
112         return -EINVAL;
113 #else
114         return NULL;
115 #endif
116     }
117     afs_was_mounted = 1;
118
119     /* Set basics of super_block */
120 #if !defined(AFS_LINUX24_ENV)
121     lock_super(sb);
122 #endif
123 #if defined(AFS_LINUX26_ENV)
124    __module_get(THIS_MODULE);
125 #else
126     MOD_INC_USE_COUNT;
127 #endif
128
129     afs_globalVFS = sb;
130     sb->s_blocksize = 1024;
131     sb->s_blocksize_bits = 10;
132     sb->s_magic = AFS_VFSMAGIC;
133     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
134 #if defined(MAX_NON_LFS)
135 #ifdef AFS_64BIT_CLIENT
136 #if !defined(MAX_LFS_FILESIZE)
137 #if BITS_PER_LONG==32
138 #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) 
139 #elif BITS_PER_LONG==64
140 #define MAX_LFS_FILESIZE 0x7fffffffffffffff
141 #endif
142 #endif
143     sb->s_maxbytes = MAX_LFS_FILESIZE;
144 #else
145     sb->s_maxbytes = MAX_NON_LFS;
146 #endif
147 #endif
148     code = afs_root(sb);
149     if (code) {
150         afs_globalVFS = NULL;
151 #if defined(AFS_LINUX26_ENV)
152         module_put(THIS_MODULE);
153 #else
154         MOD_DEC_USE_COUNT;
155 #endif
156     }
157
158 #if !defined(AFS_LINUX24_ENV)
159     unlock_super(sb);
160 #endif
161
162     AFS_GUNLOCK();
163 #if defined(AFS_LINUX26_ENV)
164     return code ? -EINVAL : 0;
165 #else
166     return code ? NULL : sb;
167 #endif
168 }
169
170
171 /* afs_root - stat the root of the file system. AFS global held on entry. */
172 static int
173 afs_root(struct super_block *afsp)
174 {
175     register afs_int32 code = 0;
176     struct vrequest treq;
177     register struct vcache *tvp = 0;
178
179     AFS_STATCNT(afs_root);
180     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
181         tvp = afs_globalVp;
182     } else {
183         cred_t *credp = crref();
184
185         if (afs_globalVp) {
186             afs_PutVCache(afs_globalVp);
187             afs_globalVp = NULL;
188         }
189
190         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
191             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
192             if (tvp) {
193                 struct inode *ip = AFSTOV(tvp);
194                 struct vattr vattr;
195
196                 afs_getattr(tvp, &vattr, credp);
197                 afs_fill_inode(ip, &vattr);
198
199                 /* setup super_block and mount point inode. */
200                 afs_globalVp = tvp;
201 #if defined(AFS_LINUX24_ENV)
202                 afsp->s_root = d_alloc_root(ip);
203 #else
204                 afsp->s_root = d_alloc_root(ip, NULL);
205 #endif
206                 afsp->s_root->d_op = &afs_dentry_operations;
207             } else
208                 code = ENOENT;
209         }
210         crfree(credp);
211     }
212
213     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
214                ICL_TYPE_INT32, code);
215     return code;
216 }
217
218 /* super_operations */
219
220 /* afs_notify_change
221  * Linux version of setattr call. What to change is in the iattr struct.
222  * We need to set bits in both the Linux inode as well as the vcache.
223  */
224 int
225 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
226 {
227     struct vattr vattr;
228     cred_t *credp = crref();
229     struct inode *ip = dp->d_inode;
230     int code;
231
232     VATTR_NULL(&vattr);
233     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
234
235 #if defined(AFS_LINUX26_ENV)
236     lock_kernel();
237 #endif
238     AFS_GLOCK();
239     code = afs_setattr(VTOAFS(ip), &vattr, credp);
240     if (!code) {
241         afs_getattr(VTOAFS(ip), &vattr, credp);
242         vattr2inode(ip, &vattr);
243     }
244     AFS_GUNLOCK();
245 #if defined(AFS_LINUX26_ENV)
246     unlock_kernel();
247 #endif
248     crfree(credp);
249     return -code;
250 }
251
252
253 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
254 static kmem_cache_t *afs_inode_cachep;
255
256 static struct inode *
257 afs_alloc_inode(struct super_block *sb)
258 {
259     struct vcache *vcp;
260
261     vcp = (struct vcache *) kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
262     if (!vcp)
263         return NULL;
264
265     return AFSTOV(vcp);
266 }
267
268 static void
269 afs_destroy_inode(struct inode *inode)
270 {
271     kmem_cache_free(afs_inode_cachep, inode);
272 }
273
274 static void
275 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
276 {
277     struct vcache *vcp = (struct vcache *) foo;
278
279     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
280         SLAB_CTOR_CONSTRUCTOR)
281         inode_init_once(AFSTOV(vcp));
282 }
283
284 int
285 afs_init_inodecache(void)
286 {
287 #ifndef SLAB_RECLAIM_ACCOUNT
288 #define SLAB_RECLAIM_ACCOUNT 0
289 #endif
290
291     afs_inode_cachep = kmem_cache_create("afs_inode_cache",
292                                          sizeof(struct vcache),
293                                          0, SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT,
294                                          init_once, NULL);
295     if (afs_inode_cachep == NULL)
296         return -ENOMEM;
297     return 0;
298 }
299
300 void
301 afs_destroy_inodecache(void)
302 {
303     if (kmem_cache_destroy(afs_inode_cachep))
304         printk(KERN_INFO "afs_inode_cache: not all structures were freed\n");
305 }
306 #else
307 int
308 afs_init_inodecache(void)
309 {
310     return 0;
311 }
312
313 void
314 afs_destroy_inodecache(void)
315 {
316     return;
317 }
318 #endif
319
320 static void
321 afs_clear_inode(struct inode *ip)
322 {
323     struct vcache *vcp = VTOAFS(ip);
324
325     if (vcp->vlruq.prev || vcp->vlruq.next)
326         osi_Panic("inode freed while on LRU");
327     if (vcp->hnext)
328         osi_Panic("inode freed while still hashed");
329
330 #if !defined(STRUCT_SUPER_HAS_ALLOC_INODE)
331     afs_osi_Free(ip->u.generic_ip, sizeof(struct vcache));
332 #endif
333 }
334
335 /* afs_put_inode
336  * Linux version of inactive.  When refcount == 2, we are about to
337  * decrement to 1 and the only reference remaining should be for
338  * the VLRU
339  */
340
341 static void
342 afs_put_inode(struct inode *ip)
343 {
344     struct vcache *vcp = VTOAFS(ip);
345
346     AFS_GLOCK();
347     if (VREFCOUNT(vcp) == 2) 
348         afs_InactiveVCache(vcp, NULL);
349     AFS_GUNLOCK();
350 }
351
352 /* afs_put_super
353  * Called from unmount to release super_block. */
354 static void
355 afs_put_super(struct super_block *sbp)
356 {
357     AFS_GLOCK();
358     AFS_STATCNT(afs_unmount);
359
360 #if !defined(AFS_LINUX26_ENV)
361     if (!suser()) {
362         AFS_GUNLOCK();
363         return;
364     }
365 #endif
366
367     afs_globalVFS = 0;
368     afs_globalVp = 0;
369
370     osi_linux_free_inode_pages();       /* invalidate and release remaining AFS inodes. */
371     afs_shutdown();
372 #if defined(AFS_LINUX24_ENV)
373     mntput(afs_cacheMnt);
374 #endif
375
376     osi_linux_verify_alloced_memory();
377     AFS_GUNLOCK();
378
379     sbp->s_dev = 0;
380 #if defined(AFS_LINUX26_ENV)
381     module_put(THIS_MODULE);
382 #else
383     MOD_DEC_USE_COUNT;
384 #endif
385 }
386
387
388 /* afs_statfs
389  * statp is in user space, so we need to cobble together a statfs, then
390  * copy it.
391  */
392 #if defined(AFS_LINUX26_ENV)
393 int
394 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
395 #elif defined(AFS_LINUX24_ENV)
396 int
397 afs_statfs(struct super_block *sbp, struct statfs *statp)
398 #else
399 int
400 afs_statfs(struct super_block *sbp, struct statfs *__statp, int size)
401 #endif
402 {
403 #if !defined(AFS_LINUX24_ENV)
404     struct statfs stat, *statp;
405
406     if (size < sizeof(struct statfs))
407         return;
408
409     memset(&stat, 0, size);
410     statp = &stat;
411 #else
412     memset(statp, 0, sizeof(*statp));
413 #endif
414
415     AFS_STATCNT(afs_statfs);
416
417     statp->f_type = 0;          /* Can we get a real type sometime? */
418     statp->f_bsize = sbp->s_blocksize;
419     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
420         statp->f_ffree = 9000000;
421     statp->f_fsid.val[0] = AFS_VFSMAGIC;
422     statp->f_fsid.val[1] = AFS_VFSFSID;
423     statp->f_namelen = 256;
424
425 #if !defined(AFS_LINUX24_ENV)
426     memcpy_tofs(__statp, &stat, size);
427 #endif
428     return 0;
429 }
430
431 struct super_operations afs_sops = {
432 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
433   .alloc_inode =        afs_alloc_inode,
434   .destroy_inode =      afs_destroy_inode,
435 #endif
436   .clear_inode =        afs_clear_inode,
437   .put_inode =          afs_put_inode,
438   .put_super =          afs_put_super,
439   .statfs =             afs_statfs,
440 #if !defined(AFS_LINUX24_ENV)
441   .notify_change =      afs_notify_change,
442 #endif
443 };
444
445 /************** Support routines ************************/
446
447 /* vattr_setattr
448  * Set iattr data into vattr. Assume vattr cleared before call.
449  */
450 static void
451 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
452 {
453     vattrp->va_mask = iattrp->ia_valid;
454     if (iattrp->ia_valid & ATTR_MODE)
455         vattrp->va_mode = iattrp->ia_mode;
456     if (iattrp->ia_valid & ATTR_UID)
457         vattrp->va_uid = iattrp->ia_uid;
458     if (iattrp->ia_valid & ATTR_GID)
459         vattrp->va_gid = iattrp->ia_gid;
460     if (iattrp->ia_valid & ATTR_SIZE)
461         vattrp->va_size = iattrp->ia_size;
462     if (iattrp->ia_valid & ATTR_ATIME) {
463 #if defined(AFS_LINUX26_ENV)
464         vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
465 #else
466         vattrp->va_atime.tv_sec = iattrp->ia_atime;
467 #endif
468         vattrp->va_atime.tv_usec = 0;
469     }
470     if (iattrp->ia_valid & ATTR_MTIME) {
471 #if defined(AFS_LINUX26_ENV)
472         vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
473 #else
474         vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
475 #endif
476         vattrp->va_mtime.tv_usec = 0;
477     }
478     if (iattrp->ia_valid & ATTR_CTIME) {
479 #if defined(AFS_LINUX26_ENV)
480         vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
481 #else
482         vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
483 #endif
484         vattrp->va_ctime.tv_usec = 0;
485     }
486 }
487
488 /* vattr2inode
489  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
490  */
491 void
492 vattr2inode(struct inode *ip, struct vattr *vp)
493 {
494     ip->i_ino = vp->va_nodeid;
495     ip->i_nlink = vp->va_nlink;
496     ip->i_blocks = vp->va_blocks;
497     ip->i_blksize = vp->va_blocksize;
498     ip->i_rdev = vp->va_rdev;
499     ip->i_mode = vp->va_mode;
500     ip->i_uid = vp->va_uid;
501     ip->i_gid = vp->va_gid;
502     ip->i_size = vp->va_size;
503 #if defined(AFS_LINUX26_ENV)
504     ip->i_atime.tv_sec = vp->va_atime.tv_sec;
505     ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
506     ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
507 #else
508     ip->i_atime = vp->va_atime.tv_sec;
509     ip->i_mtime = vp->va_mtime.tv_sec;
510     ip->i_ctime = vp->va_ctime.tv_sec;
511 #endif
512 }