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_dcache_id_t *ainode)
41 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) || defined(LINUX_USE_FH)
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 (int)sizeof(struct osi_file));
65 memset(afile, 0, sizeof(struct osi_file));
66 #if defined(HAVE_IGET)
67 tip = iget(afs_cacheSBp, ainode->ufs);
69 osi_Panic("Can't get inode %d\n", ainode->ufs);
71 dp = d_alloc_anon(tip);
73 # if defined(LINUX_USE_FH)
74 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh, sizeof(struct fid), ainode->ufs.fh_type);
76 fid.i32.ino = ainode->ufs;
78 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &fid, sizeof(fid), FILEID_INO32_GEN);
81 osi_Panic("Can't get dentry\n");
84 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
86 #if defined(STRUCT_TASK_HAS_CRED)
87 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
89 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
92 #if defined(LINUX_USE_FH)
93 osi_Panic("Can't open file\n");
95 osi_Panic("Can't open inode %d\n", ainode->ufs);
98 afile->size = i_size_read(FILE_INODE(filp));
101 afile->proc = (int (*)())0;
102 #if defined(LINUX_USE_FH)
103 afile->inum = tip->i_ino; /* for hint validity checking */
105 afile->inum = ainode->ufs; /* for hint validity checking */
107 return (void *)afile;
111 osi_UFSOpen(afs_dcache_id_t *ainode)
113 register struct osi_file *afile = NULL;
114 extern int cacheDiskType;
116 struct inode *tip = NULL;
117 struct file *filp = NULL;
118 AFS_STATCNT(osi_UFSOpen);
119 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
120 osi_Panic("UFSOpen called for non-UFS cache\n");
122 if (!afs_osicred_initialized) {
123 /* valid for alpha_osf, SunOS, Ultrix */
124 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
125 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
126 afs_osicred_initialized = 1;
128 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
131 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
132 sizeof(struct osi_file));
134 memset(afile, 0, sizeof(struct osi_file));
136 filp->f_dentry = &afile->dentry;
137 tip = iget(afs_cacheSBp, ainode->ufs);
139 osi_Panic("Can't get inode %d\n", ainode->ufs);
140 FILE_INODE(filp) = tip;
141 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
142 filp->f_flags = O_RDWR;
143 #if defined(AFS_LINUX24_ENV)
144 filp->f_mode = FMODE_READ|FMODE_WRITE;
145 filp->f_op = fops_get(tip->i_fop);
147 filp->f_op = tip->i_op->default_file_ops;
149 if (filp->f_op && filp->f_op->open)
150 code = filp->f_op->open(tip, filp);
152 osi_Panic("Can't open inode %d\n", ainode->ufs);
153 afile->size = i_size_read(tip);
156 afile->proc = (int (*)())0;
157 afile->inum = ainode->ufs; /* for hint validity checking */
158 return (void *)afile;
162 #if defined(LINUX_USE_FH)
163 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
164 int max_len = sizeof(struct fid);
166 if (dp->d_sb->s_export_op->encode_fh) {
167 ainode->fh_type = dp->d_sb->s_export_op->encode_fh(dp, (__u32 *)&ainode->fh, &max_len, 0);
169 ainode->fh_type = FILEID_INO32_GEN;
170 ainode->fh.i32.ino = dp->d_inode->i_ino;
171 ainode->fh.i32.gen = dp->d_inode->i_generation;
175 void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
176 *ainode = dp->d_inode->i_ino;
181 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
183 register afs_int32 code;
184 AFS_STATCNT(osi_Stat);
185 MObtainWriteLock(&afs_xosi, 320);
186 astat->size = i_size_read(OSIFILE_INODE(afile));
187 #if defined(AFS_LINUX26_ENV)
188 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
189 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
191 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
192 astat->atime = OSIFILE_INODE(afile)->i_atime;
195 MReleaseWriteLock(&afs_xosi);
199 #ifdef AFS_LINUX26_ENV
201 osi_UFSClose(register struct osi_file *afile)
203 AFS_STATCNT(osi_Close);
205 if (OSIFILE_INODE(afile)) {
206 filp_close(afile->filp, NULL);
210 osi_FreeLargeSpace(afile);
215 osi_UFSClose(register struct osi_file *afile)
217 AFS_STATCNT(osi_Close);
219 if (FILE_INODE(&afile->file)) {
220 struct file *filp = &afile->file;
221 if (filp->f_op && filp->f_op->release)
222 filp->f_op->release(FILE_INODE(filp), filp);
223 iput(FILE_INODE(filp));
227 osi_FreeLargeSpace(afile);
233 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
235 register afs_int32 code;
236 struct osi_stat tstat;
237 struct iattr newattrs;
238 struct inode *inode = OSIFILE_INODE(afile);
239 AFS_STATCNT(osi_Truncate);
241 /* This routine only shrinks files, and most systems
242 * have very slow truncates, even when the file is already
243 * small enough. Check now and save some time.
245 code = afs_osi_Stat(afile, &tstat);
246 if (code || tstat.size <= asize)
248 MObtainWriteLock(&afs_xosi, 321);
250 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
251 down_write(&inode->i_alloc_sem);
253 #ifdef STRUCT_INODE_HAS_I_MUTEX
254 mutex_lock(&inode->i_mutex);
258 newattrs.ia_size = asize;
259 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
260 #if defined(AFS_LINUX24_ENV)
261 newattrs.ia_ctime = CURRENT_TIME;
263 /* avoid notify_change() since it wants to update dentry->d_parent */
265 code = inode_change_ok(inode, &newattrs);
267 #ifdef INODE_SETATTR_NOT_VOID
268 code = inode_setattr(inode, &newattrs);
270 inode_setattr(inode, &newattrs);
274 truncate_inode_pages(&inode->i_data, asize);
276 i_size_write(inode, asize);
277 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
278 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
281 truncate_inode_pages(inode, asize);
282 if (inode->i_op && inode->i_op->truncate)
283 inode->i_op->truncate(inode);
287 #ifdef STRUCT_INODE_HAS_I_MUTEX
288 mutex_unlock(&inode->i_mutex);
292 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
293 up_write(&inode->i_alloc_sem);
296 MReleaseWriteLock(&afs_xosi);
301 /* Generic read interface */
303 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
310 AFS_STATCNT(osi_Read);
313 * If the osi_file passed in is NULL, panic only if AFS is not shutting
314 * down. No point in crashing when we are already shutting down
317 if (!afs_shuttingdown)
318 osi_Panic("osi_Read called with null param");
324 afile->offset = offset;
325 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
327 code = osi_rdwr(afile, &auio, UIO_READ);
330 code = asize - auio.uio_resid;
331 afile->offset += code;
333 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
334 ICL_TYPE_INT32, code);
340 /* Generic write interface */
342 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
349 AFS_STATCNT(osi_Write);
352 if (!afs_shuttingdown)
353 osi_Panic("afs_osi_Write called with null param");
359 afile->offset = offset;
360 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
362 code = osi_rdwr(afile, &auio, UIO_WRITE);
365 code = asize - auio.uio_resid;
366 afile->offset += code;
370 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
375 (*afile->proc)(afile, code);
381 /* This work should be handled by physstrat in ca/machdep.c.
382 This routine written from the RT NFS port strategy routine.
383 It has been generalized a bit, but should still be pretty clear. */
385 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
387 afs_int32 returnCode;
389 AFS_STATCNT(osi_MapStrategy);
390 returnCode = (*aproc) (bp);
396 shutdown_osifile(void)
398 AFS_STATCNT(shutdown_osifile);
399 if (afs_cold_shutdown) {
400 afs_osicred_initialized = 0;
404 /* Intialize cache device info and fragment size for disk cache partition. */
406 osi_InitCacheInfo(char *aname)
409 extern afs_dcache_id_t cacheInode;
411 extern struct osi_dev cacheDev;
412 extern afs_int32 afs_fsfragsize;
413 extern struct super_block *afs_cacheSBp;
414 extern struct vfsmount *afs_cacheMnt;
415 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
419 osi_get_fh(dp, &cacheInode.ufs);
420 cacheDev.dev = dp->d_inode->i_sb->s_dev;
421 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
422 afs_cacheSBp = dp->d_inode->i_sb;
430 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
431 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
434 * seek, then read or write to an open inode. addrp points to data in
438 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
440 #ifdef AFS_LINUX26_ENV
441 struct file *filp = osifile->filp;
443 struct file *filp = &osifile->file;
449 unsigned long savelim;
451 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
452 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
454 if (uiop->uio_seg == AFS_UIOSYS)
457 /* seek to the desired position. Return -1 on error. */
458 if (filp->f_op->llseek) {
459 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
462 filp->f_pos = uiop->uio_offset;
464 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
466 count = iov->iov_len;
474 code = FOP_READ(filp, iov->iov_base, count);
476 code = FOP_WRITE(filp, iov->iov_base, count);
481 } else if (code == 0) {
483 * This is bad -- we can't read any more data from the
484 * file, but we have no good way of signaling a partial
491 iov->iov_base += code;
492 iov->iov_len -= code;
493 uiop->uio_resid -= code;
494 uiop->uio_offset += code;
498 if (uiop->uio_seg == AFS_UIOSYS)
501 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
507 * Setup a uio struct.
510 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
511 int count, uio_flag_t flag, uio_seg_t seg)
513 iovecp->iov_base = (char *)buf;
514 iovecp->iov_len = count;
515 uiop->uio_iov = iovecp;
516 uiop->uio_iovcnt = 1;
517 uiop->uio_offset = pos;
519 uiop->uio_resid = count;
520 uiop->uio_flag = flag;
525 * UIO_READ : dp -> uio
526 * UIO_WRITE : uio -> dp
529 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
535 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
537 count = iov->iov_len;
548 switch (uiop->uio_seg) {
552 memcpy(iov->iov_base, dp, count);
555 memcpy(dp, iov->iov_base, count);
558 printf("uiomove: Bad rw = %d\n", rw);
565 AFS_COPYOUT(dp, iov->iov_base, count, code);
568 AFS_COPYIN(iov->iov_base, dp, count, code);
571 printf("uiomove: Bad rw = %d\n", rw);
576 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
582 iov->iov_base += count;
583 iov->iov_len -= count;
584 uiop->uio_offset += count;
585 uiop->uio_resid -= count;