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