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 #if defined(LINUX_USE_FH)
40 osi_UFSOpen_fh(struct fid *fh, int fh_type)
42 osi_UFSOpen(afs_int32 ainode)
45 register struct osi_file *afile = NULL;
46 extern int cacheDiskType;
47 struct inode *tip = NULL;
48 struct dentry *dp = NULL;
49 struct file *filp = NULL;
50 #if !defined(HAVE_IGET) || defined(LINUX_USE_FH)
53 AFS_STATCNT(osi_UFSOpen);
54 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
55 osi_Panic("UFSOpen called for non-UFS cache\n");
57 if (!afs_osicred_initialized) {
58 /* valid for alpha_osf, SunOS, Ultrix */
59 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
60 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
61 afs_osicred_initialized = 1;
63 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
66 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
67 (int)sizeof(struct osi_file));
69 memset(afile, 0, sizeof(struct osi_file));
70 #if defined(HAVE_IGET)
71 tip = iget(afs_cacheSBp, (u_long) ainode);
73 osi_Panic("Can't get inode %d\n", ainode);
75 dp = d_alloc_anon(tip);
77 #if defined(LINUX_USE_FH)
78 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, fh, sizeof(struct fid), fh_type);
82 dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &fid, sizeof(fid), FILEID_INO32_GEN);
85 osi_Panic("Can't get dentry\n");
88 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
90 #if defined(STRUCT_TASK_HAS_CRED)
91 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
93 filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
96 #if defined(LINUX_USE_FH)
97 osi_Panic("Can't open file\n");
99 osi_Panic("Can't open inode %d\n", ainode);
102 afile->size = i_size_read(FILE_INODE(filp));
105 afile->proc = (int (*)())0;
106 #if defined(LINUX_USE_FH)
107 afile->inum = tip->i_ino; /* for hint validity checking */
109 afile->inum = ainode; /* for hint validity checking */
111 return (void *)afile;
115 osi_UFSOpen(afs_int32 ainode)
117 register struct osi_file *afile = NULL;
118 extern int cacheDiskType;
120 struct inode *tip = NULL;
121 struct file *filp = NULL;
122 AFS_STATCNT(osi_UFSOpen);
123 if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
124 osi_Panic("UFSOpen called for non-UFS cache\n");
126 if (!afs_osicred_initialized) {
127 /* valid for alpha_osf, SunOS, Ultrix */
128 memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
129 crhold(&afs_osi_cred); /* don't let it evaporate, since it is static */
130 afs_osicred_initialized = 1;
132 afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
135 osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
136 sizeof(struct osi_file));
138 memset(afile, 0, sizeof(struct osi_file));
140 filp->f_dentry = &afile->dentry;
141 tip = iget(afs_cacheSBp, (u_long) ainode);
143 osi_Panic("Can't get inode %d\n", ainode);
144 FILE_INODE(filp) = tip;
145 tip->i_flags |= MS_NOATIME; /* Disable updating access times. */
146 filp->f_flags = O_RDWR;
147 #if defined(AFS_LINUX24_ENV)
148 filp->f_mode = FMODE_READ|FMODE_WRITE;
149 filp->f_op = fops_get(tip->i_fop);
151 filp->f_op = tip->i_op->default_file_ops;
153 if (filp->f_op && filp->f_op->open)
154 code = filp->f_op->open(tip, filp);
156 osi_Panic("Can't open inode %d\n", ainode);
157 afile->size = i_size_read(tip);
160 afile->proc = (int (*)())0;
161 afile->inum = ainode; /* for hint validity checking */
162 return (void *)afile;
166 #if defined(LINUX_USE_FH)
168 osi_get_fh(struct dentry *dp, struct fid *fh, int *max_len) {
171 if (dp->d_sb->s_export_op->encode_fh) {
172 fh_type = dp->d_sb->s_export_op->encode_fh(dp, (__u32 *)fh, max_len, 0);
174 fh_type = FILEID_INO32_GEN;
175 fh->i32.ino = dp->d_inode->i_ino;
176 fh->i32.gen = dp->d_inode->i_generation;
184 afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
186 register afs_int32 code;
187 AFS_STATCNT(osi_Stat);
188 MObtainWriteLock(&afs_xosi, 320);
189 astat->size = i_size_read(OSIFILE_INODE(afile));
190 #if defined(AFS_LINUX26_ENV)
191 astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
192 astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
194 astat->mtime = OSIFILE_INODE(afile)->i_mtime;
195 astat->atime = OSIFILE_INODE(afile)->i_atime;
198 MReleaseWriteLock(&afs_xosi);
202 #ifdef AFS_LINUX26_ENV
204 osi_UFSClose(register struct osi_file *afile)
206 AFS_STATCNT(osi_Close);
208 if (OSIFILE_INODE(afile)) {
209 filp_close(afile->filp, NULL);
213 osi_FreeLargeSpace(afile);
218 osi_UFSClose(register struct osi_file *afile)
220 AFS_STATCNT(osi_Close);
222 if (FILE_INODE(&afile->file)) {
223 struct file *filp = &afile->file;
224 if (filp->f_op && filp->f_op->release)
225 filp->f_op->release(FILE_INODE(filp), filp);
226 iput(FILE_INODE(filp));
230 osi_FreeLargeSpace(afile);
236 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
238 register afs_int32 code;
239 struct osi_stat tstat;
240 struct iattr newattrs;
241 struct inode *inode = OSIFILE_INODE(afile);
242 AFS_STATCNT(osi_Truncate);
244 /* This routine only shrinks files, and most systems
245 * have very slow truncates, even when the file is already
246 * small enough. Check now and save some time.
248 code = afs_osi_Stat(afile, &tstat);
249 if (code || tstat.size <= asize)
251 MObtainWriteLock(&afs_xosi, 321);
253 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
254 down_write(&inode->i_alloc_sem);
256 #ifdef STRUCT_INODE_HAS_I_MUTEX
257 mutex_lock(&inode->i_mutex);
261 newattrs.ia_size = asize;
262 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
263 #if defined(AFS_LINUX24_ENV)
264 newattrs.ia_ctime = CURRENT_TIME;
266 /* avoid notify_change() since it wants to update dentry->d_parent */
268 code = inode_change_ok(inode, &newattrs);
270 #ifdef INODE_SETATTR_NOT_VOID
271 code = inode_setattr(inode, &newattrs);
273 inode_setattr(inode, &newattrs);
277 truncate_inode_pages(&inode->i_data, asize);
279 i_size_write(inode, asize);
280 if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
281 code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
284 truncate_inode_pages(inode, asize);
285 if (inode->i_op && inode->i_op->truncate)
286 inode->i_op->truncate(inode);
290 #ifdef STRUCT_INODE_HAS_I_MUTEX
291 mutex_unlock(&inode->i_mutex);
295 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
296 up_write(&inode->i_alloc_sem);
299 MReleaseWriteLock(&afs_xosi);
304 /* Generic read interface */
306 afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
313 AFS_STATCNT(osi_Read);
316 * If the osi_file passed in is NULL, panic only if AFS is not shutting
317 * down. No point in crashing when we are already shutting down
320 if (!afs_shuttingdown)
321 osi_Panic("osi_Read called with null param");
327 afile->offset = offset;
328 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_READ, AFS_UIOSYS);
330 code = osi_rdwr(afile, &auio, UIO_READ);
333 code = asize - auio.uio_resid;
334 afile->offset += code;
336 afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
337 ICL_TYPE_INT32, code);
343 /* Generic write interface */
345 afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
352 AFS_STATCNT(osi_Write);
355 if (!afs_shuttingdown)
356 osi_Panic("afs_osi_Write called with null param");
362 afile->offset = offset;
363 setup_uio(&auio, &iov, aptr, afile->offset, asize, UIO_WRITE, AFS_UIOSYS);
365 code = osi_rdwr(afile, &auio, UIO_WRITE);
368 code = asize - auio.uio_resid;
369 afile->offset += code;
373 ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
378 (*afile->proc)(afile, code);
384 /* This work should be handled by physstrat in ca/machdep.c.
385 This routine written from the RT NFS port strategy routine.
386 It has been generalized a bit, but should still be pretty clear. */
388 afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
390 afs_int32 returnCode;
392 AFS_STATCNT(osi_MapStrategy);
393 returnCode = (*aproc) (bp);
399 shutdown_osifile(void)
401 AFS_STATCNT(shutdown_osifile);
402 if (afs_cold_shutdown) {
403 afs_osicred_initialized = 0;
407 /* Intialize cache device info and fragment size for disk cache partition. */
409 osi_InitCacheInfo(char *aname)
412 #if defined(LINUX_USE_FH)
413 int max_len = sizeof(struct fid);
415 extern ino_t cacheInode;
418 extern ino_t cacheInode;
419 extern struct osi_dev cacheDev;
420 extern afs_int32 afs_fsfragsize;
421 extern struct super_block *afs_cacheSBp;
422 extern struct vfsmount *afs_cacheMnt;
423 code = osi_lookupname_internal(aname, 1, &afs_cacheMnt, &dp);
427 #if defined(LINUX_USE_FH)
428 if (dp->d_sb->s_export_op->encode_fh) {
429 cacheitems_fh_type = dp->d_sb->s_export_op->encode_fh(dp, (__u32 *)&cacheitems_fh, &max_len, 0);
431 cacheitems_fh_type = FILEID_INO32_GEN;
432 cacheitems_fh.i32.ino = dp->d_inode->i_ino;
433 cacheitems_fh.i32.gen = dp->d_inode->i_generation;
436 cacheInode = dp->d_inode->i_ino;
438 cacheDev.dev = dp->d_inode->i_sb->s_dev;
439 afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
440 afs_cacheSBp = dp->d_inode->i_sb;
448 #define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
449 #define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
452 * seek, then read or write to an open inode. addrp points to data in
456 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
458 #ifdef AFS_LINUX26_ENV
459 struct file *filp = osifile->filp;
461 struct file *filp = &osifile->file;
467 unsigned long savelim;
469 savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
470 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
472 if (uiop->uio_seg == AFS_UIOSYS)
475 /* seek to the desired position. Return -1 on error. */
476 if (filp->f_op->llseek) {
477 if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
480 filp->f_pos = uiop->uio_offset;
482 while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
484 count = iov->iov_len;
492 code = FOP_READ(filp, iov->iov_base, count);
494 code = FOP_WRITE(filp, iov->iov_base, count);
499 } else if (code == 0) {
501 * This is bad -- we can't read any more data from the
502 * file, but we have no good way of signaling a partial
509 iov->iov_base += code;
510 iov->iov_len -= code;
511 uiop->uio_resid -= code;
512 uiop->uio_offset += code;
516 if (uiop->uio_seg == AFS_UIOSYS)
519 current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
525 * Setup a uio struct.
528 setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
529 int count, uio_flag_t flag, uio_seg_t seg)
531 iovecp->iov_base = (char *)buf;
532 iovecp->iov_len = count;
533 uiop->uio_iov = iovecp;
534 uiop->uio_iovcnt = 1;
535 uiop->uio_offset = pos;
537 uiop->uio_resid = count;
538 uiop->uio_flag = flag;
543 * UIO_READ : dp -> uio
544 * UIO_WRITE : uio -> dp
547 uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
553 while (length > 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
555 count = iov->iov_len;
566 switch (uiop->uio_seg) {
570 memcpy(iov->iov_base, dp, count);
573 memcpy(dp, iov->iov_base, count);
576 printf("uiomove: Bad rw = %d\n", rw);
583 AFS_COPYOUT(dp, iov->iov_base, count, code);
586 AFS_COPYIN(iov->iov_base, dp, count, code);
589 printf("uiomove: Bad rw = %d\n", rw);
594 printf("uiomove: Bad seg = %d\n", uiop->uio_seg);
600 iov->iov_base += count;
601 iov->iov_len -= count;
602 uiop->uio_offset += count;
603 uiop->uio_resid -= count;