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"
14 #ifdef AFS_LINUX24_ENV
15 #include "h/module.h" /* early to avoid printf->printk mapping */
17 #include "afs/sysincludes.h" /* Standard vendor system headers */
18 #include "afsincludes.h" /* Afs-based standard headers */
19 #include "afs/afs_stats.h" /* afs statistics */
20 #include "h/smp_lock.h"
21 #if defined(AFS_LINUX26_ENV)
24 #if defined(LINUX_USE_FH)
25 #include "h/exportfs.h"
26 int cache_fh_type = -1;
27 int cache_fh_len = -1;
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 afs_linux_raw_open(afs_dcache_id_t *ainode, ino_t *hint)
41 struct inode *tip = NULL;
42 struct dentry *dp = NULL;
45 #if !defined(LINUX_USE_FH)
46 tip = iget(afs_cacheSBp, ainode->ufs);
48 osi_Panic("Can't get inode %d\n", (int) ainode->ufs);
50 dp = d_alloc_anon(tip);
52 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
53 cache_fh_len, cache_fh_type);
55 osi_Panic("Can't get dentry\n");
58 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
60 #if defined(STRUCT_TASK_HAS_CRED)
61 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
63 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
66 #if defined(LINUX_USE_FH)
67 osi_Panic("Can't open file\n");
69 osi_Panic("Can't open inode %d\n", (int) ainode->ufs);
77 osi_UFSOpen(afs_dcache_id_t *ainode)
79 struct osi_file *afile = NULL;
80 extern int cacheDiskType;
81 struct file *filp = NULL;
82 AFS_STATCNT(osi_UFSOpen);
83 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
84 osi_Panic("UFSOpen called for non-UFS cache\n");
86 if (!afs_osicred_initialized) {
87 /* valid for alpha_osf, SunOS, Ultrix */
88 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
89 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
90 afs_osicred_initialized = 1;
92 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
95 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
96 (int)sizeof(struct osi_file));
98 memset(afile, 0, sizeof(struct osi_file));
100 afile->filp = afs_linux_raw_open(ainode, &afile->inum);
101 afile->size = i_size_read(FILE_INODE(afile->filp));
104 afile->proc = (int (*)())0;
105 return (void *)afile;
109 osi_UFSOpen(afs_dcache_id_t *ainode)
111 register struct osi_file *afile = NULL;
112 extern int cacheDiskType;
114 struct inode *tip = NULL;
115 struct file *filp = NULL;
116 AFS_STATCNT(osi_UFSOpen);
117 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
118 osi_Panic("UFSOpen called for non-UFS cache\n");
120 if (!afs_osicred_initialized) {
121 /* valid for alpha_osf, SunOS, Ultrix */
122 memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
123 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
124 afs_osicred_initialized = 1;
126 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
129 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
130 sizeof(struct osi_file));
132 memset(afile, 0, sizeof(struct osi_file));
134 filp->f_dentry = &afile->dentry;
135 tip = iget(afs_cacheSBp, ainode->ufs);
137 osi_Panic("Can't get inode %d\n", ainode->ufs);
138 FILE_INODE(filp) = tip;
139 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
140 filp->f_flags = O_RDWR;
141 #if defined(AFS_LINUX24_ENV)
142 filp->f_mode = FMODE_READ|FMODE_WRITE;
143 filp->f_op = fops_get(tip->i_fop);
145 filp->f_op = tip->i_op->default_file_ops;
147 if (filp->f_op && filp->f_op->open)
148 code = filp->f_op->open(tip, filp);
150 osi_Panic("Can't open inode %d\n", ainode->ufs);
151 afile->size = i_size_read(tip);
154 afile->proc = (int (*)())0;
155 afile->inum = ainode->ufs; /* for hint validity checking */
156 return (void *)afile;
160 #if defined(LINUX_USE_FH)
162 * Given a dentry, return the file handle as encoded by the filesystem.
163 * We can't assume anything about the length (words, not bytes).
164 * The cache has to live on a single filesystem with uniform file
165 * handles, otherwise we panic.
167 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
171 if (cache_fh_len > 0)
172 max_len = cache_fh_len;
174 max_len = MAX_FH_LEN;
175 if (dp->d_sb->s_export_op->encode_fh) {
176 type = dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], &max_len, 0);
178 osi_Panic("File handle encoding failed\n");
180 if (cache_fh_type < 0)
181 cache_fh_type = type;
182 if (cache_fh_len < 0) {
183 cache_fh_len = max_len;
185 if (type != cache_fh_type || max_len != cache_fh_len) {
186 osi_Panic("Inconsistent file handles within cache\n");
189 /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
190 if (cache_fh_type < 0)
191 cache_fh_type = FILEID_INO32_GEN;
192 if (cache_fh_len < 0)
193 cache_fh_len = sizeof(struct fid)/4;
194 ainode->fh.i32.ino = dp->d_inode->i_ino;
195 ainode->fh.i32.gen = dp->d_inode->i_generation;
199 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
200 *ainode = dp->d_inode->i_ino;
205 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
207 register afs_int32 code;
208 AFS_STATCNT(osi_Stat);
209 MObtainWriteLock(&afs_xosi, 320);
210 astat->size = i_size_read(OSIFILE_INODE(afile));
211 #if defined(AFS_LINUX26_ENV)
212 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
213 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
215 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
216 astat->atime = OSIFILE_INODE(afile)->i_atime;
219 MReleaseWriteLock(&afs_xosi);
223 #ifdef AFS_LINUX26_ENV
225 osi_UFSClose(register struct osi_file *afile)
227 AFS_STATCNT(osi_Close);
229 if (OSIFILE_INODE(afile)) {
230 filp_close(afile->filp, NULL);
234 osi_FreeLargeSpace(afile);
239 osi_UFSClose(register struct osi_file *afile)
241 AFS_STATCNT(osi_Close);
243 if (FILE_INODE(&afile->file)) {
244 struct file *filp = &afile->file;
245 if (filp->f_op && filp->f_op->release)
246 filp->f_op->release(FILE_INODE(filp), filp);
247 iput(FILE_INODE(filp));
251 osi_FreeLargeSpace(afile);
257 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
259 register afs_int32 code;
260 struct osi_stat tstat;
261 struct iattr newattrs;
262 struct inode *inode = OSIFILE_INODE(afile);
263 AFS_STATCNT(osi_Truncate);
265 /* This routine only shrinks files, and most systems
266 * have very slow truncates, even when the file is already
267 * small enough. Check now and save some time.
269 code = afs_osi_Stat(afile, &tstat);
270 if (code || tstat.size <= asize)
272 MObtainWriteLock(&afs_xosi, 321);
274 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
275 down_write(&inode->i_alloc_sem);
277 #ifdef STRUCT_INODE_HAS_I_MUTEX
278 mutex_lock(&inode->i_mutex);
282 newattrs.ia_size = asize;
283 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
284 #if defined(AFS_LINUX24_ENV)
285 newattrs.ia_ctime = CURRENT_TIME;
287 /* avoid notify_change() since it wants to update dentry->d_parent */
289 code = inode_change_ok(inode, &newattrs);
291 #ifdef INODE_SETATTR_NOT_VOID
292 #if defined(AFS_LINUX26_ENV)
293 if (inode->i_op && inode->i_op->setattr)
294 code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
297 code = inode_setattr(inode, &newattrs);
299 inode_setattr(inode, &newattrs);
304 truncate_inode_pages(&inode->i_data, asize);
306 i_size_write(inode, asize);
307 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
308 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
311 truncate_inode_pages(inode, asize);
312 if (inode->i_op && inode->i_op->truncate)
313 inode->i_op->truncate(inode);
317 #ifdef STRUCT_INODE_HAS_I_MUTEX
318 mutex_unlock(&inode->i_mutex);
322 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
323 up_write(&inode->i_alloc_sem);
326 MReleaseWriteLock(&afs_xosi);
331 /* Generic read interface */
333 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
340 AFS_STATCNT(osi_Read);
343 * If the osi_file passed in is NULL, panic only if AFS is not shutting
344 * down. No point in crashing when we are already shutting down
347 if (!afs_shuttingdown)
348 osi_Panic("osi_Read called with null param");
354 afile->offset = offset;
355 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
357 code = osi_rdwr(afile, &auio, UIO_READ);
360 code = asize - auio.uio_resid;
361 afile->offset += code;
363 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
364 ICL_TYPE_INT32, code);
370 /* Generic write interface */
372 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
379 AFS_STATCNT(osi_Write);
382 if (!afs_shuttingdown)
383 osi_Panic("afs_osi_Write called with null param");
389 afile->offset = offset;
390 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
392 code = osi_rdwr(afile, &auio, UIO_WRITE);
395 code = asize - auio.uio_resid;
396 afile->offset += code;
400 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
405 (*afile->proc)(afile, code);
411 /* This work should be handled by physstrat in ca/machdep.c.
412 This routine written from the RT NFS port strategy routine.
413 It has been generalized a bit, but should still be pretty clear. */
415 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
417 afs_int32 returnCode;
419 AFS_STATCNT(osi_MapStrategy);
420 returnCode = (*aproc) (bp);
426 shutdown_osifile(void)
428 AFS_STATCNT(shutdown_osifile);
429 if (afs_cold_shutdown) {
430 afs_osicred_initialized = 0;
434 /* Intialize cache device info and fragment size for disk cache partition. */
436 osi_InitCacheInfo(char *aname)
439 extern afs_dcache_id_t cacheInode;
441 extern struct osi_dev cacheDev;
442 extern afs_int32 afs_fsfragsize;
443 extern struct super_block *afs_cacheSBp;
444 extern struct vfsmount *afs_cacheMnt;
445 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
449 osi_get_fh(dp, &cacheInode.ufs);
450 cacheDev.dev = dp->d_inode->i_sb->s_dev;
451 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
452 afs_cacheSBp = dp->d_inode->i_sb;
460 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
461 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
464 * seek, then read or write to an open inode. addrp points to data in
468 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
470 #ifdef AFS_LINUX26_ENV
471 struct file *filp = osifile->filp;
473 struct file *filp = &osifile->file;
479 unsigned long savelim;
481 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
482 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
484 if (uiop->uio_seg == AFS_UIOSYS)
487 /* seek to the desired position. Return -1 on error. */
488 if (filp->f_op->llseek) {
489 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
492 filp->f_pos = uiop->uio_offset;
494 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
496 count = iov->iov_len;
504 code = FOP_READ(filp, iov->iov_base, count);
506 code = FOP_WRITE(filp, iov->iov_base, count);
511 } else if (code == 0) {
513 * This is bad -- we can't read any more data from the
514 * file, but we have no good way of signaling a partial
521 iov->iov_base += code;
522 iov->iov_len -= code;
523 uiop->uio_resid -= code;
524 uiop->uio_offset += code;
528 if (uiop->uio_seg == AFS_UIOSYS)
531 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
537 * Setup a uio struct.
540 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
541 int count, uio_flag_t flag, uio_seg_t seg)
543 iovecp->iov_base = (char *)buf;
544 iovecp->iov_len = count;
545 uiop->uio_iov = iovecp;
546 uiop->uio_iovcnt = 1;
547 uiop->uio_offset = pos;
549 uiop->uio_resid = count;
550 uiop->uio_flag = flag;
555 * UIO_READ : dp -> uio
556 * UIO_WRITE : uio -> dp
559 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
565 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
567 count = iov->iov_len;
578 switch (uiop->uio_seg) {
582 memcpy(iov->iov_base, dp, count);
585 memcpy(dp, iov->iov_base, count);
588 printf("uiomove: Bad rw = %d\n", rw);
595 AFS_COPYOUT(dp, iov->iov_base, count, code);
598 AFS_COPYIN(iov->iov_base, dp, count, code);
601 printf("uiomove: Bad rw = %d\n", rw);
606 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
612 iov->iov_base += count;
613 iov->iov_len -= count;
614 uiop->uio_offset += count;
615 uiop->uio_resid -= count;