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);
71 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &fid, sizeof(fid), FILEID_INO32_GEN);
73 osi_Panic("Can't get dentry for inode %d\n", ainode);
76 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
78 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
80 osi_Panic("Can't open inode %d\n", ainode);
82 afile->size = FILE_INODE(filp)->i_size;
85 afile->proc = (int (*)())0;
86 afile->inum = ainode; /* for hint validity checking */
91 osi_UFSOpen(afs_int32 ainode)
93 register struct osi_file *afile = NULL;
94 extern int cacheDiskType;
96 struct inode *tip = NULL;
97 struct file *filp = NULL;
98 AFS_STATCNT(osi_UFSOpen);
99 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
100 osi_Panic("UFSOpen called for non-UFS cache\n");
102 if (!afs_osicred_initialized) {
103 /* valid for alpha_osf, SunOS, Ultrix */
104 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
105 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
106 afs_osicred_initialized = 1;
108 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
111 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
112 sizeof(struct osi_file));
114 memset(afile, 0, sizeof(struct osi_file));
116 filp->f_dentry = &afile->dentry;
117 tip = iget(afs_cacheSBp, (u_long) ainode);
119 osi_Panic("Can't get inode %d\n", ainode);
120 FILE_INODE(filp) = tip;
121 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
122 filp->f_flags = O_RDWR;
123 #if defined(AFS_LINUX24_ENV)
124 filp->f_mode = FMODE_READ|FMODE_WRITE;
125 filp->f_op = fops_get(tip->i_fop);
127 filp->f_op = tip->i_op->default_file_ops;
129 if (filp->f_op && filp->f_op->open)
130 code = filp->f_op->open(tip, filp);
132 osi_Panic("Can't open inode %d\n", ainode);
133 afile->size = tip->i_size;
136 afile->proc = (int (*)())0;
137 afile->inum = ainode; /* for hint validity checking */
138 return (void *)afile;
143 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
145 register afs_int32 code;
146 AFS_STATCNT(osi_Stat);
147 MObtainWriteLock(&afs_xosi, 320);
148 astat->size = OSIFILE_INODE(afile)->i_size;
149 #if defined(AFS_LINUX26_ENV)
150 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
151 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
153 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
154 astat->atime = OSIFILE_INODE(afile)->i_atime;
157 MReleaseWriteLock(&afs_xosi);
161 #ifdef AFS_LINUX26_ENV
163 osi_UFSClose(register struct osi_file *afile)
165 AFS_STATCNT(osi_Close);
167 if (OSIFILE_INODE(afile)) {
168 filp_close(afile->filp, NULL);
172 osi_FreeLargeSpace(afile);
177 osi_UFSClose(register struct osi_file *afile)
179 AFS_STATCNT(osi_Close);
181 if (FILE_INODE(&afile->file)) {
182 struct file *filp = &afile->file;
183 if (filp->f_op && filp->f_op->release)
184 filp->f_op->release(FILE_INODE(filp), filp);
185 iput(FILE_INODE(filp));
189 osi_FreeLargeSpace(afile);
195 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
197 register afs_int32 code;
198 struct osi_stat tstat;
199 struct iattr newattrs;
200 struct inode *inode = OSIFILE_INODE(afile);
201 AFS_STATCNT(osi_Truncate);
203 /* This routine only shrinks files, and most systems
204 * have very slow truncates, even when the file is already
205 * small enough. Check now and save some time.
207 code = afs_osi_Stat(afile, &tstat);
208 if (code || tstat.size <= asize)
210 MObtainWriteLock(&afs_xosi, 321);
212 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
213 down_write(&inode->i_alloc_sem);
215 #ifdef STRUCT_INODE_HAS_I_MUTEX
216 mutex_lock(&inode->i_mutex);
220 newattrs.ia_size = asize;
221 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
222 #if defined(AFS_LINUX24_ENV)
223 newattrs.ia_ctime = CURRENT_TIME;
225 /* avoid notify_change() since it wants to update dentry->d_parent */
227 code = inode_change_ok(inode, &newattrs);
229 #ifdef INODE_SETATTR_NOT_VOID
230 code = inode_setattr(inode, &newattrs);
232 inode_setattr(inode, &newattrs);
236 truncate_inode_pages(&inode->i_data, asize);
238 inode->i_size = asize;
239 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
240 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
243 truncate_inode_pages(inode, asize);
244 if (inode->i_op && inode->i_op->truncate)
245 inode->i_op->truncate(inode);
249 #ifdef STRUCT_INODE_HAS_I_MUTEX
250 mutex_unlock(&inode->i_mutex);
254 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
255 up_write(&inode->i_alloc_sem);
258 MReleaseWriteLock(&afs_xosi);
263 /* Generic read interface */
265 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
272 AFS_STATCNT(osi_Read);
275 * If the osi_file passed in is NULL, panic only if AFS is not shutting
276 * down. No point in crashing when we are already shutting down
279 if (!afs_shuttingdown)
280 osi_Panic("osi_Read called with null param");
286 afile->offset = offset;
287 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
289 code = osi_rdwr(afile, &auio, UIO_READ);
292 code = asize - auio.uio_resid;
293 afile->offset += code;
295 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
296 ICL_TYPE_INT32, code);
302 /* Generic write interface */
304 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
311 AFS_STATCNT(osi_Write);
314 if (!afs_shuttingdown)
315 osi_Panic("afs_osi_Write called with null param");
321 afile->offset = offset;
322 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
324 code = osi_rdwr(afile, &auio, UIO_WRITE);
327 code = asize - auio.uio_resid;
328 afile->offset += code;
332 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
337 (*afile->proc)(afile, code);
343 /* This work should be handled by physstrat in ca/machdep.c.
344 This routine written from the RT NFS port strategy routine.
345 It has been generalized a bit, but should still be pretty clear. */
347 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
349 afs_int32 returnCode;
351 AFS_STATCNT(osi_MapStrategy);
352 returnCode = (*aproc) (bp);
358 shutdown_osifile(void)
360 AFS_STATCNT(shutdown_osifile);
361 if (afs_cold_shutdown) {
362 afs_osicred_initialized = 0;
366 /* Intialize cache device info and fragment size for disk cache partition. */
368 osi_InitCacheInfo(char *aname)
372 extern ino_t cacheInode;
373 extern struct osi_dev cacheDev;
374 extern afs_int32 afs_fsfragsize;
375 extern struct super_block *afs_cacheSBp;
376 extern struct vfsmount *afs_cacheMnt;
377 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
381 cacheInode = dp->d_inode->i_ino;
382 cacheDev.dev = dp->d_inode->i_sb->s_dev;
383 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
384 afs_cacheSBp = dp->d_inode->i_sb;
392 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
393 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
396 * seek, then read or write to an open inode. addrp points to data in
400 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
402 #ifdef AFS_LINUX26_ENV
403 struct file *filp = osifile->filp;
405 struct file *filp = &osifile->file;
411 unsigned long savelim;
413 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
414 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
416 if (uiop->uio_seg == AFS_UIOSYS)
419 /* seek to the desired position. Return -1 on error. */
420 if (filp->f_op->llseek) {
421 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
424 filp->f_pos = uiop->uio_offset;
426 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
428 count = iov->iov_len;
436 code = FOP_READ(filp, iov->iov_base, count);
438 code = FOP_WRITE(filp, iov->iov_base, count);
443 } else if (code == 0) {
445 * This is bad -- we can't read any more data from the
446 * file, but we have no good way of signaling a partial
453 iov->iov_base += code;
454 iov->iov_len -= code;
455 uiop->uio_resid -= code;
456 uiop->uio_offset += code;
460 if (uiop->uio_seg == AFS_UIOSYS)
463 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
469 * Setup a uio struct.
472 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
473 int count, uio_flag_t flag, uio_seg_t seg)
475 iovecp->iov_base = (char *)buf;
476 iovecp->iov_len = count;
477 uiop->uio_iov = iovecp;
478 uiop->uio_iovcnt = 1;
479 uiop->uio_offset = pos;
481 uiop->uio_resid = count;
482 uiop->uio_flag = flag;
487 * UIO_READ : dp -> uio
488 * UIO_WRITE : uio -> dp
491 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
497 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
499 count = iov->iov_len;
510 switch (uiop->uio_seg) {
514 memcpy(iov->iov_base, dp, count);
517 memcpy(dp, iov->iov_base, count);
520 printf("uiomove: Bad rw = %d\n", rw);
527 AFS_COPYOUT(dp, iov->iov_base, count, code);
530 AFS_COPYIN(iov->iov_base, dp, count, code);
533 printf("uiomove: Bad rw = %d\n", rw);
538 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
544 iov->iov_base += count;
545 iov->iov_len -= count;
546 uiop->uio_offset += count;
547 uiop->uio_resid -= count;