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(HAVE_IGET)
27 #include "h/exportfs.h"
30 afs_lock_t afs_xosi; /* lock is for tvattr */
31 extern struct osi_dev cacheDev;
32 #if defined(AFS_LINUX24_ENV)
33 extern struct vfsmount *afs_cacheMnt;
35 extern struct super_block *afs_cacheSBp;
37 #if defined(AFS_LINUX26_ENV)
39 osi_UFSOpen(afs_int32 ainode)
41 register struct osi_file *afile = NULL;
42 extern int cacheDiskType;
43 struct inode *tip = NULL;
44 struct dentry *dp = NULL;
45 struct file *filp = NULL;
46 #if !defined(HAVE_IGET)
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 sizeof(struct osi_file));
65 memset(afile, 0, sizeof(struct osi_file));
66 #if defined(HAVE_IGET)
67 tip = iget(afs_cacheSBp, (u_long) ainode);
69 osi_Panic("Can't get inode %d\n", ainode);
71 dp = d_alloc_anon(tip);
75 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &fid, sizeof(fid), FILEID_INO32_GEN);
77 osi_Panic("Can't get dentry for inode %d\n", ainode);
80 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
82 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
84 osi_Panic("Can't open inode %d\n", ainode);
86 afile->size = FILE_INODE(filp)->i_size;
89 afile->proc = (int (*)())0;
90 afile->inum = ainode; /* for hint validity checking */
95 osi_UFSOpen(afs_int32 ainode)
97 register struct osi_file *afile = NULL;
98 extern int cacheDiskType;
100 struct inode *tip = NULL;
101 struct file *filp = NULL;
102 AFS_STATCNT(osi_UFSOpen);
103 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
104 osi_Panic("UFSOpen called for non-UFS cache\n");
106 if (!afs_osicred_initialized) {
107 /* valid for alpha_osf, SunOS, Ultrix */
108 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
109 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
110 afs_osicred_initialized = 1;
112 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
115 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
116 sizeof(struct osi_file));
118 memset(afile, 0, sizeof(struct osi_file));
120 filp->f_dentry = &afile->dentry;
121 tip = iget(afs_cacheSBp, (u_long) ainode);
123 osi_Panic("Can't get inode %d\n", ainode);
124 FILE_INODE(filp) = tip;
125 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
126 filp->f_flags = O_RDWR;
127 #if defined(AFS_LINUX24_ENV)
128 filp->f_mode = FMODE_READ|FMODE_WRITE;
129 filp->f_op = fops_get(tip->i_fop);
131 filp->f_op = tip->i_op->default_file_ops;
133 if (filp->f_op && filp->f_op->open)
134 code = filp->f_op->open(tip, filp);
136 osi_Panic("Can't open inode %d\n", ainode);
137 afile->size = tip->i_size;
140 afile->proc = (int (*)())0;
141 afile->inum = ainode; /* for hint validity checking */
142 return (void *)afile;
147 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
149 register afs_int32 code;
150 AFS_STATCNT(osi_Stat);
151 MObtainWriteLock(&afs_xosi, 320);
152 astat->size = OSIFILE_INODE(afile)->i_size;
153 #if defined(AFS_LINUX26_ENV)
154 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
155 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
157 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
158 astat->atime = OSIFILE_INODE(afile)->i_atime;
161 MReleaseWriteLock(&afs_xosi);
165 #ifdef AFS_LINUX26_ENV
167 osi_UFSClose(register struct osi_file *afile)
169 AFS_STATCNT(osi_Close);
171 if (OSIFILE_INODE(afile)) {
172 filp_close(afile->filp, NULL);
176 osi_FreeLargeSpace(afile);
181 osi_UFSClose(register struct osi_file *afile)
183 AFS_STATCNT(osi_Close);
185 if (FILE_INODE(&afile->file)) {
186 struct file *filp = &afile->file;
187 if (filp->f_op && filp->f_op->release)
188 filp->f_op->release(FILE_INODE(filp), filp);
189 iput(FILE_INODE(filp));
193 osi_FreeLargeSpace(afile);
199 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
201 register afs_int32 code;
202 struct osi_stat tstat;
203 struct iattr newattrs;
204 struct inode *inode = OSIFILE_INODE(afile);
205 AFS_STATCNT(osi_Truncate);
207 /* This routine only shrinks files, and most systems
208 * have very slow truncates, even when the file is already
209 * small enough. Check now and save some time.
211 code = afs_osi_Stat(afile, &tstat);
212 if (code || tstat.size <= asize)
214 MObtainWriteLock(&afs_xosi, 321);
216 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
217 down_write(&inode->i_alloc_sem);
219 #ifdef STRUCT_INODE_HAS_I_MUTEX
220 mutex_lock(&inode->i_mutex);
224 newattrs.ia_size = asize;
225 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
226 #if defined(AFS_LINUX24_ENV)
227 newattrs.ia_ctime = CURRENT_TIME;
229 /* avoid notify_change() since it wants to update dentry->d_parent */
231 code = inode_change_ok(inode, &newattrs);
233 #ifdef INODE_SETATTR_NOT_VOID
234 code = inode_setattr(inode, &newattrs);
236 inode_setattr(inode, &newattrs);
240 truncate_inode_pages(&inode->i_data, asize);
242 inode->i_size = asize;
243 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
244 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
247 truncate_inode_pages(inode, asize);
248 if (inode->i_op && inode->i_op->truncate)
249 inode->i_op->truncate(inode);
253 #ifdef STRUCT_INODE_HAS_I_MUTEX
254 mutex_unlock(&inode->i_mutex);
258 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
259 up_write(&inode->i_alloc_sem);
262 MReleaseWriteLock(&afs_xosi);
267 /* Generic read interface */
269 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
276 AFS_STATCNT(osi_Read);
279 * If the osi_file passed in is NULL, panic only if AFS is not shutting
280 * down. No point in crashing when we are already shutting down
283 if (!afs_shuttingdown)
284 osi_Panic("osi_Read called with null param");
290 afile->offset = offset;
291 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
293 code = osi_rdwr(afile, &auio, UIO_READ);
296 code = asize - auio.uio_resid;
297 afile->offset += code;
299 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
300 ICL_TYPE_INT32, code);
306 /* Generic write interface */
308 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
315 AFS_STATCNT(osi_Write);
318 if (!afs_shuttingdown)
319 osi_Panic("afs_osi_Write called with null param");
325 afile->offset = offset;
326 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
328 code = osi_rdwr(afile, &auio, UIO_WRITE);
331 code = asize - auio.uio_resid;
332 afile->offset += code;
336 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
341 (*afile->proc)(afile, code);
347 /* This work should be handled by physstrat in ca/machdep.c.
348 This routine written from the RT NFS port strategy routine.
349 It has been generalized a bit, but should still be pretty clear. */
351 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
353 afs_int32 returnCode;
355 AFS_STATCNT(osi_MapStrategy);
356 returnCode = (*aproc) (bp);
362 shutdown_osifile(void)
364 AFS_STATCNT(shutdown_osifile);
365 if (afs_cold_shutdown) {
366 afs_osicred_initialized = 0;
370 /* Intialize cache device info and fragment size for disk cache partition. */
372 osi_InitCacheInfo(char *aname)
376 extern ino_t cacheInode;
377 extern struct osi_dev cacheDev;
378 extern afs_int32 afs_fsfragsize;
379 extern struct super_block *afs_cacheSBp;
380 extern struct vfsmount *afs_cacheMnt;
381 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
385 cacheInode = dp->d_inode->i_ino;
386 cacheDev.dev = dp->d_inode->i_sb->s_dev;
387 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
388 afs_cacheSBp = dp->d_inode->i_sb;
396 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
397 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
400 * seek, then read or write to an open inode. addrp points to data in
404 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
406 #ifdef AFS_LINUX26_ENV
407 struct file *filp = osifile->filp;
409 struct file *filp = &osifile->file;
415 unsigned long savelim;
417 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
418 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
420 if (uiop->uio_seg == AFS_UIOSYS)
423 /* seek to the desired position. Return -1 on error. */
424 if (filp->f_op->llseek) {
425 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
428 filp->f_pos = uiop->uio_offset;
430 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
432 count = iov->iov_len;
440 code = FOP_READ(filp, iov->iov_base, count);
442 code = FOP_WRITE(filp, iov->iov_base, count);
447 } else if (code == 0) {
449 * This is bad -- we can't read any more data from the
450 * file, but we have no good way of signaling a partial
457 iov->iov_base += code;
458 iov->iov_len -= code;
459 uiop->uio_resid -= code;
460 uiop->uio_offset += code;
464 if (uiop->uio_seg == AFS_UIOSYS)
467 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
473 * Setup a uio struct.
476 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
477 int count, uio_flag_t flag, uio_seg_t seg)
479 iovecp->iov_base = (char *)buf;
480 iovecp->iov_len = count;
481 uiop->uio_iov = iovecp;
482 uiop->uio_iovcnt = 1;
483 uiop->uio_offset = pos;
485 uiop->uio_resid = count;
486 uiop->uio_flag = flag;
491 * UIO_READ : dp -> uio
492 * UIO_WRITE : uio -> dp
495 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
501 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
503 count = iov->iov_len;
514 switch (uiop->uio_seg) {
518 memcpy(iov->iov_base, dp, count);
521 memcpy(dp, iov->iov_base, count);
524 printf("uiomove: Bad rw = %d\n", rw);
531 AFS_COPYOUT(dp, iov->iov_base, count, code);
534 AFS_COPYIN(iov->iov_base, dp, count, code);
537 printf("uiomove: Bad rw = %d\n", rw);
542 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
548 iov->iov_base += count;
549 iov->iov_len -= count;
550 uiop->uio_offset += count;
551 uiop->uio_resid -= count;