2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
16 #ifdef AFS_LINUX24_ENV
17 #include "h/module.h" /* early to avoid printf->printk mapping */
19 #include "afs/sysincludes.h" /* Standard vendor system headers */
20 #include "afsincludes.h" /* Afs-based standard headers */
21 #include "afs/afs_stats.h" /* afs statistics */
22 #include "h/smp_lock.h"
23 #if defined(AFS_LINUX26_ENV)
26 #if defined(LINUX_USE_FH)
27 #include "h/exportfs.h"
28 int cache_fh_type = -1;
29 int cache_fh_len = -1;
32 afs_lock_t afs_xosi; /* lock is for tvattr */
33 extern struct osi_dev cacheDev;
34 #if defined(AFS_LINUX24_ENV)
35 extern struct vfsmount *afs_cacheMnt;
37 extern struct super_block *afs_cacheSBp;
39 #if defined(AFS_LINUX26_ENV)
41 osi_UFSOpen(afs_dcache_id_t *ainode)
43 struct osi_file *afile = NULL;
44 extern int cacheDiskType;
45 struct inode *tip = NULL;
46 struct dentry *dp = NULL;
47 struct file *filp = NULL;
49 AFS_STATCNT(osi_UFSOpen);
50 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
51 osi_Panic("UFSOpen called for non-UFS cache\n");
53 if (!afs_osicred_initialized) {
54 /* valid for alpha_osf, SunOS, Ultrix */
55 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
56 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
57 afs_osicred_initialized = 1;
59 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
62 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
63 (int)sizeof(struct osi_file));
65 memset(afile, 0, sizeof(struct osi_file));
66 #if !defined(LINUX_USE_FH)
67 tip = iget(afs_cacheSBp, ainode->ufs);
69 osi_Panic("Can't get inode %d\n", ainode->ufs);
71 dp = d_alloc_anon(tip);
73 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh, cache_fh_len, cache_fh_type);
75 osi_Panic("Can't get dentry\n");
78 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
80 #if defined(STRUCT_TASK_HAS_CRED)
81 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
83 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
86 #if defined(LINUX_USE_FH)
87 osi_Panic("Can't open file\n");
89 osi_Panic("Can't open inode %d\n", ainode->ufs);
92 afile->size = i_size_read(FILE_INODE(filp));
95 afile->proc = (int (*)())0;
96 afile->inum = tip->i_ino; /* for hint validity checking */
101 osi_UFSOpen(afs_dcache_id_t *ainode)
103 register struct osi_file *afile = NULL;
104 extern int cacheDiskType;
106 struct inode *tip = NULL;
107 struct file *filp = NULL;
108 AFS_STATCNT(osi_UFSOpen);
109 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
110 osi_Panic("UFSOpen called for non-UFS cache\n");
112 if (!afs_osicred_initialized) {
113 /* valid for alpha_osf, SunOS, Ultrix */
114 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
115 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
116 afs_osicred_initialized = 1;
118 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
121 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
122 sizeof(struct osi_file));
124 memset(afile, 0, sizeof(struct osi_file));
126 filp->f_dentry = &afile->dentry;
127 tip = iget(afs_cacheSBp, ainode->ufs);
129 osi_Panic("Can't get inode %d\n", ainode->ufs);
130 FILE_INODE(filp) = tip;
131 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
132 filp->f_flags = O_RDWR;
133 #if defined(AFS_LINUX24_ENV)
134 filp->f_mode = FMODE_READ|FMODE_WRITE;
135 filp->f_op = fops_get(tip->i_fop);
137 filp->f_op = tip->i_op->default_file_ops;
139 if (filp->f_op && filp->f_op->open)
140 code = filp->f_op->open(tip, filp);
142 osi_Panic("Can't open inode %d\n", ainode->ufs);
143 afile->size = i_size_read(tip);
146 afile->proc = (int (*)())0;
147 afile->inum = ainode->ufs; /* for hint validity checking */
148 return (void *)afile;
152 #if defined(LINUX_USE_FH)
154 * Given a dentry, return the file handle as encoded by the filesystem.
155 * We can't assume anything about the length (words, not bytes).
156 * The cache has to live on a single filesystem with uniform file
157 * handles, otherwise we panic.
159 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
163 if (cache_fh_len > 0)
164 max_len = cache_fh_len;
166 max_len = MAX_FH_LEN;
167 if (dp->d_sb->s_export_op->encode_fh) {
168 type = dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], &max_len, 0);
170 osi_Panic("File handle encoding failed\n");
172 if (cache_fh_type < 0)
173 cache_fh_type = type;
174 if (cache_fh_len < 0) {
175 cache_fh_len = max_len;
177 if (type != cache_fh_type || max_len != cache_fh_len) {
178 osi_Panic("Inconsistent file handles within cache\n");
181 /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
182 if (cache_fh_type < 0)
183 cache_fh_type = FILEID_INO32_GEN;
184 if (cache_fh_len < 0)
185 cache_fh_len = sizeof(struct fid)/4;
186 ainode->fh.i32.ino = dp->d_inode->i_ino;
187 ainode->fh.i32.gen = dp->d_inode->i_generation;
191 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
192 *ainode = dp->d_inode->i_ino;
197 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
199 register afs_int32 code;
200 AFS_STATCNT(osi_Stat);
201 MObtainWriteLock(&afs_xosi, 320);
202 astat->size = i_size_read(OSIFILE_INODE(afile));
203 #if defined(AFS_LINUX26_ENV)
204 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
205 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
207 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
208 astat->atime = OSIFILE_INODE(afile)->i_atime;
211 MReleaseWriteLock(&afs_xosi);
215 #ifdef AFS_LINUX26_ENV
217 osi_UFSClose(register struct osi_file *afile)
219 AFS_STATCNT(osi_Close);
221 if (OSIFILE_INODE(afile)) {
222 filp_close(afile->filp, NULL);
226 osi_FreeLargeSpace(afile);
231 osi_UFSClose(register struct osi_file *afile)
233 AFS_STATCNT(osi_Close);
235 if (FILE_INODE(&afile->file)) {
236 struct file *filp = &afile->file;
237 if (filp->f_op && filp->f_op->release)
238 filp->f_op->release(FILE_INODE(filp), filp);
239 iput(FILE_INODE(filp));
243 osi_FreeLargeSpace(afile);
249 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
251 register afs_int32 code;
252 struct osi_stat tstat;
253 struct iattr newattrs;
254 struct inode *inode = OSIFILE_INODE(afile);
255 AFS_STATCNT(osi_Truncate);
257 /* This routine only shrinks files, and most systems
258 * have very slow truncates, even when the file is already
259 * small enough. Check now and save some time.
261 code = afs_osi_Stat(afile, &tstat);
262 if (code || tstat.size <= asize)
264 MObtainWriteLock(&afs_xosi, 321);
266 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
267 down_write(&inode->i_alloc_sem);
269 #ifdef STRUCT_INODE_HAS_I_MUTEX
270 mutex_lock(&inode->i_mutex);
274 newattrs.ia_size = asize;
275 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
276 #if defined(AFS_LINUX24_ENV)
277 newattrs.ia_ctime = CURRENT_TIME;
279 /* avoid notify_change() since it wants to update dentry->d_parent */
281 code = inode_change_ok(inode, &newattrs);
283 #ifdef INODE_SETATTR_NOT_VOID
284 code = inode_setattr(inode, &newattrs);
286 inode_setattr(inode, &newattrs);
290 truncate_inode_pages(&inode->i_data, asize);
292 i_size_write(inode, asize);
293 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
294 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
297 truncate_inode_pages(inode, asize);
298 if (inode->i_op && inode->i_op->truncate)
299 inode->i_op->truncate(inode);
303 #ifdef STRUCT_INODE_HAS_I_MUTEX
304 mutex_unlock(&inode->i_mutex);
308 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
309 up_write(&inode->i_alloc_sem);
312 MReleaseWriteLock(&afs_xosi);
317 /* Generic read interface */
319 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
326 AFS_STATCNT(osi_Read);
329 * If the osi_file passed in is NULL, panic only if AFS is not shutting
330 * down. No point in crashing when we are already shutting down
333 if (!afs_shuttingdown)
334 osi_Panic("osi_Read called with null param");
340 afile->offset = offset;
341 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
343 code = osi_rdwr(afile, &auio, UIO_READ);
346 code = asize - auio.uio_resid;
347 afile->offset += code;
349 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
350 ICL_TYPE_INT32, code);
356 /* Generic write interface */
358 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
365 AFS_STATCNT(osi_Write);
368 if (!afs_shuttingdown)
369 osi_Panic("afs_osi_Write called with null param");
375 afile->offset = offset;
376 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
378 code = osi_rdwr(afile, &auio, UIO_WRITE);
381 code = asize - auio.uio_resid;
382 afile->offset += code;
386 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
391 (*afile->proc)(afile, code);
397 /* This work should be handled by physstrat in ca/machdep.c.
398 This routine written from the RT NFS port strategy routine.
399 It has been generalized a bit, but should still be pretty clear. */
401 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
403 afs_int32 returnCode;
405 AFS_STATCNT(osi_MapStrategy);
406 returnCode = (*aproc) (bp);
412 shutdown_osifile(void)
414 AFS_STATCNT(shutdown_osifile);
415 if (afs_cold_shutdown) {
416 afs_osicred_initialized = 0;
420 /* Intialize cache device info and fragment size for disk cache partition. */
422 osi_InitCacheInfo(char *aname)
425 extern afs_dcache_id_t cacheInode;
427 extern struct osi_dev cacheDev;
428 extern afs_int32 afs_fsfragsize;
429 extern struct super_block *afs_cacheSBp;
430 extern struct vfsmount *afs_cacheMnt;
431 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
435 osi_get_fh(dp, &cacheInode.ufs);
436 cacheDev.dev = dp->d_inode->i_sb->s_dev;
437 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
438 afs_cacheSBp = dp->d_inode->i_sb;
446 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
447 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
450 * seek, then read or write to an open inode. addrp points to data in
454 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
456 #ifdef AFS_LINUX26_ENV
457 struct file *filp = osifile->filp;
459 struct file *filp = &osifile->file;
465 unsigned long savelim;
467 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
468 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
470 if (uiop->uio_seg == AFS_UIOSYS)
473 /* seek to the desired position. Return -1 on error. */
474 if (filp->f_op->llseek) {
475 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
478 filp->f_pos = uiop->uio_offset;
480 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
482 count = iov->iov_len;
490 code = FOP_READ(filp, iov->iov_base, count);
492 code = FOP_WRITE(filp, iov->iov_base, count);
497 } else if (code == 0) {
499 * This is bad -- we can't read any more data from the
500 * file, but we have no good way of signaling a partial
507 iov->iov_base += code;
508 iov->iov_len -= code;
509 uiop->uio_resid -= code;
510 uiop->uio_offset += code;
514 if (uiop->uio_seg == AFS_UIOSYS)
517 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
523 * Setup a uio struct.
526 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
527 int count, uio_flag_t flag, uio_seg_t seg)
529 iovecp->iov_base = (char *)buf;
530 iovecp->iov_len = count;
531 uiop->uio_iov = iovecp;
532 uiop->uio_iovcnt = 1;
533 uiop->uio_offset = pos;
535 uiop->uio_resid = count;
536 uiop->uio_flag = flag;
541 * UIO_READ : dp -> uio
542 * UIO_WRITE : uio -> dp
545 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
551 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
553 count = iov->iov_len;
564 switch (uiop->uio_seg) {
568 memcpy(iov->iov_base, dp, count);
571 memcpy(dp, iov->iov_base, count);
574 printf("uiomove: Bad rw = %d\n", rw);
581 AFS_COPYOUT(dp, iov->iov_base, count, code);
584 AFS_COPYIN(iov->iov_base, dp, count, code);
587 printf("uiomove: Bad rw = %d\n", rw);
592 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
598 iov->iov_base += count;
599 iov->iov_len -= count;
600 uiop->uio_offset += count;
601 uiop->uio_resid -= count;