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