Don't cast the pointer past to memset
[openafs.git] / src / afs / LINUX / osi_file.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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include <linux/module.h> /* early to avoid printf->printk mapping */
15 #include "afs/sysincludes.h"    /* Standard vendor system headers */
16 #include "afsincludes.h"        /* Afs-based standard headers */
17 #include "afs/afs_stats.h"      /* afs statistics */
18 #include <linux/smp_lock.h>
19 #include <linux/namei.h>
20 #if defined(LINUX_USE_FH)
21 #include <linux/exportfs.h>
22 int cache_fh_type = -1;
23 int cache_fh_len = -1;
24 #endif
25
26 afs_lock_t afs_xosi;            /* lock is for tvattr */
27 extern struct osi_dev cacheDev;
28 extern struct vfsmount *afs_cacheMnt;
29 extern struct super_block *afs_cacheSBp;
30
31 struct file *
32 afs_linux_raw_open(afs_dcache_id_t *ainode, ino_t *hint)
33 {
34     struct inode *tip = NULL;
35     struct dentry *dp = NULL;
36     struct file* filp;
37
38 #if !defined(LINUX_USE_FH)
39     tip = iget(afs_cacheSBp, ainode->ufs);
40     if (!tip)
41         osi_Panic("Can't get inode %d\n", (int) ainode->ufs);
42
43     dp = d_alloc_anon(tip);
44 #else
45     dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
46                                                  cache_fh_len, cache_fh_type);
47     if (!dp)
48            osi_Panic("Can't get dentry\n");
49     tip = dp->d_inode;
50 #endif
51     tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
52
53 #if defined(STRUCT_TASK_HAS_CRED)
54     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
55 #else
56     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
57 #endif
58     if (IS_ERR(filp))
59 #if defined(LINUX_USE_FH)
60         osi_Panic("Can't open file\n");
61 #else
62         osi_Panic("Can't open inode %d\n", (int) ainode->ufs);
63 #endif
64     if (hint)
65         *hint = tip->i_ino;
66     return filp;
67 }
68
69 void *
70 osi_UFSOpen(afs_dcache_id_t *ainode)
71 {
72     struct osi_file *afile = NULL;
73     extern int cacheDiskType;
74
75     AFS_STATCNT(osi_UFSOpen);
76     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
77         osi_Panic("UFSOpen called for non-UFS cache\n");
78     }
79     if (!afs_osicred_initialized) {
80         /* valid for alpha_osf, SunOS, Ultrix */
81         memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
82         crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
83         afs_osicred_initialized = 1;
84     }
85     afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
86     AFS_GUNLOCK();
87     if (!afile) {
88         osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
89                   (int)sizeof(struct osi_file));
90     }
91     memset(afile, 0, sizeof(struct osi_file));
92
93     afile->filp = afs_linux_raw_open(ainode, &afile->inum);
94     afile->size = i_size_read(FILE_INODE(afile->filp));
95     AFS_GLOCK();
96     afile->offset = 0;
97     afile->proc = (int (*)())0;
98     return (void *)afile;
99 }
100
101 #if defined(LINUX_USE_FH)
102 /*
103  * Given a dentry, return the file handle as encoded by the filesystem.
104  * We can't assume anything about the length (words, not bytes).
105  * The cache has to live on a single filesystem with uniform file 
106  * handles, otherwise we panic.
107  */
108 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
109     int max_len;
110     int type;
111
112     if (cache_fh_len > 0)
113         max_len = cache_fh_len;
114     else
115         max_len = MAX_FH_LEN;
116     if (dp->d_sb->s_export_op->encode_fh) {
117         type = dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], &max_len, 0);
118         if (type == 255) {
119            osi_Panic("File handle encoding failed\n");
120         }
121         if (cache_fh_type < 0)
122             cache_fh_type = type;
123         if (cache_fh_len < 0) {
124             cache_fh_len = max_len;
125         }
126         if (type != cache_fh_type || max_len != cache_fh_len) {
127            osi_Panic("Inconsistent file handles within cache\n");
128         }
129     } else {
130          /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
131         if (cache_fh_type < 0)
132             cache_fh_type = FILEID_INO32_GEN;
133         if (cache_fh_len < 0)
134             cache_fh_len = sizeof(struct fid)/4;
135         ainode->fh.i32.ino = dp->d_inode->i_ino;
136         ainode->fh.i32.gen = dp->d_inode->i_generation;
137     }
138 }
139 #else
140 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
141     *ainode = dp->d_inode->i_ino;
142 }
143 #endif
144
145 int
146 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
147 {
148     AFS_STATCNT(osi_Stat);
149     MObtainWriteLock(&afs_xosi, 320);
150     astat->size = i_size_read(OSIFILE_INODE(afile));
151     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
152     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
153
154     MReleaseWriteLock(&afs_xosi);
155     return 0;
156 }
157
158 int
159 osi_UFSClose(register struct osi_file *afile)
160 {
161     AFS_STATCNT(osi_Close);
162     if (afile) {
163         if (OSIFILE_INODE(afile)) {
164             filp_close(afile->filp, NULL);
165         }
166     }
167
168     osi_FreeLargeSpace(afile);
169     return 0;
170 }
171
172 int
173 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
174 {
175     register afs_int32 code;
176     struct osi_stat tstat;
177     struct iattr newattrs;
178     struct inode *inode = OSIFILE_INODE(afile);
179     AFS_STATCNT(osi_Truncate);
180
181     /* This routine only shrinks files, and most systems
182      * have very slow truncates, even when the file is already
183      * small enough.  Check now and save some time.
184      */
185     code = afs_osi_Stat(afile, &tstat);
186     if (code || tstat.size <= asize)
187         return code;
188     MObtainWriteLock(&afs_xosi, 321);
189     AFS_GUNLOCK();
190 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
191     down_write(&inode->i_alloc_sem);
192 #endif
193 #ifdef STRUCT_INODE_HAS_I_MUTEX
194     mutex_lock(&inode->i_mutex);
195 #else
196     down(&inode->i_sem);
197 #endif
198     newattrs.ia_size = asize;
199     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
200     newattrs.ia_ctime = CURRENT_TIME;
201
202     /* avoid notify_change() since it wants to update dentry->d_parent */
203     lock_kernel();
204     code = inode_change_ok(inode, &newattrs);
205     if (!code) {
206 #ifdef INODE_SETATTR_NOT_VOID
207         if (inode->i_op && inode->i_op->setattr)
208             code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
209         else
210             code = inode_setattr(inode, &newattrs);
211 #else
212         inode_setattr(inode, &newattrs);
213 #endif
214     }
215     unlock_kernel();
216     if (!code)
217         truncate_inode_pages(&inode->i_data, asize);
218     code = -code;
219 #ifdef STRUCT_INODE_HAS_I_MUTEX
220     mutex_unlock(&inode->i_mutex);
221 #else
222     up(&inode->i_sem);
223 #endif
224 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
225     up_write(&inode->i_alloc_sem);
226 #endif
227     AFS_GLOCK();
228     MReleaseWriteLock(&afs_xosi);
229     return code;
230 }
231
232
233 /* Generic read interface */
234 int
235 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
236              afs_int32 asize)
237 {
238     struct uio auio;
239     struct iovec iov;
240     afs_int32 code;
241
242     AFS_STATCNT(osi_Read);
243
244     /*
245      * If the osi_file passed in is NULL, panic only if AFS is not shutting
246      * down. No point in crashing when we are already shutting down
247      */
248     if (!afile) {
249         if (!afs_shuttingdown)
250             osi_Panic("osi_Read called with null param");
251         else
252             return EIO;
253     }
254
255     if (offset != -1)
256         afile->offset = offset;
257     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
258     AFS_GUNLOCK();
259     code = osi_rdwr(afile, &auio, UIO_READ);
260     AFS_GLOCK();
261     if (code == 0) {
262         code = asize - auio.uio_resid;
263         afile->offset += code;
264     } else {
265         afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
266                    ICL_TYPE_INT32, code);
267         code = -1;
268     }
269     return code;
270 }
271
272 /* Generic write interface */
273 int
274 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
275               afs_int32 asize)
276 {
277     struct uio auio;
278     struct iovec iov;
279     afs_int32 code;
280
281     AFS_STATCNT(osi_Write);
282
283     if (!afile) {
284         if (!afs_shuttingdown)
285             osi_Panic("afs_osi_Write called with null param");
286         else
287             return EIO;
288     }
289
290     if (offset != -1)
291         afile->offset = offset;
292     setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
293     AFS_GUNLOCK();
294     code = osi_rdwr(afile, &auio, UIO_WRITE);
295     AFS_GLOCK();
296     if (code == 0) {
297         code = asize - auio.uio_resid;
298         afile->offset += code;
299     } else {
300         if (code == ENOSPC)
301             afs_warnuser
302                 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
303         code = -1;
304     }
305
306     if (afile->proc)
307         (*afile->proc)(afile, code);
308
309     return code;
310 }
311
312
313 /*  This work should be handled by physstrat in ca/machdep.c.
314     This routine written from the RT NFS port strategy routine.
315     It has been generalized a bit, but should still be pretty clear. */
316 int
317 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
318 {
319     afs_int32 returnCode;
320
321     AFS_STATCNT(osi_MapStrategy);
322     returnCode = (*aproc) (bp);
323
324     return returnCode;
325 }
326
327 void
328 shutdown_osifile(void)
329 {
330     AFS_STATCNT(shutdown_osifile);
331     if (afs_cold_shutdown) {
332         afs_osicred_initialized = 0;
333     }
334 }
335
336 /* Intialize cache device info and fragment size for disk cache partition. */
337 int
338 osi_InitCacheInfo(char *aname)
339 {
340     int code;
341     extern afs_dcache_id_t cacheInode;
342     struct dentry *dp;
343     extern struct osi_dev cacheDev;
344     extern afs_int32 afs_fsfragsize;
345     extern struct super_block *afs_cacheSBp;
346     extern struct vfsmount *afs_cacheMnt;
347     code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
348     if (code)
349         return ENOENT;
350
351     osi_get_fh(dp, &cacheInode.ufs);
352     cacheDev.dev = dp->d_inode->i_sb->s_dev;
353     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
354     afs_cacheSBp = dp->d_inode->i_sb;
355
356     dput(dp);
357
358     return 0;
359 }
360
361
362 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
363 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
364
365 /* osi_rdwr
366  * seek, then read or write to an open inode. addrp points to data in
367  * kernel space.
368  */
369 int
370 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
371 {
372     struct file *filp = osifile->filp;
373     KERNEL_SPACE_DECL;
374     int code = 0;
375     struct iovec *iov;
376     afs_size_t count;
377     unsigned long savelim;
378
379     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
380     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
381
382     if (uiop->uio_seg == AFS_UIOSYS)
383         TO_USER_SPACE();
384
385     /* seek to the desired position. Return -1 on error. */
386     if (filp->f_op->llseek) {
387         if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
388             return -1;
389     } else
390         filp->f_pos = uiop->uio_offset;
391
392     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
393         iov = uiop->uio_iov;
394         count = iov->iov_len;
395         if (count == 0) {
396             uiop->uio_iov++;
397             uiop->uio_iovcnt--;
398             continue;
399         }
400
401         if (rw == UIO_READ)
402             code = FOP_READ(filp, iov->iov_base, count);
403         else
404             code = FOP_WRITE(filp, iov->iov_base, count);
405
406         if (code < 0) {
407             code = -code;
408             break;
409         } else if (code == 0) {
410             /*
411              * This is bad -- we can't read any more data from the
412              * file, but we have no good way of signaling a partial
413              * read either.
414              */
415             code = EIO;
416             break;
417         }
418
419         iov->iov_base += code;
420         iov->iov_len -= code;
421         uiop->uio_resid -= code;
422         uiop->uio_offset += code;
423         code = 0;
424     }
425
426     if (uiop->uio_seg == AFS_UIOSYS)
427         TO_KERNEL_SPACE();
428
429     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
430
431     return code;
432 }
433
434 /* setup_uio 
435  * Setup a uio struct.
436  */
437 void
438 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
439           int count, uio_flag_t flag, uio_seg_t seg)
440 {
441     iovecp->iov_base = (char *)buf;
442     iovecp->iov_len = count;
443     uiop->uio_iov = iovecp;
444     uiop->uio_iovcnt = 1;
445     uiop->uio_offset = pos;
446     uiop->uio_seg = seg;
447     uiop->uio_resid = count;
448     uiop->uio_flag = flag;
449 }
450
451
452 /* uiomove
453  * UIO_READ : dp -> uio
454  * UIO_WRITE : uio -> dp
455  */
456 int
457 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
458 {
459     int count;
460     struct iovec *iov;
461     int code;
462
463     while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
464         iov = uiop->uio_iov;
465         count = iov->iov_len;
466
467         if (!count) {
468             uiop->uio_iov++;
469             uiop->uio_iovcnt--;
470             continue;
471         }
472
473         if (count > length)
474             count = length;
475
476         switch (uiop->uio_seg) {
477         case AFS_UIOSYS:
478             switch (rw) {
479             case UIO_READ:
480                 memcpy(iov->iov_base, dp, count);
481                 break;
482             case UIO_WRITE:
483                 memcpy(dp, iov->iov_base, count);
484                 break;
485             default:
486                 printf("uiomove: Bad rw = %d\n", rw);
487                 return -EINVAL;
488             }
489             break;
490         case AFS_UIOUSER:
491             switch (rw) {
492             case UIO_READ:
493                 AFS_COPYOUT(dp, iov->iov_base, count, code);
494                 break;
495             case UIO_WRITE:
496                 AFS_COPYIN(iov->iov_base, dp, count, code);
497                 break;
498             default:
499                 printf("uiomove: Bad rw = %d\n", rw);
500                 return -EINVAL;
501             }
502             break;
503         default:
504             printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
505             return -EINVAL;
506         }
507
508         dp += count;
509         length -= count;
510         iov->iov_base += count;
511         iov->iov_len -= count;
512         uiop->uio_offset += count;
513         uiop->uio_resid -= count;
514     }
515     return 0;
516 }
517