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 #ifdef AFS_CACHE_VNODE_PATH
64 case AFS_CACHE_CELLS_INODE:
65 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
67 case AFS_CACHE_ITEMS_INODE:
68 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
70 case AFS_CACHE_VOLUME_INODE:
71 snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
74 osi_Panic("Invalid negative inode");
77 dummy = ainode / afs_numfilesperdir;
78 snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode);
81 code = osi_lookupname(fname, AFS_UIOSYS, 0, &dp);
83 osi_Panic("Failed cache file lookup: %s in UFSOpen\n", fname);
87 tip = iget(afs_cacheSBp, (u_long) ainode);
89 osi_Panic("Can't get inode %d\n", ainode);
91 dp = d_alloc_anon(tip);
93 osi_Panic("Can't get dentry for inode %d\n", ainode);
95 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
97 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
99 osi_Panic("Can't open inode %d\n", ainode);
101 afile->size = FILE_INODE(filp)->i_size;
104 afile->proc = (int (*)())0;
105 afile->inum = ainode; /* for hint validity checking */
106 return (void *)afile;
110 osi_UFSOpen(afs_int32 ainode)
112 register struct osi_file *afile = NULL;
113 extern int cacheDiskType;
115 struct inode *tip = NULL;
116 struct file *filp = NULL;
117 AFS_STATCNT(osi_UFSOpen);
118 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
119 osi_Panic("UFSOpen called for non-UFS cache\n");
121 if (!afs_osicred_initialized) {
122 /* valid for alpha_osf, SunOS, Ultrix */
123 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
124 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
125 afs_osicred_initialized = 1;
127 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
130 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
131 sizeof(struct osi_file));
133 memset(afile, 0, sizeof(struct osi_file));
135 filp->f_dentry = &afile->dentry;
136 tip = iget(afs_cacheSBp, (u_long) ainode);
138 osi_Panic("Can't get inode %d\n", ainode);
139 FILE_INODE(filp) = tip;
140 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
141 filp->f_flags = O_RDWR;
142 #if defined(AFS_LINUX24_ENV)
143 filp->f_mode = FMODE_READ|FMODE_WRITE;
144 filp->f_op = fops_get(tip->i_fop);
146 filp->f_op = tip->i_op->default_file_ops;
148 if (filp->f_op && filp->f_op->open)
149 code = filp->f_op->open(tip, filp);
151 osi_Panic("Can't open inode %d\n", ainode);
152 afile->size = tip->i_size;
155 afile->proc = (int (*)())0;
156 afile->inum = ainode; /* for hint validity checking */
157 return (void *)afile;
162 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
164 register afs_int32 code;
165 AFS_STATCNT(osi_Stat);
166 MObtainWriteLock(&afs_xosi, 320);
167 astat->size = OSIFILE_INODE(afile)->i_size;
168 #if defined(AFS_LINUX26_ENV)
169 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
170 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
172 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
173 astat->atime = OSIFILE_INODE(afile)->i_atime;
176 MReleaseWriteLock(&afs_xosi);
180 #ifdef AFS_LINUX26_ENV
182 osi_UFSClose(register struct osi_file *afile)
184 AFS_STATCNT(osi_Close);
186 if (OSIFILE_INODE(afile)) {
187 filp_close(afile->filp, NULL);
191 osi_FreeLargeSpace(afile);
196 osi_UFSClose(register struct osi_file *afile)
198 AFS_STATCNT(osi_Close);
200 if (FILE_INODE(&afile->file)) {
201 struct file *filp = &afile->file;
202 if (filp->f_op && filp->f_op->release)
203 filp->f_op->release(FILE_INODE(filp), filp);
204 iput(FILE_INODE(filp));
208 osi_FreeLargeSpace(afile);
214 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
216 register afs_int32 code;
217 struct osi_stat tstat;
218 struct iattr newattrs;
219 struct inode *inode = OSIFILE_INODE(afile);
220 AFS_STATCNT(osi_Truncate);
222 /* This routine only shrinks files, and most systems
223 * have very slow truncates, even when the file is already
224 * small enough. Check now and save some time.
226 code = afs_osi_Stat(afile, &tstat);
227 if (code || tstat.size <= asize)
229 MObtainWriteLock(&afs_xosi, 321);
231 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
232 down_write(&inode->i_alloc_sem);
234 #ifdef STRUCT_INODE_HAS_I_MUTEX
235 mutex_lock(&inode->i_mutex);
239 newattrs.ia_size = asize;
240 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
241 #if defined(AFS_LINUX24_ENV)
242 newattrs.ia_ctime = CURRENT_TIME;
244 /* avoid notify_change() since it wants to update dentry->d_parent */
246 code = inode_change_ok(inode, &newattrs);
248 #ifdef INODE_SETATTR_NOT_VOID
249 code = inode_setattr(inode, &newattrs);
251 inode_setattr(inode, &newattrs);
255 truncate_inode_pages(&inode->i_data, asize);
257 inode->i_size = asize;
258 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
259 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
262 truncate_inode_pages(inode, asize);
263 if (inode->i_op && inode->i_op->truncate)
264 inode->i_op->truncate(inode);
268 #ifdef STRUCT_INODE_HAS_I_MUTEX
269 mutex_unlock(&inode->i_mutex);
273 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
274 up_write(&inode->i_alloc_sem);
277 MReleaseWriteLock(&afs_xosi);
282 /* Generic read interface */
284 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
291 AFS_STATCNT(osi_Read);
294 * If the osi_file passed in is NULL, panic only if AFS is not shutting
295 * down. No point in crashing when we are already shutting down
298 if (!afs_shuttingdown)
299 osi_Panic("osi_Read called with null param");
305 afile->offset = offset;
306 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
308 code = osi_rdwr(afile, &auio, UIO_READ);
311 code = asize - auio.uio_resid;
312 afile->offset += code;
314 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
315 ICL_TYPE_INT32, code);
321 /* Generic write interface */
323 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
330 AFS_STATCNT(osi_Write);
333 if (!afs_shuttingdown)
334 osi_Panic("afs_osi_Write called with null param");
340 afile->offset = offset;
341 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
343 code = osi_rdwr(afile, &auio, UIO_WRITE);
346 code = asize - auio.uio_resid;
347 afile->offset += code;
351 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
356 (*afile->proc)(afile, code);
362 /* This work should be handled by physstrat in ca/machdep.c.
363 This routine written from the RT NFS port strategy routine.
364 It has been generalized a bit, but should still be pretty clear. */
366 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
368 afs_int32 returnCode;
370 AFS_STATCNT(osi_MapStrategy);
371 returnCode = (*aproc) (bp);
377 shutdown_osifile(void)
379 AFS_STATCNT(shutdown_osifile);
380 if (afs_cold_shutdown) {
381 afs_osicred_initialized = 0;
385 /* Intialize cache device info and fragment size for disk cache partition. */
387 osi_InitCacheInfo(char *aname)
391 extern ino_t cacheInode;
392 extern struct osi_dev cacheDev;
393 extern afs_int32 afs_fsfragsize;
394 extern struct super_block *afs_cacheSBp;
395 extern struct vfsmount *afs_cacheMnt;
396 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
400 cacheInode = dp->d_inode->i_ino;
401 cacheDev.dev = dp->d_inode->i_sb->s_dev;
402 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
403 afs_cacheSBp = dp->d_inode->i_sb;
411 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
412 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
415 * seek, then read or write to an open inode. addrp points to data in
419 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
421 #ifdef AFS_LINUX26_ENV
422 struct file *filp = osifile->filp;
424 struct file *filp = &osifile->file;
430 unsigned long savelim;
432 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
433 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
435 if (uiop->uio_seg == AFS_UIOSYS)
438 /* seek to the desired position. Return -1 on error. */
439 if (filp->f_op->llseek) {
440 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
443 filp->f_pos = uiop->uio_offset;
445 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
447 count = iov->iov_len;
455 code = FOP_READ(filp, iov->iov_base, count);
457 code = FOP_WRITE(filp, iov->iov_base, count);
462 } else if (code == 0) {
464 * This is bad -- we can't read any more data from the
465 * file, but we have no good way of signaling a partial
472 iov->iov_base += code;
473 iov->iov_len -= code;
474 uiop->uio_resid -= code;
475 uiop->uio_offset += code;
479 if (uiop->uio_seg == AFS_UIOSYS)
482 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
488 * Setup a uio struct.
491 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
492 int count, uio_flag_t flag, uio_seg_t seg)
494 iovecp->iov_base = (char *)buf;
495 iovecp->iov_len = count;
496 uiop->uio_iov = iovecp;
497 uiop->uio_iovcnt = 1;
498 uiop->uio_offset = pos;
500 uiop->uio_resid = count;
501 uiop->uio_flag = flag;
506 * UIO_READ : dp -> uio
507 * UIO_WRITE : uio -> dp
510 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
516 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
518 count = iov->iov_len;
529 switch (uiop->uio_seg) {
533 memcpy(iov->iov_base, dp, count);
536 memcpy(dp, iov->iov_base, count);
539 printf("uiomove: Bad rw = %d\n", rw);
546 AFS_COPYOUT(dp, iov->iov_base, count, code);
549 AFS_COPYIN(iov->iov_base, dp, count, code);
552 printf("uiomove: Bad rw = %d\n", rw);
557 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
563 iov->iov_base += count;
564 iov->iov_len -= count;
565 uiop->uio_offset += count;
566 uiop->uio_resid -= count;