linux26-more-updates-20040420
[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 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "afs/afs_stats.h"
24 #if !defined(AFS_LINUX26_ENV)
25 #include "h/locks.h"
26 #endif
27 #if defined(AFS_LINUX24_ENV)
28 #include "h/smp_lock.h"
29 #endif
30
31 #define __NO_VERSION__          /* don't define kernel_version in module.h */
32 #include <linux/module.h>
33
34
35 struct vcache *afs_globalVp = 0;
36 struct vfs *afs_globalVFS = 0;
37 #if defined(AFS_LINUX24_ENV)
38 struct nameidata afs_cacheNd;
39 #endif
40 int afs_was_mounted = 0;        /* Used to force reload if mount/unmount/mount */
41
42 extern struct super_operations afs_sops;
43 extern afs_rwlock_t afs_xvcache;
44 extern struct afs_q VLRU;
45
46 extern struct dentry_operations afs_dentry_operations;
47
48 /* Forward declarations */
49 static void iattr2vattr(struct vattr *vattrp, struct iattr *iattrp);
50 static void update_inode_cache(struct inode *ip, struct vattr *vp);
51 static int afs_root(struct super_block *afsp);
52 struct super_block *afs_read_super(struct super_block *sb, void *data, int silent);
53 int afs_fill_super(struct super_block *sb, void *data, int silent);
54 static struct super_block *afs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data);
55 void put_inode_on_dummy_list(struct inode *ip);
56
57 /* afs_file_system
58  * VFS entry for Linux - installed in init_module
59  * Linux mounts file systems by:
60  * 1) register_filesystem(&afs_file_system) - done in init_module
61  * 2) Mount call comes to us via do_mount -> read_super -> afs_read_super.
62  *    We are expected to setup the super_block. See afs_read_super.
63  */
64 #if defined(AFS_LINUX26_ENV)
65 struct backing_dev_info afs_backing_dev_info = {
66         .ra_pages       = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE,
67         .state          = 0,
68 };
69
70 struct file_system_type afs_fs_type = {
71     .owner = THIS_MODULE,
72     .name = "afs",
73     .get_sb = afs_get_sb,
74     .kill_sb = kill_anon_super,
75     .fs_flags = FS_BINARY_MOUNTDATA,
76 };
77 #elif defined(AFS_LINUX24_ENV)
78 DECLARE_FSTYPE(afs_fs_type, "afs", afs_read_super, 0);
79 #else
80 struct file_system_type afs_fs_type = {
81     "afs",                      /* name - used by mount operation. */
82     0,                          /* requires_dev - no for network filesystems. mount() will 
83                                  * pass us an "unnamed" device. */
84     afs_read_super,             /* wrapper to afs_mount */
85     NULL                        /* pointer to next file_system_type once registered. */
86 };
87 #endif
88
89 /* afs_read_super
90  * read the "super block" for AFS - roughly eguivalent to struct vfs.
91  * dev, covered, s_rd_only, s_dirt, and s_type will be set by read_super.
92  */
93 #if defined(AFS_LINUX26_ENV)
94 static struct super_block *
95 afs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
96 {
97     return get_sb_nodev(fs_type, flags, data, afs_fill_super);
98 }
99
100 int
101 afs_fill_super(struct super_block *sb, void *data, int silent)
102 #else
103 struct super_block *
104 afs_read_super(struct super_block *sb, void *data, int silent)
105 #endif
106 {
107     int code = 0;
108
109     AFS_GLOCK();
110     if (afs_was_mounted) {
111         printf
112             ("You must reload the AFS kernel extensions before remounting AFS.\n");
113         AFS_GUNLOCK();
114 #if defined(AFS_LINUX26_ENV)
115         return -EINVAL;
116 #else
117         return NULL;
118 #endif
119     }
120     afs_was_mounted = 1;
121
122     /* Set basics of super_block */
123 #if !defined(AFS_LINUX24_ENV)
124     lock_super(sb);
125 #endif
126 #if defined(AFS_LINUX26_ENV)
127    __module_get(THIS_MODULE);
128 #else
129     MOD_INC_USE_COUNT;
130 #endif
131
132     afs_globalVFS = sb;
133     sb->s_blocksize = 1024;
134     sb->s_blocksize_bits = 10;
135     sb->s_magic = AFS_VFSMAGIC;
136     sb->s_op = &afs_sops;       /* Super block (vfs) ops */
137 #if defined(MAX_NON_LFS)
138     sb->s_maxbytes = MAX_NON_LFS;
139 #endif
140     code = afs_root(sb);
141     if (code)
142 #if defined(AFS_LINUX26_ENV)
143         module_put(THIS_MODULE);
144 #else
145         MOD_DEC_USE_COUNT;
146 #endif
147
148 #if !defined(AFS_LINUX24_ENV)
149     unlock_super(sb);
150 #endif
151
152     AFS_GUNLOCK();
153 #if defined(AFS_LINUX26_ENV)
154     return code ? -EINVAL : 0;
155 #else
156     return code ? NULL : sb;
157 #endif
158 }
159
160
161 /* afs_root - stat the root of the file system. AFS global held on entry. */
162 static int
163 afs_root(struct super_block *afsp)
164 {
165     register afs_int32 code = 0;
166     struct vrequest treq;
167     register struct vcache *tvp = 0;
168
169     AFS_STATCNT(afs_root);
170     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
171         tvp = afs_globalVp;
172     } else {
173         cred_t *credp = crref();
174
175         if (afs_globalVp) {
176             afs_PutVCache(afs_globalVp);
177             afs_globalVp = NULL;
178         }
179
180         if (!(code = afs_InitReq(&treq, credp)) && !(code = afs_CheckInit())) {
181             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
182             if (tvp) {
183                 extern struct inode_operations afs_dir_iops;
184 #if defined(AFS_LINUX24_ENV)
185                 extern struct file_operations afs_dir_fops;
186 #endif
187
188                 /* "/afs" is a directory, reset inode ops accordingly. */
189                 AFSTOV(tvp)->v_op = &afs_dir_iops;
190 #if defined(AFS_LINUX24_ENV)
191                 AFSTOV(tvp)->v_fop = &afs_dir_fops;
192 #endif
193
194                 /* setup super_block and mount point inode. */
195                 afs_globalVp = tvp;
196 #if defined(AFS_LINUX24_ENV)
197                 afsp->s_root = d_alloc_root(AFSTOI(tvp));
198 #else
199                 afsp->s_root = d_alloc_root(AFSTOI(tvp), NULL);
200 #endif
201                 afsp->s_root->d_op = &afs_dentry_operations;
202             } else
203                 code = ENOENT;
204         }
205         crfree(credp);
206     }
207
208     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, afs_globalVp,
209                ICL_TYPE_INT32, code);
210     return code;
211 }
212
213 /* super_operations */
214
215 /* afs_notify_change
216  * Linux version of setattr call. What to change is in the iattr struct.
217  * We need to set bits in both the Linux inode as well as the vcache.
218  */
219 int
220 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
221 {
222     struct vattr vattr;
223     int code;
224     cred_t *credp = crref();
225     struct inode *ip = dp->d_inode;
226
227     AFS_GLOCK();
228 #if defined(AFS_LINUX26_ENV)
229     lock_kernel();
230 #endif
231     VATTR_NULL(&vattr);
232     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
233     update_inode_cache(ip, &vattr);
234     code = afs_setattr(ITOAFS(ip), &vattr, credp);
235     afs_CopyOutAttrs(ITOAFS(ip), &vattr);
236     /* Note that the inode may still not have all the correct info. But at
237      * least we've got the newest version of what was supposed to be set.
238      */
239
240 #if defined(AFS_LINUX26_ENV)
241     unlock_kernel();
242 #endif
243     AFS_GUNLOCK();
244     crfree(credp);
245     return -code;
246 }
247
248
249 /* This list is simply used to initialize the i_list member of the
250  * linux inode. This stops linux inode syncing code from choking on our
251  * inodes.
252  */
253 static LIST_HEAD(dummy_inode_list);
254
255
256 /* This is included for documentation only. */
257 /* afs_write_inode
258  * Used to flush in core inode to disk. We don't need to do this. Top level
259  * write_inode() routine will clear i_dirt. If this routine is in the table,
260  * it's expected to do the cleaning and clear i_dirt.
261  * 
262  * 9/24/99: This is what we thought until we discovered msync() does end up calling
263  * this function to sync a single inode to disk. msync() only flushes selective
264  * pages to disk. So it needs an inode syncing function to update metadata when it
265  * has synced some pages of a file to disk.
266  */
267 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
268 static void
269 afs_write_inode(struct inode *ip, int unused)
270 #else
271 static void
272 afs_write_inode(struct inode *ip)
273 #endif
274 {
275     list_del(&ip->i_list);
276     /* and put it back on our dummy list. */
277     put_inode_on_dummy_list(ip);
278
279     /* for now we don't actually update the metadata during msync. This
280      * is just to keep linux happy.  */
281 }
282
283
284 static void
285 afs_destroy_inode(struct inode *ip)
286 {
287     ip->i_state = 0;
288 }
289
290
291 /* afs_put_inode
292  * called from iput when count goes to zero. Linux version of inactive.
293  * For Linux 2.2, this funcionality has moved to the delete inode super op.
294  * If we use the common inode pool, we'll need to set i_nlink to 0 here.
295  * That will trigger the call to delete routine.
296  */
297
298 static void
299 afs_delete_inode(struct inode *ip)
300 {
301     struct vcache *vp = ITOAFS(ip);
302
303 #ifdef AFS_LINUX26_ENV
304     put_inode_on_dummy_list(ip);
305 #endif
306
307     AFS_GLOCK();
308     osi_clear_inode(ip);
309     AFS_GUNLOCK();
310 }
311
312
313 /* afs_put_super
314  * Called from unmount to release super_block. */
315 static void
316 afs_put_super(struct super_block *sbp)
317 {
318     extern int afs_afs_cold_shutdown;
319     int code = 0;
320     int fv_slept;
321
322     AFS_GLOCK();
323     AFS_STATCNT(afs_unmount);
324
325 #if !defined(AFS_LINUX26_ENV)
326     if (!suser()) {
327         AFS_GUNLOCK();
328         return;
329     }
330 #endif
331
332     afs_globalVFS = 0;
333     afs_globalVp = 0;
334     afs_shutdown();
335 #if defined(AFS_LINUX24_ENV)
336     path_release(&afs_cacheNd);
337 #endif
338
339     osi_linux_verify_alloced_memory();
340   done:
341     AFS_GUNLOCK();
342
343     if (!code) {
344         sbp->s_dev = 0;
345 #if defined(AFS_LINUX26_ENV)
346         module_put(THIS_MODULE);
347 #else
348         MOD_DEC_USE_COUNT;
349 #endif
350     }
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 #if defined(AFS_LINUX26_ENV)
359 int
360 afs_statfs(struct super_block *sbp, struct kstatfs *statp)
361 #elif defined(AFS_LINUX24_ENV)
362 int
363 afs_statfs(struct super_block *sbp, struct statfs *statp)
364 #else
365 int
366 afs_statfs(struct super_block *sbp, struct statfs *__statp, int size)
367 #endif
368 {
369 #if !defined(AFS_LINUX24_ENV)
370     struct statfs stat;
371
372     if (size < sizeof(struct statfs))
373         return;
374
375     memset(&stat, 0, size);
376     statp = &stat;
377 #endif
378
379     AFS_STATCNT(afs_statfs);
380
381     statp->f_type = 0;          /* Can we get a real type sometime? */
382     statp->f_bsize = sbp->s_blocksize;
383     statp->f_blocks = statp->f_bfree = statp->f_bavail = statp->f_files =
384         statp->f_ffree = 9000000;
385     statp->f_fsid.val[0] = AFS_VFSMAGIC;
386     statp->f_fsid.val[1] = AFS_VFSFSID;
387     statp->f_namelen = 256;
388
389 #if !defined(AFS_LINUX24_ENV)
390     memcpy_tofs(__statp, &stat, size);
391 #endif
392     return 0;
393 }
394
395 void
396 afs_umount_begin(struct super_block *sbp)
397 {
398     afs_shuttingdown = 1;
399 }
400
401 struct super_operations afs_sops = {
402 #if defined(AFS_LINUX26_ENV)
403   .drop_inode =         generic_delete_inode,
404   .destroy_inode =      afs_destroy_inode,
405 #endif
406   .delete_inode =       afs_delete_inode,
407   .write_inode =        afs_write_inode,
408   .put_super =          afs_put_super,
409   .statfs =             afs_statfs,
410   .umount_begin =       afs_umount_begin
411 #if !defined(AFS_LINUX24_ENV)
412   .notify_change =      afs_notify_change,
413 #endif
414 };
415
416 /************** Support routines ************************/
417
418 /* vattr_setattr
419  * Set iattr data into vattr. Assume vattr cleared before call.
420  */
421 static void
422 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
423 {
424     vattrp->va_mask = iattrp->ia_valid;
425     if (iattrp->ia_valid & ATTR_MODE)
426         vattrp->va_mode = iattrp->ia_mode;
427     if (iattrp->ia_valid & ATTR_UID)
428         vattrp->va_uid = iattrp->ia_uid;
429     if (iattrp->ia_valid & ATTR_GID)
430         vattrp->va_gid = iattrp->ia_gid;
431     if (iattrp->ia_valid & ATTR_SIZE)
432         vattrp->va_size = iattrp->ia_size;
433     if (iattrp->ia_valid & ATTR_ATIME) {
434 #if defined(AFS_LINUX26_ENV)
435         vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
436 #else
437         vattrp->va_atime.tv_sec = iattrp->ia_atime;
438 #endif
439         vattrp->va_atime.tv_usec = 0;
440     }
441     if (iattrp->ia_valid & ATTR_MTIME) {
442 #if defined(AFS_LINUX26_ENV)
443         vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
444 #else
445         vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
446 #endif
447         vattrp->va_mtime.tv_usec = 0;
448     }
449     if (iattrp->ia_valid & ATTR_CTIME) {
450 #if defined(AFS_LINUX26_ENV)
451         vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
452 #else
453         vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
454 #endif
455         vattrp->va_ctime.tv_usec = 0;
456     }
457 }
458
459 /* update_inode_cache
460  * Update inode with info from vattr struct. Use va_mask to determine what
461  * to update.
462  */
463 static void
464 update_inode_cache(struct inode *ip, struct vattr *vp)
465 {
466     if (vp->va_mask & ATTR_MODE)
467         ip->i_mode = vp->va_mode;
468     if (vp->va_mask & ATTR_UID)
469         ip->i_uid = vp->va_uid;
470     if (vp->va_mask & ATTR_GID)
471         ip->i_gid = vp->va_gid;
472     if (vp->va_mask & ATTR_SIZE)
473         ip->i_size = vp->va_size;
474     if (vp->va_mask & ATTR_ATIME)
475 #if defined(AFS_LINUX26_ENV)
476         ip->i_atime.tv_sec = vp->va_atime.tv_sec;
477 #else
478         ip->i_atime = vp->va_atime.tv_sec;
479 #endif
480     if (vp->va_mask & ATTR_MTIME)
481 #if defined(AFS_LINUX26_ENV)
482         ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
483 #else
484         ip->i_mtime = vp->va_mtime.tv_sec;
485 #endif
486     if (vp->va_mask & ATTR_CTIME)
487 #if defined(AFS_LINUX26_ENV)
488         ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
489 #else
490         ip->i_ctime = vp->va_ctime.tv_sec;
491 #endif
492 }
493
494 /* vattr2inode
495  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
496  */
497 void
498 vattr2inode(struct inode *ip, struct vattr *vp)
499 {
500     ip->i_ino = vp->va_nodeid;
501     ip->i_nlink = vp->va_nlink;
502     ip->i_blocks = vp->va_blocks;
503     ip->i_blksize = vp->va_blocksize;
504     ip->i_rdev = vp->va_rdev;
505     ip->i_mode = vp->va_mode;
506     ip->i_uid = vp->va_uid;
507     ip->i_gid = vp->va_gid;
508     ip->i_size = vp->va_size;
509 #if defined(AFS_LINUX26_ENV)
510     ip->i_atime.tv_sec = vp->va_atime.tv_sec;
511     ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
512     ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
513 #else
514     ip->i_atime = vp->va_atime.tv_sec;
515     ip->i_mtime = vp->va_mtime.tv_sec;
516     ip->i_ctime = vp->va_ctime.tv_sec;
517 #endif
518 }
519
520 /* Put this afs inode on our own dummy list. Linux expects to see inodes
521  * nicely strung up in lists. Linux inode syncing code chokes on our inodes if
522  * they're not on any lists.
523  */
524 void
525 put_inode_on_dummy_list(struct inode *ip)
526 {
527     /* Initialize list. See explanation above. */
528     list_add(&ip->i_list, &dummy_inode_list);
529 }
530
531 /* And yet another routine to update the inode cache - called from ProcessFS */
532 void
533 vcache2inode(struct vcache *avc)
534 {
535     struct vattr vattr;
536
537     VATTR_NULL(&vattr);
538     afs_CopyOutAttrs(avc, &vattr);      /* calls vattr2inode */
539 }
540
541 /* Yet another one for fakestat'ed mountpoints */
542 void
543 vcache2fakeinode(struct vcache *rootvp, struct vcache *mpvp)
544 {
545     struct vattr vattr;
546
547     VATTR_NULL(&vattr);
548     afs_CopyOutAttrs(rootvp, &vattr);
549     vattr2inode(AFSTOI(mpvp), &vattr);
550 }