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