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)
28 afs_lock_t afs_xosi; /* lock is for tvattr */
29 extern struct osi_dev cacheDev;
30 #if defined(AFS_LINUX24_ENV)
31 extern struct vfsmount *afs_cacheMnt;
33 extern struct super_block *afs_cacheSBp;
35 #if defined(AFS_LINUX26_ENV)
37 osi_UFSOpen(afs_int32 ainode)
39 register struct osi_file *afile = NULL;
40 extern int cacheDiskType;
41 struct inode *tip = NULL;
42 struct dentry *dp = NULL;
43 struct file *filp = NULL;
44 AFS_STATCNT(osi_UFSOpen);
45 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
46 osi_Panic("UFSOpen called for non-UFS cache\n");
48 if (!afs_osicred_initialized) {
49 /* valid for alpha_osf, SunOS, Ultrix */
50 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
51 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
52 afs_osicred_initialized = 1;
54 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
57 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
58 sizeof(struct osi_file));
60 memset(afile, 0, sizeof(struct osi_file));
61 tip = iget(afs_cacheSBp, (u_long) ainode);
63 osi_Panic("Can't get inode %d\n", ainode);
64 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
66 dp = d_alloc_anon(tip);
68 osi_Panic("Can't get dentry for inode %d\n", ainode);
70 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
72 osi_Panic("Can't open inode %d\n", ainode);
74 afile->size = FILE_INODE(filp)->i_size;
77 afile->proc = (int (*)())0;
78 afile->inum = ainode; /* for hint validity checking */
83 osi_UFSOpen(afs_int32 ainode)
85 register struct osi_file *afile = NULL;
86 extern int cacheDiskType;
88 struct inode *tip = NULL;
89 struct file *filp = NULL;
90 AFS_STATCNT(osi_UFSOpen);
91 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
92 osi_Panic("UFSOpen called for non-UFS cache\n");
94 if (!afs_osicred_initialized) {
95 /* valid for alpha_osf, SunOS, Ultrix */
96 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
97 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
98 afs_osicred_initialized = 1;
100 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
103 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
104 sizeof(struct osi_file));
106 memset(afile, 0, sizeof(struct osi_file));
108 filp->f_dentry = &afile->dentry;
109 tip = iget(afs_cacheSBp, (u_long) ainode);
111 osi_Panic("Can't get inode %d\n", ainode);
112 FILE_INODE(filp) = tip;
113 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
114 filp->f_flags = O_RDWR;
115 #if defined(AFS_LINUX24_ENV)
116 filp->f_mode = FMODE_READ|FMODE_WRITE;
117 filp->f_op = fops_get(tip->i_fop);
119 filp->f_op = tip->i_op->default_file_ops;
121 if (filp->f_op && filp->f_op->open)
122 code = filp->f_op->open(tip, filp);
124 osi_Panic("Can't open inode %d\n", ainode);
125 afile->size = tip->i_size;
128 afile->proc = (int (*)())0;
129 afile->inum = ainode; /* for hint validity checking */
130 return (void *)afile;
135 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
137 register afs_int32 code;
138 AFS_STATCNT(osi_Stat);
139 MObtainWriteLock(&afs_xosi, 320);
140 astat->size = OSIFILE_INODE(afile)->i_size;
141 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
142 astat->blksize = OSIFILE_INODE(afile)->i_blksize;
144 #if defined(AFS_LINUX26_ENV)
145 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
146 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
148 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
149 astat->atime = OSIFILE_INODE(afile)->i_atime;
152 MReleaseWriteLock(&afs_xosi);
156 #ifdef AFS_LINUX26_ENV
158 osi_UFSClose(register struct osi_file *afile)
160 AFS_STATCNT(osi_Close);
162 if (OSIFILE_INODE(afile)) {
163 filp_close(afile->filp, NULL);
167 osi_FreeLargeSpace(afile);
172 osi_UFSClose(register struct osi_file *afile)
174 AFS_STATCNT(osi_Close);
176 if (FILE_INODE(&afile->file)) {
177 struct file *filp = &afile->file;
178 if (filp->f_op && filp->f_op->release)
179 filp->f_op->release(FILE_INODE(filp), filp);
180 iput(FILE_INODE(filp));
184 osi_FreeLargeSpace(afile);
190 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
192 register afs_int32 code;
193 struct osi_stat tstat;
194 struct iattr newattrs;
195 struct inode *inode = OSIFILE_INODE(afile);
196 AFS_STATCNT(osi_Truncate);
198 /* This routine only shrinks files, and most systems
199 * have very slow truncates, even when the file is already
200 * small enough. Check now and save some time.
202 code = afs_osi_Stat(afile, &tstat);
203 if (code || tstat.size <= asize)
205 MObtainWriteLock(&afs_xosi, 321);
207 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
208 down_write(&inode->i_alloc_sem);
210 #ifdef STRUCT_INODE_HAS_I_MUTEX
211 mutex_lock(&inode->i_mutex);
215 newattrs.ia_size = asize;
216 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
217 #if defined(AFS_LINUX24_ENV)
218 newattrs.ia_ctime = CURRENT_TIME;
220 /* avoid notify_change() since it wants to update dentry->d_parent */
222 code = inode_change_ok(inode, &newattrs);
224 #ifdef INODE_SETATTR_NOT_VOID
225 code = inode_setattr(inode, &newattrs);
227 inode_setattr(inode, &newattrs);
231 truncate_inode_pages(&inode->i_data, asize);
233 inode->i_size = asize;
234 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
235 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
238 truncate_inode_pages(inode, asize);
239 if (inode->i_op && inode->i_op->truncate)
240 inode->i_op->truncate(inode);
244 #ifdef STRUCT_INODE_HAS_I_MUTEX
245 mutex_unlock(&inode->i_mutex);
249 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
250 up_write(&inode->i_alloc_sem);
253 MReleaseWriteLock(&afs_xosi);
258 /* Generic read interface */
260 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
267 AFS_STATCNT(osi_Read);
270 * If the osi_file passed in is NULL, panic only if AFS is not shutting
271 * down. No point in crashing when we are already shutting down
274 if (!afs_shuttingdown)
275 osi_Panic("osi_Read called with null param");
281 afile->offset = offset;
282 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
284 code = osi_rdwr(afile, &auio, UIO_READ);
287 code = asize - auio.uio_resid;
288 afile->offset += code;
290 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
291 ICL_TYPE_INT32, code);
297 /* Generic write interface */
299 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
306 AFS_STATCNT(osi_Write);
309 if (!afs_shuttingdown)
310 osi_Panic("afs_osi_Write called with null param");
316 afile->offset = offset;
317 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
319 code = osi_rdwr(afile, &auio, UIO_WRITE);
322 code = asize - auio.uio_resid;
323 afile->offset += code;
327 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
332 (*afile->proc)(afile, code);
338 /* This work should be handled by physstrat in ca/machdep.c.
339 This routine written from the RT NFS port strategy routine.
340 It has been generalized a bit, but should still be pretty clear. */
342 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
344 afs_int32 returnCode;
346 AFS_STATCNT(osi_MapStrategy);
347 returnCode = (*aproc) (bp);
353 shutdown_osifile(void)
355 extern int afs_cold_shutdown;
357 AFS_STATCNT(shutdown_osifile);
358 if (afs_cold_shutdown) {
359 afs_osicred_initialized = 0;
363 /* Intialize cache device info and fragment size for disk cache partition. */
365 osi_InitCacheInfo(char *aname)
369 extern ino_t cacheInode;
370 extern struct osi_dev cacheDev;
371 extern afs_int32 afs_fsfragsize;
372 extern struct super_block *afs_cacheSBp;
373 extern struct vfsmount *afs_cacheMnt;
374 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
378 cacheInode = dp->d_inode->i_ino;
379 cacheDev.dev = dp->d_inode->i_sb->s_dev;
380 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
381 afs_cacheSBp = dp->d_inode->i_sb;
389 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
390 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
393 * seek, then read or write to an open inode. addrp points to data in
397 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
399 #ifdef AFS_LINUX26_ENV
400 struct file *filp = osifile->filp;
402 struct file *filp = &osifile->file;
408 unsigned long savelim;
410 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
411 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
413 if (uiop->uio_seg == AFS_UIOSYS)
416 /* seek to the desired position. Return -1 on error. */
417 if (filp->f_op->llseek) {
418 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
421 filp->f_pos = uiop->uio_offset;
423 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
425 count = iov->iov_len;
433 code = FOP_READ(filp, iov->iov_base, count);
435 code = FOP_WRITE(filp, iov->iov_base, count);
440 } else if (code == 0) {
442 * This is bad -- we can't read any more data from the
443 * file, but we have no good way of signaling a partial
450 iov->iov_base += code;
451 iov->iov_len -= code;
452 uiop->uio_resid -= code;
453 uiop->uio_offset += code;
457 if (uiop->uio_seg == AFS_UIOSYS)
460 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
466 * Setup a uio struct.
469 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
470 int count, uio_flag_t flag, uio_seg_t seg)
472 iovecp->iov_base = (char *)buf;
473 iovecp->iov_len = count;
474 uiop->uio_iov = iovecp;
475 uiop->uio_iovcnt = 1;
476 uiop->uio_offset = pos;
478 uiop->uio_resid = count;
479 uiop->uio_flag = flag;
484 * UIO_READ : dp -> uio
485 * UIO_WRITE : uio -> dp
488 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
494 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
496 count = iov->iov_len;
507 switch (uiop->uio_seg) {
511 memcpy(iov->iov_base, dp, count);
514 memcpy(dp, iov->iov_base, count);
517 printf("uiomove: Bad rw = %d\n", rw);
524 AFS_COPYOUT(dp, iov->iov_base, count, code);
527 AFS_COPYIN(iov->iov_base, dp, count, code);
530 printf("uiomove: Bad rw = %d\n", rw);
535 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
541 iov->iov_base += count;
542 iov->iov_len -= count;
543 uiop->uio_offset += count;
544 uiop->uio_resid -= count;