linux24-fix-memcache-20020307
[openafs.git] / src / afs / LINUX / osi_misc.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  * Linux support routines.
12  *
13  */
14 #include <afsconfig.h>
15 #include "../afs/param.h"
16
17 RCSID("$Header$");
18
19 #include "../afs/sysincludes.h"
20 #include "../afs/afsincludes.h"
21 #include "../afs/afs_stats.h"
22 #if defined(AFS_LINUX24_ENV)
23 #include "../h/smp_lock.h"
24 #endif
25
26 char *crash_addr = 0; /* Induce an oops by writing here. */
27
28 /* Lookup name and return vnode for same. */
29 int osi_lookupname(char *aname, uio_seg_t seg, int followlink,
30                vnode_t **dirvpp, struct dentry **dpp)
31 {
32 #if defined(AFS_LINUX24_ENV)
33     struct nameidata nd;
34 #else
35     struct dentry *dp = NULL;
36 #endif
37     int code;
38
39     code = ENOENT;
40 #if defined(AFS_LINUX24_ENV)
41     if (seg == AFS_UIOUSER) {
42         code = followlink ?
43             user_path_walk(aname, &nd) : user_path_walk_link(aname, &nd);
44     }
45     else {
46         if (path_init(aname, followlink ? LOOKUP_FOLLOW : 0, &nd))
47             code = path_walk(aname, &nd);
48     }
49
50     if (!code) {
51         if (nd.dentry->d_inode) {
52             *dpp = dget(nd.dentry);
53             code = 0;
54         } else
55             code = ENOENT;
56         path_release(&nd);
57     }
58 #else
59     if (seg == AFS_UIOUSER) {
60         dp = followlink ? namei(aname) : lnamei(aname);
61     }
62     else {
63         dp = lookup_dentry(aname, NULL, followlink ? 1 : 0);
64     }
65
66     if (dp && !IS_ERR(dp)) {
67         if (dp->d_inode) {
68             *dpp = dp;
69             code = 0;
70         }
71         else
72             dput(dp);
73     }
74 #endif
75             
76     return code;
77 }
78
79 /* Intialize cache device info and fragment size for disk cache partition. */
80 int osi_InitCacheInfo(char *aname)
81 {
82     int code;
83     struct dentry *dp;
84     extern ino_t cacheInode;
85     extern struct osi_dev cacheDev;
86     extern afs_int32 afs_fsfragsize;
87     extern struct super_block *afs_cacheSBp;
88
89     code = osi_lookupname(aname, AFS_UIOSYS, 1, NULL, &dp);
90     if (code) return ENOENT;
91
92     cacheInode = dp->d_inode->i_ino;
93     cacheDev.dev = dp->d_inode->i_dev;
94     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
95     afs_cacheSBp = dp->d_inode->i_sb;
96
97     dput(dp);
98
99     return 0;
100 }
101
102
103 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
104 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
105
106 /* osi_rdwr
107  * Seek, then read or write to an open inode. addrp points to data in
108  * kernel space.
109  */
110 int osi_rdwr(int rw, struct osi_file *file, caddr_t addrp, size_t asize,
111              size_t *resid)
112 {
113     int code = 0;
114     KERNEL_SPACE_DECL;
115     struct file *filp = &file->file;
116     off_t offset = file->offset;
117
118     /* Seek to the desired position. Return -1 on error. */
119     if (filp->f_op->llseek) {
120         if (filp->f_op->llseek(filp, (loff_t)offset, 0) != offset)
121             return -1;
122     }
123     else
124         filp->f_pos = offset;
125
126     /* Read/Write the data. */
127     TO_USER_SPACE();
128     if (rw == UIO_READ)
129         code = FOP_READ(filp, addrp, asize);
130     else if (rw == UIO_WRITE)
131         code = FOP_WRITE(filp, addrp, asize);
132     else /* all is well? */
133         code = asize;
134     TO_KERNEL_SPACE();
135
136     if (code >=0) {
137         *resid = asize - code;
138         return 0;
139     }
140     else
141         return -1;
142 }
143
144 /* This variant is called from AFS read/write routines and takes a uio
145  * struct and, if successful, returns 0.
146  */
147 int osi_file_uio_rdwr(struct osi_file *osifile, uio_t *uiop, int rw)
148 {
149     struct file *filp = &osifile->file;
150     struct inode *ip = FILE_INODE(&osifile->file);
151     KERNEL_SPACE_DECL;
152     int code = 0;
153     struct iovec *iov;
154     int count;
155
156     if (uiop->uio_seg == AFS_UIOSYS)
157         TO_USER_SPACE();
158
159     filp->f_pos = uiop->uio_offset;
160     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
161         iov = uiop->uio_iov;
162         count = iov->iov_len;
163         if (count == 0) {
164             uiop->uio_iov++;
165             uiop->uio_iovcnt--;
166             continue;
167         }
168         
169         if (rw == UIO_READ)
170             code = FOP_READ(filp, iov->iov_base, count);
171         else
172             code = FOP_WRITE(filp, iov->iov_base, count);
173
174         if (code < 0) {
175             code = -code;
176             break;
177         }
178         
179         iov->iov_base += code;
180         iov->iov_len -= code;
181         uiop->uio_resid -= code;
182         uiop->uio_offset += code;
183         code = 0;
184     }
185
186     if (uiop->uio_seg == AFS_UIOSYS)
187         TO_KERNEL_SPACE();
188
189     return code;
190 }
191
192 /* setup_uio 
193  * Setup a uio struct.
194  */
195 void setup_uio(uio_t *uiop, struct iovec *iovecp, char *buf,
196                              afs_offs_t pos, int count, uio_flag_t flag,
197                              uio_seg_t seg)
198 {
199     iovecp->iov_base = buf;
200     iovecp->iov_len = count;
201     uiop->uio_iov = iovecp;
202     uiop->uio_iovcnt = 1;
203     uiop->uio_offset = pos;
204     uiop->uio_seg = seg;
205     uiop->uio_resid = count;
206     uiop->uio_flag = flag;
207 }
208
209
210 /* uiomove
211  * UIO_READ : dp -> uio
212  * UIO_WRITE : uio -> dp
213  */
214 int uiomove(char *dp, int length, uio_flag_t rw, uio_t *uiop)
215 {
216     int count, n;
217     struct iovec *iov;
218     int code;
219
220     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
221         iov = uiop->uio_iov;
222         count = iov->iov_len;
223
224         if (!count) {
225             uiop->uio_iov++;
226             uiop->uio_iovcnt--;
227             continue;
228         }
229
230         if (count > length)
231             count = length;
232         
233         switch(uiop->uio_seg) {
234         case AFS_UIOSYS:
235             switch(rw) {
236             case UIO_READ:
237                 memcpy(iov->iov_base, dp, count); break;
238             case UIO_WRITE:
239                 memcpy(dp, iov->iov_base, count); break;
240             default:
241                 printf("uiomove: Bad rw = %d\n", rw);
242                 return -EINVAL;
243             }
244             break;
245         case AFS_UIOUSER:
246             switch(rw) {
247             case UIO_READ:
248                 AFS_COPYOUT(dp, iov->iov_base, count, code); break;
249             case UIO_WRITE:
250                 AFS_COPYIN(iov->iov_base, dp, count, code); break;
251             default:
252                 printf("uiomove: Bad rw = %d\n", rw);
253                 return -EINVAL;
254             }
255             break;
256         default:
257             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
258             return -EINVAL;
259         }
260
261         dp += count;
262         length -= count;
263         iov->iov_base += count;
264         iov->iov_len -= count;
265         uiop->uio_offset += count;
266         uiop->uio_resid -= count;
267     }
268     return 0;
269 }
270
271 void afs_osi_SetTime(osi_timeval_t *tvp)
272 {
273     extern int (*sys_settimeofdayp)(struct timeval *tv, struct timezone *tz);
274 #ifdef AFS_LINUX_64BIT_KERNEL
275     struct timeval tv;
276     AFS_STATCNT(osi_SetTime);
277     tv.tv_sec = tvp->tv_sec;
278     tv.tv_usec = tvp->tv_usec;
279     (void) (*sys_settimeofdayp)(&tv, NULL);
280 #else
281     KERNEL_SPACE_DECL;
282
283     AFS_STATCNT(osi_SetTime);
284
285     TO_USER_SPACE();
286     (void) (*sys_settimeofdayp)(tvp, NULL);
287     TO_KERNEL_SPACE();
288 #endif
289 }
290
291 /* Free all the pages on any of the vnodes in the vlru. Must be done before
292  * freeing all memory.
293  */
294 void osi_linux_free_inode_pages(void)
295 {
296     int i;
297     struct vcache *tvc;
298     struct inode *ip;
299     extern struct vcache *afs_vhashT[VCSIZE];
300
301     for (i=0; i<VCSIZE; i++) {
302         for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
303             ip = (struct inode*)tvc;
304 #if defined(AFS_LINUX24_ENV)
305             if (ip->i_data.nrpages) {
306 #else
307             if (ip->i_nrpages) {
308 #endif
309 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
310                 truncate_inode_pages(&ip->i_data, 0);
311 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,15)
312                 truncate_inode_pages(ip, 0);
313 #else
314                 invalidate_inode_pages(ip);
315 #endif
316 #if defined(AFS_LINUX24_ENV)
317                 if (ip->i_data.nrpages) {
318 #else
319                 if (ip->i_nrpages) {
320 #endif
321                     printf("Failed to invalidate all pages on inode 0x%x\n",
322                            ip);
323                 }
324             }
325         }
326     }
327 }
328
329 void osi_clear_inode(struct inode *ip)
330 {
331     cred_t *credp = crref();
332     struct vcache *vc = (struct vcache*)ip;
333
334 #if defined(AFS_LINUX24_ENV)
335     if (atomic_read(&ip->i_count) > 1)
336 #else
337     if (ip->i_count > 1)
338 #endif
339         printf("afs_put_inode: ino %d (0x%x) has count %d\n", ip->i_ino, ip);
340
341     ObtainWriteLock(&vc->lock, 504);
342     afs_InactiveVCache(vc, credp);
343     ReleaseWriteLock(&vc->lock);
344 #if defined(AFS_LINUX24_ENV)
345     atomic_set(&ip->i_count, 0);
346 #else
347     ip->i_count = 0;
348 #endif
349     ip->i_nlink = 0; /* iput checks this after calling this routine. */
350     crfree(credp);
351 }
352
353 /* iput an inode. Since we still have a separate inode pool, we don't want
354  * to call iput on AFS inodes, since they would then end up on Linux's
355  * inode_unsed list.
356  */
357 void osi_iput(struct inode *ip)
358 {
359     extern struct vfs *afs_globalVFS;
360
361     AFS_GLOCK();
362 #if defined(AFS_LINUX24_ENV)
363     if (atomic_read(&ip->i_count) == 0 || atomic_read(&ip->i_count) & 0xffff0000) {
364 #else
365     if (ip->i_count == 0 || ip->i_count & 0xffff0000) {
366 #endif
367         osi_Panic("IPUT Bad refCount %d on inode 0x%x\n",
368 #if defined(AFS_LINUX24_ENV)
369                   atomic_read(&ip->i_count), ip);
370 #else
371                   ip->i_count, ip);
372 #endif
373     }
374     if (afs_globalVFS && afs_globalVFS == ip->i_sb ) {
375 #if defined(AFS_LINUX24_ENV)
376         atomic_dec(&ip->i_count);
377         if (!atomic_read(&ip->i_count))
378 #else
379         ip->i_count --;
380         if (!ip->i_count)
381 #endif
382             osi_clear_inode(ip);
383     }
384     else
385         iput(ip);
386     AFS_GUNLOCK();
387 }
388
389 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
390  * that has its mvid (parent dir's fid) pointer set to the wrong directory
391  * due to being mounted in multiple points at once. If so, check_bad_parent()
392  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
393  * dotdotfid and mtpoint fid members.
394  * Parameters:
395  *  dp - dentry to be checked.
396  * Return Values:
397  *  None.
398  * Sideeffects:
399  *   This dentry's vcache's mvid will be set to the correct parent directory's
400  *   fid.
401  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
402  *    to the correct parent and mountpoint fids.
403  */
404
405 void check_bad_parent(struct dentry *dp)
406 {
407   cred_t *credp;
408   struct vcache *vcp = (struct vcache*)dp->d_inode, *avc = NULL;
409   struct vcache *pvc = (struct vcache *)dp->d_parent->d_inode;
410
411   if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
412     credp = crref();
413
414
415     /* force a lookup, so vcp->mvid is fixed up */
416     afs_lookup(pvc, dp->d_name.name, &avc, credp);
417     if (!avc || vcp != avc) {    /* bad, very bad.. */
418       afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
419                  "afs_linux_revalidate : bad pointer returned from afs_lookup origvc newvc dentry",
420                  ICL_TYPE_POINTER, vcp,
421                  ICL_TYPE_POINTER, avc,
422                  ICL_TYPE_POINTER, dp);
423     }
424     if (avc)
425         AFS_RELE(avc);
426     crfree(credp);
427   } /* if bad parent */
428  
429   return;
430 }
431
432 struct task_struct *rxk_ListenerTask;
433
434 void osi_linux_mask() {
435     spin_lock_irq(&current->sigmask_lock);
436     sigfillset(&current->blocked);
437     recalc_sigpending(current);
438     spin_unlock_irq(&current->sigmask_lock);
439 }
440
441 void osi_linux_unmask() {
442     spin_lock_irq(&rxk_ListenerTask->sigmask_lock);
443     sigemptyset(&rxk_ListenerTask->blocked);
444     recalc_sigpending(rxk_ListenerTask);
445     spin_unlock_irq(&rxk_ListenerTask->sigmask_lock);
446 }
447
448 void osi_linux_rxkreg() {
449     rxk_ListenerTask = current;
450 }