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