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 #if defined(AFS_LINUX26_ENV)
142 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
143 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
145 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
146 astat->atime = OSIFILE_INODE(afile)->i_atime;
149 MReleaseWriteLock(&afs_xosi);
153 #ifdef AFS_LINUX26_ENV
155 osi_UFSClose(register struct osi_file *afile)
157 AFS_STATCNT(osi_Close);
159 if (OSIFILE_INODE(afile)) {
160 filp_close(afile->filp, NULL);
164 osi_FreeLargeSpace(afile);
169 osi_UFSClose(register struct osi_file *afile)
171 AFS_STATCNT(osi_Close);
173 if (FILE_INODE(&afile->file)) {
174 struct file *filp = &afile->file;
175 if (filp->f_op && filp->f_op->release)
176 filp->f_op->release(FILE_INODE(filp), filp);
177 iput(FILE_INODE(filp));
181 osi_FreeLargeSpace(afile);
187 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
189 register afs_int32 code;
190 struct osi_stat tstat;
191 struct iattr newattrs;
192 struct inode *inode = OSIFILE_INODE(afile);
193 AFS_STATCNT(osi_Truncate);
195 /* This routine only shrinks files, and most systems
196 * have very slow truncates, even when the file is already
197 * small enough. Check now and save some time.
199 code = afs_osi_Stat(afile, &tstat);
200 if (code || tstat.size <= asize)
202 MObtainWriteLock(&afs_xosi, 321);
204 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
205 down_write(&inode->i_alloc_sem);
207 #ifdef STRUCT_INODE_HAS_I_MUTEX
208 mutex_lock(&inode->i_mutex);
212 newattrs.ia_size = asize;
213 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
214 #if defined(AFS_LINUX24_ENV)
215 newattrs.ia_ctime = CURRENT_TIME;
217 /* avoid notify_change() since it wants to update dentry->d_parent */
219 code = inode_change_ok(inode, &newattrs);
221 #ifdef INODE_SETATTR_NOT_VOID
222 code = inode_setattr(inode, &newattrs);
224 inode_setattr(inode, &newattrs);
228 truncate_inode_pages(&inode->i_data, asize);
230 inode->i_size = asize;
231 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
232 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
235 truncate_inode_pages(inode, asize);
236 if (inode->i_op && inode->i_op->truncate)
237 inode->i_op->truncate(inode);
241 #ifdef STRUCT_INODE_HAS_I_MUTEX
242 mutex_unlock(&inode->i_mutex);
246 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
247 up_write(&inode->i_alloc_sem);
250 MReleaseWriteLock(&afs_xosi);
255 /* Generic read interface */
257 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
264 AFS_STATCNT(osi_Read);
267 * If the osi_file passed in is NULL, panic only if AFS is not shutting
268 * down. No point in crashing when we are already shutting down
271 if (!afs_shuttingdown)
272 osi_Panic("osi_Read called with null param");
278 afile->offset = offset;
279 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
281 code = osi_rdwr(afile, &auio, UIO_READ);
284 code = asize - auio.uio_resid;
285 afile->offset += code;
287 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
288 ICL_TYPE_INT32, code);
294 /* Generic write interface */
296 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
303 AFS_STATCNT(osi_Write);
306 if (!afs_shuttingdown)
307 osi_Panic("afs_osi_Write called with null param");
313 afile->offset = offset;
314 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
316 code = osi_rdwr(afile, &auio, UIO_WRITE);
319 code = asize - auio.uio_resid;
320 afile->offset += code;
324 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
329 (*afile->proc)(afile, code);
335 /* This work should be handled by physstrat in ca/machdep.c.
336 This routine written from the RT NFS port strategy routine.
337 It has been generalized a bit, but should still be pretty clear. */
339 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
341 afs_int32 returnCode;
343 AFS_STATCNT(osi_MapStrategy);
344 returnCode = (*aproc) (bp);
350 shutdown_osifile(void)
352 AFS_STATCNT(shutdown_osifile);
353 if (afs_cold_shutdown) {
354 afs_osicred_initialized = 0;
358 /* Intialize cache device info and fragment size for disk cache partition. */
360 osi_InitCacheInfo(char *aname)
364 extern ino_t cacheInode;
365 extern struct osi_dev cacheDev;
366 extern afs_int32 afs_fsfragsize;
367 extern struct super_block *afs_cacheSBp;
368 extern struct vfsmount *afs_cacheMnt;
369 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
373 cacheInode = dp->d_inode->i_ino;
374 cacheDev.dev = dp->d_inode->i_sb->s_dev;
375 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
376 afs_cacheSBp = dp->d_inode->i_sb;
384 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
385 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
388 * seek, then read or write to an open inode. addrp points to data in
392 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
394 #ifdef AFS_LINUX26_ENV
395 struct file *filp = osifile->filp;
397 struct file *filp = &osifile->file;
403 unsigned long savelim;
405 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
406 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
408 if (uiop->uio_seg == AFS_UIOSYS)
411 /* seek to the desired position. Return -1 on error. */
412 if (filp->f_op->llseek) {
413 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
416 filp->f_pos = uiop->uio_offset;
418 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
420 count = iov->iov_len;
428 code = FOP_READ(filp, iov->iov_base, count);
430 code = FOP_WRITE(filp, iov->iov_base, count);
435 } else if (code == 0) {
437 * This is bad -- we can't read any more data from the
438 * file, but we have no good way of signaling a partial
445 iov->iov_base += code;
446 iov->iov_len -= code;
447 uiop->uio_resid -= code;
448 uiop->uio_offset += code;
452 if (uiop->uio_seg == AFS_UIOSYS)
455 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
461 * Setup a uio struct.
464 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
465 int count, uio_flag_t flag, uio_seg_t seg)
467 iovecp->iov_base = (char *)buf;
468 iovecp->iov_len = count;
469 uiop->uio_iov = iovecp;
470 uiop->uio_iovcnt = 1;
471 uiop->uio_offset = pos;
473 uiop->uio_resid = count;
474 uiop->uio_flag = flag;
479 * UIO_READ : dp -> uio
480 * UIO_WRITE : uio -> dp
483 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
489 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
491 count = iov->iov_len;
502 switch (uiop->uio_seg) {
506 memcpy(iov->iov_base, dp, count);
509 memcpy(dp, iov->iov_base, count);
512 printf("uiomove: Bad rw = %d\n", rw);
519 AFS_COPYOUT(dp, iov->iov_base, count, code);
522 AFS_COPYIN(iov->iov_base, dp, count, code);
525 printf("uiomove: Bad rw = %d\n", rw);
530 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
536 iov->iov_base += count;
537 iov->iov_len -= count;
538 uiop->uio_offset += count;
539 uiop->uio_resid -= count;