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